unit-tests: modernize
Use TempDir, join_paths etc.
This commit is contained in:
@ -28,130 +28,91 @@
|
||||
|
||||
#include "utils/mu-test-utils.hh"
|
||||
#include "mu-maildir.hh"
|
||||
#include "utils/mu-utils.hh"
|
||||
#include "utils/mu-utils-file.hh"
|
||||
#include "utils/mu-result.hh"
|
||||
|
||||
using namespace Mu;
|
||||
|
||||
static void
|
||||
test_maildir_mkdir_01(void)
|
||||
test_maildir_mkdir_01()
|
||||
{
|
||||
int i;
|
||||
gchar * tmpdir, *mdir, *tmp;
|
||||
const gchar* subs[] = {"tmp", "cur", "new"};
|
||||
TempDir temp_dir;
|
||||
auto mdir = join_paths(temp_dir.path(), "cuux");
|
||||
auto res{maildir_mkdir(mdir, 0755, false/*!noindex*/)};
|
||||
assert_valid_result(res);
|
||||
|
||||
tmpdir = test_mu_common_get_random_tmpdir();
|
||||
mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux");
|
||||
|
||||
g_assert_true(!!maildir_mkdir(mdir, 0755, FALSE));
|
||||
|
||||
for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
|
||||
gchar* dir;
|
||||
|
||||
dir = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, subs[i]);
|
||||
g_assert_cmpuint(g_access(dir, R_OK), ==, 0);
|
||||
g_assert_cmpuint(g_access(dir, W_OK), ==, 0);
|
||||
g_free(dir);
|
||||
for (auto sub : {"tmp", "cur", "new"}) {
|
||||
auto subpath = join_paths(mdir, sub);
|
||||
g_assert_cmpuint(g_access(subpath.c_str(), R_OK), ==, 0);
|
||||
g_assert_cmpuint(g_access(subpath.c_str(), W_OK), ==, 0);
|
||||
}
|
||||
|
||||
tmp = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex");
|
||||
g_assert_cmpuint(g_access(tmp, F_OK), !=, 0);
|
||||
|
||||
g_free(tmp);
|
||||
g_free(tmpdir);
|
||||
g_free(mdir);
|
||||
auto noindex = join_paths(mdir, ".noindex");
|
||||
g_assert_cmpuint(g_access(noindex.c_str(), F_OK), !=, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_maildir_mkdir_02(void)
|
||||
test_maildir_mkdir_02()
|
||||
{
|
||||
int i;
|
||||
gchar * tmpdir, *mdir, *tmp;
|
||||
const gchar* subs[] = {"tmp", "cur", "new"};
|
||||
TempDir temp_dir;
|
||||
auto mdir = join_paths(temp_dir.path(), "cuux");
|
||||
auto res{maildir_mkdir(mdir, 0755, true/*noindex*/)};
|
||||
assert_valid_result(res);
|
||||
|
||||
tmpdir = test_mu_common_get_random_tmpdir();
|
||||
mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux");
|
||||
|
||||
g_assert_true(!!maildir_mkdir(mdir, 0755, TRUE));
|
||||
|
||||
for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
|
||||
gchar* dir;
|
||||
|
||||
dir = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, subs[i]);
|
||||
g_assert_cmpuint(g_access(dir, R_OK), ==, 0);
|
||||
|
||||
g_assert_cmpuint(g_access(dir, W_OK), ==, 0);
|
||||
g_free(dir);
|
||||
for (auto sub : {"tmp", "cur", "new"}) {
|
||||
auto subpath = join_paths(mdir, sub);
|
||||
g_assert_cmpuint(g_access(subpath.c_str(), R_OK), ==, 0);
|
||||
g_assert_cmpuint(g_access(subpath.c_str(), W_OK), ==, 0);
|
||||
}
|
||||
|
||||
tmp = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex");
|
||||
g_assert_cmpuint(g_access(tmp, F_OK), ==, 0);
|
||||
|
||||
g_free(tmp);
|
||||
g_free(tmpdir);
|
||||
g_free(mdir);
|
||||
auto noindex = join_paths(mdir, ".noindex");
|
||||
g_assert_cmpuint(g_access(noindex.c_str(), F_OK), ==, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_maildir_mkdir_03(void)
|
||||
test_maildir_mkdir_03()
|
||||
{
|
||||
int i;
|
||||
gchar * tmpdir, *mdir, *tmp;
|
||||
const gchar* subs[] = {"tmp", "cur", "new"};
|
||||
TempDir temp_dir;
|
||||
auto mdir = join_paths(temp_dir.path(), "cuux");
|
||||
|
||||
tmpdir = test_mu_common_get_random_tmpdir();
|
||||
mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux");
|
||||
// create part already
|
||||
auto curdir = join_paths(mdir, "cur");
|
||||
g_assert_cmpuint(g_mkdir_with_parents(curdir.c_str(), 0755), ==, 0);
|
||||
|
||||
/* create part of the structure already... */
|
||||
{
|
||||
gchar* dir;
|
||||
dir = g_strdup_printf("%s%ccur", mdir, G_DIR_SEPARATOR);
|
||||
g_assert_cmpuint(g_mkdir_with_parents(dir, 0755), ==, 0);
|
||||
g_free(dir);
|
||||
auto res{maildir_mkdir(mdir, 0755, false/*!noindex*/)};
|
||||
assert_valid_result(res);
|
||||
|
||||
// should still work.
|
||||
for (auto sub : {"tmp", "cur", "new"}) {
|
||||
auto subpath = join_paths(mdir, sub);
|
||||
g_assert_cmpuint(g_access(subpath.c_str(), R_OK), ==, 0);
|
||||
g_assert_cmpuint(g_access(subpath.c_str(), W_OK), ==, 0);
|
||||
}
|
||||
|
||||
/* this should still work */
|
||||
g_assert_true(!!maildir_mkdir(mdir, 0755, FALSE));
|
||||
|
||||
for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
|
||||
gchar* dir;
|
||||
|
||||
dir = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, subs[i]);
|
||||
g_assert_cmpuint(g_access(dir, R_OK), ==, 0);
|
||||
g_assert_cmpuint(g_access(dir, W_OK), ==, 0);
|
||||
g_free(dir);
|
||||
}
|
||||
|
||||
tmp = g_strdup_printf("%s%c%s", mdir, G_DIR_SEPARATOR, ".noindex");
|
||||
g_assert_cmpuint(g_access(tmp, F_OK), !=, 0);
|
||||
|
||||
g_free(tmp);
|
||||
g_free(tmpdir);
|
||||
g_free(mdir);
|
||||
auto noindex = join_paths(mdir, ".noindex");
|
||||
g_assert_cmpuint(g_access(noindex.c_str(), F_OK), !=, 0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_maildir_mkdir_04(void)
|
||||
test_maildir_mkdir_04()
|
||||
{
|
||||
gchar *tmpdir, *mdir;
|
||||
|
||||
tmpdir = test_mu_common_get_random_tmpdir();
|
||||
mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux");
|
||||
|
||||
/* create part of the structure already... */
|
||||
{
|
||||
gchar* dir;
|
||||
g_assert_cmpuint(g_mkdir_with_parents(mdir, 0755), ==, 0);
|
||||
dir = g_strdup_printf("%s%ccur", mdir, G_DIR_SEPARATOR);
|
||||
g_assert_cmpuint(g_mkdir_with_parents(dir, 0000), ==, 0);
|
||||
g_free(dir);
|
||||
if (geteuid() == 0) {
|
||||
g_test_skip("not useful when run as root");
|
||||
return;
|
||||
}
|
||||
|
||||
TempDir temp_dir;
|
||||
auto mdir = join_paths(temp_dir.path(), "cuux");
|
||||
g_assert_cmpuint(g_mkdir_with_parents(mdir.c_str(), 0755), ==, 0);
|
||||
|
||||
auto curdir = join_paths(mdir, "cur");
|
||||
g_assert_cmpuint(g_mkdir_with_parents(curdir.c_str(), 0000), ==, 0);
|
||||
|
||||
/* this should fail now, because cur is not read/writable */
|
||||
if (geteuid() != 0)
|
||||
g_assert_false(!!maildir_mkdir(mdir, 0755, false));
|
||||
|
||||
g_free(tmpdir);
|
||||
g_free(mdir);
|
||||
auto res = maildir_mkdir(mdir, 0755, false);
|
||||
g_assert_false(!!res);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
|
||||
@ -37,29 +37,27 @@ static void
|
||||
test_query()
|
||||
{
|
||||
allow_warnings();
|
||||
char* tdir;
|
||||
TempDir temp_dir;
|
||||
|
||||
tdir = test_mu_common_get_random_tmpdir();
|
||||
auto store = Store::make_new(tdir, std::string{MU_TESTMAILDIR});
|
||||
auto store = Store::make_new(temp_dir.path(), std::string{MU_TESTMAILDIR});
|
||||
assert_valid_result(store);
|
||||
g_free(tdir);
|
||||
|
||||
auto&& idx{store->indexer()};
|
||||
|
||||
g_assert_true(idx.start(Indexer::Config{}));
|
||||
while (idx.is_running()) {
|
||||
sleep(1);
|
||||
g_usleep(1000);
|
||||
}
|
||||
|
||||
auto dump_matches = [](const QueryResults& res) {
|
||||
size_t n{};
|
||||
for (auto&& item : res) {
|
||||
std::cout << item.query_match() << '\n';
|
||||
if (g_test_verbose())
|
||||
g_debug("%02zu %s %s",
|
||||
++n,
|
||||
item.path().value_or("<none>").c_str(),
|
||||
item.message_id().value_or("<none>").c_str());
|
||||
if (g_test_verbose()) {
|
||||
std::cout << item.query_match() << '\n';
|
||||
mu_debug("{:02d} {} {}",
|
||||
++n,
|
||||
item.path().value_or("<none>"),
|
||||
item.message_id().value_or("<none>"));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -84,8 +82,8 @@ test_query()
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
try {
|
||||
main(int argc, char* argv[]) try {
|
||||
|
||||
mu_test_init(&argc, &argv);
|
||||
|
||||
g_test_add_func("/query", test_query);
|
||||
|
||||
@ -189,14 +189,16 @@ Logger::~Logger()
|
||||
#include <atomic>
|
||||
|
||||
#include "mu-test-utils.hh"
|
||||
#include "mu-utils-file.hh"
|
||||
|
||||
static void
|
||||
test_logger_threads(void)
|
||||
{
|
||||
const auto testpath{test_random_tmpdir() + "/test.log"};
|
||||
TempDir temp_dir;
|
||||
const auto testpath{join_paths(temp_dir.path(), "test.log")};
|
||||
mu_message("log-file: {}", testpath);
|
||||
|
||||
auto logger = Logger::make(testpath.c_str(), Logger::Options::File | Logger::Options::Debug);
|
||||
auto logger = Logger::make(testpath, Logger::Options::File | Logger::Options::Debug);
|
||||
assert_valid_result(logger);
|
||||
|
||||
const auto thread_num = 16;
|
||||
|
||||
@ -34,25 +34,15 @@
|
||||
#include "utils/mu-utils-file.hh"
|
||||
#include "utils/mu-error.hh"
|
||||
|
||||
|
||||
using namespace Mu;
|
||||
|
||||
std::string
|
||||
Mu::test_random_tmpdir()
|
||||
bool
|
||||
Mu::mu_test_mu_hacker()
|
||||
{
|
||||
auto&& dir = mu_format("{}{}mu-test-{}{}test-{:x}",
|
||||
g_get_tmp_dir(),
|
||||
G_DIR_SEPARATOR,
|
||||
getuid(),
|
||||
G_DIR_SEPARATOR,
|
||||
::random() * getpid() * ::time({}));
|
||||
|
||||
auto res = g_mkdir_with_parents(dir.c_str(), 0700);
|
||||
g_assert(res != -1);
|
||||
|
||||
return dir;
|
||||
return !!g_getenv("MU_HACKER");
|
||||
}
|
||||
|
||||
|
||||
const char*
|
||||
Mu::set_tz(const char* tz)
|
||||
{
|
||||
@ -92,11 +82,11 @@ black_hole(void)
|
||||
void
|
||||
Mu::mu_test_init(int *argc, char ***argv)
|
||||
{
|
||||
const auto tmpdir{test_random_tmpdir()};
|
||||
TempDir temp_dir;
|
||||
|
||||
g_unsetenv("XAPIAN_CJK_NGRAM");
|
||||
g_setenv("MU_TEST", "yes", TRUE);
|
||||
g_setenv("XDG_CACHE_HOME", tmpdir.c_str(), TRUE);
|
||||
g_setenv("XDG_CACHE_HOME", temp_dir.path().c_str(), TRUE);
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
|
||||
|
||||
@ -27,17 +27,6 @@
|
||||
|
||||
namespace Mu {
|
||||
|
||||
/**
|
||||
* get a dir name for a random temporary directory to do tests
|
||||
*
|
||||
* @return a random dir name
|
||||
*/
|
||||
std::string test_random_tmpdir(void);
|
||||
|
||||
inline gchar* test_mu_common_get_random_tmpdir() {
|
||||
return g_strdup(test_random_tmpdir().c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
* mu wrapper for g_test_init. Sets environment variable MU_TEST to 1.
|
||||
*
|
||||
@ -46,6 +35,14 @@ inline gchar* test_mu_common_get_random_tmpdir() {
|
||||
*/
|
||||
void mu_test_init(int *argc, char ***argv);
|
||||
|
||||
|
||||
/**
|
||||
* Are we running in a MU_HACKER environment?
|
||||
*
|
||||
* @return true or false
|
||||
*/
|
||||
bool mu_test_mu_hacker();
|
||||
|
||||
/**
|
||||
* set the timezone
|
||||
*
|
||||
@ -62,6 +59,7 @@ const char* set_tz(const char* tz);
|
||||
*/
|
||||
bool set_en_us_utf8_locale();
|
||||
|
||||
|
||||
/**
|
||||
* For unit tests, assert two std::string's are equal.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user