summaryrefslogtreecommitdiff
path: root/mu/mu-cmd-add.cc
blob: 46dcf9add969546dc1569435035a2a380b7e84d9 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
** 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_add(Mu::Store& store, const Options& opts)
{
	for (auto&& file: opts.add.files) {
		const auto docid{store.add_message(file)};
		if (!docid)
			return Err(docid.error());
		else
			mu_debug("added message @ {}, docid={}", file, *docid);
	}

	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_command({MU_PROGRAM, "add", mu_format("--muhome={}", testhome),
				MU_TESTMAILDIR "/cur/1220863042.12663_1.mindcrime!2,S"});
		assert_valid_command(res);
	}

	{
		auto&& store = Store::make(dbpath);
		assert_valid_result(store);
		g_assert_cmpuint(store->size(),==,1);
	}

	{ // re-add the same
		auto res = run_command({MU_PROGRAM, "add", mu_format("--muhome={}",testhome),
				MU_TESTMAILDIR "/cur/1220863042.12663_1.mindcrime!2,S"});
		assert_valid_command(res);
	}

	{
		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_command({MU_PROGRAM, "add", mu_format("--muhome={}", testhome),
				       MU_TESTMAILDIR "/cur/1220863042.12663_1.mindcrime!2,S"});
		assert_valid_result(res);
		g_assert_cmpuint(res->exit_code,!=,0);
	}


	{ // non-existent
		auto res = run_command({MU_PROGRAM, "add", mu_format("--muhome={}", testhome),
				"/foo/bar/non-existent"});
		assert_valid_result(res);
		g_assert_cmpuint(res->exit_code,!=,0);
	}

	remove_directory(testhome);
}


int
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();
}
#endif /*BUILD_TESTS*/