diff --git a/tests/test_progitlibgit.py b/tests/test_progitlibgit.py index 2e9051d..54644cf 100644 --- a/tests/test_progitlibgit.py +++ b/tests/test_progitlibgit.py @@ -16,6 +16,7 @@ import shutil import sys import os +import pygit2 from mock import patch sys.path.insert(0, os.path.join(os.path.dirname( @@ -55,6 +56,152 @@ class ProgitLibGittests(tests.Modeltests): os.unlink(outputconf) self.assertFalse(os.path.exists(outputconf)) + def test_commit_to_patch(self): + """ Test the commit_to_patch function of progit.lib.git. """ + # Create a git repo to play with + self.gitrepo = os.path.join(HERE, 'test_repo.git') + os.makedirs(self.gitrepo) + repo = pygit2.init_repository(self.gitrepo) + + # Create a file in that git repo + with open(os.path.join(self.gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add sources file for testing', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [] + ) + + first_commit = repo.revparse_single('HEAD') + + # Edit the sources file again + with open(os.path.join(self.gitrepo, 'sources'), 'w') as stream: + stream.write('foo\n bar\nbaz\n boose') + repo.index.add('sources') + repo.index.write() + + # Commits the files added + tree = repo.index.write_tree() + author = pygit2.Signature( + 'Alice Author', 'alice@authors.tld') + committer = pygit2.Signature( + 'Cecil Committer', 'cecil@committers.tld') + repo.create_commit( + 'refs/heads/master', # the name of the reference to update + author, + committer, + 'Add baz and boose to the sources\n\n There are more objects to ' + 'consider', + # binary string representing the tree object ID + tree, + # list of binary strings representing parents of the new commit + [first_commit.oid.hex] + ) + + second_commit = repo.revparse_single('HEAD') + + # Generate a patch for 2 commits + patch = progit.lib.git.commit_to_patch( + repo, [first_commit, second_commit]) + exp = """Mon Sep 17 00:00:00 2001 +From: Alice Author +Subject: [PATCH 1/2] Add sources file for testing + + +--- + +diff --git a/sources b/sources +new file mode 100644 +index 0000000..9f44358 +--- /dev/null ++++ b/sources +@@ -0,0 +1,2 @@ ++foo ++ bar +\ No newline at end of file + +Mon Sep 17 00:00:00 2001 +From: Alice Author +Subject: [PATCH 2/2] Add baz and boose to the sources + + + There are more objects to consider +--- + +diff --git a/sources b/sources +index 9f44358..2a552bb 100644 +--- a/sources ++++ b/sources +@@ -1,2 +1,4 @@ + foo +- bar +\ No newline at end of file ++ bar ++baz ++ boose +\ No newline at end of file + +""" + npatch = [] + for row in patch.split('\n'): + if row.startswith('Date:'): + continue + if row.startswith('From '): + row = row.split(' ', 2)[2] + npatch.append(row) + + patch = '\n'.join(npatch) + self.assertEqual(patch, exp) + + # Generate a patch for a single commit + patch = progit.lib.git.commit_to_patch(repo, second_commit) + exp = """Mon Sep 17 00:00:00 2001 +From: Alice Author +Subject: Add baz and boose to the sources + + + There are more objects to consider +--- + +diff --git a/sources b/sources +index 9f44358..2a552bb 100644 +--- a/sources ++++ b/sources +@@ -1,2 +1,4 @@ + foo +- bar +\ No newline at end of file ++ bar ++baz ++ boose +\ No newline at end of file + +""" + npatch = [] + for row in patch.split('\n'): + if row.startswith('Date:'): + continue + if row.startswith('From '): + row = row.split(' ', 2)[2] + npatch.append(row) + + patch = '\n'.join(npatch) + self.assertEqual(patch, exp) + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(ProgitLibGittests)