Esempio n. 1
0
class TestPathFactory(BasicEnvironment):
    def setUp(self):
        BasicEnvironment.init_environment(self, test_dir=os.path.join(os.path.sep, 'tmp', 'ntx_unit_test'))

        git = GitWrapperI(git_dir=self._test_git_dir, commit='ad45ba8')
        config = ConfigI()
        self.path_factory = PathFactory(git, config)

        self.data_file = os.path.join('data', 'file.txt')
        dummy_md5 = 'fsymlinc.txt'
        self.cache_file = os.path.join(ConfigI.CONFIG_DIR, ConfigI.CACHE_DIR_NAME, dummy_md5)
        self.state_file = os.path.join(ConfigI.CONFIG_DIR, ConfigI.STATE_DIR_NAME, 'data', 'file.txt' + DataItem.STATE_FILE_SUFFIX)

        with open(self.cache_file, 'w+') as fd:
            fd.write('some text')

        with open(self.state_file, 'w+') as fd:
            fd.write('{"Md5" : "' + dummy_md5 + '", "Command" : "run", "Cwd" : "dir"}')

        os.chdir('data')
        System.hardlink(os.path.join('..', self.cache_file), 'file.txt')
        os.chdir('..')
        pass

    def test_data_file_factory(self):
        data_path = self.path_factory.data_item(self.data_file)
        self.assertEqual(data_path.data.dvc, self.data_file)

        indirect_file_path = os.path.join('..', self._proj_dir, self.data_file)
        data_path_indirect = self.path_factory.data_item(indirect_file_path)
        self.assertEqual(data_path_indirect.data.dvc, self.data_file)
        pass

    def test_data_hardlink_factory(self):
        data_path = self.path_factory.existing_data_item(self.data_file)
        self.assertEqual(data_path.cache.dvc, self.cache_file)
        pass

    def test_data_hardlink_factory_cache(self):
        data_path = self.path_factory.existing_data_item(self.data_file)
        self.assertEqual(data_path.data.dvc, self.data_file)
        pass

    def test_data_hardlink_factory_exception(self):
        with self.assertRaises(DataItemError):
            self.path_factory.existing_data_item(self.cache_file)
        pass

    def test_data_path(self):
        file = os.path.join('data', 'file1')
        path = self.path_factory.path(file)
        self.assertEqual(path.dvc, file)
        self.assertEqual(path.relative, file)
        self.assertTrue(path.abs.endswith(file))
Esempio n. 2
0
class TestPathFactory(BasicEnvironment):
    def setUp(self):
        BasicEnvironment.init_environment(self,
                                          test_dir=os.path.join(
                                              os.path.sep, 'tmp',
                                              'ntx_unit_test'))

        git = GitWrapperI(git_dir=self._test_git_dir, commit='ad45ba8')
        config = ConfigI('data', 'cache', 'state')
        self.path_factory = PathFactory(git, config)

        self.data_file = os.path.join('data', 'file.txt')
        self.cache_file = os.path.join('cache', 'fsymlinc.txt')

        fd = open(self.cache_file, 'w+')
        fd.write('some text')
        fd.close()

        os.chdir('data')
        System.symlink(os.path.join('..', self.cache_file), 'file.txt')
        os.chdir('..')
        pass

    def test_data_file_factory(self):
        data_path = self.path_factory.data_item(self.data_file)
        self.assertEqual(data_path.data.dvc, self.data_file)

        indirect_file_path = os.path.join('..', self._proj_dir, self.data_file)
        data_path_indirect = self.path_factory.data_item(indirect_file_path)
        self.assertEqual(data_path_indirect.data.dvc, self.data_file)
        pass

    def test_data_symlink_factory(self):
        data_path = self.path_factory.existing_data_item(self.data_file)
        self.assertEqual(data_path.cache.dvc, self.cache_file)
        pass

    def test_data_symlink_factory_cache(self):
        data_path = self.path_factory.existing_data_item(self.data_file)
        self.assertEqual(data_path.data.dvc, self.data_file)
        pass

    def test_data_symlink_factory_exception(self):
        with self.assertRaises(DataItemError):
            self.path_factory.existing_data_item(self.cache_file)
        pass

    def test_data_path(self):
        file = os.path.join('data', 'file1')
        path = self.path_factory.path(file)
        self.assertEqual(path.dvc, file)
        self.assertEqual(path.relative, file)
        self.assertTrue(path.abs.endswith(file))

    def test_to_data_path(self):
        exclude_file = os.path.join('cache', 'file2')
        data_path_file1 = os.path.join('data', 'file1')
        data_path_file2 = os.path.join('data', 'file2')
        files = [data_path_file1, exclude_file, data_path_file2]

        data_path_list, exclude_file_list = self.path_factory.to_data_items(
            files)

        self.assertEqual(len(data_path_list), 2)
        self.assertEqual(len(exclude_file_list), 1)

        self.assertEqual(exclude_file_list[0], exclude_file)
        data_path_set = set(x.data.dvc for x in data_path_list)
        self.assertEqual(data_path_set, {data_path_file1, data_path_file2})