From 7fd81cc15422705be34350b9f6ea0bb06302c376 Mon Sep 17 00:00:00 2001 From: djcb Date: Thu, 8 Mar 2012 00:11:31 +0200 Subject: [PATCH] * tests: more gracefully deal with the case where there's no en_US.utf8 locale --- src/tests/test-mu-cmd-cfind.c | 64 +++++++++++++++++------------------ src/tests/test-mu-cmd.c | 3 +- src/tests/test-mu-common.c | 29 +++++++++++++--- src/tests/test-mu-common.h | 33 ++++++++++++++---- 4 files changed, 86 insertions(+), 43 deletions(-) diff --git a/src/tests/test-mu-cmd-cfind.c b/src/tests/test-mu-cmd-cfind.c index 56e046de..9aac27c8 100644 --- a/src/tests/test-mu-cmd-cfind.c +++ b/src/tests/test-mu-cmd-cfind.c @@ -1,6 +1,6 @@ /* -*- mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- -** -** Copyright (C) 2008-2011 Dirk-Jan C. Binnema +** +** Copyright (C) 2008-2012 Dirk-Jan C. Binnema ** ** 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 @@ -14,8 +14,8 @@ ** ** 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. -** +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** */ #if HAVE_CONFIG_H @@ -38,16 +38,16 @@ static gchar* fill_contacts_cache (void) { gchar *cmdline, *tmpdir; - + tmpdir = test_mu_common_get_random_tmpdir(); cmdline = g_strdup_printf ("%s index --muhome=%s --maildir=%s" " --quiet", MU_PROGRAM, tmpdir, MU_TESTMAILDIR); - + g_assert (g_spawn_command_line_sync (cmdline, NULL, NULL, NULL, NULL)); g_free (cmdline); - + return tmpdir; } @@ -56,14 +56,14 @@ static void test_mu_cfind_plain (void) { gchar *muhome, *cmdline, *output, *erroutput; - + muhome = fill_contacts_cache (); g_assert (muhome != NULL); cmdline = g_strdup_printf ("%s cfind --muhome=%s --format=plain " "'testmu\\.xxx?'", MU_PROGRAM, muhome); - + output = erroutput = NULL; g_assert (g_spawn_command_line_sync (cmdline, &output, &erroutput, NULL, NULL)); @@ -86,20 +86,20 @@ test_mu_cfind_bbdb (void) struct tm *tmtoday; time_t now; const char *old_tz; - + muhome = fill_contacts_cache (); g_assert (muhome != NULL); - + old_tz = set_tz ("Europe/Helsinki"); - + cmdline = g_strdup_printf ("%s cfind --muhome=%s --format=bbdb " "'testmu\\.xxx?'", MU_PROGRAM, muhome); - + output = erroutput = NULL; g_assert (g_spawn_command_line_sync (cmdline, &output, &erroutput, NULL, NULL)); - + frm = ";; -*-coding: utf-8-emacs;-*-\n" ";;; file-version: 6\n" "[\"Helmut\" \"Kröger\" nil nil nil nil (\"hk@testmu.xxx\") " @@ -112,10 +112,10 @@ test_mu_cfind_bbdb (void) now = time(NULL); tmtoday = localtime(&now); strftime(today,sizeof(today),"%Y-%m-%d", tmtoday); - expected = g_strdup_printf (frm, today, today); - + expected = g_strdup_printf (frm, today, today); + /* g_print ("\n%s\n", output); */ - + g_assert_cmpstr (output, ==, expected); g_free (cmdline); @@ -132,14 +132,14 @@ static void test_mu_cfind_wl (void) { gchar *muhome, *cmdline, *output, *erroutput; - + muhome = fill_contacts_cache (); g_assert (muhome != NULL); cmdline = g_strdup_printf ("%s cfind --muhome=%s --format=wl " "'testmu\\.xxx?'", MU_PROGRAM, muhome); - + output = erroutput = NULL; g_assert (g_spawn_command_line_sync (cmdline, &output, &erroutput, NULL, NULL)); @@ -158,14 +158,14 @@ static void test_mu_cfind_mutt_alias (void) { gchar *muhome, *cmdline, *output, *erroutput; - + muhome = fill_contacts_cache (); g_assert (muhome != NULL); cmdline = g_strdup_printf ("%s cfind --muhome=%s --format=mutt-alias " "'testmu\\.xxx?'", MU_PROGRAM, muhome); - + output = erroutput = NULL; g_assert (g_spawn_command_line_sync (cmdline, &output, &erroutput, NULL, NULL)); @@ -183,21 +183,21 @@ static void test_mu_cfind_mutt_ab (void) { gchar *muhome, *cmdline, *output, *erroutput; - + muhome = fill_contacts_cache (); g_assert (muhome != NULL); cmdline = g_strdup_printf ("%s cfind --muhome=%s --format=mutt-ab " "'testmu\\.xxx?'", MU_PROGRAM, muhome); - + output = erroutput = NULL; g_assert (g_spawn_command_line_sync (cmdline, &output, &erroutput, NULL, NULL)); g_assert_cmpstr (output, ==, "Matching addresses in the mu database:\n" - "hk@testmu.xxx\tHelmut Kröger\t\n" + "hk@testmu.xxx\tHelmut Kröger\t\n" "testmu@testmu.xx\tMü\t\n"); g_free (cmdline); g_free (muhome); @@ -210,14 +210,14 @@ static void test_mu_cfind_org_contact (void) { gchar *muhome, *cmdline, *output, *erroutput; - + muhome = fill_contacts_cache (); g_assert (muhome != NULL); cmdline = g_strdup_printf ("%s cfind --muhome=%s --format=org-contact " "'testmu\\.xxx?'", MU_PROGRAM, muhome); - + output = erroutput = NULL; g_assert (g_spawn_command_line_sync (cmdline, &output, &erroutput, NULL, NULL)); @@ -244,14 +244,14 @@ static void test_mu_cfind_csv (void) { gchar *muhome, *cmdline, *output, *erroutput; - + muhome = fill_contacts_cache (); g_assert (muhome != NULL); cmdline = g_strdup_printf ("%s cfind --muhome=%s --format=csv " "'testmu\\.xxx?'", MU_PROGRAM, muhome); - + output = erroutput = NULL; g_assert (g_spawn_command_line_sync (cmdline, &output, &erroutput, NULL, NULL)); @@ -272,8 +272,9 @@ main (int argc, char *argv[]) int rv; g_test_init (&argc, &argv, NULL); - setenv ("LC_ALL", "en_US.utf8", 1); - + if (!set_en_us_utf8_locale()) + return 0; /* don't error out... */ + g_test_add_func ("/mu-cmd-cfind/test-mu-cfind-plain", test_mu_cfind_plain); g_test_add_func ("/mu-cmd-cfind/test-mu-cfind-bbdb", test_mu_cfind_bbdb); g_test_add_func ("/mu-cmd-cfind/test-mu-cfind-wl", test_mu_cfind_wl); @@ -285,7 +286,7 @@ main (int argc, char *argv[]) test_mu_cfind_org_contact); g_test_add_func ("/mu-cmd-cfind/test-mu-cfind-csv", test_mu_cfind_csv); - + g_log_set_handler (NULL, G_LOG_LEVEL_MASK | G_LOG_LEVEL_WARNING| G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION, @@ -295,4 +296,3 @@ main (int argc, char *argv[]) return rv; } - diff --git a/src/tests/test-mu-cmd.c b/src/tests/test-mu-cmd.c index 2cbcaa18..b0db4a24 100644 --- a/src/tests/test-mu-cmd.c +++ b/src/tests/test-mu-cmd.c @@ -754,7 +754,8 @@ main (int argc, char *argv[]) int rv; g_test_init (&argc, &argv, NULL); - setenv ("LC_ALL", "en_US.utf8", 1); + if (!set_en_us_utf8_locale()) + return 0; /* don't error out... */ g_test_add_func ("/mu-cmd/test-mu-index", test_mu_index); diff --git a/src/tests/test-mu-common.c b/src/tests/test-mu-common.c index d8b0802c..e71b43b4 100644 --- a/src/tests/test-mu-common.c +++ b/src/tests/test-mu-common.c @@ -1,5 +1,5 @@ -/* -** Copyright (C) 2008-2010 Dirk-Jan C. Binnema +/* +** Copyright (C) 2008-2012 Dirk-Jan C. Binnema ** ** 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 @@ -13,8 +13,8 @@ ** ** 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. -** +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** */ #if HAVE_CONFIG_H @@ -28,6 +28,9 @@ #include #include +#include +#include + #include "test-mu-common.h" char* @@ -57,6 +60,24 @@ set_tz (const char* tz) } +gboolean +set_en_us_utf8_locale (void) +{ + setenv ("LC_ALL", "en_US.utf8", 1); + setlocale (LC_ALL, "en_US.utf8"); + + if (strcmp (nl_langinfo(CODESET), "UTF-8") != 0) { + g_print ("Note: Unit tests require the en_US.utf8 locale. " + "Ignoring test cases."); + return FALSE; + } + + return TRUE; +} + + + + void black_hole (void) diff --git a/src/tests/test-mu-common.h b/src/tests/test-mu-common.h index c443e50d..ac71eb1c 100644 --- a/src/tests/test-mu-common.h +++ b/src/tests/test-mu-common.h @@ -1,5 +1,5 @@ -/* -** Copyright (C) 2008-2010 Dirk-Jan C. Binnema +/* +** Copyright (C) 2008-2012 Dirk-Jan C. Binnema ** ** 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 @@ -13,8 +13,8 @@ ** ** 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. -** +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** */ #ifndef __TEST_MU_COMMON_H__ @@ -24,17 +24,38 @@ G_BEGIN_DECLS -/** +/** * get a dir name for a random temporary directory to do tests - * + * * @return a random dir name, g_free when it's no longer needed */ char* test_mu_common_get_random_tmpdir (void); + + +/** + * set the output to /dev/null + * + */ void black_hole (void); +/** + * set the timezone + * + * @param tz timezone + * + * @return the old timezone + */ const char* set_tz (const char* tz); + +/** + * switch the locale to en_US.utf8, return TRUE if it succeeds + * + * @return TRUE if the switch succeeds, FALSE otherwise + */ +gboolean set_en_us_utf8_locale (void); + G_END_DECLS #endif /*__TEST_MU_COMMON_H__*/