Commit a140c138757e6d4dbcd34a2c68ca34e87cc76238

Nika Layzell 2018-04-08T03:01:37

mailmap: Updates tests for new API and features

diff --git a/tests/mailmap/basic.c b/tests/mailmap/basic.c
index 23f447f..b8a9132 100644
--- a/tests/mailmap/basic.c
+++ b/tests/mailmap/basic.c
@@ -2,7 +2,7 @@
 #include "clar_libgit2.h"
 
 #include "common.h"
-#include "git2/mailmap.h"
+#include "mailmap.h"
 
 static git_mailmap *mailmap = NULL;
 
@@ -41,27 +41,25 @@ void test_mailmap_basic__cleanup(void)
 
 void test_mailmap_basic__entry(void)
 {
+	size_t idx;
 	const git_mailmap_entry *entry;
 
-	cl_assert_equal_sz(ARRAY_SIZE(expected), git_mailmap_entry_count(mailmap));
+	/* Check that we have the expected # of entries */
+	cl_assert_equal_sz(ARRAY_SIZE(expected), git_vector_length(&mailmap->entries));
+
+	for (idx = 0; idx < ARRAY_SIZE(expected); ++idx) {
+		/* Try to look up each entry and make sure they match */
+		entry = git_mailmap_entry_lookup(
+			mailmap, expected[idx].replace_name, expected[idx].replace_email);
 
-	for (size_t i = 0; i < ARRAY_SIZE(expected); ++i) {
-		entry = git_mailmap_entry_byindex(mailmap, i);
 		cl_assert(entry);
-		cl_assert_equal_s(entry->real_name, expected[i].real_name);
-		cl_assert_equal_s(entry->real_email, expected[i].real_email);
-		cl_assert_equal_s(entry->replace_name, expected[i].replace_name);
-		cl_assert_equal_s(entry->replace_email, expected[i].replace_email);
+		cl_assert_equal_s(entry->real_name, expected[idx].real_name);
+		cl_assert_equal_s(entry->real_email, expected[idx].real_email);
+		cl_assert_equal_s(entry->replace_name, expected[idx].replace_name);
+		cl_assert_equal_s(entry->replace_email, expected[idx].replace_email);
 	}
 }
 
-void test_mailmap_basic__entry_large_index(void)
-{
-	const git_mailmap_entry *entry =
-		git_mailmap_entry_byindex(mailmap, 10000);
-	cl_assert(!entry);
-}
-
 void test_mailmap_basic__lookup_not_found(void)
 {
 	const git_mailmap_entry *entry = git_mailmap_entry_lookup(
diff --git a/tests/mailmap/blame.c b/tests/mailmap/blame.c
index 9ab085b..328f4bc 100644
--- a/tests/mailmap/blame.c
+++ b/tests/mailmap/blame.c
@@ -1,7 +1,7 @@
 #include "clar_libgit2.h"
 #include "git2/repository.h"
 #include "git2/blame.h"
-#include "git2/mailmap.h"
+#include "mailmap.h"
 #include "mailmap_helpers.h"
 
 static git_repository *g_repo;
diff --git a/tests/mailmap/mailmap_helpers.h b/tests/mailmap/mailmap_helpers.h
index 49df55f..173536d 100644
--- a/tests/mailmap/mailmap_helpers.h
+++ b/tests/mailmap/mailmap_helpers.h
@@ -1,4 +1,4 @@
-#include "git2/mailmap.h"
+#include "mailmap.h"
 
 typedef struct mailmap_entry {
 	const char *real_name;
@@ -42,11 +42,3 @@ static const mailmap_entry resolved[] = {
 	{ "Santa Claus", "santa.claus@northpole.xx", "Clause", "me@company.xx" },
 	{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" }
 };
-
-static const mailmap_entry resolved_bare[] = {
-	{ "xx", "untracked@company.xx", "xx", "untracked@company.xx" }
-};
-
-static const mailmap_entry resolved_untracked[] = {
-	{ "Untracked", "untracked@company.xx", "xx", "untracked@company.xx" }
-};
diff --git a/tests/mailmap/parsing.c b/tests/mailmap/parsing.c
index a40d93b..70110c0 100644
--- a/tests/mailmap/parsing.c
+++ b/tests/mailmap/parsing.c
@@ -2,32 +2,41 @@
 #include "repository.h"
 #include "git2/sys/repository.h"
 #include "mailmap_helpers.h"
+#include "buf_text.h"
 
 static git_repository *g_repo;
 static git_mailmap *g_mailmap;
+static git_config *g_config;
 
 void test_mailmap_parsing__initialize(void)
 {
 	g_repo = NULL;
 	g_mailmap = NULL;
+	g_config = NULL;
 }
 
 void test_mailmap_parsing__cleanup(void)
 {
 	git_mailmap_free(g_mailmap);
+	git_config_free(g_config);
 	cl_git_sandbox_cleanup();
 }
 
 static void check_mailmap_entries(
 	const git_mailmap *mailmap, const mailmap_entry *entries, size_t entries_size)
 {
-	const git_mailmap_entry *parsed = NULL;
+	const git_mailmap_entry *parsed;
 	size_t idx;
 
-	/* Check that the parsed entries match */
-	cl_assert_equal_sz(entries_size, git_mailmap_entry_count(mailmap));
+	/* Check the correct # of entries were parsed */
+	cl_assert_equal_sz(entries_size, git_vector_length(&mailmap->entries));
+
+	/* Make sure looking up each entry succeeds */
 	for (idx = 0; idx < entries_size; ++idx) {
-		parsed = git_mailmap_entry_byindex(mailmap, idx);
+		parsed = git_mailmap_entry_lookup(
+			mailmap, entries[idx].replace_name, entries[idx].replace_email);
+
+		cl_assert(parsed);
 		cl_assert_equal_s(parsed->real_name, entries[idx].real_name);
 		cl_assert_equal_s(parsed->real_email, entries[idx].real_email);
 		cl_assert_equal_s(parsed->replace_name, entries[idx].replace_name);
@@ -52,6 +61,10 @@ static void check_mailmap_resolve(
 	}
 }
 
+static const mailmap_entry resolved_untracked[] = {
+	{ "Untracked", "untracked@company.xx", "xx", "untracked@company.xx" }
+};
+
 void test_mailmap_parsing__string(void)
 {
 	git_buf buf = GIT_BUF_INIT;
@@ -77,7 +90,7 @@ void test_mailmap_parsing__windows_string(void)
 	git_buf_text_lf_to_crlf(&winbuf, &unixbuf);
 
 	cl_git_pass(git_mailmap_from_buffer(&g_mailmap, &winbuf));
-	git_buf_free(winbuf);
+	git_buf_free(&winbuf);
 
 	/* We should have parsed all of the entries */
 	check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries));
@@ -93,7 +106,7 @@ void test_mailmap_parsing__fromrepo(void)
 	g_repo = cl_git_sandbox_init("mailmap");
 	cl_check(!git_repository_is_bare(g_repo));
 
-	cl_git_pass(git_mailmap_from_repo(&g_mailmap, g_repo));
+	cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
 
 	/* We should have parsed all of the entries */
 	check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries));
@@ -104,13 +117,17 @@ void test_mailmap_parsing__fromrepo(void)
 		g_mailmap, resolved_untracked, ARRAY_SIZE(resolved_untracked));
 }
 
+static const mailmap_entry resolved_bare[] = {
+	{ "xx", "untracked@company.xx", "xx", "untracked@company.xx" }
+};
+
 void test_mailmap_parsing__frombare(void)
 {
 	g_repo = cl_git_sandbox_init("mailmap/.gitted");
 	cl_git_pass(git_repository_set_bare(g_repo));
 	cl_check(git_repository_is_bare(g_repo));
 
-	cl_git_pass(git_mailmap_from_repo(&g_mailmap, g_repo));
+	cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
 
 	/* We should have parsed all of the entries, except for the untracked one */
 	check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries) - 1);
@@ -120,3 +137,112 @@ void test_mailmap_parsing__frombare(void)
 	check_mailmap_resolve(
 		g_mailmap, resolved_bare, ARRAY_SIZE(resolved_bare));
 }
+
+static const mailmap_entry resolved_with_file_override[] = {
+	{ "Brad", "cto@company.xx", "Brad", "cto@coompany.xx" },
+	{ "Brad L", "cto@company.xx", "Brad L", "cto@coompany.xx" },
+	{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
+	{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
+	{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
+	{ "Other Author", "other@author.xx", "Some Garbage", "nick2@company.xx" },
+	{ "Joseph", "joseph@company.xx", "Joseph", "bugs@company.xx" },
+	{ "Santa Claus", "santa.claus@northpole.xx", "Clause", "me@company.xx" },
+	{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" },
+
+	/* This name is overridden by file_override */
+	{ "File Override", "phil@company.xx", "unknown", "phil@company.xx" },
+	{ "Other Name", "fileoverridename@company.xx", "override", "fileoverridename@company.xx" }
+};
+
+void test_mailmap_parsing__file_config(void)
+{
+	g_repo = cl_git_sandbox_init("mailmap");
+	cl_git_pass(git_repository_config(&g_config, g_repo));
+
+	cl_git_pass(git_config_set_string(
+		g_config, "mailmap.file", cl_fixture("mailmap/file_override")));
+
+	cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
+
+	/* Check we don't have duplicate entries */
+	cl_assert_equal_sz(git_vector_length(&g_mailmap->entries), 9);
+
+	/* Check that resolving the entries works */
+	check_mailmap_resolve(
+		g_mailmap, resolved_with_file_override,
+		ARRAY_SIZE(resolved_with_file_override));
+}
+
+static const mailmap_entry resolved_with_blob_override[] = {
+	{ "Brad", "cto@company.xx", "Brad", "cto@coompany.xx" },
+	{ "Brad L", "cto@company.xx", "Brad L", "cto@coompany.xx" },
+	{ "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" },
+	{ "Other Author", "other@author.xx", "nick2", "bugs@company.xx" },
+	{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
+	{ "Other Author", "other@author.xx", "Some Garbage", "nick2@company.xx" },
+	{ "Joseph", "joseph@company.xx", "Joseph", "bugs@company.xx" },
+	{ "Santa Claus", "santa.claus@northpole.xx", "Clause", "me@company.xx" },
+	{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" },
+
+	/* This name is overridden by blob_override */
+	{ "Blob Override", "phil@company.xx", "unknown", "phil@company.xx" },
+	{ "Other Name", "bloboverridename@company.xx", "override", "bloboverridename@company.xx" }
+};
+
+void test_mailmap_parsing__blob_config(void)
+{
+	g_repo = cl_git_sandbox_init("mailmap");
+	cl_git_pass(git_repository_config(&g_config, g_repo));
+
+	cl_git_pass(git_config_set_string(
+		g_config, "mailmap.blob", "HEAD:blob_override"));
+
+	cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
+
+	/* Check we don't have duplicate entries */
+	cl_assert_equal_sz(git_vector_length(&g_mailmap->entries), 9);
+
+	/* Check that resolving the entries works */
+	check_mailmap_resolve(
+		g_mailmap, resolved_with_blob_override,
+		ARRAY_SIZE(resolved_with_blob_override));
+}
+
+static const mailmap_entry bare_resolved_with_blob_override[] = {
+	/* As mailmap.blob is set, we won't load HEAD:.mailmap */
+	{ "Brad", "cto@coompany.xx", "Brad", "cto@coompany.xx" },
+	{ "Brad L", "cto@coompany.xx", "Brad L", "cto@coompany.xx" },
+	{ "nick1", "bugs@company.xx", "nick1", "bugs@company.xx" },
+	{ "nick2", "bugs@company.xx", "nick2", "bugs@company.xx" },
+	{ "nick3", "bugs@company.xx", "nick3", "bugs@company.xx" },
+	{ "Some Garbage", "nick2@company.xx", "Some Garbage", "nick2@company.xx" },
+	{ "Joseph", "bugs@company.xx", "Joseph", "bugs@company.xx" },
+	{ "Clause", "me@company.xx", "Clause", "me@company.xx" },
+	{ "Charles", "charles@charles.xx", "Charles", "charles@charles.xx" },
+
+	/* This name is overridden by blob_override */
+	{ "Blob Override", "phil@company.xx", "unknown", "phil@company.xx" },
+	{ "Other Name", "bloboverridename@company.xx", "override", "bloboverridename@company.xx" }
+};
+
+void test_mailmap_parsing__bare_blob_config(void)
+{
+	g_repo = cl_git_sandbox_init("mailmap/.gitted");
+	cl_git_pass(git_repository_set_bare(g_repo));
+	cl_check(git_repository_is_bare(g_repo));
+
+	cl_git_pass(git_repository_config(&g_config, g_repo));
+
+	cl_git_pass(git_config_set_string(
+		g_config, "mailmap.blob", "HEAD:blob_override"));
+
+	cl_git_pass(git_mailmap_from_repository(&g_mailmap, g_repo));
+
+	/* Check that we only have the 2 entries */
+	cl_assert_equal_sz(git_vector_length(&g_mailmap->entries), 2);
+
+	/* Check that resolving the entries works */
+	check_mailmap_resolve(
+		g_mailmap, bare_resolved_with_blob_override,
+		ARRAY_SIZE(bare_resolved_with_blob_override));
+}
diff --git a/tests/resources/mailmap/.gitted/index b/tests/resources/mailmap/.gitted/index
index c46a1b4..af175ca 100644
Binary files a/tests/resources/mailmap/.gitted/index and b/tests/resources/mailmap/.gitted/index differ
diff --git a/tests/resources/mailmap/.gitted/objects/69/b9768d022706dab26e2af4dd5a13f42039e36f b/tests/resources/mailmap/.gitted/objects/69/b9768d022706dab26e2af4dd5a13f42039e36f
new file mode 100644
index 0000000..bda7a5d
Binary files /dev/null and b/tests/resources/mailmap/.gitted/objects/69/b9768d022706dab26e2af4dd5a13f42039e36f differ
diff --git a/tests/resources/mailmap/.gitted/objects/6a/0fc44893d4867166f9d321f78c269b3e42b08b b/tests/resources/mailmap/.gitted/objects/6a/0fc44893d4867166f9d321f78c269b3e42b08b
new file mode 100644
index 0000000..9c70031
Binary files /dev/null and b/tests/resources/mailmap/.gitted/objects/6a/0fc44893d4867166f9d321f78c269b3e42b08b differ
diff --git a/tests/resources/mailmap/.gitted/objects/f6/3578091d884c3066a003c50eb6c85ae7542269 b/tests/resources/mailmap/.gitted/objects/f6/3578091d884c3066a003c50eb6c85ae7542269
new file mode 100644
index 0000000..16fd918
--- /dev/null
+++ b/tests/resources/mailmap/.gitted/objects/f6/3578091d884c3066a003c50eb6c85ae7542269
@@ -0,0 +1,2 @@
+x[j0EUB_2ҌmE-$;߹.\u`z8`dm)YK32x(S`t$ρ%ю#<K
+لVӧk|%m_>/\3`!‡Z]޼+3+,}ZL{
\ No newline at end of file
diff --git a/tests/resources/mailmap/.gitted/refs/heads/master b/tests/resources/mailmap/.gitted/refs/heads/master
index 0daf56a..b6dd308 100644
--- a/tests/resources/mailmap/.gitted/refs/heads/master
+++ b/tests/resources/mailmap/.gitted/refs/heads/master
@@ -1 +1 @@
-7100e631fb4d5deba31fdc8acc98f4fb5c1573fd
+f63578091d884c3066a003c50eb6c85ae7542269
diff --git a/tests/resources/mailmap/file_override b/tests/resources/mailmap/file_override
new file mode 100644
index 0000000..94293a9
--- /dev/null
+++ b/tests/resources/mailmap/file_override
@@ -0,0 +1,2 @@
+File Override <phil@company.xx>
+Other Name <fileoverridename@company.xx>