def test_not_existing_repo_on_init(self): """Test if init fails when the repos does not exists""" expected = "git repository '%s' does not exist" % (self.tmp_path) with self.assertRaisesRegex(RepositoryError, expected): _ = GitRepository('http://example.org', self.tmp_path)
def test_log_from_date(self): """Test if commits are returned from the given date""" new_path = os.path.join(self.tmp_path, 'newgit') repo = GitRepository.clone(self.git_path, new_path) gitlog = repo.log(from_date=datetime.datetime(2014, 2, 11, 22, 7, 49)) gitlog = [line for line in gitlog] self.assertEqual(len(gitlog), 36) self.assertEqual(gitlog[0][:14], "commit ce8e0b8") # Use a timezone, it will return an empty line from_date = datetime.datetime(2014, 2, 11, 22, 7, 49, tzinfo=dateutil.tz.tzoffset( None, -36000)) gitlog = repo.log(from_date=from_date) gitlog = [line for line in gitlog] self.assertEqual(gitlog, []) shutil.rmtree(new_path)
def test_clone_existing_directory(self): """Test if it raises an exception when tries to clone an existing directory""" expected = "git command - fatal: destination path '%s' already exists" % (self.tmp_path) with self.assertRaisesRegex(RepositoryError, expected): _ = GitRepository.clone(self.git_path, self.tmp_path)
def test_clone_existing_directory(self): """Test if it raises an exception when tries to clone an existing directory""" expected = "git command - fatal: destination path '%s' already exists" \ % (self.tmp_path) with self.assertRaisesRegex(RepositoryError, expected): _ = GitRepository.clone(self.git_path, self.tmp_path)
def test_init(self): """Test initialization""" repo = GitRepository('http://example.git', self.git_path) self.assertIsInstance(repo, GitRepository) self.assertEqual(repo.uri, 'http://example.git') self.assertEqual(repo.dirpath, self.git_path)
def test_git_parser_from_iter(self): """Test if the static method parses a git log from a repository""" repo = GitRepository(self.git_path, self.git_path) commits = Git.parse_git_log_from_iter(repo.log()) result = [commit['commit'] for commit in commits] expected = ['bc57a9209f096a130dcc5ba7089a8663f758a703', '87783129c3f00d2c81a3a8e585eb86a47e39891a', '7debcf8a2f57f86663809c58b5c07a398be7674c', 'c0d66f92a95e31c77be08dc9d0f11a16715d1885', 'c6ba8f7a1058db3e6b4bc6f1090e932b107605fb', '589bb080f059834829a2a5955bebfd7c2baa110a', 'ce8e0b86a1e9877f42fe9453ede418519115f367', '51a3b654f252210572297f47597b31527c475fb8', '456a68ee1407a77f3e804a30dff245bb6c6b872f'] self.assertListEqual(result, expected)
def test_pull(self): """Test if the repository is updated to 'origin' status""" def count_commits(): """Get the number of commits counting the entries on the log""" cmd = ['git', 'log', '--oneline'] gitlog = subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=new_path, env={ 'LANG': 'C', 'PAGER': '' }) commits = gitlog.strip(b'\n').split(b'\n') return len(commits) new_path = os.path.join(self.tmp_path, 'newgit') new_file = os.path.join(new_path, 'newfile') repo = GitRepository.clone(self.git_path, new_path) # Count the number of commits before adding a new one ncommits = count_commits() self.assertEqual(ncommits, 9) # Create a new file and commit it to the repository with open(new_file, 'w') as f: f.write("Testing pull method") cmd = ['git', 'add', new_file] subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=new_path, env={'LANG': 'C'}) cmd = [ 'git', '-c', 'user.name="mock"', '-c', 'user.email="*****@*****.**"', 'commit', '-m', 'Testing pull' ] subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=new_path, env={'LANG': 'C'}) # Count the number of commits after the adding a new one ncommits = count_commits() self.assertEqual(ncommits, 10) # Update the repository to its original status repo.pull() # The number of commits should be updated to its original value ncommits = count_commits() self.assertEqual(ncommits, 9) shutil.rmtree(new_path)
def test_clone_error(self): """Test if it raises an exception when an error occurs cloning a repository""" # Clone a non-git repository new_path = os.path.join(self.tmp_path, "newgit") expected = "git command - fatal: repository '%s' does not exist" % self.tmp_path with self.assertRaisesRegex(RepositoryError, expected): _ = GitRepository.clone(self.tmp_path, new_path)
def test_clone_error(self): """Test if it raises an exception when an error occurs cloning a repository""" # Clone a non-git repository new_path = os.path.join(self.tmp_path, 'newgit') expected = "git command - fatal: repository '%s' does not exist" \ % self.tmp_path with self.assertRaisesRegex(RepositoryError, expected): _ = GitRepository.clone(self.tmp_path, new_path)
def test_log(self): """Test log command""" new_path = os.path.join(self.tmp_path, 'newgit') repo = GitRepository.clone(self.git_path, new_path) gitlog = repo.log() gitlog = [line for line in gitlog] self.assertEqual(len(gitlog), 108) self.assertEqual(gitlog[0][:14], "commit bc57a92") shutil.rmtree(new_path)
def test_log_empty(self): """Test if no line is returned when the log is empty""" new_path = os.path.join(self.tmp_path, 'newgit') repo = GitRepository.clone(self.git_path, new_path) gitlog = repo.log(from_date=datetime.datetime(2020, 1, 1, 1, 1, 1)) gitlog = [line for line in gitlog] self.assertListEqual(gitlog, []) shutil.rmtree(new_path)
def test_not_git(self): """Test if a supposed git repo is not a git repo""" new_path = os.path.join(self.tmp_path, 'falsegit') if not os.path.isdir(new_path): os.makedirs(new_path) expected = "git repository '%s' does not exist" % new_path with self.assertRaisesRegex(RepositoryError, expected): repo = GitRepository(uri="", dirpath=new_path) shutil.rmtree(new_path)
def test_clone(self): """Test if a git repository is cloned""" new_path = os.path.join(self.tmp_path, 'newgit') repo = GitRepository.clone(self.git_path, new_path) self.assertIsInstance(repo, GitRepository) self.assertEqual(repo.uri, self.git_path) self.assertEqual(repo.dirpath, new_path) self.assertTrue(os.path.exists(new_path)) self.assertTrue(os.path.exists(os.path.join(new_path, '.git'))) shutil.rmtree(new_path)
def test_pull(self): """Test if the repository is updated to 'origin' status""" def count_commits(): """Get the number of commits counting the entries on the log""" cmd = ['git', 'log', '--oneline'] gitlog = subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=new_path, env={'LANG' : 'C', 'PAGER' : ''}) commits = gitlog.strip(b'\n').split(b'\n') return len(commits) new_path = os.path.join(self.tmp_path, 'newgit') new_file = os.path.join(new_path, 'newfile') repo = GitRepository.clone(self.git_path, new_path) # Count the number of commits before adding a new one ncommits = count_commits() self.assertEqual(ncommits, 9) # Create a new file and commit it to the repository with open(new_file, 'w') as f: f.write("Testing pull method") cmd = ['git', 'add', new_file] subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=new_path, env={'LANG' : 'C'}) cmd = ['git', '-c', 'user.name="mock"', '-c', 'user.email="*****@*****.**"', 'commit', '-m', 'Testing pull'] subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=new_path, env={'LANG' : 'C'}) # Count the number of commits after the adding a new one ncommits = count_commits() self.assertEqual(ncommits, 10) # Update the repository to its original status repo.pull() # The number of commits should be updated to its original value ncommits = count_commits() self.assertEqual(ncommits, 9) shutil.rmtree(new_path)
def test_pull(self): """Test if the repository is updated to 'origin' status""" def count_commits(): """Get the number of commits counting the entries on the log""" cmd = ["git", "log", "--oneline"] gitlog = subprocess.check_output( cmd, stderr=subprocess.STDOUT, cwd=new_path, env={"LANG": "C", "PAGER": ""} ) commits = gitlog.strip(b"\n").split(b"\n") return len(commits) new_path = os.path.join(self.tmp_path, "newgit") new_file = os.path.join(new_path, "newfile") repo = GitRepository.clone(self.git_path, new_path) # Count the number of commits before adding a new one ncommits = count_commits() self.assertEqual(ncommits, 9) # Create a new file and commit it to the repository with open(new_file, "w") as f: f.write("Testing pull method") cmd = ["git", "add", new_file] subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=new_path, env={"LANG": "C"}) cmd = ["git", "-c", 'user.name="mock"', "-c", 'user.email="*****@*****.**"', "commit", "-m", "Testing pull"] subprocess.check_output(cmd, stderr=subprocess.STDOUT, cwd=new_path, env={"LANG": "C"}) # Count the number of commits after the adding a new one ncommits = count_commits() self.assertEqual(ncommits, 10) # Update the repository to its original status repo.pull() # The number of commits should be updated to its original value ncommits = count_commits() self.assertEqual(ncommits, 9) shutil.rmtree(new_path)
def test_log_from_date(self): """Test if commits are returned from the given date""" new_path = os.path.join(self.tmp_path, "newgit") repo = GitRepository.clone(self.git_path, new_path) gitlog = repo.log(from_date=datetime.datetime(2014, 2, 11, 22, 7, 49)) gitlog = [line for line in gitlog] self.assertEqual(len(gitlog), 36) self.assertEqual(gitlog[0][:14], "commit ce8e0b8") # Use a timezone, it will return an empty line from_date = datetime.datetime(2014, 2, 11, 22, 7, 49, tzinfo=dateutil.tz.tzoffset(None, -36000)) gitlog = repo.log(from_date=from_date) gitlog = [line for line in gitlog] self.assertEqual(gitlog, []) shutil.rmtree(new_path)