Commit 702d4bec01aceb5dd0d23f91e567341af9195754

Edward Thomson 2018-06-26T15:26:37

apply tests: use `git_iterator_foreach` for tests Use the new `git_iterator_foreach` API to validate the workdir against the expected workdir values instead of using the paired/multi iterator comparison callback. This allows us to use the `git_iterator_foreach` to validate the index as well, instead of assuming that the index and HEAD must always match.

diff --git a/tests/apply/workdir.c b/tests/apply/workdir.c
index 3c55203..c30d207 100644
--- a/tests/apply/workdir.c
+++ b/tests/apply/workdir.c
@@ -21,31 +21,17 @@ struct iterator_compare_data {
 	size_t idx;
 };
 
-static int iterator_compare(const git_index_entry **entries, void *_data)
+static int iterator_compare(const git_index_entry *entry, void *_data)
 {
-	const git_index_entry *head_entry = entries[0];
-	const git_index_entry *index_entry = entries[1];
-	const git_index_entry *workdir_entry = entries[2];
 	git_oid expected_id;
-	struct iterator_compare_data *data = (struct iterator_compare_data *)_data;
-
-	if (!head_entry || !index_entry) {
-		cl_assert_equal_p(head_entry, index_entry);
-	} else {
-		cl_assert_equal_i(GIT_IDXENTRY_STAGE(head_entry), GIT_IDXENTRY_STAGE(index_entry));
-		cl_assert_equal_oid(&head_entry->id, &index_entry->id);
-		cl_assert_equal_i(head_entry->mode, index_entry->mode);
-		cl_assert_equal_s(head_entry->path, index_entry->path);
-	}
 
-	if (!workdir_entry)
-		return 0;
+	struct iterator_compare_data *data = (struct iterator_compare_data *)_data;
 
-	cl_assert_equal_i(GIT_IDXENTRY_STAGE(workdir_entry), data->expected[data->idx].stage);
+	cl_assert_equal_i(GIT_IDXENTRY_STAGE(entry), data->expected[data->idx].stage);
 	cl_git_pass(git_oid_fromstr(&expected_id, data->expected[data->idx].oid_str));
-	cl_assert_equal_oid(&workdir_entry->id, &expected_id);
-	cl_assert_equal_i(workdir_entry->mode, data->expected[data->idx].mode);
-	cl_assert_equal_s(workdir_entry->path, data->expected[data->idx].path);
+	cl_assert_equal_oid(&entry->id, &expected_id);
+	cl_assert_equal_i(entry->mode, data->expected[data->idx].mode);
+	cl_assert_equal_s(entry->path, data->expected[data->idx].path);
 
 	if (data->idx >= data->cnt)
 		return -1;
@@ -57,37 +43,63 @@ static int iterator_compare(const git_index_entry **entries, void *_data)
 
 static void validate_apply_workdir(
 	git_repository *repo,
-	struct merge_index_entry *entries,
-	size_t cnt)
+	struct merge_index_entry *workdir_entries,
+	size_t workdir_cnt)
 {
-	git_tree *head;
-	git_index *repo_index;
-	git_iterator *head_iterator, *index_iterator, *workdir_iterator, *iterators[3];
-	git_iterator_options workdir_opts = GIT_ITERATOR_OPTIONS_INIT;
+	git_index *index;
+	git_iterator *iterator;
+	git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
+	struct iterator_compare_data data = { workdir_entries, workdir_cnt };
+
+	opts.flags |= GIT_ITERATOR_INCLUDE_HASH;
+
+	cl_git_pass(git_repository_index(&index, repo));
+	cl_git_pass(git_iterator_for_workdir(&iterator, repo, index, NULL, &opts));
+
+	cl_git_pass(git_iterator_foreach(iterator, iterator_compare, &data));
+	cl_assert_equal_i(data.idx, data.cnt);
+
+	git_iterator_free(iterator);
+	git_index_free(index);
+}
+
+static int iterator_eq(const git_index_entry **entry, void *_data)
+{
+	GIT_UNUSED(_data);
+
+	if (!entry[0] || !entry[1])
+		return -1;
 
-	struct iterator_compare_data data = { entries, cnt };
+	cl_assert_equal_i(GIT_IDXENTRY_STAGE(entry[0]), GIT_IDXENTRY_STAGE(entry[1]));
+	cl_assert_equal_oid(&entry[0]->id, &entry[1]->id);
+	cl_assert_equal_i(entry[0]->mode, entry[1]->mode);
+	cl_assert_equal_s(entry[0]->path, entry[1]->path);
 
-	workdir_opts.flags |= GIT_ITERATOR_INCLUDE_HASH;
+	return 0;
+}
+
+static void validate_index_unchanged(git_repository *repo)
+{
+	git_tree *head;
+	git_index *index;
+	git_iterator *head_iterator, *index_iterator, *iterators[2];
 
 	cl_git_pass(git_repository_head_tree(&head, repo));
-	cl_git_pass(git_repository_index(&repo_index, repo));
+	cl_git_pass(git_repository_index(&index, repo));
 
 	cl_git_pass(git_iterator_for_tree(&head_iterator, head, NULL));
-	cl_git_pass(git_iterator_for_index(&index_iterator, repo, repo_index, NULL));
-	cl_git_pass(git_iterator_for_workdir(&workdir_iterator, repo, repo_index, NULL, &workdir_opts));
+	cl_git_pass(git_iterator_for_index(&index_iterator, repo, index, NULL));
 
 	iterators[0] = head_iterator;
 	iterators[1] = index_iterator;
-	iterators[2] = workdir_iterator;
 
-	cl_git_pass(git_iterator_walk(iterators, 3, iterator_compare, &data));
-	cl_assert_equal_i(data.idx, data.cnt);
+	cl_git_pass(git_iterator_walk(iterators, 2, iterator_eq, NULL));
 
 	git_iterator_free(head_iterator);
 	git_iterator_free(index_iterator);
-	git_iterator_free(workdir_iterator);
-	git_index_free(repo_index);
+
 	git_tree_free(head);
+	git_index_free(index);
 }
 
 void test_apply_workdir__generated_diff(void)
@@ -98,7 +110,7 @@ void test_apply_workdir__generated_diff(void)
 	git_diff *diff;
 	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
 
-	struct merge_index_entry expected[] = {
+	struct merge_index_entry workdir_expected[] = {
 		{ 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
 		{ 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
 		{ 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
@@ -106,12 +118,11 @@ void test_apply_workdir__generated_diff(void)
 		{ 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
 		{ 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
 	};
-	size_t expected_cnt = sizeof(expected) / sizeof(struct merge_index_entry);
+	size_t workdir_expected_cnt = sizeof(workdir_expected) /
+		sizeof(struct merge_index_entry);
 
 	git_oid_fromstr(&a_oid, "539bd011c4822c560c1d17cab095006b7a10f707");
-	git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f");
-
-	cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid));
+	git_oid_fromstr(&b_oid, "7c7bf85e978f1d18c0566f702d2cb7766b9c8d4f"); cl_git_pass(git_commit_lookup(&a_commit, repo, &a_oid));
 	cl_git_pass(git_commit_lookup(&b_commit, repo, &b_oid));
 
 	cl_git_pass(git_commit_tree(&a_tree, a_commit));
@@ -122,7 +133,8 @@ void test_apply_workdir__generated_diff(void)
     cl_git_pass(git_reset(repo, (git_object *)a_commit, GIT_RESET_HARD, NULL));
 	cl_git_pass(git_apply(repo, diff, NULL));
 
-	validate_apply_workdir(repo, expected, expected_cnt);
+	validate_index_unchanged(repo);
+	validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
 
 	git_diff_free(diff);
 	git_tree_free(a_tree);
@@ -156,7 +168,7 @@ void test_apply_workdir__parsed_diff(void)
 		"-longer. take out the slices of ham, and skim off the grease if any\n"
 		"+longer; take out the slices of ham, and skim off the grease if any\n";
 
-	struct merge_index_entry expected[] = {
+	struct merge_index_entry workdir_expected[] = {
 		{ 0100644, "ffb36e513f5fdf8a6ba850a20142676a2ac4807d", 0, "asparagus.txt" },
 		{ 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
 		{ 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
@@ -164,7 +176,8 @@ void test_apply_workdir__parsed_diff(void)
 		{ 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
 		{ 0100644, "a7b066537e6be7109abfe4ff97b675d4e077da20", 0, "veal.txt" },
 	};
-	size_t expected_cnt = sizeof(expected) / sizeof(struct merge_index_entry);
+	size_t workdir_expected_cnt = sizeof(workdir_expected) /
+	    sizeof(struct merge_index_entry);
 
 	git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
 	cl_git_pass(git_commit_lookup(&commit, repo, &oid));
@@ -174,7 +187,8 @@ void test_apply_workdir__parsed_diff(void)
 	cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
 	cl_git_pass(git_apply(repo, diff, NULL));
 
-	validate_apply_workdir(repo, expected, expected_cnt);
+	validate_index_unchanged(repo);
+	validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
 
 	git_diff_free(diff);
 	git_commit_free(commit);
@@ -202,14 +216,15 @@ void test_apply_workdir__removes_file(void)
 		"-toasted bread in it; two table-spoonsful of mushroom catsup will add a\n"
 		"-fine flavour to the soup.\n";
 
-	struct merge_index_entry expected[] = {
+	struct merge_index_entry workdir_expected[] = {
 		{ 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
 		{ 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
 		{ 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
 		{ 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
 		{ 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
 	};
-	size_t expected_cnt = sizeof(expected) / sizeof(struct merge_index_entry);
+	size_t workdir_expected_cnt = sizeof(workdir_expected) /
+	    sizeof(struct merge_index_entry);
 
 	git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
 	cl_git_pass(git_commit_lookup(&commit, repo, &oid));
@@ -219,7 +234,8 @@ void test_apply_workdir__removes_file(void)
 	cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
 	cl_git_pass(git_apply(repo, diff, NULL));
 
-	validate_apply_workdir(repo, expected, expected_cnt);
+	validate_index_unchanged(repo);
+	validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
 
 	git_diff_free(diff);
 	git_commit_free(commit);
@@ -241,7 +257,7 @@ void test_apply_workdir__adds_file(void)
 		"+This is a new file!\n"
 		"+Added by a patch.\n";
 
-	struct merge_index_entry expected[] = {
+	struct merge_index_entry workdir_expected[] = {
 		{ 0100644, "f51658077d85f2264fa179b4d0848268cb3475c3", 0, "asparagus.txt" },
 		{ 0100644, "68f6182f4c85d39e1309d97c7e456156dc9c0096", 0, "beef.txt" },
 		{ 0100644, "4b7c5650008b2e747fe1809eeb5a1dde0e80850a", 0, "bouilli.txt" },
@@ -250,7 +266,8 @@ void test_apply_workdir__adds_file(void)
 		{ 0100644, "68af1fc7407fd9addf1701a87eb1c95c7494c598", 0, "oyster.txt" },
 		{ 0100644, "94d2c01087f48213bd157222d54edfefd77c9bba", 0, "veal.txt" },
 	};
-	size_t expected_cnt = sizeof(expected) / sizeof(struct merge_index_entry);
+	size_t workdir_expected_cnt = sizeof(workdir_expected) /
+	    sizeof(struct merge_index_entry);
 
 	git_oid_fromstr(&oid, "539bd011c4822c560c1d17cab095006b7a10f707");
 	cl_git_pass(git_commit_lookup(&commit, repo, &oid));
@@ -260,7 +277,8 @@ void test_apply_workdir__adds_file(void)
 	cl_git_pass(git_reset(repo, (git_object *)commit, GIT_RESET_HARD, NULL));
 	cl_git_pass(git_apply(repo, diff, NULL));
 
-	validate_apply_workdir(repo, expected, expected_cnt);
+	validate_index_unchanged(repo);
+	validate_apply_workdir(repo, workdir_expected, workdir_expected_cnt);
 
 	git_diff_free(diff);
 	git_commit_free(commit);