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 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))
 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))
 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_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)
Exemple #6
0
 def __init__(self):
     self.repository = Repository()
     self.index = Index()
     self.working_directory = WorkingDirectory()
     self.directory = DirectoryInfo()
Exemple #7
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()
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)