diff options
author | Jeff King <peff@peff.net> | 2009-08-30 22:26:46 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2009-08-30 22:04:46 -0700 |
commit | 4f6552ea4cf2f1fdfd850b17b25864c761d14580 (patch) | |
tree | d39a0e074d3b5c259aabe396c9202ebd2b3f711b /test-date.c | |
parent | 33012fc429af24fee64f39406d401662feb947a8 (diff) | |
download | git-4f6552ea4cf2f1fdfd850b17b25864c761d14580.tar.gz |
refactor test-date interface
The test-date program goes back to the early days of git,
where it was presumably used to do manual sanity checks on
changes to the date code. However, it is not actually used
by the test suite to do any sort of automatic of systematic
tests.
This patch refactors the interface to the program to try to
make it more suitable for use by the test suite. There
should be no fallouts to changing the interface since it is
not actually installed and is not internally called by any
other programs.
The changes are:
- add a "mode" parameter so the caller can specify which
operation to test
- add a mode to test relative date output from show_date
- allow faking a fixed time via the TEST_DATE_NOW
environment variable, which allows consistent automated
testing
- drop the use of ctime for showing dates in favor of our
internal iso8601 printing routines. The ctime output is
somewhat redundant (because of the day-of-week) which
makes writing test cases more annoying.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'test-date.c')
-rw-r--r-- | test-date.c | 63 |
1 files changed, 55 insertions, 8 deletions
diff --git a/test-date.c b/test-date.c index 62e8f2387a..a9e705f79a 100644 --- a/test-date.c +++ b/test-date.c @@ -1,20 +1,67 @@ #include "cache.h" -int main(int argc, char **argv) +static const char *usage_msg = "\n" +" test-date show [time_t]...\n" +" test-date parse [date]...\n" +" test-date approxidate [date]...\n"; + +static void show_dates(char **argv, struct timeval *now) { - int i; + char buf[128]; + + for (; *argv; argv++) { + time_t t = atoi(*argv); + show_date_relative(t, 0, now, buf, sizeof(buf)); + printf("%s -> %s\n", *argv, buf); + } +} - for (i = 1; i < argc; i++) { +static void parse_dates(char **argv, struct timeval *now) +{ + for (; *argv; argv++) { char result[100]; time_t t; - memcpy(result, "bad", 4); - parse_date(argv[i], result, sizeof(result)); + result[0] = 0; + parse_date(*argv, result, sizeof(result)); t = strtoul(result, NULL, 0); - printf("%s -> %s -> %s", argv[i], result, ctime(&t)); + printf("%s -> %s\n", *argv, + t ? show_date(t, 0, DATE_ISO8601) : "bad"); + } +} - t = approxidate(argv[i]); - printf("%s -> %s\n", argv[i], ctime(&t)); +static void parse_approxidate(char **argv, struct timeval *now) +{ + for (; *argv; argv++) { + time_t t; + t = approxidate_relative(*argv, now); + printf("%s -> %s\n", *argv, show_date(t, 0, DATE_ISO8601)); + } +} + +int main(int argc, char **argv) +{ + struct timeval now; + const char *x; + + x = getenv("TEST_DATE_NOW"); + if (x) { + now.tv_sec = atoi(x); + now.tv_usec = 0; } + else + gettimeofday(&now, NULL); + + argv++; + if (!*argv) + usage(usage_msg); + if (!strcmp(*argv, "show")) + show_dates(argv+1, &now); + else if (!strcmp(*argv, "parse")) + parse_dates(argv+1, &now); + else if (!strcmp(*argv, "approxidate")) + parse_approxidate(argv+1, &now); + else + usage(usage_msg); return 0; } |