From b9d9ab7dae14815cc5f40aae0170cd8ed7d342eb Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 2 Feb 2018 10:05:15 +0100 Subject: [PATCH] cal: add ifdef TEST_CAL The test program follows CAL_TEST_TIME= 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 --- misc-utils/Makemodule.am | 5 +++++ misc-utils/cal.c | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/misc-utils/Makemodule.am b/misc-utils/Makemodule.am index 01d43d67f4..36195b7a30 100644 --- a/misc-utils/Makemodule.am +++ b/misc-utils/Makemodule.am @@ -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 diff --git a/misc-utils/cal.c b/misc-utils/cal.c index a110b1b8f6..266f77bdcd 100644 --- a/misc-utils/cal.c +++ b/misc-utils/cal.c @@ -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 */ 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); -- 2.47.3