def test_vfs_write_stream(self): vfs = VirtualFileSystem(root_path=".", readonly=False) with vfs.open_write("unittest.txt") as f: f.write("test write") self.assertEqual("test write", vfs["unittest.txt"].data) with vfs.open_write("unittest.txt", append=False) as f: f.write("overwritten") self.assertEqual("overwritten", vfs["unittest.txt"].data) with vfs.open_write("unittest.txt", append=True) as f: f.write("appended") self.assertEqual("overwrittenappended", vfs["unittest.txt"].data) del vfs["unittest.txt"]
def test_vfs_load_and_names(self): vfs = VirtualFileSystem(root_package="os") with self.assertRaises(VfsError): _ = vfs["a\\b"] with self.assertRaises(VfsError): _ = vfs["/abs/path"] with self.assertRaises(IOError): _ = vfs["normal/text"] with self.assertRaises(IOError): _ = vfs["normal/image"] vfs = VirtualFileSystem(root_path=".") with self.assertRaises(IOError): _ = vfs["test_doesnt_exist_999.txt"] with self.assertRaises(VfsError): _ = VirtualFileSystem(root_path="@@@does/not/exist.foo@@@") with self.assertRaises(VfsError): _ = VirtualFileSystem(root_package="non.existing.package.name") with self.assertRaises(VfsError): _ = VirtualFileSystem(root_package="non_existing_package_name")
def test_vfs_storage(self): with self.assertRaises(ValueError): _ = VirtualFileSystem(root_package="os", readonly=False) vfs = VirtualFileSystem(root_path=".", readonly=False) with self.assertRaises(IOError): _ = vfs["test_doesnt_exist_999.txt"] vfs["unittest.txt"] = "Test1\nTest2\n" rsc = vfs["unittest.txt"] self.assertEqual("Test1\nTest2\n", rsc.data) self.assertEqual("text/plain", rsc.mimetype) self.assertEqual(12, len(rsc)) self.assertEqual("unittest.txt", rsc.name) vfs["unittest.txt"] = "Test1\nTest2\n" rsc = vfs["unittest.txt"] self.assertEqual("Test1\nTest2\n", rsc.data) vfs["unittest.jpg"] = b"imagedata\nblob" rsc = vfs["unittest.jpg"] self.assertEqual(b"imagedata\nblob", rsc.data) self.assertTrue(rsc.mimetype in ("image/jpeg", "image/pjpeg")) self.assertEqual(14, len(rsc)) self.assertEqual("unittest.jpg", rsc.name) vfs["unittest.jpg"] = rsc del vfs["unittest.txt"] del vfs["unittest.jpg"]
def test_vfs_readonly(self): vfs = VirtualFileSystem(root_path=".") with self.assertRaises(VfsError): vfs.open_write("test.txt") with self.assertRaises(VfsError): vfs["test.txt"] = "data"
def test_vfs_validate_path(self): vfs = VirtualFileSystem(root_path=".") vfs.validate_path(".") vfs.validate_path("./foo") vfs.validate_path("./foo/bar") vfs.validate_path(".") with self.assertRaises(VfsError): vfs.validate_path(r".\wrong\slash") with self.assertRaises(VfsError): vfs.validate_path(r"/absolute/not/allowed") with self.assertRaises(VfsError): vfs.validate_path(r"./foo/../../../../../rootescape/notallowed")