From 0cb78fe4d124ad739eb129c34e8e9d1565c15ce8 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 18 Jul 2023 21:33:59 +0300 Subject: [PATCH] mu-remove: add unit test --- meson.build | 11 +++++++- mu/meson.build | 8 ++++++ mu/mu-cmd-remove.cc | 63 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 7c897deb..a8056cdf 100644 --- a/meson.build +++ b/meson.build @@ -142,11 +142,20 @@ else message('CLD2 not found; no support for language detection') endif - dependency('cld2', required : false) +dependency('cld2', required : false) +cp=find_program('cp') +mv=find_program('mv') +rm=find_program('rm') awk=find_program(['gawk', 'awk']) gzip=find_program('gzip') +config_h_data.set_quoted('CP_PROGRAM', cp.full_path()) +config_h_data.set_quoted('MV_PROGRAM', mv.full_path()) +config_h_data.set_quoted('RM_PROGRAM', rm.full_path()) +config_h_data.set_quoted('AWK_PROGRAM', awk.full_path()) +config_h_data.set_quoted('GZIP_PROGRAM', gzip.full_path()) + # soft dependencies guile_dep = dependency('guile-3.0', required: get_option('guile')) # soft dependencies diff --git a/mu/meson.build b/mu/meson.build index 299522bc..7337f3d2 100644 --- a/mu/meson.build +++ b/mu/meson.build @@ -48,5 +48,13 @@ test('test-cmd-add', cpp_args: ['-DBUILD_TESTS'], dependencies: [glib_dep, lib_mu_dep])) +test('test-cmd-remove', + executable('test-cmd-remove', + 'mu-cmd-remove.cc', + install: false, + cpp_args: ['-DBUILD_TESTS'], + dependencies: [glib_dep, lib_mu_dep])) + + subdir('tests') diff --git a/mu/mu-cmd-remove.cc b/mu/mu-cmd-remove.cc index 093813d4..5eb96b83 100644 --- a/mu/mu-cmd-remove.cc +++ b/mu/mu-cmd-remove.cc @@ -35,3 +35,66 @@ Mu::mu_cmd_remove(Mu::Store& store, const Options& opts) return Ok(); } + + +#ifdef BUILD_TESTS +/* + * Tests. + * + */ + +#include "utils/mu-test-utils.hh" + +static void +test_remove_ok() +{ + auto testhome{unwrap(make_temp_dir())}; + auto dbpath{runtime_path(RuntimePath::XapianDb, testhome)}; + + /* create a writable copy */ + const auto testmdir = join_paths(testhome, "test-maildir"); + const auto testmsg = join_paths(testmdir, "/cur/1220863042.12663_1.mindcrime!2,S"); + auto cres = run_command({CP_PROGRAM, "-r", MU_TESTMAILDIR, testmdir}); + assert_valid_command(cres); + + { + auto&& store = unwrap(Store::make_new(dbpath, testmdir)); + auto res = store.add_message(testmsg); + assert_valid_result(res); + g_assert_true(store.contains_message(testmsg)); + } + + { // remove the same + auto res = run_command({MU_PROGRAM, "remove", + mu_format("--muhome={}", testhome), + testmsg}); + assert_valid_command(res); + } + + { + auto&& store = unwrap(Store::make(dbpath)); + g_assert_false(!!store.contains_message(testmsg)); + g_assert_cmpuint(::access(testmsg.c_str(), F_OK), ==, 0); + } + + remove_directory(testhome); +} + + +int +main(int argc, char* argv[]) try { + + mu_test_init(&argc, &argv); + + g_test_add_func("/cmd/remove/ok", test_remove_ok); + + return g_test_run(); + +} catch (const Error& e) { + mu_printerrln("{}", e.what()); + return 1; +} catch (...) { + mu_printerrln("caught exception"); + return 1; +} +#endif /*BUILD_TESTS*/