summaryrefslogtreecommitdiff
path: root/mu/mu-cmd-add.cc
diff options
context:
space:
mode:
authorDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2023-07-09 12:52:31 +0300
committerDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2023-07-10 23:15:40 +0300
commita32b924692ccf6ec7792fc80a253f21e5afb6da6 (patch)
tree56dbbe16b59083398d7b0f2abe7dd6610aec37b2 /mu/mu-cmd-add.cc
parentcc65b8b401498807be12f430cc2b581adaaa57dc (diff)
mu: add "unit" tests for 'mu add'
Diffstat (limited to 'mu/mu-cmd-add.cc')
-rw-r--r--mu/mu-cmd-add.cc105
1 files changed, 105 insertions, 0 deletions
diff --git a/mu/mu-cmd-add.cc b/mu/mu-cmd-add.cc
index af1f142..7e57fe0 100644
--- a/mu/mu-cmd-add.cc
+++ b/mu/mu-cmd-add.cc
@@ -35,3 +35,108 @@ Mu::mu_cmd_add(Mu::Store& store, const Options& opts)
return Ok();
}
+
+
+#ifdef BUILD_TESTS
+/*
+ * Tests.
+ *
+ */
+
+#include "utils/mu-test-utils.hh"
+
+static void
+test_add_ok()
+{
+ auto testhome{unwrap(make_temp_dir())};
+ auto dbpath{runtime_path(RuntimePath::XapianDb, testhome)};
+
+ {
+ unwrap(Store::make_new(dbpath, MU_TESTMAILDIR));
+ }
+
+ {
+ auto res = run_mu_command(
+ mu_format("add --muhome={} {}",
+ testhome,
+ MU_TESTMAILDIR "/cur/1220863042.12663_1.mindcrime!2,S"));
+ assert_valid_result(res);
+ g_assert_cmpuint(*res,==,0);
+ }
+
+ {
+ auto&& store = Store::make(dbpath);
+ assert_valid_result(store);
+ g_assert_cmpuint(store->size(),==,1);
+ }
+
+ { // re-add the same
+ auto res = run_mu_command(
+ mu_format("add --muhome={} {}",
+ testhome,
+ MU_TESTMAILDIR "/cur/1220863042.12663_1.mindcrime!2,S"));
+ assert_valid_result(res);
+ g_assert_cmpuint(*res,==,0);
+ }
+
+
+ {
+ auto&& store = Store::make(dbpath);
+ assert_valid_result(store);
+ g_assert_cmpuint(store->size(),==,1);
+ }
+
+
+ remove_directory(testhome);
+}
+
+static void
+test_add_fail()
+{
+ auto testhome{unwrap(make_temp_dir())};
+ auto dbpath{runtime_path(RuntimePath::XapianDb, testhome)};
+
+ {
+ unwrap(Store::make_new(dbpath, MU_TESTMAILDIR2));
+ }
+
+ { // wrong maildir
+ auto res = run_mu_command(
+ mu_format("add --muhome={} {}",
+ testhome,
+ MU_TESTMAILDIR "/cur/1220863042.12663_1.mindcrime!2,S"));
+ assert_valid_result(res);
+ g_assert_cmpuint(*res,!=,0);
+ }
+
+
+ { // non-existent
+ auto res = run_mu_command(
+ mu_format("add --muhome={} {}",
+ testhome, "/foo/bar/non-existent"));
+ assert_valid_result(res);
+ g_assert_cmpuint(*res,!=,0);
+ }
+
+ remove_directory(testhome);
+}
+
+
+int
+main(int argc, char* argv[]) try {
+
+ 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*/