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_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_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_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_bind(): """ Testing the multiplexing file system (TODO: split up into multiple tests, clean up) """ mp_root = filesystem.multiplexing.path() mountpoint = mp_root.join('mnt/tmp/') real_pathname = maketemp() mountpoint.bind(filesystem.path(real_pathname)) real_path = filesystem.path(real_pathname) ## TODO: RFC: I'm not sure whether it should pass or not since the ## parent differs. #eq(real_path, mountpoint) #eq(mountpoint, real_path) ne(real_path, mountpoint) ne(mountpoint, real_path) ## mountpoint.parent should give the parent node in the multiplexing fs ## real_path.parent should give the real parent directory ne(mountpoint.parent(), real_path.parent()) assert mountpoint.isdir() assert real_path.isdir() ## Let's make a subdirectory under our "mounted" directory foo = mp_root.join('mnt/tmp/foo') foo2 = mountpoint.child('foo') real_foo = real_path.child('foo') ## same directory, same parent eq(foo, foo2) ## same directory, different parents #eq(foo, real_foo) ne(foo, real_foo) assert not foo.exists() assert not foo2.exists() assert not real_foo.exists() foo.mkdir() assert foo.isdir() assert foo2.isdir() assert real_foo.isdir() ## same directory, different parent #eq(real_foo, foo2) ne(real_foo, foo2)
def test_open_for_reading(): tmp = maketemp() foo = os.path.join(tmp, u"foo") # write file with Python's standard API ... with open(foo, "w") as f: f.write("bar") # ... and read it back with our fs code p = filesystem.copyonwrite.path(filesystem.path(foo)) with p.open() as f: got = f.read() eq(got, "bar")
def add_directory(mpq, from_path, from_name = fs.path('')): src_path = from_path / from_name for name in os.listdir(str(src_path)): file_path = src_path / name if fs.is_directory(file_path): add_directory(mpq, from_path, from_name / name) else: try: mpq.AddFileEx(str(file_path).encode('ascii'), str(from_name / name).encode('ascii')) except: pass
def test_open_for_reading(): tmp = maketemp() foo = os.path.join(tmp, u'foo') # write file with Python's standard API ... with open(foo, 'w') as f: f.write('bar') # ... and read it back with our fs code p = filesystem.path(foo) with p.open() as f: got = f.read() eq(got, 'bar')
def test_open_for_writing(): tmp = maketemp() foo = os.path.join(tmp, u'foo') # write test content p = filesystem.path(foo) with p.open('w') as f: f.write('bar') # read back in and compare with open(foo) as f: got = f.read() eq(got, 'bar')
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_iter(): temp_dir = maketemp() temp_files = ["file1", "file2"] # put some files in the temporary directory for i in temp_files: f = open(os.path.join(temp_dir, i), "w") f.close() p = filesystem.copyonwrite.path(filesystem.path(temp_dir)) ## add one more file with p.child("file3").open("w") as f: f.write("ubba") temp_files.append("file3") # see whether we actually get the file names with the iterator eq(sorted(str(x) for x in p), sorted(str(p.child(x)) for x in temp_files))
def test_new_object(): p = filesystem.path(u"/") c = p.child(u"segment1", u"segment2") assert p is not c eq(unicode(p), u"/") eq(unicode(c), u"/segment1/segment2")
def test_mkdir(): tmp = maketemp() filesystem.copyonwrite.path(filesystem.path(tmp)).child("foo").mkdir() foo = os.path.join(tmp, "foo") assert not os.path.isdir(foo)
def test_open_nonexisting(): p = filesystem.path(u'/does-not-exist') e = assert_raises(IOError, p.open) eq(e.errno, errno.ENOENT)
def test_relative_target(): source_name, target_name = set_up(absolute=False) p = filesystem.path(source_name) eq(p.readlink(), target_name)
def setUp(self): real_path = filesystem.path(maketemp()) self.path = filesystem.multiplexing.path() self.path.bind(real_path) assert self.path.exists()
def test_repr(): p = filesystem.path(u"/test\xe4") assert isinstance(repr(p), str)
def test_parent_of_curdir(): p = filesystem.path('.') eq(p.parent(), p)
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_str_simple(): p = filesystem.path(u'/foo') got = str(p) eq(got, u'/foo')
def test_readlink_on_nonexistent_file(): p = filesystem.path(u"non-existent-file") assert_raises(OSError, p.readlink)
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 setUp(self): real_path = filesystem.path(maketemp()) self.path = filesystem.copyonwrite.path(real_path) assert self.path.exists()
def test_path_unicode_argument(): p = filesystem.path(u"/") assert isinstance(str(p), str) assert isinstance(unicode(p), unicode)
def test_parent_of_relative(): p = filesystem.path('foo') eq(p.parent(), filesystem.path('.'))
def setUp(self): self.path = filesystem.path(maketemp())
def test_parent_of_relative_subdir(): p = filesystem.path('foo/bar') eq(p.parent(), filesystem.path('foo'))
def test_name_simple(): eq(filesystem.path("foo/bar").name(), "bar")
def test_absolute_target(): source_name, target_name = set_up(absolute=True) p = filesystem.path(source_name) eq(p.readlink(), target_name)