class WorkingDirectoryTest(unittest.TestCase):
    def setUp(self) -> None:
        self.wd = WorkingDirectory()
        self.di = DirectoryInfo()
        path = os.getcwd()
        self.di.init(path)
        self.wd.init_config()
        self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
        with open(self.file_path, "w+") as file:
            file.write('SOME STRING')

    def tearDown(self) -> None:
        if os.path.exists(self.di.cvs_path):
            shutil.rmtree(self.di.cvs_path)
        if os.path.exists(self.file_path):
            os.remove(self.file_path)

    def test_find_not_indexed_files_should_find_when_empty_indexed(self):
        self.wd.find_not_indexed_files(set())
        self.assertGreater(len(self.wd.not_indexed_files), 0)

    def test_find_not_indexed_files_should_return_files_in_working_path(self):
        self.wd.find_not_indexed_files(set())
        result = self.wd.not_indexed_files
        self.assertTrue("TESTING.txt" in result)

    def test_reset_should_rewrite_files_from_index(self):
        index = Index()
        index.init_config()
        index.set_directory_info(self.di)
        index.add_new_file('TESTING.txt')
        os.remove(self.file_path)
        self.wd.reset(index)
        self.assertTrue(os.path.exists(self.file_path))
class TestRepository(unittest.TestCase):
    def setUp(self) -> None:
        self.di = DirectoryInfo()
        path = os.getcwd()
        self.di.init(path)
        self.di.add_branch_path('master')
        self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
        with open(self.file_path, "w+") as file:
            file.write('SOME STRING')
        self.rep = Repository()
        self.rep.set_directory_info(self.di)
        self.rep.init()

    def tearDown(self) -> None:
        if os.path.exists(self.di.cvs_path):
            shutil.rmtree(self.di.cvs_path)
        if os.path.exists(self.file_path):
            os.remove(self.file_path)

    def test_add_commit__should_copy_commit_files(self):
        index = Index()
        index.init_config()
        index.set_directory_info(self.di)
        index.add_new_file('TESTING.txt')
        commit = index.make_commit('Testing commit', 'master')
        self.rep.add_commit(commit)
        commits_path = self.di.get_commits_path('master')
        commit_path = os.path.join(commits_path, commit.commit_number)
        full_path = os.path.join(commit_path, 'TESTING.txt')
        self.assertTrue(os.path.exists(full_path))
示例#3
0
class TestCommit(unittest.TestCase):
    def setUp(self) -> None:
        self.commit = Commit("NONE")
        self.wd = WorkingDirectory()
        self.di = DirectoryInfo()
        path = os.getcwd()
        self.di.init(path)
        self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
        with open(self.file_path, "w+") as file:
            file.write('SOME STRING')

    def tearDown(self) -> None:
        if os.path.exists(self.di.cvs_path):
            shutil.rmtree(self.di.cvs_path)
        if os.path.exists(self.file_path):
            os.remove(self.file_path)

    def test_freeze_files_should_remember_copy_path_of_indexed_files(self):
        index = Index()
        index.init_config()
        index.set_directory_info(self.di)
        index.add_new_file('TESTING.txt')
        self.commit.branch_name = 'master'
        self.commit.init_config()
        self.commit.freeze_files(index.indexed_files, self.di)
        keys = self.commit.files_with_copying_paths.keys()
        self.assertTrue('TESTING.txt' in keys)

    def test_freeze_files_should_make_hash_from_indexed_files(self):
        index = Index()
        index.init_config()
        index.set_directory_info(self.di)
        index.add_new_file('TESTING.txt')
        self.commit.branch_name = 'master'
        self.commit.init_config()
        self.commit.freeze_files(index.indexed_files, self.di)
        keys = self.commit.files_hashes.keys()
        self.assertTrue('TESTING.txt' in keys)

    def test_commit_number_should_return_unique_commit_number(self):
        first_number = self.commit.commit_number
        other_commit = Commit("Other")
        second_number = other_commit.commit_number
        self.assertNotEqual(first_number, second_number)
示例#4
0
 def test_reset_should_move_head_to_previous_commit(self):
     di = DirectoryInfo()
     di.init(os.getcwd())
     head = Head()
     head.init_config()
     branch = Branch('master')
     branch.init_config()
     head.current_branch = branch
     commit = Commit('first')
     commit.branch_name = 'master'
     commit.init_config()
     prev_commit = Commit('second')
     prev_commit.branch_name = 'master'
     prev_commit.init_config()
     commit.set_previous_commit_number(prev_commit.commit_number)
     previous_commit_number = commit.get_previous_commit_number()
     head.current_branch.set_current_commit(commit)
     head.reset()
     current_commit = head.current_branch.get_current_commit()
     self.assertEqual(current_commit.commit_number, previous_commit_number)
class DirectoryInfoTest(unittest.TestCase):
    def setUp(self) -> None:
        self.info = DirectoryInfo()
        self.info.init(os.getcwd())

    def test_init_should_initialize_working_path(self):
        result = self.info.working_path
        self.assertIsNotNone(result)

    def test_init_should_initialize_cvs_path(self):
        result = self.info.cvs_path
        self.assertTrue(result.endswith('CVS'))

    def test_init_should_initialize_index_path(self):
        result = self.info.index_path
        self.assertTrue(result.endswith('INDEX'))

    def test_add_branch_path_should_add_path_to_branch(self):
        self.info.add_branch_path("master")
        count = len(self.info.branches_paths)
        self.assertGreater(count, 0)

    def test_add_branch_path_should_add_path_to_commits(self):
        self.info.add_branch_path("master")
        count = len(self.info.branches_commits_paths)
        self.assertGreater(count, 0)

    def test_get_branch_path_should_get_path_to_branch(self):
        self.info.add_branch_path("master")
        path = self.info.get_branch_path("master")
        self.assertTrue(path.endswith("master"))

    def test_get_commits_path_should_get_commits_path(self):
        self.info.add_branch_path("master")
        path = self.info.get_commits_path("master")
        self.assertTrue(path.endswith("COMMITS"))
示例#6
0
class CVS:
    def __init__(self):
        self.repository = Repository()
        self.index = Index()
        self.working_directory = WorkingDirectory()
        self.directory = DirectoryInfo()

    def init(self, path):
        """Initializes new repository at given path"""
        self.directory.init(path)
        self.index.init_config()
        self.index.set_directory_info(self.directory)

        self.repository.init()
        self.working_directory.init_config()
        self.working_directory.find_not_indexed_files(self.index.indexed_files)
        print(f'Working path is {path}')
        print(f'CVS path is {self.directory.cvs_path}')

    def add(self, filename):
        """Adds file to index"""
        self.index.add_new_file(filename)

    def commit(self, commit_message):
        """Makes new commit"""
        if len(self.index.indexed_files) == 0:
            print('No changes detected!')
            return
        branch_name = self.repository.current_branch.name
        print('commit is at ' + branch_name)
        commit = self.index.make_commit(commit_message, branch_name)
        self.repository.add_commit(commit)

    def update(self, filename, version):
        """Updates file with a specific version from repository"""
        branch_name = self.repository.current_branch.name
        branch = Branch.make_branch_from_config(branch_name)
        branch.update(filename, version)

    def branch(self, name):
        self.repository.make_branch(name)

    def branches(self):
        di = DirectoryInfo()
        di.print_branches()

    def checkout(self, branch_name):
        di = DirectoryInfo()
        if not di.branch_exists(branch_name):
            print(f'No branch {branch_name} exists!')
        self.repository.set_current_branch_name(branch_name)
        print(f'Current branch is {branch_name}')

    def diff(self, filename, first_version, second_version):
        branch_name = self.repository.current_branch.name
        branch = Branch.make_branch_from_config(branch_name)
        branch.diff(filename, first_version, second_version)

    def reset(self, mode):
        """Resets current cvs state"""
        if mode == 'soft':
            self.soft_reset()
            return
        if mode == 'mixed' or mode == '':
            self.mixed_reset()
            return
        if mode == 'hard':
            self.hard_reset()

    def soft_reset(self):
        """Resets head"""
        self.repository.reset_head()

    def mixed_reset(self):
        """Resets head and index"""
        if self.repository.reset_head():
            self.index.reset(self.repository.head)

    def hard_reset(self):
        """Resets head, index and rewrites files in working path"""
        if self.repository.reset_head():
            self.index.reset(self.repository.head)
            self.working_directory.reset(self.index)

    def log(self):
        """
        Returns commit history at current branch
        :returns commit history string
        """
        self.repository.get_commit_history()
示例#7
0
class TestIndex(unittest.TestCase):
    def setUp(self) -> None:
        path = os.getcwd()
        self.di = DirectoryInfo()
        self.di.init(path)
        self.index = Index()
        self.index.init_config()
        self.file_path = os.path.join(self.di.working_path, 'TESTING.txt')
        with open(self.file_path, "w+") as file:
            file.write('SOME STRING')
        self.index.set_directory_info(self.di)

    def tearDown(self) -> None:
        if os.path.exists(self.di.cvs_path):
            shutil.rmtree(self.di.cvs_path)
        if os.path.exists(self.file_path):
            os.remove(self.file_path)

    def test_add_new_file_raises_file_not_found_error_when_no_such_file(self):
        with self.assertRaises(FileNotFoundError):
            self.index.add_new_file("nosuchfile")

    def test_add_new_file_should_add_to_indexed_files_when_file_exists(self):
        self.index.add_new_file('TESTING.txt')
        count = len(self.index.indexed_files)
        self.assertGreater(count, 0)

    def test_add_new_file_should_copy_file_to_index_when_file_exists(self):
        self.index.add_new_file('TESTING.txt')
        full_path = os.path.join(self.di.working_path, 'TESTING.txt')
        file_exists = os.path.exists(full_path)
        self.assertTrue(file_exists)

    def test_make_commit_should_return_new_commit(self):
        branch = Branch('master')
        branch.init_config()
        self.index.add_new_file('TESTING.txt')
        commit = self.index.make_commit("new commit", 'master')
        self.assertIsNotNone(commit)

    def test_make_commit_should_set_last_commit(self):
        branch = Branch('master')
        branch.init_config()
        self.assertEquals(self.index.last_commit, 'None')
        self.index.add_new_file('TESTING.txt')
        self.index.make_commit("new commit", 'master')
        self.assertIsNotNone(self.index.last_commit)

    def test_reset_should_reset_to_head_commit(self):
        head = Head()
        head.init_config()
        branch = Branch('master')
        branch.init_config()
        head.current_branch = branch
        commit = Commit('test')
        commit.branch_name = 'master'
        commit.init_config()
        head.current_branch.set_current_commit(commit)
        self.index.reset(head)
        self.assertEqual(self.index.last_commit.commit_number,
                         commit.commit_number)