tests: Fix GDateTime format tests on non-English locales

It seems that the test expects g_date_time_format to return formatted
results in English, and there is no setlocale (LC_ALL, "") call in the
file so the test does run in the default C locale. However, gettext
seems to read the value of LC_MESSAGES from the environment by itself.
Even if the value of LC_MESSAGES locale is C because of not calling
setlocale, gettext still translates the name of the month according to
the LC_MESSAGES environment variable, causing g_date_time_format_locale
to fail on the "%b" test case because it cannot convert UTF-8 text
returned by get_month_name_with_day to ASCII.

To avoid the test failure, we set the LC_MESSAGES environment variable
to C before format tests and restore it at the end of the function.

https://bugzilla.gnome.org/show_bug.cgi?id=795165
This commit is contained in:
Ting-Wei Lan 2018-04-29 02:04:01 +08:00 committed by Philip Withnall
parent c47cf7355c
commit a0c7f85437

View File

@ -1324,6 +1324,7 @@ test_GDateTime_printf (void)
* that long, and it will cause the test to fail if dst isn't big
* enough.
*/
gchar *old_lc_messages;
gchar dst[64];
struct tm tt;
time_t t;
@ -1353,6 +1354,9 @@ GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
g_date_time_unref (dt); \
g_free (p); } G_STMT_END
old_lc_messages = g_strdup (g_getenv ("LC_MESSAGES"));
g_setenv ("LC_MESSAGES", "C", TRUE);
/*
* This is a little helper to make sure we can compare timezones to
* that of the generated timezone.
@ -1420,6 +1424,12 @@ GDateTime *__dt = g_date_time_new_local (2009, 10, 24, 0, 0, 0);\
#elif defined G_OS_WIN32
TEST_PRINTF ("%Z", "Pacific Standard Time");
#endif
if (old_lc_messages != NULL)
g_setenv ("LC_MESSAGES", old_lc_messages, TRUE);
else
g_unsetenv ("LC_MESSAGES");
g_free (old_lc_messages);
}
static void