diff options
author | Anil Khatri <27620628+imkaka@users.noreply.github.com> | 2019-10-23 23:37:43 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-23 23:37:43 +0530 |
commit | abb18968516c6c3c9e1d736bfe6f435392b3d3af (patch) | |
tree | 46b45bc52901f0f52526f9573f06b2fc0f777231 /git/test | |
parent | 284f89d768080cb86e0d986bfa1dd503cfe6b682 (diff) | |
parent | dfa0eac1578bff14a8f7fa00bfc3c57aba24f877 (diff) | |
download | gitpython-abb18968516c6c3c9e1d736bfe6f435392b3d3af.tar.gz |
Merge branch 'master' into fix/deepsource-issues
Diffstat (limited to 'git/test')
-rw-r--r-- | git/test/fixtures/diff_copied_mode | 4 | ||||
-rw-r--r-- | git/test/fixtures/diff_copied_mode_raw | 1 | ||||
-rw-r--r-- | git/test/test_diff.py | 80 | ||||
-rw-r--r-- | git/test/test_util.py | 7 |
4 files changed, 89 insertions, 3 deletions
diff --git a/git/test/fixtures/diff_copied_mode b/git/test/fixtures/diff_copied_mode new file mode 100644 index 00000000..60707afc --- /dev/null +++ b/git/test/fixtures/diff_copied_mode @@ -0,0 +1,4 @@ +diff --git a/test1.txt b/test2.txt +similarity index 100% +copy from test1.txt +copy to test2.txt diff --git a/git/test/fixtures/diff_copied_mode_raw b/git/test/fixtures/diff_copied_mode_raw new file mode 100644 index 00000000..7640f3ab --- /dev/null +++ b/git/test/fixtures/diff_copied_mode_raw @@ -0,0 +1 @@ +:100644 100644 cfe9deac6e10683917e80f877566b58644aa21df cfe9deac6e10683917e80f877566b58644aa21df C100 test1.txt test2.txt diff --git a/git/test/test_diff.py b/git/test/test_diff.py index e47b9331..e4e7556d 100644 --- a/git/test/test_diff.py +++ b/git/test/test_diff.py @@ -5,12 +5,15 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php import ddt +import shutil +import tempfile from git import ( Repo, GitCommandError, Diff, DiffIndex, NULL_TREE, + Submodule, ) from git.cmd import Git from git.test.lib import ( @@ -19,7 +22,6 @@ from git.test.lib import ( fixture, assert_equal, assert_true, - ) from git.test.lib import with_rw_directory @@ -29,9 +31,15 @@ import os.path as osp @ddt.ddt class TestDiff(TestBase): + def setUp(self): + self.repo_dir = tempfile.mkdtemp() + self.submodule_dir = tempfile.mkdtemp() + def tearDown(self): import gc gc.collect() + shutil.rmtree(self.repo_dir) + shutil.rmtree(self.submodule_dir) def _assert_diff_format(self, diffs): # verify that the format of the diff is sane @@ -60,7 +68,12 @@ class TestDiff(TestBase): with open(fp, 'w') as fs: fs.write("Hola Mundo") - r.git.commit(all=True, message="change on master") + r.git.add(Git.polish_url(fp)) + self.assertEqual(len(r.index.diff("HEAD", create_patch=True)), 1, + "create_patch should generate patch of diff to HEAD") + r.git.commit(message="change on master") + self.assertEqual(len(r.index.diff("HEAD", create_patch=True)), 0, + "create_patch should generate no patch, already on HEAD") r.git.checkout('HEAD~1', b='topic') with open(fp, 'w') as fs: @@ -68,7 +81,8 @@ class TestDiff(TestBase): r.git.commit(all=True, message="change on topic branch") # there must be a merge-conflict - self.failUnlessRaises(GitCommandError, r.git.cherry_pick, 'master') + with self.assertRaises(GitCommandError): + r.git.cherry_pick('master') # Now do the actual testing - this should just work self.assertEqual(len(r.index.diff(None)), 2) @@ -112,6 +126,29 @@ class TestDiff(TestBase): self.assertEqual(diff.score, 100) self.assertEqual(len(list(diffs.iter_change_type('R'))), 1) + def test_diff_with_copied_file(self): + output = StringProcessAdapter(fixture('diff_copied_mode')) + diffs = Diff._index_from_patch_format(self.rorepo, output) + self._assert_diff_format(diffs) + + assert_equal(1, len(diffs)) + + diff = diffs[0] + assert_true(diff.copied_file) + assert_true(diff.a_path, u'test1.txt') + assert_true(diff.b_path, u'test2.txt') + assert isinstance(str(diff), str) + + output = StringProcessAdapter(fixture('diff_copied_mode_raw')) + diffs = Diff._index_from_raw_format(self.rorepo, output) + self.assertEqual(len(diffs), 1) + diff = diffs[0] + self.assertEqual(diff.change_type, 'C') + self.assertEqual(diff.score, 100) + self.assertEqual(diff.a_path, u'test1.txt') + self.assertEqual(diff.b_path, u'test2.txt') + self.assertEqual(len(list(diffs.iter_change_type('C'))), 1) + def test_diff_with_change_in_type(self): output = StringProcessAdapter(fixture('diff_change_in_type')) diffs = Diff._index_from_patch_format(self.rorepo, output) @@ -244,6 +281,43 @@ class TestDiff(TestBase): self.assertIsNone(diff_index[0].a_path, repr(diff_index[0].a_path)) self.assertEqual(diff_index[0].b_path, u'file with spaces', repr(diff_index[0].b_path)) + def test_diff_submodule(self): + """Test that diff is able to correctly diff commits that cover submodule changes""" + # Init a temp git repo that will be referenced as a submodule + sub = Repo.init(self.submodule_dir) + with open(self.submodule_dir + "/subfile", "w") as sub_subfile: + sub_subfile.write("") + sub.index.add(["subfile"]) + sub.index.commit("first commit") + + # Init a temp git repo that will incorporate the submodule + repo = Repo.init(self.repo_dir) + with open(self.repo_dir + "/test", "w") as foo_test: + foo_test.write("") + repo.index.add(['test']) + Submodule.add(repo, "subtest", "sub", url="file://" + self.submodule_dir) + repo.index.commit("first commit") + repo.create_tag('1') + + # Add a commit to the submodule + submodule = repo.submodule('subtest') + with open(self.repo_dir + "/sub/subfile", "w") as foo_sub_subfile: + foo_sub_subfile.write("blub") + submodule.module().index.add(["subfile"]) + submodule.module().index.commit("changed subfile") + submodule.binsha = submodule.module().head.commit.binsha + + # Commit submodule updates in parent repo + repo.index.add([submodule]) + repo.index.commit("submodule changed") + repo.create_tag('2') + + diff = repo.commit('1').diff(repo.commit('2'))[0] + # If diff is unable to find the commit hashes (looks in wrong repo) the *_blob.size + # property will be a string containing exception text, an int indicates success + self.assertIsInstance(diff.a_blob.size, int) + self.assertIsInstance(diff.b_blob.size, int) + def test_diff_interface(self): # test a few variations of the main diff routine assertion_map = {} diff --git a/git/test/test_util.py b/git/test/test_util.py index b5f9d222..a4d9d7ad 100644 --- a/git/test/test_util.py +++ b/git/test/test_util.py @@ -4,6 +4,7 @@ # This module is part of GitPython and is released under # the BSD License: http://www.opensource.org/licenses/bsd-license.php +import pickle import tempfile import time from unittest import skipIf @@ -280,3 +281,9 @@ class TestUtils(TestBase): # Wrong offset: UTC-9000, should return datetime + tzoffset(UTC) altz = utctz_to_altz('-9000') self.assertEqual(datetime.fromtimestamp(1522827734, tzoffset(0)), from_timestamp(1522827734, altz)) + + def test_pickle_tzoffset(self): + t1 = tzoffset(555) + t2 = pickle.loads(pickle.dumps(t1)) + self.assertEqual(t1._offset, t2._offset) + self.assertEqual(t1._name, t2._name) |