def test_dir(self): testdir = URI('testdir') testdir.makedirs() self.assertTrue(testdir.exists()) self.assertTrue(testdir.isdir()) self.assertTrue(not testdir.isfile()) testfile = URI('testdir/somefile') create_file(testfile, content='test') testdir.remove(recursive=True) self.assertTrue(not testdir.exists()) self.assertTrue(not testfile.exists()) testdir = URI(self.existing_dir) self.assertTrue(testdir.exists()) self.assertTrue(testdir.isdir())
class TestWritingZip(ZipTestCase): def setUp(self): super(TestWritingZip, self).setUp() self.zip_uri = 'file://./file.zip' self.zip_path = URI(self.zip_uri) def tearDown(self): # reset the connection registry, otherwise the zip file cannot # be deleted on windows since it is still opened by the # backend CONNECTION_REGISTRY.cleanup(force=True) if self.zip_path.exists(): self.zip_path.remove() def test_write_file_to_non_existing_zip(self): foo = URI('zip://((%s))/foo.txt' % self.zip_uri) with foo.open('wb') as fd: fd.write(b'bar') def test_write_file_to_non_existing_zip_2(self): foo = URI('zip://((%s))/deeper/foo.txt' % self.zip_uri) with foo.open('wb') as fd: fd.write(b'bar') def test_write_two_files(self): foo = URI('zip://((%s))/foo.txt' % self.zip_uri) with foo.open('wb') as fd: fd.write(b'bar') bar = URI('zip://((%s))/bar.txt' % self.zip_uri) with bar.open('wb') as fd: fd.write(b'foo')
def tearDown(self): p = URI("test.txt") if p.exists(): p.remove() l = URI("test_link") if l.islink(): l.remove()
def test_file(self): path = URI(self.baseurl) / 'testfile.txt' create_file(path, content='hallo') content = load_file(path) self.assertEqual(content, 'hallo') self.assertTrue(path.exists()) self.assertTrue(path.isfile()) path.remove() self.assertTrue(not path.exists()) path = URI(self.existing_file) self.assertTrue(path.exists()) self.assertTrue(path.isfile()) content = load_file(path) self.assertTrue(content)
def test_locking(self): try: p = URI("lock.txt") content = "I'm something written into a locked file" with p.lock() as inf: inf.write(content) self.assertEqual(p.open().read(), content) finally: if p.exists(): p.remove()
def test_copy_and_move_file(self): single_file = URI(self.non_existing_file) target_file = URI(self.baseurl) / 'target_file.txt' create_file(single_file) single_file.copy(target_file) self.assertTrue(target_file.exists()) self.assertTrue(target_file.isfile()) self.assertEqual(load_file(target_file), 'content') target_file.remove() self.assertTrue(not target_file.exists()) single_file.move(target_file) self.assertTrue(not single_file.exists()) self.assertTrue(target_file.exists()) self.assertTrue(target_file.isfile()) self.assertEqual(load_file(target_file), 'content') target_file.remove()
class TestReadingZip(ZipTestCase): def setUp(self): super(TestReadingZip, self).setUp() self.zip_path = URI('memory:///file.zip') zip_handle = self.zip_path.open('wb') try: self.fp_zip = ZipFile(zip_handle, 'w') self.fp_zip.writestr('/foo.txt', 'bar') self.fp_zip.close() finally: zip_handle.close() def tearDown(self): if self.zip_path.exists(): self.zip_path.remove() def test_read_a_file(self): p = URI('zip://((memory:///file.zip))/foo.txt') with p.open('rb') as fd: self.assertEqual(fd.read(), b'bar') def test_write_a_file(self): p = URI('zip://((memory:///file.zip))/bar.txt') with p.open('wb') as fd: fd.write(b'foo') with p.open() as fd: self.assertEqual(fd.read(), b'foo') def test_exists(self): p = URI('zip://((memory:///file.zip))/foo.txt') with p.open('wb') as fd: fd.write(b'foo') self.assertTrue(p.exists()) def test_isfile(self): p = URI('zip://((memory:///file.zip))/foo.txt') with p.open('wb') as fd: fd.write(b'foo') self.assertTrue(p.isfile()) def test_isdir(self): dir_path = URI('zip://((memory:///file.zip))/somedir') p = dir_path / 'foo.txt' with p.open('wb') as fd: fd.write(b'foo') self.assertTrue(dir_path.isdir()) def test_path(self): dir_path = URI('zip://((memory:///file.zip))/somedir') self.assertEqual(dir_path.path, '/somedir') new_path = dir_path / 'other' self.assertEqual(new_path.path, '/somedir/other')
def test_copy_and_move_dir(self): folder = URI(self.baseurl) / 'folder' folder.makedirs() self.assertTrue(folder.isdir()) afile = folder / 'afile.txt' create_file(afile) target = URI(self.baseurl) / 'target' self.assertTrue(not target.exists()) folder.copy(target, recursive=True) self.assertTrue(target.exists()) target_file = target / 'afile.txt' self.assertTrue(target_file.exists()) self.assertEqual(load_file(target_file), 'content') target.remove(recursive=True) self.assertTrue(not target.exists()) target.makedirs() folder.copy(target, recursive=True) newtarget = target / 'folder' self.assertTrue(newtarget.exists()) self.assertTrue(newtarget.isdir()) newtarget_file = newtarget / 'afile.txt' self.assertTrue(newtarget_file.exists()) self.assertTrue(newtarget_file.isfile()) target.remove(recursive=True) folder.move(target) self.assertTrue(not folder.exists()) self.assertTrue(target.exists()) self.assertTrue(target.isdir()) self.assertTrue(target_file.exists()) self.assertTrue(target_file.isfile()) target.remove(recursive=True)
def test_remove_recursive_with_readonly_file(self): foo_path = URI(self.baseurl) / 'foo' bar_path = foo_path / 'bar' bar_path.makedirs() gaz_path = bar_path / 'ghaz.txt' create_file(gaz_path) mode = gaz_path.info().mode gaz_path.info(set_info=dict(mode=mode & ~stat.S_IWUSR)) foo_path.remove(recursive=True) self.assertTrue(not foo_path.exists())
class TestListDir(ZipTestCase): def setUp(self): super(TestListDir, self).setUp() self.zip_path = URI('memory:///file.zip') def tearDown(self): if self.zip_path.exists(): self.zip_path.remove() def test_listdir(self): base_path = URI('zip://((%s))/' % self.zip_path.uri) self.assertEqual(base_path.listdir(), []) p1 = URI('zip://((%s))/foo.txt' % self.zip_path.uri) with p1.open('wb') as fd: fd.write(b'foo') self.assertEqual(base_path.listdir(), ['foo.txt']) p2 = URI('zip://((%s))/dir/foo.txt' % self.zip_path.uri) with p2.open('w') as fd: fd.write(b'foo') self.assertEqual(set(base_path.listdir()), set(['foo.txt', 'dir']))
def test_exists(self): p = URI('zip://((memory:///file.zip))/foo.txt') with p.open('wb') as fd: fd.write(b'foo') self.assertTrue(p.exists())