From abb0fb4fd5c14723f860d2fa6b5f9d9b6d30427f Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 24 Sep 2023 15:49:58 +0300 Subject: [PATCH] utils/add: improve unit test coverage --- lib/utils/mu-test-utils.cc | 15 ++++++++------- lib/utils/mu-utils-file.cc | 23 ++++++++++++++++++++++- lib/utils/mu-utils.hh | 6 ++++-- mu/mu-cmd-add.cc | 11 ++--------- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/lib/utils/mu-test-utils.cc b/lib/utils/mu-test-utils.cc index db4d4010..8df13237 100644 --- a/lib/utils/mu-test-utils.cc +++ b/lib/utils/mu-test-utils.cc @@ -36,11 +36,13 @@ using namespace Mu; +/* LCOV_EXCL_START*/ bool Mu::mu_test_mu_hacker() { return !!g_getenv("MU_HACKER"); } +/* LCOV_EXCL_STOP*/ const char* @@ -65,8 +67,10 @@ Mu::set_en_us_utf8_locale() setlocale(LC_ALL, "en_US.UTF-8"); if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) { + /* LCOV_EXCL_START*/ mu_println("Note: Unit tests require the en_US.utf8 locale. " "Ignoring test cases."); + /* LCOV_EXCL_STOP*/ return false; } @@ -130,13 +134,10 @@ Mu::TempDir::~TempDir() return; } - /* ugly */ - GError *err{}; - const auto cmd{mu_format("/bin/rm -rf '{}'", path_)}; - if (!g_spawn_command_line_sync(cmd.c_str(), NULL, - NULL, NULL, &err)) { - mu_warning("error: {}", err ? err->message : "?"); - g_clear_error(&err); + if (auto&& res{run_command0({RM_PROGRAM, "-fr", path_})}; !res) { + /* LCOV_EXCL_START*/ + mu_warning("error removing {}: {}", path_, format_as(res.error())); + /* LCOV_EXCL_STOP*/ } else mu_debug("removed '{}'", path_); } diff --git a/lib/utils/mu-utils-file.cc b/lib/utils/mu-utils-file.cc index e121594a..5813bcf1 100644 --- a/lib/utils/mu-utils-file.cc +++ b/lib/utils/mu-utils-file.cc @@ -158,10 +158,13 @@ Mu::runtime_path(Mu::RuntimePath path, const std::string& muhome) return mu_config; case Mu::RuntimePath::Scripts: return join_paths(mu_config, "scripts"); + /*LCOV_EXCL_START*/ default: throw std::logic_error("unknown path"); + /*LCOV_EXCL_STOP*/ } } + /* LCOV_EXCL_START*/ static gpointer cancel_wait(gpointer data) @@ -212,6 +215,7 @@ Mu::g_cancellable_new_with_timeout(guint timeout) } /* LCOV_EXCL_STOP*/ +/* LCOV_EXCL_START*/ Result Mu::read_from_stdin() { @@ -234,13 +238,14 @@ Mu::read_from_stdin() G_MEMORY_OUTPUT_STREAM(outmem))), g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(outmem))}); } - +/* LCOV_EXCL_STOP*/ /* * Set the child to a group leader to avoid being killed when the * parent group is killed. */ +/*LCOV_EXCL_START*/ static void maybe_setsid (G_GNUC_UNUSED gpointer user_data) { @@ -248,6 +253,7 @@ maybe_setsid (G_GNUC_UNUSED gpointer user_data) setsid(); #endif /*HAVE_SETSID*/ } +/*LCOV_EXCL_STOP*/ Result Mu::run_command(std::initializer_list args, bool try_setsid) @@ -451,7 +457,20 @@ test_join_paths() assert_equal(join_paths("/a/b///c/d//", "e"), "/a/b/c/d/e"); } +static void +test_runtime_paths() +{ + TempDir tdir; + assert_equal(runtime_path(RuntimePath::Cache, tdir.path()), tdir.path()); + assert_equal(runtime_path(RuntimePath::XapianDb, tdir.path()), + join_paths(tdir.path(), "xapian")); + assert_equal(runtime_path(RuntimePath::Bookmarks, tdir.path()), + join_paths(tdir.path(), "bookmarks")); + assert_equal(runtime_path(RuntimePath::Config, tdir.path()), tdir.path()); + assert_equal(runtime_path(RuntimePath::Scripts, tdir.path()), + join_paths(tdir.path(), "scripts")); +} int main(int argc, char* argv[]) @@ -473,6 +492,8 @@ main(int argc, char* argv[]) test_program_in_path); g_test_add_func("/utils/join-paths", test_join_paths); + g_test_add_func("/utils/runtime-paths", + test_runtime_paths); return g_test_run(); } diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index fcc450d0..9937bee7 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -47,7 +47,6 @@ namespace Mu { - /* * Separator characters used in various places; importantly, * they are not used in UTF-8 @@ -55,7 +54,6 @@ namespace Mu { constexpr const auto SepaChar1 = '\xfe'; constexpr const auto SepaChar2 = '\xff'; - /* * Logging/printing/formatting functions connect libfmt with the Glib logging * system. We wrap so perhaps at some point (C++23?) we can use std:: instead. @@ -88,6 +86,7 @@ void mu_warning(fmt::format_string frm, T&&... args) noexcept { g_log("mu", G_LOG_LEVEL_WARNING, "%s", fmt::format(frm, std::forward(args)...).c_str()); } +/* LCOV_EXCL_START*/ template void mu_critical(fmt::format_string frm, T&&... args) noexcept { g_log("mu", G_LOG_LEVEL_CRITICAL, "%s", @@ -98,6 +97,7 @@ void mu_error(fmt::format_string frm, T&&... args) noexcept { g_log("mu", G_LOG_LEVEL_ERROR, "%s", fmt::format(frm, std::forward(args)...).c_str()); } +/* LCOV_EXCL_STOP*/ /* * Printing; add our wrapper functions, one day we might be able to use std:: @@ -355,8 +355,10 @@ struct StopWatch { StopWatch(const std::string name) : start_{Clock::now()}, name_{name} {} ~StopWatch() { const auto us{static_cast(to_us(Clock::now() - start_))}; + /* LCOV_EXCL_START*/ if (us > 2000000) mu_debug("sw: {}: finished after {:.1f} s", name_, us / 1000000); + /* LCOV_EXCL_STOP*/ else if (us > 2000) mu_debug("sw: {}: finished after {:.1f} ms", name_, us / 1000); else diff --git a/mu/mu-cmd-add.cc b/mu/mu-cmd-add.cc index 5ad79baa..46dcf9ad 100644 --- a/mu/mu-cmd-add.cc +++ b/mu/mu-cmd-add.cc @@ -113,20 +113,13 @@ test_add_fail() int -main(int argc, char* argv[]) try { - +main(int argc, char* argv[]) +{ mu_test_init(&argc, &argv); g_test_add_func("/cmd/add/ok", test_add_ok); g_test_add_func("/cmd/add/fail", test_add_fail); return g_test_run(); - -} catch (const Error& e) { - mu_printerrln("{}", e.what()); - return 1; -} catch (...) { - mu_printerrln("caught exception"); - return 1; } #endif /*BUILD_TESTS*/