Skip to content

Add more tests to test_patch and test_blob #758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions test/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
\ No newline at end of file
"""

BLOB_PATCH_2 = """diff --git a/file b/file
index a520c24..d675fa4 100644
--- a/file
+++ b/file
@@ -1,3 +1 @@
-hello world
-hola mundo
-bonjour le monde
+foo bar
"""

BLOB_PATCH_DELETED = """diff --git a/file b/file
deleted file mode 100644
index a520c24..0000000
Expand Down Expand Up @@ -170,5 +181,24 @@ def test_diff_blob_to_buffer_delete(self):
patch = blob.diff_to_buffer(None)
self.assertEqual(patch.patch, BLOB_PATCH_DELETED)

def test_diff_blob_create(self):
old = self.repo[self.repo.create_blob(BLOB_CONTENT)]
new = self.repo[self.repo.create_blob(BLOB_NEW_CONTENT)]

patch = old.diff(new)
self.assertEqual(patch.patch, BLOB_PATCH_2)

def test_blob_from_repo(self):
blob = self.repo[BLOB_SHA]
patch_one = blob.diff_to_buffer(None)

blob = self.repo[BLOB_SHA]
patch_two = blob.diff_to_buffer(None)

self.assertEqual(
patch_one.patch,
patch_two.patch,
)

if __name__ == '__main__':
unittest.main()
81 changes: 81 additions & 0 deletions test/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,84 @@ def test_no_context_lines(self):
)

self.assertEqual(context_count, 0)


def test_patch_create_blob_blobs(self):
old_blob = self.repo[self.repo.create_blob(BLOB_OLD_CONTENT)]
new_blob = self.repo[self.repo.create_blob(BLOB_NEW_CONTENT)]

patch = pygit2.Patch.create_from(
old_blob,
new_blob,
old_as_path=BLOB_OLD_PATH,
new_as_path=BLOB_NEW_PATH,
)

self.assertEqual(patch.patch, BLOB_PATCH)

def test_patch_create_blob_buffer(self):
blob = self.repo[self.repo.create_blob(BLOB_OLD_CONTENT)]
patch = pygit2.Patch.create_from(
blob,
BLOB_NEW_CONTENT,
old_as_path=BLOB_OLD_PATH,
new_as_path=BLOB_NEW_PATH,
)

self.assertEqual(patch.patch, BLOB_PATCH)

def test_patch_create_blob_delete(self):
blob = self.repo[self.repo.create_blob(BLOB_OLD_CONTENT)]
patch = pygit2.Patch.create_from(
blob,
None,
old_as_path=BLOB_OLD_PATH,
new_as_path=BLOB_NEW_PATH,
)

self.assertEqual(patch.patch, BLOB_PATCH_DELETED)

def test_patch_create_blob_add(self):
blob = self.repo[self.repo.create_blob(BLOB_NEW_CONTENT)]
patch = pygit2.Patch.create_from(
None,
blob,
old_as_path=BLOB_OLD_PATH,
new_as_path=BLOB_NEW_PATH,
)

self.assertEqual(patch.patch, BLOB_PATCH_ADDED)

def test_patch_delete_blob(self):
blob = self.repo[BLOB_OLD_SHA]
patch = pygit2.Patch.create_from(
blob,
None,
old_as_path=BLOB_OLD_PATH,
new_as_path=BLOB_NEW_PATH,
)

# Make sure that even after deleting the blob the patch still has the
# necessary references to generate its patch
del blob
self.assertEqual(patch.patch, BLOB_PATCH_DELETED)

def test_patch_multi_blob(self):
blob = self.repo[BLOB_OLD_SHA]
patch = pygit2.Patch.create_from(
blob,
None
)
patch_text = patch.patch

blob = self.repo[BLOB_OLD_SHA]
patch2 = pygit2.Patch.create_from(
blob,
None
)
patch_text2 = patch.patch

self.assertEqual(patch_text, patch_text2)
self.assertEqual(patch_text, patch.patch)
self.assertEqual(patch_text2, patch2.patch)
self.assertEqual(patch.patch, patch2.patch)