mu-init: guess maildir when --maildir is missing

Re-instate the guessing that's in the manpage. Add unit tests.
Update documentation.

Fixes #2616.
This commit is contained in:
Dirk-Jan C. Binnema
2024-01-01 10:02:56 +02:00
parent 8366e009cb
commit b4c768e6d0
6 changed files with 107 additions and 32 deletions

View File

@ -77,6 +77,13 @@ test('test-cmd-index',
cpp_args: ['-DBUILD_TESTS'],
dependencies: [glib_dep, lib_mu_dep]))
test('test-cmd-init',
executable('test-cmd-init',
'mu-cmd-init.cc',
install: false,
cpp_args: ['-DBUILD_TESTS'],
dependencies: [glib_dep, lib_mu_dep]))
test('test-cmd-mkdir',
executable('test-cmd-mkdir',
'mu-cmd-mkdir.cc',
@ -105,7 +112,6 @@ test('test-cmd-verify',
cpp_args: ['-DBUILD_TESTS'],
dependencies: [glib_dep, lib_mu_dep]))
test('test-cmd-view',
executable('test-cmd-view',
'mu-cmd-view.cc',

View File

@ -152,11 +152,11 @@ test_mu_index(size_t batch_size=0)
auto res1 = run_command({MU_PROGRAM, "--quiet", "init", "--batch-size",
mu_format("{}", batch_size == 0 ? 10000 : batch_size),
"--muhome", mu_home, "--maildir" , MU_TESTMAILDIR2});
assert_valid_result(res1);
assert_valid_command(res1);
auto res2 = run_command({MU_PROGRAM, "--quiet", "index",
"--muhome", mu_home});
assert_valid_result(res2);
assert_valid_command(res2);
auto&& store = unwrap(Store::make(join_paths(temp_dir.path(), "xapian")));
g_assert_cmpuint(store.size(),==,14);

View File

@ -23,6 +23,8 @@
using namespace Mu;
#ifndef BUILD_TESTS
Result<void>
Mu::mu_cmd_init(const Options& opts)
{
@ -79,3 +81,61 @@ Mu::mu_cmd_init(const Options& opts)
return Ok();
}
#else /* BUILD_TESTS */
/*
* Tests.
*
*/
#include <config.h>
#include <mu-store.hh>
#include "utils/mu-test-utils.hh"
static void
test_mu_init_basic()
{
TempDir temp_dir{};
const auto mu_home{temp_dir.path()};
auto res1 = run_command({MU_PROGRAM, "--quiet", "init",
"--muhome", mu_home, "--maildir" , MU_TESTMAILDIR2});
assert_valid_command(res1);
auto&& store = unwrap(Store::make(join_paths(temp_dir.path(), "xapian")));
g_assert_true(store.empty());
}
static void
test_mu_init_maildir()
{
TempDir temp_dir{};
const auto mu_home{temp_dir.path()};
g_setenv("MAILDIR", MU_TESTMAILDIR2, 1);
auto res1 = run_command({MU_PROGRAM, "--quiet", "init",
"--muhome", mu_home});
assert_valid_command(res1);
auto&& store = unwrap(Store::make(join_paths(temp_dir.path(), "xapian")));
g_assert_true(store.empty());
assert_equal(store.root_maildir(), MU_TESTMAILDIR2);
}
int
main(int argc, char* argv[])
{
mu_test_init(&argc, &argv);
g_test_add_func("/cmd/init/basic", test_mu_init_basic);
g_test_add_func("/cmd/init/maildir", test_mu_init_maildir);
return g_test_run();
}
#endif /*BUILD_TESTS*/

View File

@ -184,20 +184,16 @@ options_map(const IE& ie)
return map;
}
// transformers
// Expand the path using wordexp
static const std::function ExpandPath = [](std::string filepath)->std::string {
if (auto&& res{expand_path(filepath)}; !res)
throw CLI::ValidationError{res.error().what()};
else
return filepath = std::move(res.value());
return res.value();
};
// Canonicalize path
static const std::function CanonicalizePath = [](std::string filepath)->std::string {
return filepath = canonicalize_filename(filepath);
@ -289,7 +285,7 @@ sub_extract(CLI::App& sub, Options& opts)
sub.add_option("--target-dir", opts.extract.targetdir,
"Target directory for saving")
->type_name("<dir>")
->transform(ExpandPath, "expand path")
->transform(ExpandPath, "expand target path")
->default_str("<current>")
->default_val(".");
sub.add_flag("--uncooked,-u", opts.extract.uncooked,
@ -403,7 +399,7 @@ sub_find(CLI::App& sub, Options& opts)
sub.add_option("--linksdir", opts.find.linksdir,
"Use bookmarked query")
->type_name("<dir>")
->transform(ExpandPath, "expand path");
->transform(ExpandPath, "expand linksdir path");
sub.add_option("--summary-len", opts.find.summary_len,
"Use up to so many lines for the summary")
@ -448,10 +444,20 @@ sub_info(CLI::App& sub, Options& opts)
static void
sub_init(CLI::App& sub, Options& opts)
{
sub.add_option("--maildir,-m", opts.init.maildir,
"Top of the maildir")
const auto default_mdir = std::invoke([]()->std::string {
if (const auto mdir_env{::getenv("MAILDIR")}; mdir_env)
return mdir_env;
else if (const auto mdir_home = ::join_paths(g_get_home_dir(), "Maildir");
check_dir(mdir_home))
return mdir_home;
else
return {};
});
sub.add_option("--maildir,-m", opts.init.maildir, "Top of the maildir")
->type_name("<maildir>")
->transform(ExpandPath, "expand path");
->default_val(default_mdir)
->transform(ExpandPath, "expand maildir path");
sub.add_option("--my-address", opts.init.my_addresses,
"Personal e-mail address or regexp")
->type_name("<address>");
@ -503,8 +509,8 @@ sub_move(CLI::App& sub, Options& opts)
sub.add_option("source", opts.move.src, "Message file to move")
->type_name("<message-path>")
->transform(ExpandPath, "expand path")
->transform(CanonicalizePath, "canonicalize path")
->transform(ExpandPath, "expand source path")
->transform(CanonicalizePath, "canonicalize source path")
->required();
sub.add_option("destination", opts.move.dest,
"Destination maildir")
@ -813,7 +819,7 @@ There is NO WARRANTY, to the extent permitted by law.
opts.muhome, "Specify alternative mu directory")
->envname("MUHOME")
->type_name("<dir>")
->transform(ExpandPath, "expand path");
->transform(ExpandPath, "expand muhome path");
}
/* add scripts (if supported) as semi-subcommands as well */