From af9eb36ca01d45ad6ce953fad12900fd7acf0cce Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Mon, 11 Sep 2023 23:48:31 +0300 Subject: [PATCH] unit-tests: modernize Use TempDir, join_paths etc. --- lib/tests/test-mu-maildir.cc | 147 +++++++++++++---------------------- lib/tests/test-query.cc | 26 +++---- lib/utils/mu-logger.cc | 6 +- lib/utils/mu-test-utils.cc | 22 ++---- lib/utils/mu-test-utils.hh | 20 +++-- mu/tests/test-mu-query.cc | 91 +++++++++------------- 6 files changed, 120 insertions(+), 192 deletions(-) diff --git a/lib/tests/test-mu-maildir.cc b/lib/tests/test-mu-maildir.cc index 5b9d545d..c14b753f 100644 --- a/lib/tests/test-mu-maildir.cc +++ b/lib/tests/test-mu-maildir.cc @@ -28,130 +28,91 @@ #include "utils/mu-test-utils.hh" #include "mu-maildir.hh" +#include "utils/mu-utils.hh" +#include "utils/mu-utils-file.hh" #include "utils/mu-result.hh" using namespace Mu; static void -test_maildir_mkdir_01(void) +test_maildir_mkdir_01() { - int i; - gchar * tmpdir, *mdir, *tmp; - const gchar* subs[] = {"tmp", "cur", "new"}; + TempDir temp_dir; + auto mdir = join_paths(temp_dir.path(), "cuux"); + auto res{maildir_mkdir(mdir, 0755, false/*!noindex*/)}; + assert_valid_result(res); - tmpdir = test_mu_common_get_random_tmpdir(); - mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux"); - - g_assert_true(!!maildir_mkdir(mdir, 0755, FALSE)); - - for (i = 0; i != G_N_ELEMENTS(subs); ++i) { - gchar* dir; - - dir = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, subs[i]); - g_assert_cmpuint(g_access(dir, R_OK), ==, 0); - g_assert_cmpuint(g_access(dir, W_OK), ==, 0); - g_free(dir); + for (auto sub : {"tmp", "cur", "new"}) { + auto subpath = join_paths(mdir, sub); + g_assert_cmpuint(g_access(subpath.c_str(), R_OK), ==, 0); + g_assert_cmpuint(g_access(subpath.c_str(), W_OK), ==, 0); } - tmp = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex"); - g_assert_cmpuint(g_access(tmp, F_OK), !=, 0); - - g_free(tmp); - g_free(tmpdir); - g_free(mdir); + auto noindex = join_paths(mdir, ".noindex"); + g_assert_cmpuint(g_access(noindex.c_str(), F_OK), !=, 0); } static void -test_maildir_mkdir_02(void) +test_maildir_mkdir_02() { - int i; - gchar * tmpdir, *mdir, *tmp; - const gchar* subs[] = {"tmp", "cur", "new"}; + TempDir temp_dir; + auto mdir = join_paths(temp_dir.path(), "cuux"); + auto res{maildir_mkdir(mdir, 0755, true/*noindex*/)}; + assert_valid_result(res); - tmpdir = test_mu_common_get_random_tmpdir(); - mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux"); - - g_assert_true(!!maildir_mkdir(mdir, 0755, TRUE)); - - for (i = 0; i != G_N_ELEMENTS(subs); ++i) { - gchar* dir; - - dir = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, subs[i]); - g_assert_cmpuint(g_access(dir, R_OK), ==, 0); - - g_assert_cmpuint(g_access(dir, W_OK), ==, 0); - g_free(dir); + for (auto sub : {"tmp", "cur", "new"}) { + auto subpath = join_paths(mdir, sub); + g_assert_cmpuint(g_access(subpath.c_str(), R_OK), ==, 0); + g_assert_cmpuint(g_access(subpath.c_str(), W_OK), ==, 0); } - tmp = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex"); - g_assert_cmpuint(g_access(tmp, F_OK), ==, 0); - - g_free(tmp); - g_free(tmpdir); - g_free(mdir); + auto noindex = join_paths(mdir, ".noindex"); + g_assert_cmpuint(g_access(noindex.c_str(), F_OK), ==, 0); } static void -test_maildir_mkdir_03(void) +test_maildir_mkdir_03() { - int i; - gchar * tmpdir, *mdir, *tmp; - const gchar* subs[] = {"tmp", "cur", "new"}; + TempDir temp_dir; + auto mdir = join_paths(temp_dir.path(), "cuux"); - tmpdir = test_mu_common_get_random_tmpdir(); - mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux"); + // create part already + auto curdir = join_paths(mdir, "cur"); + g_assert_cmpuint(g_mkdir_with_parents(curdir.c_str(), 0755), ==, 0); - /* create part of the structure already... */ - { - gchar* dir; - dir = g_strdup_printf("%s%ccur", mdir, G_DIR_SEPARATOR); - g_assert_cmpuint(g_mkdir_with_parents(dir, 0755), ==, 0); - g_free(dir); + auto res{maildir_mkdir(mdir, 0755, false/*!noindex*/)}; + assert_valid_result(res); + + // should still work. + for (auto sub : {"tmp", "cur", "new"}) { + auto subpath = join_paths(mdir, sub); + g_assert_cmpuint(g_access(subpath.c_str(), R_OK), ==, 0); + g_assert_cmpuint(g_access(subpath.c_str(), W_OK), ==, 0); } - /* this should still work */ - g_assert_true(!!maildir_mkdir(mdir, 0755, FALSE)); - - for (i = 0; i != G_N_ELEMENTS(subs); ++i) { - gchar* dir; - - dir = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, subs[i]); - g_assert_cmpuint(g_access(dir, R_OK), ==, 0); - g_assert_cmpuint(g_access(dir, W_OK), ==, 0); - g_free(dir); - } - - tmp = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex"); - g_assert_cmpuint(g_access(tmp, F_OK), !=, 0); - - g_free(tmp); - g_free(tmpdir); - g_free(mdir); + auto noindex = join_paths(mdir, ".noindex"); + g_assert_cmpuint(g_access(noindex.c_str(), F_OK), !=, 0); } + static void -test_maildir_mkdir_04(void) +test_maildir_mkdir_04() { - gchar *tmpdir, *mdir; - - tmpdir = test_mu_common_get_random_tmpdir(); - mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux"); - - /* create part of the structure already... */ - { - gchar* dir; - g_assert_cmpuint(g_mkdir_with_parents(mdir, 0755), ==, 0); - dir = g_strdup_printf("%s%ccur", mdir, G_DIR_SEPARATOR); - g_assert_cmpuint(g_mkdir_with_parents(dir, 0000), ==, 0); - g_free(dir); + if (geteuid() == 0) { + g_test_skip("not useful when run as root"); + return; } + TempDir temp_dir; + auto mdir = join_paths(temp_dir.path(), "cuux"); + g_assert_cmpuint(g_mkdir_with_parents(mdir.c_str(), 0755), ==, 0); + + auto curdir = join_paths(mdir, "cur"); + g_assert_cmpuint(g_mkdir_with_parents(curdir.c_str(), 0000), ==, 0); + /* this should fail now, because cur is not read/writable */ - if (geteuid() != 0) - g_assert_false(!!maildir_mkdir(mdir, 0755, false)); - - g_free(tmpdir); - g_free(mdir); + auto res = maildir_mkdir(mdir, 0755, false); + g_assert_false(!!res); } static gboolean diff --git a/lib/tests/test-query.cc b/lib/tests/test-query.cc index 1fd57481..fd9ff1d4 100644 --- a/lib/tests/test-query.cc +++ b/lib/tests/test-query.cc @@ -37,29 +37,27 @@ static void test_query() { allow_warnings(); - char* tdir; + TempDir temp_dir; - tdir = test_mu_common_get_random_tmpdir(); - auto store = Store::make_new(tdir, std::string{MU_TESTMAILDIR}); + auto store = Store::make_new(temp_dir.path(), std::string{MU_TESTMAILDIR}); assert_valid_result(store); - g_free(tdir); auto&& idx{store->indexer()}; - g_assert_true(idx.start(Indexer::Config{})); while (idx.is_running()) { - sleep(1); + g_usleep(1000); } auto dump_matches = [](const QueryResults& res) { size_t n{}; for (auto&& item : res) { - std::cout << item.query_match() << '\n'; - if (g_test_verbose()) - g_debug("%02zu %s %s", - ++n, - item.path().value_or("").c_str(), - item.message_id().value_or("").c_str()); + if (g_test_verbose()) { + std::cout << item.query_match() << '\n'; + mu_debug("{:02d} {} {}", + ++n, + item.path().value_or(""), + item.message_id().value_or("")); + } } }; @@ -84,8 +82,8 @@ test_query() } int -main(int argc, char* argv[]) -try { +main(int argc, char* argv[]) try { + mu_test_init(&argc, &argv); g_test_add_func("/query", test_query); diff --git a/lib/utils/mu-logger.cc b/lib/utils/mu-logger.cc index 49c2fbf3..258643c9 100644 --- a/lib/utils/mu-logger.cc +++ b/lib/utils/mu-logger.cc @@ -189,14 +189,16 @@ Logger::~Logger() #include #include "mu-test-utils.hh" +#include "mu-utils-file.hh" static void test_logger_threads(void) { - const auto testpath{test_random_tmpdir() + "/test.log"}; + TempDir temp_dir; + const auto testpath{join_paths(temp_dir.path(), "test.log")}; mu_message("log-file: {}", testpath); - auto logger = Logger::make(testpath.c_str(), Logger::Options::File | Logger::Options::Debug); + auto logger = Logger::make(testpath, Logger::Options::File | Logger::Options::Debug); assert_valid_result(logger); const auto thread_num = 16; diff --git a/lib/utils/mu-test-utils.cc b/lib/utils/mu-test-utils.cc index 164e22e0..db4d4010 100644 --- a/lib/utils/mu-test-utils.cc +++ b/lib/utils/mu-test-utils.cc @@ -34,25 +34,15 @@ #include "utils/mu-utils-file.hh" #include "utils/mu-error.hh" - using namespace Mu; -std::string -Mu::test_random_tmpdir() +bool +Mu::mu_test_mu_hacker() { - auto&& dir = mu_format("{}{}mu-test-{}{}test-{:x}", - g_get_tmp_dir(), - G_DIR_SEPARATOR, - getuid(), - G_DIR_SEPARATOR, - ::random() * getpid() * ::time({})); - - auto res = g_mkdir_with_parents(dir.c_str(), 0700); - g_assert(res != -1); - - return dir; + return !!g_getenv("MU_HACKER"); } + const char* Mu::set_tz(const char* tz) { @@ -92,11 +82,11 @@ black_hole(void) void Mu::mu_test_init(int *argc, char ***argv) { - const auto tmpdir{test_random_tmpdir()}; + TempDir temp_dir; g_unsetenv("XAPIAN_CJK_NGRAM"); g_setenv("MU_TEST", "yes", TRUE); - g_setenv("XDG_CACHE_HOME", tmpdir.c_str(), TRUE); + g_setenv("XDG_CACHE_HOME", temp_dir.path().c_str(), TRUE); setlocale(LC_ALL, ""); diff --git a/lib/utils/mu-test-utils.hh b/lib/utils/mu-test-utils.hh index e1f3d0c7..051230ad 100644 --- a/lib/utils/mu-test-utils.hh +++ b/lib/utils/mu-test-utils.hh @@ -27,17 +27,6 @@ namespace Mu { -/** - * get a dir name for a random temporary directory to do tests - * - * @return a random dir name - */ -std::string test_random_tmpdir(void); - -inline gchar* test_mu_common_get_random_tmpdir() { - return g_strdup(test_random_tmpdir().c_str()); -} - /** * mu wrapper for g_test_init. Sets environment variable MU_TEST to 1. * @@ -46,6 +35,14 @@ inline gchar* test_mu_common_get_random_tmpdir() { */ void mu_test_init(int *argc, char ***argv); + +/** + * Are we running in a MU_HACKER environment? + * + * @return true or false + */ +bool mu_test_mu_hacker(); + /** * set the timezone * @@ -62,6 +59,7 @@ const char* set_tz(const char* tz); */ bool set_en_us_utf8_locale(); + /** * For unit tests, assert two std::string's are equal. * diff --git a/mu/tests/test-mu-query.cc b/mu/tests/test-mu-query.cc index 2ecc28bd..23f13d41 100644 --- a/mu/tests/test-mu-query.cc +++ b/mu/tests/test-mu-query.cc @@ -34,6 +34,7 @@ #include "mu-query.hh" #include "utils/mu-result.hh" #include "utils/mu-utils.hh" +#include "utils/mu-utils-file.hh" #include "mu-store.hh" using namespace Mu; @@ -42,13 +43,10 @@ static std::string DB_PATH1; static std::string DB_PATH2; static std::string -make_database(const std::string& testdir) +make_database(const std::string& dbdir, const std::string& testdir) { - auto&& tmpdir{test_random_tmpdir()}; - /* use the env var rather than `--muhome` */ - - g_setenv("MUHOME", tmpdir.c_str(), 1); + g_setenv("MUHOME", dbdir.c_str(), 1); const auto cmdline{mu_format( "/bin/sh -c '" "{} --quiet init --maildir={} ; " @@ -59,8 +57,7 @@ make_database(const std::string& testdir) mu_printerrln("\n{}", cmdline); g_assert(g_spawn_command_line_sync(cmdline.c_str(), NULL, NULL, NULL, NULL)); - auto xpath = mu_format("{}{}{}", - tmpdir, G_DIR_SEPARATOR, "xapian"); + auto xpath = join_paths(dbdir, "xapian"); /* ensure MUHOME worked */ g_assert_cmpuint(::access(xpath.c_str(), F_OK), ==, 0); @@ -104,7 +101,6 @@ run_and_count_matches(const std::string& xpath, g_assert_true(!!qres); assert_no_dups(*qres); - if (g_test_verbose()) mu_println("'{}' => {}\n", expr, qres->size()); @@ -143,8 +139,7 @@ test_mu_query_01(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(DB_PATH1, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } static void @@ -186,8 +181,7 @@ test_mu_query_03(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(DB_PATH1, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } static void @@ -213,8 +207,7 @@ test_mu_query_04(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(DB_PATH1, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } static void @@ -231,8 +224,7 @@ test_mu_query_logic(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(DB_PATH1, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } static void @@ -272,8 +264,7 @@ test_mu_query_accented_chars_02(void) mu_warning("query '{}'; expected {} but got {}", queries[i].query, queries[i].count, count); g_assert_cmpuint(run_and_count_matches(DB_PATH1, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } } @@ -295,8 +286,7 @@ test_mu_query_accented_chars_fraiche(void) mu_println("{}", queries[i].query); g_assert_cmpuint(run_and_count_matches(DB_PATH2, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } } @@ -315,8 +305,7 @@ test_mu_query_wildcards(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(DB_PATH1, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } static void @@ -338,14 +327,13 @@ test_mu_query_dates_helsinki(void) {"date:200808110801..now", 7}}; old_tz = set_tz(hki); - - const auto xpath{make_database(MU_TESTMAILDIR)}; + TempDir tdir; + const auto xpath{make_database(tdir.path(), MU_TESTMAILDIR)}; g_assert_false(xpath.empty()); for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(xpath, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); set_tz(old_tz); } @@ -368,13 +356,13 @@ test_mu_query_dates_sydney(void) {"date:200808110801..now", 7}}; old_tz = set_tz(syd); - const auto xpath{make_database(MU_TESTMAILDIR)}; + TempDir tdir; + const auto xpath{make_database(tdir.path(), MU_TESTMAILDIR)}; g_assert_false(xpath.empty()); for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(xpath, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); set_tz(old_tz); } @@ -399,14 +387,14 @@ test_mu_query_dates_la(void) {"date:200808110801..now", 6}}; old_tz = set_tz(la); - const auto xpath = make_database(MU_TESTMAILDIR); + TempDir tdir; + const auto xpath{make_database(tdir.path(), MU_TESTMAILDIR)}; g_assert_false(xpath.empty()); for (i = 0; i != G_N_ELEMENTS(queries); ++i) { /* g_print ("%s\n", queries[i].query); */ g_assert_cmpuint(run_and_count_matches(xpath, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } set_tz(old_tz); @@ -427,8 +415,7 @@ test_mu_query_sizes(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(DB_PATH1, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } static void @@ -441,8 +428,7 @@ test_mu_query_attach(void) if (g_test_verbose()) mu_println("query: {}", queries[i].query); g_assert_cmpuint(run_and_count_matches(DB_PATH2, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } } @@ -464,8 +450,7 @@ test_mu_query_msgid(void) if (g_test_verbose()) mu_println("query: {}", queries[i].query); g_assert_cmpuint(run_and_count_matches(DB_PATH2, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } } @@ -488,8 +473,7 @@ test_mu_query_tags(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(DB_PATH2, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } static void @@ -504,8 +488,7 @@ test_mu_query_wom_bat(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(DB_PATH2, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } static void @@ -537,8 +520,7 @@ test_mu_query_multi_to_cc(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) g_assert_cmpuint(run_and_count_matches(DB_PATH1, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } static void @@ -554,8 +536,7 @@ test_mu_query_tags_02(void) for (i = 0; i != G_N_ELEMENTS(queries); ++i) { g_assert_cmpuint(run_and_count_matches(DB_PATH2, queries[i].query), - ==, - queries[i].count); + ==, queries[i].count); } } @@ -567,27 +548,27 @@ test_mu_query_tags_02(void) static void test_mu_query_threads_compilation_error(void) { - const auto xpath = make_database(MU_TESTMAILDIR); + TempDir tdir; + const auto xpath = make_database(tdir.path(), MU_TESTMAILDIR); g_assert_cmpuint(run_and_count_matches(xpath, "msgid:uwsireh25.fsf@one.dot.net"), ==, 1); g_assert_cmpuint(run_and_count_matches(xpath, "msgid:uwsireh25.fsf@one.dot.net", - QueryFlags::IncludeRelated), - ==, - 3); + QueryFlags::IncludeRelated), ==, 3); } int main(int argc, char* argv[]) { - int rv; + TempDir td1; + TempDir td2; mu_test_init(&argc, &argv); - DB_PATH1 = make_database(MU_TESTMAILDIR); + DB_PATH1 = make_database(td1.path(), MU_TESTMAILDIR); g_assert_false(DB_PATH1.empty()); - DB_PATH2 = make_database(MU_TESTMAILDIR2); + DB_PATH2 = make_database(td2.path(), MU_TESTMAILDIR2); g_assert_false(DB_PATH2.empty()); g_test_add_func("/mu-query/test-mu-query-01", test_mu_query_01); @@ -624,7 +605,5 @@ main(int argc, char* argv[]) g_test_add_func("/mu-query/test-mu-query-threads-compilation-error", test_mu_query_threads_compilation_error); - rv = g_test_run(); - - return rv; + return g_test_run(); }