]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Merge pull request #2648 from stoeckmann/test_en_us
authorTim Kientzle <kientzle@acm.org>
Sat, 31 May 2025 02:35:44 +0000 (19:35 -0700)
committerMartin Matuska <martin@matuska.de>
Sat, 31 May 2025 19:25:32 +0000 (21:25 +0200)
test_utils: Enforce C locale for all tests

(cherry picked from commit 8540cb7cfbfb47340ecae856d7f2a1292096e936)

tar/test/test_list_item.c
test_utils/test_main.c

index df64acfada4048f5caeda4f20322b58fc008237f..fd131c475275ff434080b5587020f3a0bd8b447e 100644 (file)
@@ -43,54 +43,8 @@ static const char *tvf_out =
 "-rw-r--r--  0 1000            1000            0 Jan  1  1980 f\n";
 #endif
 
-static void
-set_lc_time(const char * str)
-{
-
-#if defined(_WIN32) && !defined(__CYGWIN__)
-       if (!SetEnvironmentVariable("LC_TIME", str)) {
-               fprintf(stderr, "SetEnvironmentVariable failed with %d\n",
-                   (int)GetLastError());
-       }
-#else
-       if (setenv("LC_TIME", str, 1) == -1)
-               fprintf(stderr, "setenv: %s\n", strerror(errno));
-#endif
-}
-
-static int
-run_tvf(void)
-{
-       char * orig_lc_time;
-       char * lc_time;
-       int exact_tvf_check;
-
-       orig_lc_time = getenv("LC_TIME");
-
-       /* Try to set LC_TIME to known (English) dates. */
-       set_lc_time("en_US.UTF-8");
-
-       /* Check if we've got the right LC_TIME; if not, don't check output. */
-       lc_time = getenv("LC_TIME");
-       if ((lc_time != NULL) && strcmp(lc_time, "en_US.UTF-8") == 0)
-               exact_tvf_check = 1;
-       else
-               exact_tvf_check = 0;
-
-       assertEqualInt(0,
-           systemf("%s tvf test_list_item.tar >tvf.out 2>tvf.err", testprog));
-
-       /* Restore the original date formatting. */
-       if (orig_lc_time != NULL)
-               set_lc_time(orig_lc_time);
-
-       return (exact_tvf_check);
-}
-
 DEFINE_TEST(test_list_item)
 {
-       int exact_tvf_check;
-
        extract_reference_file("test_list_item.tar");
 
        /* Run 'tf' and check output. */
@@ -100,16 +54,10 @@ DEFINE_TEST(test_list_item)
        assertTextFileContents(tf_out, "tf.out");
        assertEmptyFile("tf.err");
 
-       /* Run 'tvf'. */
-       exact_tvf_check = run_tvf();
-
-       /* Check 'tvf' output. */
+       /* Run 'tvf' and check output. */
+       assertEqualInt(0,
+           systemf("%s tvf test_list_item.tar >tvf.out 2>tvf.err", testprog));
        failure("'t' mode with 'v' should write more results to stdout");
+       assertTextFileContents(tvf_out, "tvf.out");
        assertEmptyFile("tvf.err");
-       if (exact_tvf_check)
-               assertTextFileContents(tvf_out, "tvf.out");
-       else {
-               /* The 'skipping' macro requires braces. */
-               skipping("Can't check exact tvf output");
-       }
 }
index 98f77b671a8876af5a229f2084a2e1ca31f988ae..afd077fc6d0bf4408e46661c9a1b2cc35c2fd615 100644 (file)
@@ -3554,6 +3554,59 @@ test_summarize(int failed, int skips_num)
        memset(failed_lines, 0, sizeof(failed_lines));
 }
 
+/*
+ * Set or unset environment variable.
+ */
+static void
+set_environment(const char *key, const char *value)
+{
+
+#if defined(_WIN32) && !defined(__CYGWIN__)
+       if (!SetEnvironmentVariable(key, value)) {
+               fprintf(stderr, "SetEnvironmentVariable failed with %d\n",
+                   (int)GetLastError());
+       }
+#else
+       if (value == NULL) {
+               if (unsetenv(key) == -1)
+                       fprintf(stderr, "unsetenv: %s\n", strerror(errno));
+       } else {
+               if (setenv(key, value, 1) == -1)
+                       fprintf(stderr, "setenv: %s\n", strerror(errno));
+       }
+#endif
+}
+
+/*
+ * Enforce C locale for (sub)processes.
+ */
+static void
+set_c_locale()
+{
+       static const char *lcs[] = {
+               "LC_ADDRESS",
+               "LC_ALL",
+               "LC_COLLATE",
+               "LC_CTYPE",
+               "LC_IDENTIFICATION",
+               "LC_MEASUREMENT",
+               "LC_MESSAGES",
+               "LC_MONETARY",
+               "LC_NAME",
+               "LC_NUMERIC",
+               "LC_PAPER",
+               "LC_TELEPHONE",
+               "LC_TIME",
+               NULL
+       };
+       size_t i;
+
+       setlocale(LC_ALL, "C");
+       set_environment("LANG", "C");
+       for (i = 0; lcs[i] != NULL; i++)
+               set_environment(lcs[i], NULL);
+}
+
 /*
  * Actually run a single test, with appropriate setup and cleanup.
  */
@@ -3629,7 +3682,7 @@ test_run(int i, const char *tmpdir)
                exit(1);
        }
        /* Explicitly reset the locale before each test. */
-       setlocale(LC_ALL, "C");
+       set_c_locale();
        /* Record the umask before we run the test. */
        umask(oldumask = umask(0));
        /*
@@ -3643,7 +3696,7 @@ test_run(int i, const char *tmpdir)
        /* Restore umask */
        umask(oldumask);
        /* Reset locale. */
-       setlocale(LC_ALL, "C");
+       set_c_locale();
        /* Reset directory. */
        if (!assertChdir(tmpdir)) {
                fprintf(stderr, "ERROR: Couldn't chdir to temp dir %s\n",