Exemplo n.º 1
0
    def test_cli_revert_file(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        rename_kwargs = {
            'settings_json': {
                'CHECK_DIRS': dirs,
                "DIR_FORMATS": {
                    "LETTER_CASE": "upper_case"
                }
            },
            'rename': True,
            'rename_backup': True,
        }
        for backup_file in driver.find_recent_backup_files():
            os.remove(backup_file)
        assert driver.find_recent_backup_files() == []
        driver.entry(**rename_kwargs)
        assert driver.find_recent_backup_files() != []
        revert_kwargs = {
            'revert': True,
            'revert_file': driver.result['rename_backup_name']
        }

        driver.entry(**revert_kwargs)

        assert driver.result['errors'] == []
Exemplo n.º 2
0
    def test_cli_rename_backup_path_to_file(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        backup_filename = os.sep.join([dirs[0], '123'])
        kwargs = {
            'settings_json': {
                'CHECK_DIRS': dirs,
                "DIR_FORMATS": {
                    "LETTER_CASE": "upper_case",
                },
            },
            'rename': True,
            'rename_backup_path': backup_filename,
        }
        logger().debug('before')
        logger().info(os.listdir(dirs[0]))
        assert os.path.isdir(dirs[0])
        assert not os.path.isfile(backup_filename)

        driver.entry(**kwargs)

        logger().debug('after')
        logger().info(driver.result['rename_backup_name'])
        if not os.path.isdir(dirs[0]):
            dirs[0] = os.sep.join(dirs[0].split(os.sep)[:-1] +
                                  [dirs[0].split(os.sep)[-1].upper()])
            backup_filename = os.sep.join([dirs[0], '123'])
            assert os.path.isdir(dirs[0])
        assert os.path.isfile(backup_filename)
        assert driver.result['errors'] == []
Exemplo n.º 3
0
    def test_cli_revert_last_existed(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        rename_kwargs = {
            'settings_json': {
                'CHECK_DIRS': dirs,
                "DIR_FORMATS": {
                    "LETTER_CASE": "upper_case"
                }
            },
            'rename': True,
            'rename_backup': True,
        }
        revert_kwargs = {'revert': True, 'revert_last': True}
        for backup_file in driver.find_recent_backup_files():
            os.remove(backup_file)
        assert driver.find_recent_backup_files() == []

        # rename 1
        driver.entry(**rename_kwargs)
        assert len(driver.find_recent_backup_files()) == 1
        first_backup = driver.result['rename_backup_name']

        # rename 2
        rename_kwargs['settings_json']['DIR_FORMATS']['LETTER_CASE'] = (
            'lower_case')
        # prevent write to same filename
        time.sleep(1)
        driver.entry(**rename_kwargs)
        assert len(driver.find_recent_backup_files()) == 2
        second_backup = driver.result['rename_backup_name']

        # revert 2
        driver.entry(**revert_kwargs)
        os.remove(second_backup)
        assert len(driver.find_recent_backup_files()) == 1

        # revert 1
        driver.entry(**revert_kwargs)

        os.remove(first_backup)
        assert driver.result['errors'] == []
Exemplo n.º 4
0
    def test_cli_rename(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        kwargs = {
            'settings_json': {
                'CHECK_DIRS': dirs,
                "DIR_FORMATS": {
                    "LETTER_CASE": "upper_case",
                    "SEP": ["dash_to_underscore"],
                },
            },
            'rename': True,
        }

        driver.entry(**kwargs)

        assert driver.result['errors'] == []
Exemplo n.º 5
0
    def test_cli_init_as_file(self):
        driver = Driver()
        dirs = helper.mkdtemps(1,
                               root=self.TMP_ROOT,
                               prefix=self.TMPFILE_PREFIX)
        init_filename = os.path.join(dirs[0], '123.json')
        recover_filename = None
        kwargs = {
            'init': True,
            'init_path': init_filename,
        }
        if os.path.exists(init_filename):
            recover_filename = self.temporarity_move_path(init_filename)

        driver.entry(**kwargs)

        assert os.path.exists(init_filename)
        assert driver.result['errors'] == []
        if recover_filename is not None:
            os.rename(recover_filename, init_filename)
Exemplo n.º 6
0
    def test_cli_init_under_dir(self):
        driver = Driver()
        init_dir = helper.mkdtemps(1,
                                   root=self.TMP_ROOT,
                                   prefix=self.TMPFILE_PREFIX)[0]
        init_filename = os.sep.join([init_dir, 'settings.json'])
        recover_filename = None
        kwargs = {
            'init': True,
            'init_path': init_dir,
        }
        if os.path.exists(init_filename):
            recover_filename = self.temporarity_move_path(init_filename)

        driver.entry(**kwargs)

        assert os.path.exists(init_filename)
        assert driver.result['errors'] == []
        if recover_filename is not None:
            os.rename(recover_filename, init_filename)
Exemplo n.º 7
0
    def test_sort_path_pair(self):
        # only tests for directories
        am = ArchieveManager()
        errors = []
        # create some temp directories under different hierarchy
        expt_hrchy1_paths = helper.mkdtemps(1, prefix=self.TMPFILE_PREFIX,
                                            root=self.TMP_ROOT)
        expt_hrchy2_paths = (
            helper.mkdtemps(2, root=expt_hrchy1_paths[0],
                            prefix=self.TMPFILE_PREFIX) +
            helper.mkdtemps(1, root=expt_hrchy1_paths[0],
                            prefix=self.TMPFILE_PREFIX))
        expt_hrchy3_paths = (
            helper.mkdtemps(1, root=expt_hrchy2_paths[0],
                            prefix=self.TMPFILE_PREFIX) +
            helper.mkdtemps(1, root=expt_hrchy2_paths[1],
                            prefix=self.TMPFILE_PREFIX) +
            helper.mkdtemps(1, root=expt_hrchy2_paths[1],
                            prefix=self.TMPFILE_PREFIX))
        src_dst_pairs = self._gen_src_dst_rename_pairs(expt_hrchy1_paths +
                                                       expt_hrchy2_paths +
                                                       expt_hrchy3_paths)

        for perm_pair in itertools.permutations(src_dst_pairs):
            actl_pairs = am._sort_path_pair(perm_pair, reverse=True)

            actl_hrchy1_pairs = actl_pairs[-len(expt_hrchy1_paths):]
            actl_hrchy2_pairs = actl_pairs[
                len(expt_hrchy3_paths):-len(expt_hrchy1_paths)]
            actl_hrchy3_pairs = actl_pairs[:len(expt_hrchy3_paths)]

            actl_hrchy1_paths = [path[0] for path in actl_hrchy1_pairs]
            actl_hrchy2_paths = [path[0] for path in actl_hrchy2_pairs]
            actl_hrchy3_paths = [path[0] for path in actl_hrchy3_pairs]

            for expt, actl in ((expt_hrchy1_paths, actl_hrchy1_paths),
                               (expt_hrchy2_paths, actl_hrchy2_paths),
                               (expt_hrchy3_paths, actl_hrchy3_paths)):
                helper.append_to_error_if_not_expect_with_msg(
                    errors, helper.is_same_disorderly(expt, actl),
                    "The path not sort by hrchy"
                    "\nExpect:\n{0}\nActual:\n{1}".format(
                        json.dumps(expt, indent=4, sort_keys=True),
                        json.dumps(actl, indent=4, sort_keys=True)))

            assert errors == [], Exception(helper.get_error_string(errors))
Exemplo n.º 8
0
    def test_import_settings(self):
        driver = Driver()
        init_dir = helper.mkdtemps(1,
                                   root=self.TMP_ROOT,
                                   prefix=self.TMPFILE_PREFIX)[0]
        init_filename = os.sep.join([init_dir, 'settings.json'])
        recover_filename = None
        kwargs = {
            'init': True,
            'init_path': init_dir,
        }
        expect = enums.SETTINGS
        if os.path.exists(init_filename):
            recover_filename = self.temporarity_move_path(init_filename)

        driver.entry(**kwargs)
        actual = driver.import_settings(init_filename)

        assert helper.is_same_disorderly(expect, actual)
        assert driver.result['errors'] == []
        os.remove(init_filename)
        if recover_filename is not None:
            os.rename(recover_filename, init_filename)