Exemple #1
0

if __name__ == "__main__":
    # Create a new folder
    folder = file_system.create(EntityTypes.FOLDER.value, 'my_folder', MAIN_DRIVE)
    folder2 = file_system.create(EntityTypes.FOLDER.value, 'my_folder2', MAIN_DRIVE)
    zip_file = file_system.create(EntityTypes.ZIP_FILE.value, 'my_zip', f'{MAIN_DRIVE}{SEPARATOR}my_folder')
    print(f'Parent of zip_file => {zip_file.parent.name}')
    text_file = file_system.create(EntityTypes.TEXT_FILE.value, 'my_txt_file', f'{MAIN_DRIVE}{SEPARATOR}my_folder')

    print('===============================')
    print('Printing the file system tree (with size)')
    print_file_system(file_system.drives[MAIN_DRIVE])
    print('===============================')

    moved_file = file_system.move(text_file.path, f'{MAIN_DRIVE}{SEPARATOR}my_folder2{SEPARATOR}my_moved_txt_file')

    print('===============================')
    print('File system tree (with size) after my_folder/my_txt_file moved to my_folder2/my_moved_txt_file')
    print_file_system(file_system.drives[MAIN_DRIVE])
    print('===============================')

    text_file2 = file_system.create(EntityTypes.TEXT_FILE.value, 'my_txt_file2', f'{MAIN_DRIVE}{SEPARATOR}my_folder')
    text_file3 = file_system.create(EntityTypes.TEXT_FILE.value, 'my_txt_file3', f'{MAIN_DRIVE}{SEPARATOR}my_folder'
                                                                                 f'{SEPARATOR}my_zip')

    file_system.write_to_file(text_file2.path, 'This is the content of my file')
    file_system.write_to_file(text_file3.path, 'This is the content of my other file')
    file_system.write_to_file(moved_file.path, 'Content')

    print('===============================')
class TestFileSystem(unittest.TestCase):
    def setUp(self):
        self.file_system = FileSystem()
        drive = self.file_system.get_root_node()
        self.test_folder = self.file_system.create(2, 'test_folder', drive.path)
        self.test_folder2 = self.file_system.create(2, 'test_folder2', drive.path)
        self.text_file1 = self.file_system.create(3, 'text_file1', self.test_folder.path)
        self.text_file2 = self.file_system.create(3, 'text_file2', self.test_folder.path)
        self.text_file3 = self.file_system.create(3, 'text_file3', self.test_folder2.path)
        self.zip_file1 = self.file_system.create(4, 'zip_file1', self.test_folder2.path)
        self.text_file4 = self.file_system.create(3, 'text_file4', self.zip_file1.path)

    def test_create_should_create_folder(self):
        type = EntityTypes.FOLDER.value
        name = 'testfolder'
        parent = self.test_folder
        created = self.file_system.create(type, name, parent.path)
        self.assertIn(created.name, parent.children)

    def test_create_should_create_file(self):
        type = EntityTypes.TEXT_FILE.value
        name = 'testfile'
        parent = self.test_folder
        created = self.file_system.create(type, name, parent.path)
        self.assertIn(created.name, parent.children)

    def test_create_should_not_allow_duplicate(self):
        type = EntityTypes.FOLDER.value
        name = 'test_folder'
        parent = self.file_system.get_root_node()
        with self.assertRaises(FileExistsError):
            self.file_system.create(type, name, parent.path)

    def test_create_should_not_allow_if_no_parent(self):
        type = EntityTypes.ZIP_FILE.value
        name = 'test'
        with self.assertRaises(FileNotFoundError):
            self.file_system.create(type, name, 'C:\\cant_find_me')

    def test_delete_should_delete_file(self):
        type = EntityTypes.TEXT_FILE.value
        name = 'testfile1'
        parent = self.test_folder
        created = self.file_system.create(type, name, parent.path)
        self.file_system.delete(created.path)
        self.assertNotIn(created.name, parent.children)

    def test_delete_should_error_if_not_found(self):
        with self.assertRaises(FileNotFoundError):
            self.file_system.delete('C:\\i_dont_exist')

    def test_move_should_move_file(self):
        type = EntityTypes.TEXT_FILE.value
        name = 'test_pre_move_file'
        moved_name = 'test_moved_file'
        parent = self.test_folder
        moved_parent = self.test_folder2
        created = self.file_system.create(type, name, parent.path)
        moved = self.file_system.move(created.path, f'{moved_parent.path}\\{moved_name}')
        self.assertNotIn(created.name, parent.children)
        self.assertIn(moved.name, moved_parent.children)

    def test_write_to_file_should_change_content(self):
        content = 'Test Content'
        self.file_system.write_to_file(self.text_file1.path, content)
        self.assertEqual(content, self.text_file1.content)

    def test_write_to_file_should_change_size(self):
        content = 'More Test Content'
        expected_size = len(content)
        self.file_system.write_to_file(self.text_file2.path, content)
        self.assertEqual(expected_size, self.text_file2.size)

    def test_write_to_file_should_change_parent_size(self):
        content = 'More Test Content'
        expected_size = len(content)
        self.file_system.write_to_file(self.text_file3.path, content)
        self.assertEqual(expected_size, self.test_folder2.size)
        
    def test_write_to_file_in_zip_file_should_change_parent_size(self):
        content = 'File inside zip file content'
        # Zip file should expect to be half the size of the inner contents
        expected_size = len(content) / 2
        self.file_system.write_to_file(self.text_file4.path, content)
        self.assertEqual(expected_size, self.zip_file1.size)
class FileSystemTests(IceCommon.TestCase):
    
    def removeDirectory(self, dir):
        if os.path.exists(dir):
            files = os.listdir(dir)
            for file in files:
                    file = dir + "/" + file
                    if os.path.isdir(file):
                            self.removeDirectory(file)
                    else:
                            os.remove(file)
            os.rmdir(dir)
        
    
    def setUp(self):
        self.__testDir = "tempTestDir"
        self.fs = FileSystem(self.__testDir)
        self.removeDirectory(self.__testDir)
        assert(os.path.exists(self.__testDir)==False)
        try: os.mkdir(self.__testDir)
        except: pass
    
    def tearDown(self):
        self.removeDirectory(self.__testDir)
        pass
    
    def testCurrentWorkingDirectory(self):
        testDir = os.path.abspath(self.__testDir).replace("\\", "/")
        self.assertEqual(self.fs.currentWorkingDirectory, testDir)

    #    absolutePath(path)
    def testAbsolutePath(self):
        # on windows root will equal "c:/"
        root = os.path.abspath("/").replace("\\", "/")
        #print "root='%s'" % root
        self.assertEqual(self.fs.absolutePath("."), self.fs.currentWorkingDirectory)
        self.assertEqual(self.fs.absolutePath(".."), os.path.abspath(".").replace("\\", "/"))
        self.assertEqual(self.fs.absolutePath("/"), root)
        self.assertEqual(self.fs.absolutePath("/test/one"), root + "test/one")
        self.assertEqual(self.fs.absolutePath("/test\\one"), root + "test/one")
    
    #    join(*args)
    def testJoin(self):
        self.assertEqual(self.fs.join("one", "two", "3"), "one/two/3")
        self.assertEqual(self.fs.join("one/", "two/"), "one/two/")
        self.assertEqual(self.fs.join("one", "/two"), "/two")
        self.assertEqual(self.fs.join(".", ".."), "./..")
        
    #    split(path)
    def testSplit(self):
        self.assertEqual(self.fs.split("one/two/three"), ("one/two", "three"))
        self.assertEqual(self.fs.split("one"), ("", "one"))
        self.assertEqual(self.fs.split("../."), ("..", "."))
        self.assertEqual(self.fs.split(""), ("", ""))
    
    #    splitExt(path)
    def testSplitExt(self):
        self.assertEqual(self.fs.splitExt("one/two"), ("one/two", ""))
        self.assertEqual(self.fs.splitExt("one.two.ext"), ("one.two", ".ext"))
        self.assertEqual(self.fs.splitExt("one/two.ext/three"), ("one/two.ext/three", ""))
        self.assertEqual(self.fs.splitExt(".ext"), ("", ".ext"))
        self.assertEqual(self.fs.splitExt(""), ("", ""))
    
    #    splitPathFileExt(path)\
    def testSplitPathFileExt(self):
        self.assertEqual(self.fs.splitPathFileExt("/one/two"), ("/one", "two", ""))
        self.assertEqual(self.fs.splitPathFileExt("one.two.ext"), ("", "one.two", ".ext"))
        self.assertEqual(self.fs.splitPathFileExt("one/two.ext/three"), ("one/two.ext", "three", ""))
        self.assertEqual(self.fs.splitPathFileExt(".ext"), ("", "", ".ext"))
        self.assertEqual(self.fs.splitPathFileExt(""), ("", "", ""))
    
    #    exists(path)
    def testExists(self):
        testFile = "testExistsFile"
        testDir = "testExistsDir"
        
        self.assertFalse(self.fs.exists(testFile))
        self.assertFalse(self.fs.exists(testDir))
        f = open(os.path.join(self.__testDir, testFile), "wb")
        f.write("Testing")
        f.close()
        os.mkdir(os.path.join(self.__testDir, testDir))
        self.assertTrue(self.fs.exists(testFile))
        self.assertTrue(self.fs.exists(testDir))
        
    
    #    isFile(path)
    def testIsFile(self):
        testFile = "testIsFile"
        testDir = "testIsFileDir"
        
        self.assertFalse(self.fs.isFile(testFile))
        self.assertFalse(self.fs.isFile(testDir))
        f = open(os.path.join(self.__testDir, testFile), "wb")
        f.write("Testing")
        f.close()
        os.mkdir(os.path.join(self.__testDir, testDir))
        self.assertTrue(self.fs.isFile(testFile))
        self.assertFalse(self.fs.isFile(testDir))
    
    #    isDirectory(path)
    def testIsDirectory(self):
        testFile = "testIsDirFile"
        testDir = "testIsDir"
        
        self.assertFalse(self.fs.isDirectory(testFile))
        self.assertFalse(self.fs.isDirectory(testDir))
        f = open(os.path.join(self.__testDir, testFile), "wb")
        f.write("Testing")
        f.close()
        os.mkdir(os.path.join(self.__testDir, testDir))
        self.assertFalse(self.fs.isDirectory(testFile))
        self.assertTrue(self.fs.isDirectory(testDir))
    
    #    makeDirectory(path)
    def testMakeDirectory(self):
        dir1 = "makeDir"
        dir2 = "makeDir2/subDir/subSubDir"
        self.assertFalse(self.fs.isDirectory(dir1))
        self.assertFalse(self.fs.isDirectory(dir2))
        self.fs.makeDirectory(dir1)
        self.fs.makeDirectory(dir2)
        self.assertTrue(self.fs.isDirectory(dir1))
        self.assertTrue(self.fs.isDirectory(dir2))
    
    #    delete(path)
    def testDelete(self):
        file = "deleteFile"
        dir = "dir/subDir/subSubDir"
        self.fs.writeFile(file, "testing")
        self.fs.writeFile(self.fs.join(dir, "file"), "test")
        self.assertTrue(self.fs.isDirectory(dir))
        self.assertTrue(self.fs.isFile(file))
        self.fs.delete(file)
        self.fs.delete(dir)
        self.assertFalse(self.fs.isDirectory(dir))
        self.assertFalse(self.fs.isFile(file))
    
    #    copy(fromPath, toPath)
    def testCopy(self):
        # Simple file copy test
        fromFile = "copyFile"
        toFile = "copyToFile"
        toDirFile = "dir/subDir/copyToFile"
        self.fs.writeFile(fromFile, "test data")
        self.assertFalse(self.fs.exists(toFile))
        self.fs.copy(fromFile, toFile)
        self.fs.copy(fromFile, toDirFile)
        self.assertTrue(self.fs.exists(toFile))
        self.assertEqual(self.fs.readFile(toFile), self.fs.readFile(fromFile))
        self.assertEqual(self.fs.readFile(toDirFile), self.fs.readFile(fromFile))
        
        # Directory, subDirectory and files copy test
        dir = "copyDir"
        toDir = "copyToDir"
        self.fs.writeFile("copyDir/subDir/file", "testing")
        self.assertFalse(self.fs.exists(toDir))
        self.fs.copy(dir, toDir)
        self.assertEqual(self.fs.readFile("copyDir/subDir/file"), self.fs.readFile("copyToDir/subDir/file"))
    
    #    move(fromPath, toPath)
    def testMove(self):
        # move file test
        file = "file"
        toFile = "toFile"
        toDirFile = "dir/subDir/toFile"
        self.fs.writeFile(file, "testing")
        self.fs.move(file, toFile)
        self.assertFalse(self.fs.isFile(file))
        self.assertTrue(self.fs.isFile(toFile))
        self.fs.move(toFile, toDirFile)
        self.assertFalse(self.fs.isFile(toFile))
        self.assertTrue(self.fs.isFile(toDirFile))
        
        # move dir test
        dir = "dirToMove"
        toDir = "movedDir"
        self.fs.writeFile(self.fs.join(dir, "file"), "testing")
        self.fs.move(dir, toDir)
        self.assertFalse(self.fs.isDirectory(dir))
        self.assertTrue(self.fs.isDirectory(toDir))
        self.assertEqual(self.fs.readFile(self.fs.join(toDir, "file")), "testing")
    
    
    #  walk(path)
    def testWalk(self):
        tree = ["./one.txt", "./test/two.txt", "./test/subDir/three.txt"]
        tree.sort()
        for f in tree:
            self.fs.writeFile(f, "testData")
        rFiles = []
        for path, dirs, files in self.fs.walk("."):
            self.assertTrue(path.endswith("/"))
            for file in files:
                rFiles.append(path + file)
        rFiles.sort()
        self.assertEqual(tree, rFiles)
    
    
    #  walker(path, func)
    def testWalker(self):
        tree = ["./one.txt", "./test/two.txt", "./test/subDir/three.txt"]
        tree.sort()
        for f in tree:
            self.fs.writeFile(f, "testData")
            
        def callback(path, dirs, files):
            self.assertTrue(path.endswith("/"))
            for file in files:
                rFiles.append(path + file)
            
        rFiles = []
        self.fs.walker(".", callback)
        rFiles.sort()
        self.assertEqual(tree, rFiles)
    
    
    #  createTempDirectory
    def testCreateTempDirectory(self):
        tempDir = self.fs.createTempDirectory()
        self.assertNotEqual(tempDir, None)
        self.assertTrue(len(str(tempDir))>1)
        self.assertTrue(tempDir.isTempDirectory)
        self.assertTrue(os.path.isdir(str(tempDir)))
        
        tempDir.delete()
        #print str(tempDir)
    
    #  writeFile(path, data)
    def testWriteFile(self):
        file = "writeFile"
        data = "Testing\nThe End.\n"
        self.assertFalse(self.fs.isFile(file))
        self.fs.writeFile(file, data)
        self.assertTrue(self.fs.isFile(file))
        self.assertEqual(self.fs.readFile(file), data)
    
    #  readFile(path)
    def testReadFile(self):
        file = "writeFile"
        data = "Testing\nThe End.\n"
        self.assertFalse(self.fs.isFile(file))
        self.fs.writeFile(file, data)
        self.assertTrue(self.fs.isFile(file))
        self.assertEqual(self.fs.readFile(file), data)
    
    #  zip(toZipFile, path="./")
    def testZip(self, data="TestData\n", files=None):
        pathToZip = "zipDir"
        if files is None:
            files = ["one.txt", "two.txt", "dir/three.txt", "dir2/subDir/four.txt"]
        zipFile = "test.zip"
        for file in files:
            path = self.fs.join(pathToZip, file)
            self.fs.writeFile(path, data)
        self.fs.zip(zipFile, pathToZip)
        self.assertTrue(self.fs.isFile(zipFile))
        
    
    #  unzipToTempDirectory(zipFile)
    def testUnzipToTempDirectory(self):
        self.testZip()
        tempDir = self.fs.unzipToTempDirectory("test.zip")
        self.assertTrue(tempDir.isFile("one.txt"))
        tempDir.delete()
        
    
    #  unzipToDirectory(zipFile, path)
    def testUnzipToDirectory(self):
        data="Testing\n123\n"
        self.testZip(data=data)
        self.fs.unzipToDirectory("test.zip", "temp")
        self.assertTrue(self.fs.isDirectory("temp"))
        self.assertEqual(self.fs.readFile("temp/dir2/subDir/four.txt"), data)
    
    
    def testZipList(self):
        files = ["one.txt", "two.txt", "dir/three.txt", "dir2/subDir/four.txt"]
        self.testZip(files=files)
        list = self.fs.zipList("test.zip")
        files.sort()
        list.sort()
        self.assertEqual(list, files)
    
    #  readFromZipFile(zipFile, filename)
    def testReadFromZipFile(self):
        data="Testing\n123\n"
        self.testZip(data=data)
        result = self.fs.readFromZipFile("test.zip", "one.txt")
        self.assertEqual(data, result)
        result = self.fs.readFromZipFile("test.zip", "dir2/subDir/four.txt")
        self.assertEqual(data, result)
    
    
    #  addToZipFile(zipFile, filename, data)
    def testAddToZipFile(self):
        files = ["one.txt", "two.txt", "dir/three.txt", "dir2/subDir/four.txt"]
        self.testZip(files=files)
        # Test adding a new File
        self.fs.addToZipFile("test.zip", "newFile.txt", "testing")
        files.append("newFile.txt")
        files.sort()
        rList = self.fs.zipList("test.zip")
        rList.sort()
        self.assertEqual(rList, files)
        # Test replacing a file
        self.fs.addToZipFile("test.zip", "one.txt", "replaceData")
        rList = self.fs.zipList("test.zip")
        rList.sort()
        self.assertEqual(rList, files)
        self.assertEqual(self.fs.readFromZipFile("test.zip", "one.txt"), "replaceData")
    
    
    #  list(path=".")
    def testList(self):
        dir = "listDir"
        files = ["one", "two", "three"]
        dirs = ["four", "five"]
        for f in files:
            self.fs.writeFile(self.fs.join(dir, f), "testing")
        for d in dirs:
            self.fs.makeDirectory(self.fs.join(dir, d))
        list = self.fs.list(dir)
        list.sort()
        all = files + dirs
        all.sort()
        self.assertEqual(list, all)
    
    #  listFiles(path=".")
    def testListFiles(self):
        dir = "listDir"
        files = ["one", "two", "three"]
        dirs = ["four", "five"]
        files.sort()
        dirs.sort()
        for f in files:
            self.fs.writeFile(self.fs.join(dir, f), "testing")
        for d in dirs:
            self.fs.makeDirectory(self.fs.join(dir, d))
        list = self.fs.listFiles(dir)
        list.sort()
        self.assertEqual(list, files)
    
    #  listDirectories(path=".")
    def testDirectories(self):
        dir = "listDir"
        files = ["one", "two", "three"]
        dirs = ["four", "five"]
        for f in files:
            self.fs.writeFile(self.fs.join(dir, f), "testing")
        for d in dirs:
            self.fs.makeDirectory(self.fs.join(dir, d))
        list = self.fs.listDirectories(dir)
        list.sort()
        dirs.sort()
        self.assertEqual(list, dirs)

    #  listDirsFiles(path=".")
    def testListDirsFiles(self):
        dir = "listDir"
        files = ["one", "two", "three"]
        dirs = ["four", "five"]
        files.sort()
        dirs.sort()
        for f in files:
            self.fs.writeFile(self.fs.join(dir, f), "testing")
        for d in dirs:
            self.fs.makeDirectory(self.fs.join(dir, d))
        rdirs, rfiles = self.fs.listDirsFiles(dir)
        rdirs.sort()
        rfiles.sort()
        self.assertEqual(rdirs, dirs)
        self.assertEqual(rfiles, files)