tests: skip missing timezones in queries, too

This commit is contained in:
Dirk-Jan C. Binnema 2022-05-18 01:08:40 +03:00
parent 33d30775ee
commit b03590cca8
4 changed files with 54 additions and 18 deletions

View File

@ -625,3 +625,19 @@ Mu::locale_workaround()
return false;
}
bool
Mu::timezone_available(const std::string& tz)
{
const auto old_tz = g_getenv("TZ");
g_setenv("TZ", tz.c_str(), TRUE);
auto tzone = g_time_zone_new_local ();
bool have_tz = g_strcmp0(g_time_zone_get_identifier(tzone), tz.c_str()) == 0;
g_time_zone_unref (tzone);
g_setenv("TZ", old_tz, TRUE);
return have_tz;
}

View File

@ -155,6 +155,17 @@ std::string time_to_string(const std::string& frm, time_t t, bool utc = false) G
bool locale_workaround();
/**
* Is the given timezone available? For tests
*
* @param tz a timezone, such as Europe/Helsinki
*
* @return true or false
*/
bool timezone_available(const std::string& tz);
// https://stackoverflow.com/questions/19053351/how-do-i-use-a-custom-deleter-with-a-stdunique-ptr-member
template <auto fn>
struct deleter_from_fn {

View File

@ -56,22 +56,15 @@ test_cases(const CaseVec& cases, ProcFunc proc)
static void
test_date_basic()
{
// ensure we have the needed TZ or skip the test.
const auto hki = "Europe/Helsinki";
g_setenv("TZ", hki, TRUE);
{
auto tz = g_time_zone_new_local ();
bool have_hki = g_strcmp0(g_time_zone_get_identifier(tz), hki) == 0;
g_time_zone_unref (tz);
if (!have_hki) {
g_test_skip("timezone Europe/Helsinki not available");
return;
}
// ensure we have the needed TZ or skip the test.
if (!timezone_available(hki)) {
g_test_skip("timezone Europe/Helsinki not available");
return;
}
g_setenv("TZ", hki, TRUE);
constexpr std::array<std::tuple<const char*, bool, int64_t>, 9> cases = {{
{"2015-09-18T09:10:23", true, 1442556623},
{"1972-12-14T09:10:23", true, 93165023},

View File

@ -323,6 +323,12 @@ test_mu_query_wildcards(void)
static void
test_mu_query_dates_helsinki(void)
{
const auto hki = "Europe/Helsinki";
if (!timezone_available(hki)) {
g_test_skip("timezone not available");
return;
}
int i;
const char* old_tz;
@ -332,7 +338,7 @@ test_mu_query_dates_helsinki(void)
{"date:200808110803..today", 7},
{"date:200808110801..now", 7}};
old_tz = set_tz("Europe/Helsinki");
old_tz = set_tz(hki);
const auto xpath{make_database(MU_TESTMAILDIR)};
g_assert_false(xpath.empty());
@ -348,6 +354,12 @@ test_mu_query_dates_helsinki(void)
static void
test_mu_query_dates_sydney(void)
{
const auto syd = "Australia/Sydney";
if (!timezone_available(syd)) {
g_test_skip("timezone not available");
return;
}
int i;
const char* old_tz;
QResults queries[] = {{"date:20080731..20080804", 5},
@ -355,8 +367,7 @@ test_mu_query_dates_sydney(void)
{"date:200808110803..now", 7},
{"date:200808110803..today", 7},
{"date:200808110801..now", 7}};
old_tz = set_tz("Australia/Sydney");
old_tz = set_tz(syd);
const auto xpath{make_database(MU_TESTMAILDIR)};
g_assert_false(xpath.empty());
@ -371,6 +382,12 @@ test_mu_query_dates_sydney(void)
static void
test_mu_query_dates_la(void)
{
const auto la = "America/Los_Angeles";
if (!timezone_available(la)) {
g_test_skip("timezone not available");
return;
}
int i;
const char* old_tz;
@ -381,8 +398,7 @@ test_mu_query_dates_la(void)
{"date:200808110803..now", 6},
{"date:200808110803..today", 6},
{"date:200808110801..now", 6}};
old_tz = set_tz("America/Los_Angeles");
old_tz = set_tz(la);
const auto xpath = make_database(MU_TESTMAILDIR);
g_assert_false(xpath.empty());
@ -605,7 +621,7 @@ main(int argc, char* argv[])
setlocale(LC_ALL, "");
g_test_init(&argc, &argv, NULL);
g_test_init(&argc, &argv, nullptr);
DB_PATH1 = make_database(MU_TESTMAILDIR);
g_assert_false(DB_PATH1.empty());