summaryrefslogtreecommitdiff
path: root/mu/mu-cmd-remove.cc
blob: 5eb96b83ad949d489d737852463017cd6779f22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
** Copyright (C) 2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/

#include "config.h"
#include "mu-cmd.hh"

using namespace Mu;

Result<void>
Mu::mu_cmd_remove(Mu::Store& store, const Options& opts)
{
	for (auto&& file: opts.remove.files) {
		const auto res = store.remove_message(file);
		if (!res)
			return Err(Error::Code::File, "failed to remove {}", file.c_str());
		else
			mu_debug("removed message @ {}", file);
	}

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