예제 #1
0
    def setUp(self):
        # Create a temporary directory
        self._test_dir = tempfile.mkdtemp()
        subdir = Path(self._test_dir).joinpath('subdir')
        subdir.mkdir(exist_ok=True)
        Path(self._test_dir).joinpath('f1.txt').write_text('xxx')
        Path(self._test_dir).joinpath('f2.txt').write_text('xxx')
        subdir.joinpath('s1.txt').write_text('xxx')

        # Mocks os.stat
        self._orig_os_stat = os.stat

        def fake_stat(path, *arg, **kwargs):
            return self._get_file_stat(self._orig_os_stat, path)

        os.stat = fake_stat

        self._fm = DefaultFileManager()
class DefaultFileManagerTest(unittest.TestCase):
    def setUp(self):
        # Create a temporary directory
        self._test_dir = tempfile.mkdtemp()
        subdir = Path(self._test_dir).joinpath('subdir')
        subdir.mkdir()
        Path(self._test_dir).joinpath('f1.txt').write_text('xxx')
        Path(self._test_dir).joinpath('f2.txt').write_text('xxx')
        subdir.joinpath('s1.txt').write_text('xxx')
        self.FILE_SIZE = 3

        self._fm = DefaultFileManager()

    def tearDown(self):
        # Remove the directory after the test
        shutil.rmtree(self._test_dir)

    def _get_temp_path(self, file_path: str = None) -> str:
        return str(Path(self._test_dir, file_path or '').absolute())

    def test_can_handle(self):
        self.assertTrue(self._fm.can_handle('/data/abc'))
        self.assertFalse(self._fm.can_handle('data'))

    def test_ls(self):
        # List file
        self.assertEqual(
            self._fm.ls(self._get_temp_path('f1.txt')),
            [File(path=self._get_temp_path('f1.txt'), size=self.FILE_SIZE)])
        # List folder
        self.assertEqual(
            sorted(self._fm.ls(self._get_temp_path()),
                   key=lambda file: file.path),
            sorted([
                File(path=self._get_temp_path('f1.txt'), size=self.FILE_SIZE),
                File(path=self._get_temp_path('f2.txt'), size=self.FILE_SIZE)
            ],
                   key=lambda file: file.path))
        # List folder recursively
        self.assertEqual(
            sorted(self._fm.ls(self._get_temp_path(), recursive=True),
                   key=lambda file: file.path),
            sorted([
                File(path=self._get_temp_path('f1.txt'), size=self.FILE_SIZE),
                File(path=self._get_temp_path('f2.txt'), size=self.FILE_SIZE),
                File(path=self._get_temp_path('subdir/s1.txt'),
                     size=self.FILE_SIZE),
            ],
                   key=lambda file: file.path))

    def test_move(self):
        # Moves to another folder
        self._fm.move(self._get_temp_path('f1.txt'),
                      self._get_temp_path('subdir/'))
        self.assertEqual(
            sorted(self._fm.ls(self._get_temp_path('subdir')),
                   key=lambda file: file.path),
            sorted([
                File(path=self._get_temp_path('subdir/s1.txt'),
                     size=self.FILE_SIZE),
                File(path=self._get_temp_path('subdir/f1.txt'),
                     size=self.FILE_SIZE),
            ],
                   key=lambda file: file.path))
        # Renames
        self._fm.move(self._get_temp_path('f2.txt'),
                      self._get_temp_path('f3.txt'))
        self.assertEqual(self._fm.ls(self._get_temp_path('f2.txt')), [])
        self.assertEqual(
            self._fm.ls(self._get_temp_path('f3.txt')),
            [File(path=self._get_temp_path('f3.txt'), size=self.FILE_SIZE)])

    def test_remove(self):
        self._fm.remove(self._get_temp_path('f1.txt'))
        self._fm.remove(self._get_temp_path('subdir'))
        self.assertEqual(
            self._fm.ls(self._get_temp_path(), recursive=True),
            [File(path=self._get_temp_path('f2.txt'), size=self.FILE_SIZE)])

    def test_copy(self):
        self._fm.copy(self._get_temp_path('f1.txt'),
                      self._get_temp_path('subdir'))
        self.assertEqual(
            self._fm.ls(self._get_temp_path('f1.txt')),
            [File(path=self._get_temp_path('f1.txt'), size=self.FILE_SIZE)])
        self.assertEqual(self._fm.ls(self._get_temp_path('subdir/f1.txt')), [
            File(path=self._get_temp_path('subdir/f1.txt'),
                 size=self.FILE_SIZE)
        ])

    def test_mkdir(self):
        self._fm.mkdir(os.path.join(self._get_temp_path(), 'subdir2'))
        self.assertTrue(os.path.isdir(self._get_temp_path('subdir2')))
예제 #3
0
class DefaultFileManagerTest(unittest.TestCase):
    _F1_SIZE = 3
    _F2_SIZE = 4
    _S1_SIZE = 55
    _F1_MTIME = 1613982390
    _F2_MTIME = 1613982391
    _S1_MTIME = 1613982392

    def _get_file_stat(self, orig_os_stat, path):
        faked = list(orig_os_stat(path))
        if path == self._get_temp_path('f1.txt') or \
                path == self._get_temp_path('subdir/f1.txt'):
            faked[stat.ST_SIZE] = self._F1_SIZE
            faked[stat.ST_MTIME] = self._F1_MTIME
            return os.stat_result(faked)
        elif path == self._get_temp_path('f2.txt') or \
                path == self._get_temp_path('f3.txt'):
            faked[stat.ST_SIZE] = self._F2_SIZE
            faked[stat.ST_MTIME] = self._F2_MTIME
            return os.stat_result(faked)
        elif path == self._get_temp_path('subdir/s1.txt'):
            faked[stat.ST_SIZE] = self._S1_SIZE
            faked[stat.ST_MTIME] = self._S1_MTIME
            return os.stat_result(faked)
        else:
            return orig_os_stat(path)

    def setUp(self):
        # Create a temporary directory
        self._test_dir = tempfile.mkdtemp()
        subdir = Path(self._test_dir).joinpath('subdir')
        subdir.mkdir(exist_ok=True)
        Path(self._test_dir).joinpath('f1.txt').write_text('xxx')
        Path(self._test_dir).joinpath('f2.txt').write_text('xxx')
        subdir.joinpath('s1.txt').write_text('xxx')

        # Mocks os.stat
        self._orig_os_stat = os.stat

        def fake_stat(path, *arg, **kwargs):
            return self._get_file_stat(self._orig_os_stat, path)

        os.stat = fake_stat

        self._fm = DefaultFileManager()

    def tearDown(self):
        os.stat = self._orig_os_stat
        # Remove the directory after the test
        shutil.rmtree(self._test_dir)

    def _get_temp_path(self, file_path: str = None) -> str:
        return str(Path(self._test_dir, file_path or '').absolute())

    def test_can_handle(self):
        self.assertTrue(self._fm.can_handle('/data/abc'))
        self.assertFalse(self._fm.can_handle('data'))

    def test_ls(self):
        # List file
        self.assertEqual(self._fm.ls(self._get_temp_path('f1.txt')), [
            File(path=self._get_temp_path('f1.txt'),
                 size=self._F1_SIZE,
                 mtime=self._F1_MTIME)
        ])
        # List folder
        self.assertEqual(
            sorted(self._fm.ls(self._get_temp_path()),
                   key=lambda file: file.path),
            sorted([
                File(path=self._get_temp_path('f1.txt'),
                     size=self._F1_SIZE,
                     mtime=self._F1_MTIME),
                File(path=self._get_temp_path('f2.txt'),
                     size=self._F2_SIZE,
                     mtime=self._F2_MTIME)
            ],
                   key=lambda file: file.path))
        # List folder recursively
        self.assertEqual(
            sorted(self._fm.ls(self._get_temp_path(), recursive=True),
                   key=lambda file: file.path),
            sorted([
                File(path=self._get_temp_path('f1.txt'),
                     size=self._F1_SIZE,
                     mtime=self._F1_MTIME),
                File(path=self._get_temp_path('f2.txt'),
                     size=self._F2_SIZE,
                     mtime=self._F2_MTIME),
                File(path=self._get_temp_path('subdir/s1.txt'),
                     size=self._S1_SIZE,
                     mtime=self._S1_MTIME),
            ],
                   key=lambda file: file.path))

    def test_move(self):
        # Moves to another folder
        self._fm.move(self._get_temp_path('f1.txt'),
                      self._get_temp_path('subdir/'))
        self.assertEqual(
            sorted(self._fm.ls(self._get_temp_path('subdir')),
                   key=lambda file: file.path),
            sorted([
                File(path=self._get_temp_path('subdir/s1.txt'),
                     size=self._S1_SIZE,
                     mtime=self._S1_MTIME),
                File(path=self._get_temp_path('subdir/f1.txt'),
                     size=self._F1_SIZE,
                     mtime=self._F1_MTIME),
            ],
                   key=lambda file: file.path))
        # Renames
        self._fm.move(self._get_temp_path('f2.txt'),
                      self._get_temp_path('f3.txt'))
        self.assertEqual(self._fm.ls(self._get_temp_path('f2.txt')), [])
        self.assertEqual(self._fm.ls(self._get_temp_path('f3.txt')), [
            File(path=self._get_temp_path('f3.txt'),
                 size=self._F2_SIZE,
                 mtime=self._F2_MTIME)
        ])

    def test_remove(self):
        self._fm.remove(self._get_temp_path('f1.txt'))
        self._fm.remove(self._get_temp_path('subdir'))
        self.assertEqual(self._fm.ls(self._get_temp_path(), recursive=True), [
            File(path=self._get_temp_path('f2.txt'),
                 size=self._F2_SIZE,
                 mtime=self._F2_MTIME)
        ])

    def test_copy(self):
        self._fm.copy(self._get_temp_path('f1.txt'),
                      self._get_temp_path('subdir'))
        self.assertEqual(self._fm.ls(self._get_temp_path('f1.txt')), [
            File(path=self._get_temp_path('f1.txt'),
                 size=self._F1_SIZE,
                 mtime=self._F1_MTIME)
        ])
        self.assertEqual(self._fm.ls(self._get_temp_path('subdir/f1.txt')), [
            File(path=self._get_temp_path('subdir/f1.txt'),
                 size=self._F1_SIZE,
                 mtime=self._F1_MTIME)
        ])

    def test_mkdir(self):
        self._fm.mkdir(os.path.join(self._get_temp_path(), 'subdir2'))
        self.assertTrue(os.path.isdir(self._get_temp_path('subdir2')))
예제 #4
0
class DefaultFileManagerTest(unittest.TestCase):
    def setUp(self):
        # Create a temporary directory
        self._test_dir = tempfile.mkdtemp()
        Path(self._test_dir).joinpath('f1.txt').touch()
        Path(self._test_dir).joinpath('f2.txt').touch()
        subdir = Path(self._test_dir).joinpath('subdir')
        subdir.mkdir()
        subdir.joinpath('s1.txt').touch()

        self._fm = DefaultFileManager()

    def tearDown(self):
        # Remove the directory after the test
        shutil.rmtree(self._test_dir)

    def _get_temp_path(self, file_path: str = None) -> str:
        return str(Path(self._test_dir, file_path or '').absolute())

    def test_can_handle(self):
        self.assertTrue(self._fm.can_handle('/data/abc'))
        self.assertFalse(self._fm.can_handle('data'))

    def test_ls(self):
        # List file
        self.assertEqual(self._fm.ls(self._get_temp_path('f1.txt')),
                         [self._get_temp_path('f1.txt')])
        # List folder
        self.assertEqual(
            sorted(self._fm.ls(self._get_temp_path())),
            sorted(
                [self._get_temp_path('f1.txt'),
                 self._get_temp_path('f2.txt')]))
        # List folder recursively
        self.assertEqual(
            sorted(self._fm.ls(self._get_temp_path(), recursive=True)),
            sorted([
                self._get_temp_path('f1.txt'),
                self._get_temp_path('f2.txt'),
                self._get_temp_path('subdir/s1.txt')
            ]))

    def test_move(self):
        # Moves to another folder
        self._fm.move(self._get_temp_path('f1.txt'),
                      self._get_temp_path('subdir/'))
        self.assertEqual(
            sorted(self._fm.ls(self._get_temp_path('subdir'))),
            sorted([
                self._get_temp_path('subdir/s1.txt'),
                self._get_temp_path('subdir/f1.txt')
            ]))
        # Renames
        self._fm.move(self._get_temp_path('f2.txt'),
                      self._get_temp_path('f3.txt'))
        self.assertEqual(self._fm.ls(self._get_temp_path('f2.txt')), [])
        self.assertEqual(self._fm.ls(self._get_temp_path('f3.txt')),
                         [self._get_temp_path('f3.txt')])

    def test_remove(self):
        self._fm.remove(self._get_temp_path('f1.txt'))
        self._fm.remove(self._get_temp_path('subdir'))
        self.assertEqual(self._fm.ls(self._get_temp_path(), recursive=True),
                         [self._get_temp_path('f2.txt')])