def test_gitrepo_deprecated_methods(self): """ Delete this class as deprecated methods are removed. """ repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True) testfilename = 'testfile.name.txt' with patch('pynag.Utils.GitRepo.add') as add_method_mock: repo._git_add(testfilename) add_method_mock.assert_called_once_with(testfilename) with patch('pynag.Utils.GitRepo.commit') as commit_method_mock: repo._git_commit(filename=testfilename, message='test') commit_method_mock.assert_called_once_with(message='test', filelist=[testfilename]) commit_method_mock.reset_mock() repo._git_commit(filename=None, message='test', filelist=[testfilename]) commit_method_mock.assert_called_once_with(message='test', filelist=[testfilename]) commit_method_mock.reset_mock() repo._git_commit(filename=testfilename, message='test', filelist=[testfilename]) commit_method_mock.assert_called_once_with( message='test', filelist=[testfilename, testfilename])
def test_gitrepo_init_with_files(self): tmp_file = tempfile.mkstemp(dir=self.tmp_dir) # If pynag defaults will fail, correctly, adjust for test author_email = None from getpass import getuser from platform import node nodename = node() if nodename.endswith('.(none)'): nodename[:-7] + '.example.com' author_email = '%s@%s' % (getuser(), nodename) repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True, author_name=None, author_email=author_email) # Check that there is an initial commit expected_email = '%s@%s' % (getuser(), nodename) self.assertEquals(len(repo.log()), 1) self.assertEquals(repo.log()[0]['comment'], 'Initial Commit') self.assertEquals(repo.log()[0]['author_name'], 'Pynag User') self.assertEquals(repo.log()[0]['author_email'], expected_email) # Test kwargs functionality self.assertEquals( repo.log(author_email=expected_email)[0]['author_email'], expected_email) self.assertEquals( repo.log(comment__contains='Initial')[0]['comment'], 'Initial Commit') self.assertEquals(len(repo.log(comment__contains='nothing')), 0) # Test show method initial_hash = repo.log()[0]['hash'] initial_hash_valid_commits = repo.get_valid_commits()[0] self.assertEquals(initial_hash, initial_hash_valid_commits) gitrunpatcher = patch('pynag.Utils.GitRepo._run_command') validcommitspatcher = patch('pynag.Utils.GitRepo.get_valid_commits') gitrunpatch = gitrunpatcher.start() validcommitspatch = validcommitspatcher.start() validcommitspatch.return_value = [initial_hash] repo.show(initial_hash) gitrunpatch.assert_called_once_with('git show %s' % initial_hash) gitrunpatcher.stop() validcommitspatcher.stop() invalid_hash = '123' self.assertRaisesRegexp(PynagError, '123 is not a valid commit id', repo.show, '123') # Add file tmp_file_2 = tempfile.mkstemp(dir=self.tmp_dir) self.assertEquals(len(repo.get_uncommited_files()), 1) self.assertEquals(repo.is_up_to_date(), False) # Commit file repo.commit(filelist=repo.get_uncommited_files()[0]['filename']) self.assertEquals(repo.is_up_to_date(), True) self.assertEquals(len(repo.get_uncommited_files()), 0) self.assertEquals(len(repo.get_valid_commits()), 2) log_entry = repo.log()[0] self.assertEquals(log_entry['comment'], 'commited by pynag')
def test_gitrepo_init_empty(self): from getpass import getuser from platform import node repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True, author_name=None, author_email=None) self.assertEquals(repo.author_name, 'Pynag User') expected_email = '<%s@%s>' % (getuser(), node()) self.assertEquals(repo.author_email, expected_email)
def test_gitrepo_init_empty(self): from getpass import getuser from platform import node emptyish = [None, '', ' ', '\n '] for x in emptyish: repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True, author_name=x, author_email=x) self.assertEquals(repo.author_name, 'Pynag User') expected_email = '%s@%s' % (getuser(), node()) self.assertEquals(repo.author_email, expected_email)
def test_gitrepo_diff(self): """ Test git diff works as expected """ # Create repo and write one test commit git = utils.GitRepo(directory=self.tmp_dir, auto_init=True) tmp_filename = "%s/%s" % (self.tmp_dir, 'testfile.txt') open(tmp_filename, 'w').write('test data\n') git.commit() # First try diff with no changes made: diff = git.diff() self.assertEquals(diff, '') # Now append to our file and see the difference: extra_data = 'extra data\n' open(tmp_filename, 'a').write(extra_data) # Call diff with no params, check if extra_data is in the diff diff = git.diff() self.assertTrue(diff.find(extra_data) > 0) # Call diff with filename as parameter, check if extra_data is in the # diff diff = git.diff(commit_id_or_filename=tmp_filename) self.assertTrue(diff.find(extra_data) > 0) # Call commit again and confirm there is no diff git.commit() diff = git.diff() self.assertEquals(diff, '') # Call a diff against first commit, see if we find our changes in the # commit. all_commits = git.get_valid_commits() first_commit = all_commits.pop() diff = git.diff(commit_id_or_filename=first_commit) self.assertTrue(diff.find(extra_data) > 0) # Revert latest change, and make sure diff is gone. last_commit = all_commits.pop(0) git.revert(last_commit) diff = git.diff(commit_id_or_filename=first_commit) self.assertTrue(diff.find(extra_data) == -1) # At last try to diff against an invalid commit id try: git.diff('invalid commit id') self.assertTrue( False, "we wanted exception when calling diff on invalid commit id") except PynagError: pass
def test_gitrepo_init_with_author(self): tmp_file = tempfile.mkstemp(dir=self.tmp_dir) author_name = 'Git Owner' author_email = '*****@*****.**' repo = utils.GitRepo(directory=self.tmp_dir, auto_init=True, author_name=author_name, author_email=author_email) self.assertEquals(repo.author_name, author_name) self.assertEquals(repo.author_email, author_email) self.assertEquals(len(repo.log()), 1) self.assertEquals(repo.log()[0]['author_name'], author_name) self.assertEquals(repo.log()[0]['author_email'], author_email)