Skip to content
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

Use existing blobs in test_patch; Make create_from args optional #753

Merged
merged 1 commit into from
Dec 4, 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
12 changes: 8 additions & 4 deletions src/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ PyDoc_STRVAR(Patch_create_from__doc__,
"Create a patch from blobs, buffers, or a blob and a buffer");

static PyObject *
Patch_create_from(PyObject *self, PyObject *args)
Patch_create_from(PyObject *self, PyObject *args, PyObject *kwds)
{
/* A generic wrapper around
* git_patch_from_blob_and_buffer
Expand All @@ -124,8 +124,11 @@ Patch_create_from(PyObject *self, PyObject *args)
Py_ssize_t oldbuflen, newbuflen;
int err;

if (!PyArg_ParseTuple(args, "OzOz|I", &oldobj, &old_as_path, &newobj,
&new_as_path, &opts.flags))
char *keywords[] = {"old", "new", "flag", "old_as_path", "new_as_path", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|Izz", keywords,
&oldobj, &newobj, &opts.flags,
&old_as_path, &new_as_path))
return NULL;

if (oldobj != Py_None && PyObject_TypeCheck(oldobj, &BlobType))
Expand Down Expand Up @@ -201,7 +204,8 @@ Patch_patch__get__(Patch *self)
}

PyMethodDef Patch_methods[] = {
{"create_from", (PyCFunction) Patch_create_from, METH_VARARGS | METH_STATIC, Patch_create_from__doc__},
{"create_from", (PyCFunction) Patch_create_from,
METH_KEYWORDS | METH_VARARGS | METH_STATIC, Patch_create_from__doc__},
{NULL}
};

Expand Down
59 changes: 33 additions & 26 deletions test/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,27 @@
import pygit2
from . import utils

BLOB_SHA = 'a520c24d85fbfc815d385957eed41406ca5a860b'
BLOB_OLD_CONTENT = """hello world
BLOB_OLD_SHA = 'a520c24d85fbfc815d385957eed41406ca5a860b'
BLOB_NEW_SHA = '3b18e512dba79e4c8300dd08aeb37f8e728b8dad'
BLOB_OLD_CONTENT = b"""hello world
hola mundo
bonjour le monde
""".encode()
"""
BLOB_NEW_CONTENT = b'foo bar\n'

BLOB_OLD_PATH = 'a/file'
BLOB_NEW_PATH = 'b/file'

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

BLOB_PATCH = """diff --git a/a/file b/b/file
index a520c24..d675fa4 100644
--- a/a/file
Expand Down Expand Up @@ -78,55 +89,55 @@ class PatchTest(utils.RepoTestCase):
def test_patch_create_from_buffers(self):
patch = pygit2.Patch.create_from(
BLOB_OLD_CONTENT,
BLOB_OLD_PATH,
BLOB_NEW_CONTENT,
BLOB_NEW_PATH,
old_as_path=BLOB_OLD_PATH,
new_as_path=BLOB_NEW_PATH,
)

self.assertEqual(patch.patch, BLOB_PATCH)

def test_patch_create_from_blobs(self):
old_blob = self.repo.create_blob(BLOB_OLD_CONTENT)
new_blob = self.repo.create_blob(BLOB_NEW_CONTENT)
old_blob = self.repo[BLOB_OLD_SHA]
new_blob = self.repo[BLOB_NEW_SHA]

patch = pygit2.Patch.create_from(
self.repo[old_blob],
BLOB_OLD_PATH,
self.repo[new_blob],
BLOB_NEW_PATH,
old_blob,
new_blob,
old_as_path=BLOB_OLD_PATH,
new_as_path=BLOB_NEW_PATH,
)

self.assertEqual(patch.patch, BLOB_PATCH)
self.assertEqual(patch.patch, BLOB_PATCH2)

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

self.assertEqual(patch.patch, BLOB_PATCH)

def test_patch_create_from_blob_buffer_add(self):
patch = pygit2.Patch.create_from(
None,
BLOB_OLD_PATH,
BLOB_NEW_CONTENT,
BLOB_NEW_PATH,
old_as_path=BLOB_OLD_PATH,
new_as_path=BLOB_NEW_PATH,
)

self.assertEqual(patch.patch, BLOB_PATCH_ADDED)

def test_patch_create_from_blob_buffer_delete(self):
old_blob = self.repo.create_blob(BLOB_OLD_CONTENT)
old_blob = self.repo[BLOB_OLD_SHA]

patch = pygit2.Patch.create_from(
self.repo[old_blob],
BLOB_OLD_PATH,
old_blob,
None,
BLOB_NEW_PATH,
old_as_path=BLOB_OLD_PATH,
new_as_path=BLOB_NEW_PATH,
)

self.assertEqual(patch.patch, BLOB_PATCH_DELETED)
Expand All @@ -135,16 +146,12 @@ def test_patch_create_from_bad_old_type_arg(self):
with self.assertRaises(TypeError):
pygit2.Patch.create_from(
self.repo,
BLOB_OLD_PATH,
BLOB_NEW_CONTENT,
BLOB_NEW_PATH,
)

def test_patch_create_from_bad_new_type_arg(self):
with self.assertRaises(TypeError):
pygit2.Patch.create_from(
None,
BLOB_OLD_PATH,
self.repo,
BLOB_NEW_PATH,
)