diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2021-11-16 23:29:22 -0500 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2022-02-22 22:07:45 -0500 |
| commit | 3344fddc97bbdea9c1b6ebb6f7fb6dbd70b41dfb (patch) | |
| tree | fd6368a72944571c51627b40c592e7d58e0036e1 /tests/libgit2/mailmap | |
| parent | 91ba089663f5efc3bd4ba14a5099372cf5ce57a6 (diff) | |
| download | libgit2-3344fddc97bbdea9c1b6ebb6f7fb6dbd70b41dfb.tar.gz | |
refactor: `tests` is now `tests/libgit2`
Like we want to separate libgit2 and utility source code, we want to
separate libgit2 and utility tests. Start by moving all the tests into
libgit2.
Diffstat (limited to 'tests/libgit2/mailmap')
| -rw-r--r-- | tests/libgit2/mailmap/basic.c | 101 | ||||
| -rw-r--r-- | tests/libgit2/mailmap/blame.c | 64 | ||||
| -rw-r--r-- | tests/libgit2/mailmap/mailmap_testdata.h | 21 | ||||
| -rw-r--r-- | tests/libgit2/mailmap/parsing.c | 269 |
4 files changed, 455 insertions, 0 deletions
diff --git a/tests/libgit2/mailmap/basic.c b/tests/libgit2/mailmap/basic.c new file mode 100644 index 000000000..1f8ca56c2 --- /dev/null +++ b/tests/libgit2/mailmap/basic.c @@ -0,0 +1,101 @@ +#include "clar.h" +#include "clar_libgit2.h" + +#include "common.h" +#include "mailmap.h" + +static git_mailmap *mailmap = NULL; + +const char TEST_MAILMAP[] = + "Foo bar <foo@bar.com> <foo@baz.com> \n" + "Blatantly invalid line\n" + "Foo bar <foo@bar.com> <foo@bal.com>\n" + "<email@foo.com> <otheremail@foo.com>\n" + "<email@foo.com> Other Name <yetanotheremail@foo.com>\n"; + +struct { + const char *real_name; + const char *real_email; + const char *replace_name; + const char *replace_email; +} expected[] = { + { "Foo bar", "foo@bar.com", NULL, "foo@baz.com" }, + { "Foo bar", "foo@bar.com", NULL, "foo@bal.com" }, + { NULL, "email@foo.com", NULL, "otheremail@foo.com" }, + { NULL, "email@foo.com", "Other Name", "yetanotheremail@foo.com" } +}; + +void test_mailmap_basic__initialize(void) +{ + cl_git_pass(git_mailmap_from_buffer( + &mailmap, TEST_MAILMAP, strlen(TEST_MAILMAP))); +} + +void test_mailmap_basic__cleanup(void) +{ + git_mailmap_free(mailmap); + mailmap = NULL; +} + +void test_mailmap_basic__entry(void) +{ + size_t idx; + const git_mailmap_entry *entry; + + /* 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); + + cl_assert(entry); + 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__lookup_not_found(void) +{ + const git_mailmap_entry *entry = git_mailmap_entry_lookup( + mailmap, "Whoever", "doesnotexist@fo.com"); + cl_assert(!entry); +} + +void test_mailmap_basic__lookup(void) +{ + const git_mailmap_entry *entry = git_mailmap_entry_lookup( + mailmap, "Typoed the name once", "foo@baz.com"); + cl_assert(entry); + cl_assert_equal_s(entry->real_name, "Foo bar"); +} + +void test_mailmap_basic__empty_email_query(void) +{ + const char *name; + const char *email; + cl_git_pass(git_mailmap_resolve( + &name, &email, mailmap, "Author name", "otheremail@foo.com")); + cl_assert_equal_s(name, "Author name"); + cl_assert_equal_s(email, "email@foo.com"); +} + +void test_mailmap_basic__name_matching(void) +{ + const char *name; + const char *email; + cl_git_pass(git_mailmap_resolve( + &name, &email, mailmap, "Other Name", "yetanotheremail@foo.com")); + + cl_assert_equal_s(name, "Other Name"); + cl_assert_equal_s(email, "email@foo.com"); + + cl_git_pass(git_mailmap_resolve( + &name, &email, mailmap, + "Other Name That Doesn't Match", "yetanotheremail@foo.com")); + cl_assert_equal_s(name, "Other Name That Doesn't Match"); + cl_assert_equal_s(email, "yetanotheremail@foo.com"); +} diff --git a/tests/libgit2/mailmap/blame.c b/tests/libgit2/mailmap/blame.c new file mode 100644 index 000000000..e6bc1a41f --- /dev/null +++ b/tests/libgit2/mailmap/blame.c @@ -0,0 +1,64 @@ +#include "clar_libgit2.h" +#include "git2/repository.h" +#include "git2/blame.h" +#include "mailmap.h" +#include "mailmap_testdata.h" + +static git_repository *g_repo; +static git_blame *g_blame; + +void test_mailmap_blame__initialize(void) +{ + g_repo = NULL; + g_blame = NULL; +} + +void test_mailmap_blame__cleanup(void) +{ + git_blame_free(g_blame); + cl_git_sandbox_cleanup(); +} + +void test_mailmap_blame__hunks(void) +{ + size_t idx = 0; + const git_blame_hunk *hunk = NULL; + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init("mailmap"); + + opts.flags |= GIT_BLAME_USE_MAILMAP; + + cl_git_pass(git_blame_file(&g_blame, g_repo, "file.txt", &opts)); + cl_assert(g_blame); + + for (idx = 0; idx < ARRAY_SIZE(resolved); ++idx) { + hunk = git_blame_get_hunk_byline(g_blame, idx + 1); + + cl_assert(hunk->final_signature != NULL); + cl_assert(hunk->orig_signature != NULL); + cl_assert_equal_s(hunk->final_signature->name, resolved[idx].real_name); + cl_assert_equal_s(hunk->final_signature->email, resolved[idx].real_email); + } +} + +void test_mailmap_blame__hunks_no_mailmap(void) +{ + size_t idx = 0; + const git_blame_hunk *hunk = NULL; + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + g_repo = cl_git_sandbox_init("mailmap"); + + cl_git_pass(git_blame_file(&g_blame, g_repo, "file.txt", &opts)); + cl_assert(g_blame); + + for (idx = 0; idx < ARRAY_SIZE(resolved); ++idx) { + hunk = git_blame_get_hunk_byline(g_blame, idx + 1); + + cl_assert(hunk->final_signature != NULL); + cl_assert(hunk->orig_signature != NULL); + cl_assert_equal_s(hunk->final_signature->name, resolved[idx].replace_name); + cl_assert_equal_s(hunk->final_signature->email, resolved[idx].replace_email); + } +} diff --git a/tests/libgit2/mailmap/mailmap_testdata.h b/tests/libgit2/mailmap/mailmap_testdata.h new file mode 100644 index 000000000..a06606b4b --- /dev/null +++ b/tests/libgit2/mailmap/mailmap_testdata.h @@ -0,0 +1,21 @@ +#include "mailmap.h" + +typedef struct mailmap_entry { + const char *real_name; + const char *real_email; + const char *replace_name; + const char *replace_email; +} mailmap_entry; + +static const mailmap_entry resolved[] = { + { "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" }, + { "Phil Hill", "phil@company.xx", "unknown", "phil@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" } +}; diff --git a/tests/libgit2/mailmap/parsing.c b/tests/libgit2/mailmap/parsing.c new file mode 100644 index 000000000..5ea470f34 --- /dev/null +++ b/tests/libgit2/mailmap/parsing.c @@ -0,0 +1,269 @@ +#include "clar_libgit2.h" +#include "repository.h" +#include "git2/sys/repository.h" +#include "mailmap_testdata.h" + +static git_repository *g_repo; +static git_mailmap *g_mailmap; +static git_config *g_config; + +static const char string_mailmap[] = + "# Simple Comment line\n" + "<cto@company.xx> <cto@coompany.xx>\n" + "Some Dude <some@dude.xx> nick1 <bugs@company.xx>\n" + "Other Author <other@author.xx> nick2 <bugs@company.xx>\n" + "Other Author <other@author.xx> <nick2@company.xx>\n" + "Phil Hill <phil@company.xx> # Comment at end of line\n" + "<joseph@company.xx> Joseph <bugs@company.xx>\n" + "Santa Claus <santa.claus@northpole.xx> <me@company.xx>\n" + "Untracked <untracked@company.xx>"; + +static const mailmap_entry entries[] = { + { NULL, "cto@company.xx", NULL, "cto@coompany.xx" }, + { "Some Dude", "some@dude.xx", "nick1", "bugs@company.xx" }, + { "Other Author", "other@author.xx", "nick2", "bugs@company.xx" }, + { "Other Author", "other@author.xx", NULL, "nick2@company.xx" }, + { "Phil Hill", NULL, NULL, "phil@company.xx" }, + { NULL, "joseph@company.xx", "Joseph", "bugs@company.xx" }, + { "Santa Claus", "santa.claus@northpole.xx", NULL, "me@company.xx" }, + /* This entry isn't in the bare repository */ + { "Untracked", NULL, NULL, "untracked@company.xx" } +}; + +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; + size_t idx; + + /* 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_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); + cl_assert_equal_s(parsed->replace_email, entries[idx].replace_email); + } +} + +static void check_mailmap_resolve( + const git_mailmap *mailmap, const mailmap_entry *resolved, size_t resolved_size) +{ + const char *resolved_name = NULL; + const char *resolved_email = NULL; + size_t idx; + + /* Check that the resolver behaves correctly */ + for (idx = 0; idx < resolved_size; ++idx) { + cl_git_pass(git_mailmap_resolve( + &resolved_name, &resolved_email, mailmap, + resolved[idx].replace_name, resolved[idx].replace_email)); + cl_assert_equal_s(resolved_name, resolved[idx].real_name); + cl_assert_equal_s(resolved_email, resolved[idx].real_email); + } +} + +static const mailmap_entry resolved_untracked[] = { + { "Untracked", "untracked@company.xx", "xx", "untracked@company.xx" } +}; + +void test_mailmap_parsing__string(void) +{ + cl_git_pass(git_mailmap_from_buffer( + &g_mailmap, string_mailmap, strlen(string_mailmap))); + + /* We should have parsed all of the entries */ + check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries)); + + /* Check that resolving the entries works */ + check_mailmap_resolve(g_mailmap, resolved, ARRAY_SIZE(resolved)); + check_mailmap_resolve( + g_mailmap, resolved_untracked, ARRAY_SIZE(resolved_untracked)); +} + +void test_mailmap_parsing__windows_string(void) +{ + git_str unixbuf = GIT_STR_INIT; + git_str winbuf = GIT_STR_INIT; + + /* Parse with windows-style line endings */ + git_str_attach_notowned(&unixbuf, string_mailmap, strlen(string_mailmap)); + cl_git_pass(git_str_lf_to_crlf(&winbuf, &unixbuf)); + + cl_git_pass(git_mailmap_from_buffer(&g_mailmap, winbuf.ptr, winbuf.size)); + git_str_dispose(&winbuf); + + /* We should have parsed all of the entries */ + check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries)); + + /* Check that resolving the entries works */ + check_mailmap_resolve(g_mailmap, resolved, ARRAY_SIZE(resolved)); + check_mailmap_resolve( + g_mailmap, resolved_untracked, ARRAY_SIZE(resolved_untracked)); +} + +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_repository(&g_mailmap, g_repo)); + + /* We should have parsed all of the entries */ + check_mailmap_entries(g_mailmap, entries, ARRAY_SIZE(entries)); + + /* Check that resolving the entries works */ + check_mailmap_resolve(g_mailmap, resolved, ARRAY_SIZE(resolved)); + check_mailmap_resolve( + 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_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); + + /* Check that resolving the entries works */ + check_mailmap_resolve(g_mailmap, resolved, ARRAY_SIZE(resolved)); + 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)); +} |
