Skip to content

Commit a96da22

Browse files
committed
Dont create blobs on the fly in test_patch, make patch kwargs
1 parent cae2c28 commit a96da22

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

src/patch.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ PyDoc_STRVAR(Patch_create_from__doc__,
108108
"Create a patch from blobs, buffers, or a blob and a buffer");
109109

110110
static PyObject *
111-
Patch_create_from(PyObject *self, PyObject *args)
111+
Patch_create_from(PyObject *self, PyObject *args, PyObject *kwds)
112112
{
113113
/* A generic wrapper around
114114
* git_patch_from_blob_and_buffer
@@ -124,8 +124,11 @@ Patch_create_from(PyObject *self, PyObject *args)
124124
Py_ssize_t oldbuflen, newbuflen;
125125
int err;
126126

127-
if (!PyArg_ParseTuple(args, "OzOz|I", &oldobj, &old_as_path, &newobj,
128-
&new_as_path, &opts.flags))
127+
char *keywords[] = {"old", "new", "flag", "old_as_path", "new_as_path", NULL};
128+
129+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|Izz", keywords,
130+
&oldobj, &newobj, &opts.flags,
131+
&old_as_path, &new_as_path))
129132
return NULL;
130133

131134
if (oldobj != Py_None && PyObject_TypeCheck(oldobj, &BlobType))
@@ -201,7 +204,8 @@ Patch_patch__get__(Patch *self)
201204
}
202205

203206
PyMethodDef Patch_methods[] = {
204-
{"create_from", (PyCFunction) Patch_create_from, METH_VARARGS | METH_STATIC, Patch_create_from__doc__},
207+
{"create_from", (PyCFunction) Patch_create_from,
208+
METH_KEYWORDS | METH_VARARGS | METH_STATIC, Patch_create_from__doc__},
205209
{NULL}
206210
};
207211

test/test_patch.py

+33-26
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,27 @@
3131
import pygit2
3232
from . import utils
3333

34-
BLOB_SHA = 'a520c24d85fbfc815d385957eed41406ca5a860b'
35-
BLOB_OLD_CONTENT = """hello world
34+
BLOB_OLD_SHA = 'a520c24d85fbfc815d385957eed41406ca5a860b'
35+
BLOB_NEW_SHA = '3b18e512dba79e4c8300dd08aeb37f8e728b8dad'
36+
BLOB_OLD_CONTENT = b"""hello world
3637
hola mundo
3738
bonjour le monde
38-
""".encode()
39+
"""
3940
BLOB_NEW_CONTENT = b'foo bar\n'
4041

4142
BLOB_OLD_PATH = 'a/file'
4243
BLOB_NEW_PATH = 'b/file'
4344

45+
BLOB_PATCH2 = """diff --git a/a/file b/b/file
46+
index a520c24..3b18e51 100644
47+
--- a/a/file
48+
+++ b/b/file
49+
@@ -1,3 +1 @@
50+
hello world
51+
-hola mundo
52+
-bonjour le monde
53+
"""
54+
4455
BLOB_PATCH = """diff --git a/a/file b/b/file
4556
index a520c24..d675fa4 100644
4657
--- a/a/file
@@ -78,55 +89,55 @@ class PatchTest(utils.RepoTestCase):
7889
def test_patch_create_from_buffers(self):
7990
patch = pygit2.Patch.create_from(
8091
BLOB_OLD_CONTENT,
81-
BLOB_OLD_PATH,
8292
BLOB_NEW_CONTENT,
83-
BLOB_NEW_PATH,
93+
old_as_path=BLOB_OLD_PATH,
94+
new_as_path=BLOB_NEW_PATH,
8495
)
8596

8697
self.assertEqual(patch.patch, BLOB_PATCH)
8798

8899
def test_patch_create_from_blobs(self):
89-
old_blob = self.repo.create_blob(BLOB_OLD_CONTENT)
90-
new_blob = self.repo.create_blob(BLOB_NEW_CONTENT)
100+
old_blob = self.repo[BLOB_OLD_SHA]
101+
new_blob = self.repo[BLOB_NEW_SHA]
91102

92103
patch = pygit2.Patch.create_from(
93-
self.repo[old_blob],
94-
BLOB_OLD_PATH,
95-
self.repo[new_blob],
96-
BLOB_NEW_PATH,
104+
old_blob,
105+
new_blob,
106+
old_as_path=BLOB_OLD_PATH,
107+
new_as_path=BLOB_NEW_PATH,
97108
)
98109

99-
self.assertEqual(patch.patch, BLOB_PATCH)
110+
self.assertEqual(patch.patch, BLOB_PATCH2)
100111

101112
def test_patch_create_from_blob_buffer(self):
102-
old_blob = self.repo.create_blob(BLOB_OLD_CONTENT)
113+
old_blob = self.repo[BLOB_OLD_SHA]
103114
patch = pygit2.Patch.create_from(
104-
self.repo[old_blob],
105-
BLOB_OLD_PATH,
115+
old_blob,
106116
BLOB_NEW_CONTENT,
107-
BLOB_NEW_PATH,
117+
old_as_path=BLOB_OLD_PATH,
118+
new_as_path=BLOB_NEW_PATH,
108119
)
109120

110121
self.assertEqual(patch.patch, BLOB_PATCH)
111122

112123
def test_patch_create_from_blob_buffer_add(self):
113124
patch = pygit2.Patch.create_from(
114125
None,
115-
BLOB_OLD_PATH,
116126
BLOB_NEW_CONTENT,
117-
BLOB_NEW_PATH,
127+
old_as_path=BLOB_OLD_PATH,
128+
new_as_path=BLOB_NEW_PATH,
118129
)
119130

120131
self.assertEqual(patch.patch, BLOB_PATCH_ADDED)
121132

122133
def test_patch_create_from_blob_buffer_delete(self):
123-
old_blob = self.repo.create_blob(BLOB_OLD_CONTENT)
134+
old_blob = self.repo[BLOB_OLD_SHA]
124135

125136
patch = pygit2.Patch.create_from(
126-
self.repo[old_blob],
127-
BLOB_OLD_PATH,
137+
old_blob,
128138
None,
129-
BLOB_NEW_PATH,
139+
old_as_path=BLOB_OLD_PATH,
140+
new_as_path=BLOB_NEW_PATH,
130141
)
131142

132143
self.assertEqual(patch.patch, BLOB_PATCH_DELETED)
@@ -135,16 +146,12 @@ def test_patch_create_from_bad_old_type_arg(self):
135146
with self.assertRaises(TypeError):
136147
pygit2.Patch.create_from(
137148
self.repo,
138-
BLOB_OLD_PATH,
139149
BLOB_NEW_CONTENT,
140-
BLOB_NEW_PATH,
141150
)
142151

143152
def test_patch_create_from_bad_new_type_arg(self):
144153
with self.assertRaises(TypeError):
145154
pygit2.Patch.create_from(
146155
None,
147-
BLOB_OLD_PATH,
148156
self.repo,
149-
BLOB_NEW_PATH,
150157
)

0 commit comments

Comments
 (0)