]>
Commit | Line | Data |
---|---|---|
3a437590 RB |
1 | #include "clar_libgit2.h" |
2 | #include "diff_helpers.h" | |
3 | ||
4 | static git_repository *g_repo = NULL; | |
1956693f | 5 | static diff_expects expected; |
4f806761 | 6 | static git_diff_options opts; |
28ef7f9b | 7 | static git_blob *d, *alien; |
3a437590 RB |
8 | |
9 | void test_diff_blob__initialize(void) | |
10 | { | |
28ef7f9b | 11 | git_oid oid; |
4f806761 | 12 | |
854eccbb | 13 | g_repo = cl_git_sandbox_init("attr"); |
4f806761 | 14 | |
0ab3a2ab | 15 | GIT_INIT_STRUCTURE(&opts, GIT_DIFF_OPTIONS_VERSION); |
4f806761 | 16 | opts.context_lines = 1; |
29f9186d | 17 | opts.interhunk_lines = 0; |
4f806761 | 18 | |
1956693f | 19 | memset(&expected, 0, sizeof(expected)); |
4f806761 | 20 | |
21 | /* tests/resources/attr/root_test4.txt */ | |
29f9186d | 22 | cl_git_pass(git_oid_fromstrn(&oid, "a0f7217a", 8)); |
23 | cl_git_pass(git_blob_lookup_prefix(&d, g_repo, &oid, 4)); | |
28ef7f9b | 24 | |
25 | /* alien.png */ | |
26 | cl_git_pass(git_oid_fromstrn(&oid, "edf3dcee", 8)); | |
27 | cl_git_pass(git_blob_lookup_prefix(&alien, g_repo, &oid, 4)); | |
3a437590 RB |
28 | } |
29 | ||
30 | void test_diff_blob__cleanup(void) | |
31 | { | |
4f806761 | 32 | git_blob_free(d); |
9094d30b SC |
33 | d = NULL; |
34 | ||
28ef7f9b | 35 | git_blob_free(alien); |
9094d30b | 36 | alien = NULL; |
4f806761 | 37 | |
854eccbb | 38 | cl_git_sandbox_cleanup(); |
3a437590 RB |
39 | } |
40 | ||
245c5eae | 41 | void test_diff_blob__can_compare_text_blobs(void) |
3a437590 | 42 | { |
4f806761 | 43 | git_blob *a, *b, *c; |
44 | git_oid a_oid, b_oid, c_oid; | |
3a437590 RB |
45 | |
46 | /* tests/resources/attr/root_test1 */ | |
47 | cl_git_pass(git_oid_fromstrn(&a_oid, "45141a79", 8)); | |
48 | cl_git_pass(git_blob_lookup_prefix(&a, g_repo, &a_oid, 4)); | |
49 | ||
50 | /* tests/resources/attr/root_test2 */ | |
51 | cl_git_pass(git_oid_fromstrn(&b_oid, "4d713dc4", 8)); | |
52 | cl_git_pass(git_blob_lookup_prefix(&b, g_repo, &b_oid, 4)); | |
53 | ||
54 | /* tests/resources/attr/root_test3 */ | |
55 | cl_git_pass(git_oid_fromstrn(&c_oid, "c96bbb2c2557a832", 16)); | |
56 | cl_git_pass(git_blob_lookup_prefix(&c, g_repo, &c_oid, 8)); | |
57 | ||
3a437590 RB |
58 | /* Doing the equivalent of a `git diff -U1` on these files */ |
59 | ||
29f9186d | 60 | /* diff on tests/resources/attr/root_test1 */ |
3a437590 | 61 | cl_git_pass(git_diff_blobs( |
793c4385 | 62 | a, b, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 63 | |
f335ecd6 | 64 | cl_assert_equal_i(1, expected.files); |
b4f5bb07 RB |
65 | cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); |
66 | cl_assert_equal_i(0, expected.files_binary); | |
3a437590 | 67 | |
f335ecd6 RB |
68 | cl_assert_equal_i(1, expected.hunks); |
69 | cl_assert_equal_i(6, expected.lines); | |
70 | cl_assert_equal_i(1, expected.line_ctxt); | |
71 | cl_assert_equal_i(5, expected.line_adds); | |
72 | cl_assert_equal_i(0, expected.line_dels); | |
3a437590 | 73 | |
29f9186d | 74 | /* diff on tests/resources/attr/root_test2 */ |
1956693f | 75 | memset(&expected, 0, sizeof(expected)); |
3a437590 | 76 | cl_git_pass(git_diff_blobs( |
793c4385 | 77 | b, c, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 78 | |
f335ecd6 | 79 | cl_assert_equal_i(1, expected.files); |
b4f5bb07 RB |
80 | cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); |
81 | cl_assert_equal_i(0, expected.files_binary); | |
3a437590 | 82 | |
f335ecd6 RB |
83 | cl_assert_equal_i(1, expected.hunks); |
84 | cl_assert_equal_i(15, expected.lines); | |
85 | cl_assert_equal_i(3, expected.line_ctxt); | |
86 | cl_assert_equal_i(9, expected.line_adds); | |
87 | cl_assert_equal_i(3, expected.line_dels); | |
3a437590 | 88 | |
29f9186d | 89 | /* diff on tests/resources/attr/root_test3 */ |
1956693f | 90 | memset(&expected, 0, sizeof(expected)); |
3a437590 | 91 | cl_git_pass(git_diff_blobs( |
793c4385 | 92 | a, c, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 93 | |
f335ecd6 | 94 | cl_assert_equal_i(1, expected.files); |
b4f5bb07 RB |
95 | cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); |
96 | cl_assert_equal_i(0, expected.files_binary); | |
3a437590 | 97 | |
f335ecd6 RB |
98 | cl_assert_equal_i(1, expected.hunks); |
99 | cl_assert_equal_i(13, expected.lines); | |
100 | cl_assert_equal_i(0, expected.line_ctxt); | |
101 | cl_assert_equal_i(12, expected.line_adds); | |
102 | cl_assert_equal_i(1, expected.line_dels); | |
3a437590 | 103 | |
1956693f | 104 | memset(&expected, 0, sizeof(expected)); |
3a437590 | 105 | cl_git_pass(git_diff_blobs( |
793c4385 | 106 | c, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 107 | |
f335ecd6 | 108 | cl_assert_equal_i(1, expected.files); |
b4f5bb07 RB |
109 | cl_assert_equal_i(1, expected.file_status[GIT_DELTA_MODIFIED]); |
110 | cl_assert_equal_i(0, expected.files_binary); | |
3a437590 | 111 | |
f335ecd6 RB |
112 | cl_assert_equal_i(2, expected.hunks); |
113 | cl_assert_equal_i(14, expected.lines); | |
114 | cl_assert_equal_i(4, expected.line_ctxt); | |
115 | cl_assert_equal_i(6, expected.line_adds); | |
116 | cl_assert_equal_i(4, expected.line_dels); | |
3a437590 RB |
117 | |
118 | git_blob_free(a); | |
119 | git_blob_free(b); | |
120 | git_blob_free(c); | |
3a437590 RB |
121 | } |
122 | ||
4f806761 | 123 | void test_diff_blob__can_compare_against_null_blobs(void) |
124 | { | |
125 | git_blob *e = NULL; | |
126 | ||
127 | cl_git_pass(git_diff_blobs( | |
793c4385 | 128 | d, e, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 129 | |
f335ecd6 | 130 | cl_assert_equal_i(1, expected.files); |
b4f5bb07 RB |
131 | cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]); |
132 | cl_assert_equal_i(0, expected.files_binary); | |
4f806761 | 133 | |
f335ecd6 RB |
134 | cl_assert_equal_i(1, expected.hunks); |
135 | cl_assert_equal_i(14, expected.hunk_old_lines); | |
136 | cl_assert_equal_i(14, expected.lines); | |
137 | cl_assert_equal_i(14, expected.line_dels); | |
4f806761 | 138 | |
139 | opts.flags |= GIT_DIFF_REVERSE; | |
1956693f | 140 | memset(&expected, 0, sizeof(expected)); |
4f806761 | 141 | |
142 | cl_git_pass(git_diff_blobs( | |
793c4385 | 143 | d, e, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 144 | |
f335ecd6 | 145 | cl_assert_equal_i(1, expected.files); |
b4f5bb07 RB |
146 | cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]); |
147 | cl_assert_equal_i(0, expected.files_binary); | |
4f806761 | 148 | |
f335ecd6 RB |
149 | cl_assert_equal_i(1, expected.hunks); |
150 | cl_assert_equal_i(14, expected.hunk_new_lines); | |
151 | cl_assert_equal_i(14, expected.lines); | |
152 | cl_assert_equal_i(14, expected.line_adds); | |
28ef7f9b | 153 | |
154 | opts.flags ^= GIT_DIFF_REVERSE; | |
1956693f | 155 | memset(&expected, 0, sizeof(expected)); |
28ef7f9b | 156 | |
157 | cl_git_pass(git_diff_blobs( | |
793c4385 | 158 | alien, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 159 | |
f335ecd6 | 160 | cl_assert_equal_i(1, expected.files); |
b4f5bb07 RB |
161 | cl_assert_equal_i(1, expected.files_binary); |
162 | cl_assert_equal_i(1, expected.file_status[GIT_DELTA_DELETED]); | |
f335ecd6 RB |
163 | cl_assert_equal_i(0, expected.hunks); |
164 | cl_assert_equal_i(0, expected.lines); | |
28ef7f9b | 165 | |
1956693f | 166 | memset(&expected, 0, sizeof(expected)); |
28ef7f9b | 167 | |
168 | cl_git_pass(git_diff_blobs( | |
793c4385 | 169 | NULL, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 170 | |
f335ecd6 | 171 | cl_assert_equal_i(1, expected.files); |
b4f5bb07 RB |
172 | cl_assert_equal_i(1, expected.files_binary); |
173 | cl_assert_equal_i(1, expected.file_status[GIT_DELTA_ADDED]); | |
f335ecd6 RB |
174 | cl_assert_equal_i(0, expected.hunks); |
175 | cl_assert_equal_i(0, expected.lines); | |
28ef7f9b | 176 | } |
177 | ||
b4f5bb07 | 178 | static void assert_identical_blobs_comparison(diff_expects *expected) |
9a29f8d5 | 179 | { |
b4f5bb07 RB |
180 | cl_assert_equal_i(1, expected->files); |
181 | cl_assert_equal_i(1, expected->file_status[GIT_DELTA_UNMODIFIED]); | |
182 | cl_assert_equal_i(0, expected->hunks); | |
183 | cl_assert_equal_i(0, expected->lines); | |
9a29f8d5 | 184 | } |
185 | ||
186 | void test_diff_blob__can_compare_identical_blobs(void) | |
187 | { | |
188 | cl_git_pass(git_diff_blobs( | |
793c4385 | 189 | d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
9a29f8d5 | 190 | |
b4f5bb07 RB |
191 | cl_assert_equal_i(0, expected.files_binary); |
192 | assert_identical_blobs_comparison(&expected); | |
9a29f8d5 | 193 | |
1956693f | 194 | memset(&expected, 0, sizeof(expected)); |
9a29f8d5 | 195 | cl_git_pass(git_diff_blobs( |
793c4385 | 196 | NULL, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
9a29f8d5 | 197 | |
b4f5bb07 RB |
198 | cl_assert_equal_i(0, expected.files_binary); |
199 | assert_identical_blobs_comparison(&expected); | |
9a29f8d5 | 200 | |
1956693f | 201 | memset(&expected, 0, sizeof(expected)); |
9a29f8d5 | 202 | cl_git_pass(git_diff_blobs( |
793c4385 | 203 | alien, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
9a29f8d5 | 204 | |
b4f5bb07 RB |
205 | cl_assert(expected.files_binary > 0); |
206 | assert_identical_blobs_comparison(&expected); | |
9a29f8d5 | 207 | } |
208 | ||
b4f5bb07 | 209 | static void assert_binary_blobs_comparison(diff_expects *expected) |
28ef7f9b | 210 | { |
b4f5bb07 | 211 | cl_assert(expected->files_binary > 0); |
28ef7f9b | 212 | |
b4f5bb07 RB |
213 | cl_assert_equal_i(1, expected->files); |
214 | cl_assert_equal_i(1, expected->file_status[GIT_DELTA_MODIFIED]); | |
215 | cl_assert_equal_i(0, expected->hunks); | |
216 | cl_assert_equal_i(0, expected->lines); | |
28ef7f9b | 217 | } |
218 | ||
219 | void test_diff_blob__can_compare_two_binary_blobs(void) | |
220 | { | |
221 | git_blob *heart; | |
222 | git_oid h_oid; | |
223 | ||
224 | /* heart.png */ | |
225 | cl_git_pass(git_oid_fromstrn(&h_oid, "de863bff", 8)); | |
226 | cl_git_pass(git_blob_lookup_prefix(&heart, g_repo, &h_oid, 4)); | |
227 | ||
228 | cl_git_pass(git_diff_blobs( | |
793c4385 | 229 | alien, heart, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 230 | |
b4f5bb07 | 231 | assert_binary_blobs_comparison(&expected); |
28ef7f9b | 232 | |
1956693f | 233 | memset(&expected, 0, sizeof(expected)); |
28ef7f9b | 234 | |
235 | cl_git_pass(git_diff_blobs( | |
793c4385 | 236 | heart, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 237 | |
b4f5bb07 | 238 | assert_binary_blobs_comparison(&expected); |
28ef7f9b | 239 | |
240 | git_blob_free(heart); | |
241 | } | |
242 | ||
243 | void test_diff_blob__can_compare_a_binary_blob_and_a_text_blob(void) | |
244 | { | |
245 | cl_git_pass(git_diff_blobs( | |
793c4385 | 246 | alien, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 247 | |
b4f5bb07 | 248 | assert_binary_blobs_comparison(&expected); |
28ef7f9b | 249 | |
1956693f | 250 | memset(&expected, 0, sizeof(expected)); |
28ef7f9b | 251 | |
252 | cl_git_pass(git_diff_blobs( | |
793c4385 | 253 | d, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
28ef7f9b | 254 | |
b4f5bb07 | 255 | assert_binary_blobs_comparison(&expected); |
4f806761 | 256 | } |
29f9186d | 257 | |
258 | /* | |
259 | * $ git diff fe773770 a0f7217 | |
260 | * diff --git a/fe773770 b/a0f7217 | |
261 | * index fe77377..a0f7217 100644 | |
262 | * --- a/fe773770 | |
263 | * +++ b/a0f7217 | |
264 | * @@ -1,6 +1,6 @@ | |
265 | * Here is some stuff at the start | |
266 | * | |
267 | * -This should go in one hunk | |
268 | * +This should go in one hunk (first) | |
269 | * | |
270 | * Some additional lines | |
271 | * | |
272 | * @@ -8,7 +8,7 @@ Down here below the other lines | |
273 | * | |
274 | * With even more at the end | |
275 | * | |
276 | * -Followed by a second hunk of stuff | |
277 | * +Followed by a second hunk of stuff (second) | |
278 | * | |
279 | * That happens down here | |
280 | */ | |
281 | void test_diff_blob__comparing_two_text_blobs_honors_interhunkcontext(void) | |
282 | { | |
283 | git_blob *old_d; | |
284 | git_oid old_d_oid; | |
285 | ||
286 | opts.context_lines = 3; | |
287 | ||
288 | /* tests/resources/attr/root_test1 from commit f5b0af1 */ | |
289 | cl_git_pass(git_oid_fromstrn(&old_d_oid, "fe773770", 8)); | |
290 | cl_git_pass(git_blob_lookup_prefix(&old_d, g_repo, &old_d_oid, 4)); | |
291 | ||
292 | /* Test with default inter-hunk-context (not set) => default is 0 */ | |
293 | cl_git_pass(git_diff_blobs( | |
793c4385 | 294 | old_d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
29f9186d | 295 | |
f335ecd6 | 296 | cl_assert_equal_i(2, expected.hunks); |
29f9186d | 297 | |
298 | /* Test with inter-hunk-context explicitly set to 0 */ | |
299 | opts.interhunk_lines = 0; | |
300 | memset(&expected, 0, sizeof(expected)); | |
301 | cl_git_pass(git_diff_blobs( | |
793c4385 | 302 | old_d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
29f9186d | 303 | |
f335ecd6 | 304 | cl_assert_equal_i(2, expected.hunks); |
29f9186d | 305 | |
306 | /* Test with inter-hunk-context explicitly set to 1 */ | |
307 | opts.interhunk_lines = 1; | |
308 | memset(&expected, 0, sizeof(expected)); | |
309 | cl_git_pass(git_diff_blobs( | |
793c4385 | 310 | old_d, d, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); |
29f9186d | 311 | |
f335ecd6 | 312 | cl_assert_equal_i(1, expected.hunks); |
29f9186d | 313 | |
314 | git_blob_free(old_d); | |
315 | } | |
2f8d30be BS |
316 | |
317 | void test_diff_blob__checks_options_version_too_low(void) | |
318 | { | |
319 | const git_error *err; | |
320 | ||
321 | opts.version = 0; | |
322 | cl_git_fail(git_diff_blobs( | |
323 | d, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); | |
324 | err = giterr_last(); | |
325 | cl_assert_equal_i(GITERR_INVALID, err->klass); | |
326 | } | |
327 | ||
328 | void test_diff_blob__checks_options_version_too_high(void) | |
329 | { | |
330 | const git_error *err; | |
331 | ||
332 | opts.version = 1024; | |
333 | cl_git_fail(git_diff_blobs( | |
334 | d, alien, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected)); | |
335 | err = giterr_last(); | |
336 | cl_assert_equal_i(GITERR_INVALID, err->klass); | |
337 | } | |
a3337f10 | 338 | |
339 | void test_diff_blob__can_correctly_detect_a_binary_blob_as_binary(void) | |
340 | { | |
341 | /* alien.png */ | |
342 | cl_assert_equal_i(true, git_blob_is_binary(alien)); | |
343 | } | |
344 | ||
345 | void test_diff_blob__can_correctly_detect_a_textual_blob_as_non_binary(void) | |
346 | { | |
347 | /* tests/resources/attr/root_test4.txt */ | |
348 | cl_assert_equal_i(false, git_blob_is_binary(d)); | |
349 | } |