def test_symlink_readlink_islink(self): p = self.path ## create a mock file src = self._create_file(u'testfile', 'bar') ## create a symlink to it src.symlink(p.child(u'testlink')) ## assert that the link can be read ## TODO: readlink seems to return a string instead of a path object ## that's probably a bug? eq(str(p.child(u'testlink').readlink()), str(p.child(u'testfile'))) ## assert that the file cannot be read as a link assert_raises(OSError, p.child(u'testfile').readlink) ## assert that islink works assert(p.child(u'testfile').islink() is False) assert(p.child(u'testlink').islink() is True) ## assert that the link can be opened as a file, ## and that we'll get the file content by doing so with p.child(u'testlink').open(u'r') as f: eq(f.read(3), u'bar')
def test_rename_bad_string(): tmp = maketemp() parent = filesystem.path(tmp) old = parent.join(u'foo') assert_raises( filesystem.CrossDeviceRenameError, old.rename, 'foo') eq(filesystem.root, filesystem.root.parent())
def test_child_bad_slash(self): e = assert_raises(filesystem.InsecurePathError, self.path.child, u'ev/il') eq( str(e), 'child name contains directory separator', ) ## Exception should be raised even if it's not evil (?) e = assert_raises(filesystem.InsecurePathError, self.path.child, u'notsoevil/')
def test_not_directory(): temp_dir = maketemp() # prepare a file on which to call the iterator f = open(os.path.join(temp_dir, "some_file"), "w") f.close() # check reaction on getting the iterator p = filesystem.copyonwrite.path(filesystem.path(temp_dir).join("some_file")) # note: the exception is only raised after calling ``next`` assert_raises(OSError, list, p)
def test_lstat(self): link = self.path.child(u'stat_test_link') self.path.child(u'doesnexist').symlink(link) lstats = link.lstat() assert(stat.S_ISLNK(lstats.st_mode)) ## assert a "sane" size (is this sane?) assert(lstats.st_size>0 and lstats.st_size<1024) self._verify_stats(lstats) assert_raises(OSError, link.stat)
def test_chown_lchown_raises_error(self): ## create a file and a link file = self._create_file(u'chown_test_file1') link = self.path.child('chown_test_link1') file.symlink(link) if not os.geteuid(): raise nose.SkipTest("this test can not be run as root") ## should give permission denied assert_raises(OSError, file.chown, 0) assert_raises(OSError, file.lchown, 0)
def test_open_for_writing(): tmp = maketemp() foo = os.path.join(tmp, u"foo") # write test content p = filesystem.copyonwrite.path(filesystem.path(foo)) assert_raises(IOError, open, foo) with p.open("w") as f: f.write("bar") # since this is the copy_on_write, the write should not be written # to the actual file system assert_raises(IOError, open, foo)
def test_join_with_leading_slash(): """ join should raise an exception if one tries to escape from the path by giving an absolute path """ e = assert_raises(filesystem.InsecurePathError, filesystem.path(u'/tmp').join, u'/usr') eq(str(e), 'path name to join must be relative')
def test_mkdir_bad_exists(): tmp = maketemp() p = filesystem.copyonwrite.path(filesystem.path(tmp)).child("foo") with p.open("w") as f: f.write("bar") e = assert_raises(OSError, p.mkdir) eq(e.errno, errno.EEXIST)
def test_rmdir_bad_notdir(self): p = self._create_file() e = assert_raises( OSError, p.rmdir, ) eq(e.errno, errno.ENOTDIR)
def test_rmdir_bad_notfound(self): p = self.path.child(u'foo') e = assert_raises( OSError, p.rmdir, ) eq(e.errno, errno.ENOENT)
def test_remove_notfound(self): p = self.path.child(u'foo') e = assert_raises( OSError, p.remove, ) eq(e.errno, errno.ENOENT)
def test_unlink_notfound(self): p = self.path.child(u'foo') e = assert_raises( OSError, p.unlink, ) eq(e.errno, errno.ENOENT)
def test_child_bad_dotdot(self): e = assert_raises(filesystem.InsecurePathError, self.path.child, u'..') eq( str(e), 'child trying to climb out of directory', ) ## of course, those should also raise errors assert_raises(filesystem.InsecurePathError, self.path.child, u'../') assert_raises(filesystem.InsecurePathError, self.path.child, u'..//') assert_raises(filesystem.InsecurePathError, self.path.child, u'..//..')
def test_mkdir(self): eq(list(self.path), []) self.path.child(u'foo').mkdir() eq(list(self.path), [self.path.child(u'foo')]) # create a new object, just in case .mkdir() stored something # in p eq(self.path.child(u'foo').isdir(), True) ## if the dir already exists, an error should be raised e = assert_raises(OSError, self.path.child(u'foo').mkdir) eq(e.errno, errno.EEXIST) ## but one can ask for this error not to be raised p = self.path.child(u'foo').mkdir(may_exist=True) ## mkdir will raise errors if the parent dirs doesnu't exist e = assert_raises( OSError, self.path.join(u'newdir/subdir1/subdir2').mkdir) eq(e.errno, errno.ENOENT) ## but one can ask for this error not to be raised self.path.join(u'newdir/subdir1/subdir2').mkdir(create_parents=True) eq(self.path.join(u'newdir/subdir1/subdir2').isdir(), True)
def test_size_of_nonexisting_item(self): p = self.path.child(u"non-existent-item") e = assert_raises(OSError, p.size) eq(e.errno, errno.ENOENT)
def test_readlink_on_regular_file(): source_name, target_name = set_up(absolute=False) p = filesystem.path(target_name) assert_raises(OSError, p.readlink)
def test_stat_missing_file(self): p = self.path.child('inexistent_file') if hasattr(p, 'stat'): e = assert_raises(OSError, p.stat) eq(e.errno, errno.ENOENT) assert(p.exists() is False)
def test_readlink_on_nonexistent_file(): p = filesystem.path(u"non-existent-file") assert_raises(OSError, p.readlink)
def test_join_wrong_parameters(): """join should raise an error if it doesn't get the right parameters""" assert_raises(TypeError, filesystem.root.join) assert_raises(AttributeError, filesystem.root.join, 32)
def test_mkdir_bad_exists_file(self): with self.path.child(u'foo').open('w') as f: f.write('FOO') e = assert_raises(OSError, self.path.child(u'foo').mkdir) eq(e.errno, errno.EEXIST)
def test_open_nonexisting(): p = filesystem.copyonwrite.path(filesystem.path(u"/does-not-exist")) e = assert_raises(IOError, p.open) eq(e.errno, errno.ENOENT)
def test_open_nonexisting(): p = filesystem.path(u'/does-not-exist') e = assert_raises(IOError, p.open) eq(e.errno, errno.ENOENT)