]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
cal: add ifdef TEST_CAL
authorKarel Zak <kzak@redhat.com>
Fri, 2 Feb 2018 09:05:15 +0000 (10:05 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 2 Feb 2018 09:05:15 +0000 (10:05 +0100)
The test program follows CAL_TEST_TIME=<sec> rather than libc time().
It allows to use cal(1) in regression tests in cases where output
depends on the current time.

(We already use the same for example for logger.)

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/Makemodule.am
misc-utils/cal.c

index 01d43d67f4006a220fb6841731ecde6122e59117..36195b7a309f1c886117c7d5b7b115bdf44a1ab3 100644 (file)
@@ -14,6 +14,11 @@ cal_LDADD += $(NCURSES_LIBS)
 cal_CFLAGS += $(NCURSES_CFLAGS)
 endif
 endif # !HAVE_TINFO
+
+check_PROGRAMS += test_cal
+test_cal_SOURCES = $(cal_SOURCES)
+test_cal_LDADD = $(cal_LDADD)
+test_cal_CFLAGS = -DTEST_CAL $(cal_CFLAGS)
 endif # BUILD_CAL
 
 
index a110b1b8f6653ef5cfe4d8f6a0a42f2d0d9888d0..266f77bdcde5b0a71f79c5834d6c1d717ca95ca1 100644 (file)
@@ -261,6 +261,24 @@ static void center(const char *str, size_t len, int separate);
 static int parse_reform_year(const char *reform_year);
 static void __attribute__((__noreturn__)) usage(void);
 
+#ifdef TEST_CAL
+static time_t cal_time(time_t *t)
+{
+       char *str = getenv("CAL_TEST_TIME");
+
+       if (str) {
+               uint64_t x = strtou64_or_err(str, "failed to parse CAL_TEST_TIME");
+
+               *t = x;
+               return *t;
+       }
+
+       return time(t);
+}
+#else
+# define cal_time(t)   time(t)
+#endif
+
 int main(int argc, char **argv)
 {
        struct tm *local_time;
@@ -443,12 +461,12 @@ int main(int argc, char **argv)
                        now = (time_t) (x / 1000000);
                /* cal <monthname> */
                else if ((ctl.req.month = monthname_to_number(&ctl, *argv)) > 0)
-                       time(&now);     /* this year */
+                       cal_time(&now); /* this year */
                else
                        errx(EXIT_FAILURE, _("failed to parse timestamp or unknown month name: %s"), *argv);
                argc = 0;
        } else
-               time(&now);
+               cal_time(&now);
 
        local_time = localtime(&now);