guile: Fix and re-enable unit test

This commit is contained in:
Dirk-Jan C. Binnema 2021-03-17 18:33:05 +02:00
parent 811ae26574
commit 21374c12a1
5 changed files with 38 additions and 23 deletions

View File

@ -56,10 +56,11 @@ libguile_mu_la_CXXFLAGS=$(AM_CXXFLAGS)
libguile_mu_la_LIBADD= \ libguile_mu_la_LIBADD= \
${top_builddir}/lib/libmu.la \ ${top_builddir}/lib/libmu.la \
${top_builddir}/lib/utils/libmu-utils.la \
$(READLINE_LIBS) \ $(READLINE_LIBS) \
${GUILE_LIBS} ${GUILE_LIBS}
libguile_mu_la_LDFLAGS= \ libguile_mu_la_LDFLAGS= \
$(ASAN_LDFLAGS) \ $(ASAN_LDFLAGS) \
-shared \ -shared \
-export-dynamic -export-dynamic

View File

@ -82,11 +82,13 @@ typedef struct _FlagData FlagData;
#define MU_GUILE_INITIALIZED_OR_ERROR \ #define MU_GUILE_INITIALIZED_OR_ERROR \
do { if (!(mu_guile_initialized())) \ do { \
if (!(mu_guile_initialized())) { \
mu_guile_error (FUNC_NAME, 0, \ mu_guile_error (FUNC_NAME, 0, \
"mu not initialized; call mu:initialize", \ "mu not initialized; call mu:initialize", \
SCM_UNDEFINED); \ SCM_UNDEFINED); \
return SCM_UNSPECIFIED; \ return SCM_UNSPECIFIED; \
} \
} while (0) } while (0)
@ -423,8 +425,10 @@ SCM_DEFINE (get_header, "mu:c:get-header", 2, 0, 0,
#undef FUNC_NAME #undef FUNC_NAME
static Mu::Option<Mu::QueryResults> static Mu::Option<Mu::QueryResults>
get_query_results (Mu::Query& query, const char* expr, int maxnum) get_query_results (Mu::Store& store, const char* expr, int maxnum)
{ {
Mu::Query query(store);
return query.run(expr, MU_MSG_FIELD_ID_NONE, return query.run(expr, MU_MSG_FIELD_ID_NONE,
Mu::QueryFlags::None, maxnum); Mu::QueryFlags::None, maxnum);
} }
@ -456,7 +460,7 @@ SCM_DEFINE (for_each_message, "mu:c:for-each-message", 3, 0, 0,
else else
expr = scm_to_utf8_string(EXPR); expr = scm_to_utf8_string(EXPR);
const auto res{get_query_results(mu_guile_query(), expr, const auto res{get_query_results(mu_guile_store(), expr,
scm_to_int(MAXNUM))}; scm_to_int(MAXNUM))};
free (expr); free (expr);
if (!res) if (!res)

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2011-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2011-2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** 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 ** under the terms of the GNU General Public License as published by the
@ -72,46 +72,53 @@ mu_guile_g_error (const char *func_name, GError *err)
/* there can be only one */ /* there can be only one */
static std::unique_ptr<Mu::Query> QuerySingleton; static std::unique_ptr<Mu::Store> StoreSingleton;
static gboolean static gboolean
mu_guile_init_instance (const char *muhome) try mu_guile_init_instance (const char *muhome) try
{ {
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
if (!mu_runtime_init (muhome, "guile", FALSE)) if (!mu_runtime_init (muhome, "guile", true) || StoreSingleton)
return FALSE; return FALSE;
Mu::Store store{mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB)}; StoreSingleton = std::make_unique<Mu::Store>(
QuerySingleton = std::make_unique<Mu::Query>(store); mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB));
g_debug ("mu-guile: opened store @ %s (n=%zu); maildir: %s",
StoreSingleton->metadata().database_path.c_str(),
StoreSingleton->size(),
StoreSingleton->metadata().root_maildir.c_str());
return TRUE; return TRUE;
} catch (...) { } catch (...) {
return FALSE; return FALSE;
} }
static void static void
mu_guile_uninit_instance () mu_guile_uninit_instance ()
{ {
QuerySingleton.reset(); StoreSingleton.reset();
mu_runtime_uninit (); mu_runtime_uninit ();
} }
Mu::Query& Mu::Store&
mu_guile_query () mu_guile_store ()
{ {
if (!QuerySingleton) if (!StoreSingleton)
g_error("mu guile not initialized"); g_error("mu guile not initialized");
return *QuerySingleton.get(); return *StoreSingleton.get();
} }
gboolean gboolean
mu_guile_initialized () mu_guile_initialized ()
{ {
return !!QuerySingleton; g_debug ("initialized ? %u", !!StoreSingleton);
return !!StoreSingleton;
} }
@ -145,6 +152,9 @@ SCM_DEFINE_PUBLIC (mu_initialize, "mu:initialize", 0, 1, 0,
return mu_guile_error (FUNC_NAME, 0, "Failed to initialize mu", return mu_guile_error (FUNC_NAME, 0, "Failed to initialize mu",
SCM_UNSPECIFIED); SCM_UNSPECIFIED);
g_debug ("mu-guile: initialized @ %s (%p)",
muhome ? muhome : "<default>", StoreSingleton.get());
/* cleanup when we're exiting */ /* cleanup when we're exiting */
atexit (mu_guile_uninit_instance); atexit (mu_guile_uninit_instance);

View File

@ -26,9 +26,9 @@
/** /**
* get the singleton Query instance * get the singleton Store instance
*/ */
Mu::Query& mu_guile_query (void); Mu::Store& mu_guile_store ();
/** /**

View File

@ -39,8 +39,8 @@ AM_LDFLAGS=$(ASAN_LDFLAGS)
noinst_PROGRAMS= $(TEST_PROGS) noinst_PROGRAMS= $(TEST_PROGS)
# TEST_PROGS += test-mu-guile TEST_PROGS += test-mu-guile
# test_mu_guile_SOURCES= test-mu-guile.cc test_mu_guile_SOURCES= test-mu-guile.cc
# test_mu_guile_LDADD=${top_builddir}/lib/libtestmucommon.la test_mu_guile_LDADD=${top_builddir}/lib/libtestmucommon.la
EXTRA_DIST=test-mu-guile.scm test-mu-guile.cc EXTRA_DIST=test-mu-guile.scm test-mu-guile.cc