Commit e774d5af764c521f3a5740f8c0f7b859ebb109c8

Edward Thomson 2016-04-25T16:47:48

diff::parse tests: test parsing a diff Test that we can create a diff file, then parse the results and that the two are identical in-memory.

diff --git a/tests/diff/diff_helpers.c b/tests/diff/diff_helpers.c
index c6cdf80..8fa8e3e 100644
--- a/tests/diff/diff_helpers.c
+++ b/tests/diff/diff_helpers.c
@@ -241,3 +241,39 @@ void diff_print_raw(FILE *fp, git_diff *diff)
 		git_diff_print(diff, GIT_DIFF_FORMAT_RAW,
 			git_diff_print_callback__to_file_handle, fp ? fp : stderr));
 }
+
+void diff_assert_equal(git_diff *a, git_diff *b)
+{
+	const git_diff_delta *ad, *bd;
+	size_t i;
+
+	assert(a && b);
+
+	cl_assert_equal_i(git_diff_num_deltas(a), git_diff_num_deltas(b));
+
+	for (i = 0; i < git_diff_num_deltas(a); i++) {
+		ad = git_diff_get_delta(a, i);
+		bd = git_diff_get_delta(b, i);
+
+		cl_assert_equal_i(ad->status, bd->status);
+		cl_assert_equal_i(ad->flags, bd->flags);
+		cl_assert_equal_i(ad->similarity, bd->similarity);
+		cl_assert_equal_i(ad->nfiles, bd->nfiles);
+
+		/* Don't examine the size or the flags of the deltas;
+		 * computed deltas have sizes (parsed deltas do not) and
+		 * computed deltas will have flags of `VALID_ID` and
+		 * `EXISTS` (parsed deltas will not query the ODB.)
+		 */
+		cl_assert_equal_oid(&ad->old_file.id, &bd->old_file.id);
+		cl_assert_equal_i(ad->old_file.id_abbrev, bd->old_file.id_abbrev);
+		cl_assert_equal_s(ad->old_file.path, bd->old_file.path);
+		cl_assert_equal_i(ad->old_file.mode, bd->old_file.mode);
+
+		cl_assert_equal_oid(&ad->new_file.id, &bd->new_file.id);
+		cl_assert_equal_i(ad->new_file.id_abbrev, bd->new_file.id_abbrev);
+		cl_assert_equal_s(ad->new_file.path, bd->new_file.path);
+		cl_assert_equal_i(ad->new_file.mode, bd->new_file.mode);
+	}
+}
+
diff --git a/tests/diff/diff_helpers.h b/tests/diff/diff_helpers.h
index 4d3cd34..520b654 100644
--- a/tests/diff/diff_helpers.h
+++ b/tests/diff/diff_helpers.h
@@ -68,3 +68,6 @@ extern int diff_foreach_via_iterator(
 
 extern void diff_print(FILE *fp, git_diff *diff);
 extern void diff_print_raw(FILE *fp, git_diff *diff);
+
+extern void diff_assert_equal(git_diff *a, git_diff *b);
+
diff --git a/tests/diff/parse.c b/tests/diff/parse.c
index 8eb9842..2d912c0 100644
--- a/tests/diff/parse.c
+++ b/tests/diff/parse.c
@@ -1,9 +1,15 @@
 #include "clar_libgit2.h"
 #include "patch.h"
 #include "patch_parse.h"
+#include "diff_helpers.h"
 
 #include "../patch/patch_common.h"
 
+void test_diff_parse__cleanup(void)
+{
+	cl_git_sandbox_cleanup();
+}
+
 void test_diff_parse__nonpatches_fail_with_notfound(void)
 {
 	git_diff *diff;
@@ -58,3 +64,65 @@ void test_diff_parse__invalid_patches_fails(void)
 	test_parse_invalid_diff(PATCH_CORRUPT_MISSING_HUNK_HEADER);
 }
 
+static void test_tree_to_tree_computed_to_parsed(
+	const char *sandbox, const char *a_id, const char *b_id)
+{
+	git_repository *repo;
+	git_diff *computed, *parsed;
+	git_tree *a, *b;
+	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
+	git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
+	git_buf computed_buf = GIT_BUF_INIT;
+
+	repo = cl_git_sandbox_init(sandbox);
+
+	opts.id_abbrev = GIT_OID_HEXSZ;
+	opts.flags = GIT_DIFF_SHOW_BINARY;
+
+	cl_assert((a = resolve_commit_oid_to_tree(repo, a_id)) != NULL);
+	cl_assert((b = resolve_commit_oid_to_tree(repo, b_id)) != NULL);
+
+	cl_git_pass(git_diff_tree_to_tree(&computed, repo, a, b, &opts));
+	cl_git_pass(git_diff_to_buf(&computed_buf,
+		computed, GIT_DIFF_FORMAT_PATCH));
+
+	cl_git_pass(git_diff_from_buffer(&parsed,
+		computed_buf.ptr, computed_buf.size));
+
+	diff_assert_equal(computed, parsed);
+
+	git_tree_free(a);
+	git_tree_free(b);
+
+	git_diff_free(computed);
+	git_diff_free(parsed);
+
+	git_buf_free(&computed_buf);
+
+	cl_git_sandbox_cleanup();
+}
+
+void test_diff_parse__can_parse_generated_diff(void)
+{
+	test_tree_to_tree_computed_to_parsed("diff", "d70d245e", "7a9e0b02");
+	test_tree_to_tree_computed_to_parsed(
+		"unsymlinked.git", "806999", "a8595c");
+	test_tree_to_tree_computed_to_parsed("diff",
+		"d70d245ed97ed2aa596dd1af6536e4bfdb047b69",
+		"7a9e0b02e63179929fed24f0a3e0f19168114d10");
+	test_tree_to_tree_computed_to_parsed(
+		"unsymlinked.git", "7fccd7", "806999");
+	test_tree_to_tree_computed_to_parsed(
+		"unsymlinked.git", "7fccd7", "a8595c");
+	test_tree_to_tree_computed_to_parsed("attr", "605812a", "370fe9ec22");
+	test_tree_to_tree_computed_to_parsed(
+		"attr", "f5b0af1fb4f5c", "370fe9ec22");
+	test_tree_to_tree_computed_to_parsed("diff", "d70d245e", "d70d245e");
+	test_tree_to_tree_computed_to_parsed("diff_format_email",
+		"873806f6f27e631eb0b23e4b56bea2bfac14a373",
+		"897d3af16ca9e420cd071b1c4541bd2b91d04c8c");
+	test_tree_to_tree_computed_to_parsed("diff_format_email",
+		"897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
+		"873806f6f27e631eb0b23e4b56bea2bfac14a373");
+}
+