utils/add: improve unit test coverage

This commit is contained in:
Dirk-Jan C. Binnema 2023-09-24 15:49:58 +03:00
parent b0dca49dc0
commit abb0fb4fd5
4 changed files with 36 additions and 19 deletions

View File

@ -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_);
}

View File

@ -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<std::string>
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::CommandOutput>
Mu::run_command(std::initializer_list<std::string> 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();
}

View File

@ -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<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_WARNING, "%s",
fmt::format(frm, std::forward<T>(args)...).c_str());
}
/* LCOV_EXCL_START*/
template<typename...T>
void mu_critical(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_CRITICAL, "%s",
@ -98,6 +97,7 @@ void mu_error(fmt::format_string<T...> frm, T&&... args) noexcept {
g_log("mu", G_LOG_LEVEL_ERROR, "%s",
fmt::format(frm, std::forward<T>(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<double>(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

View File

@ -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*/