Skip to content

Commit 4f6552e

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33012fc commit 4f6552e

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

test-date.c

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,67 @@
11
#include "cache.h"
22

3-
int main(int argc, char **argv)
3+
static const char *usage_msg = "\n"
4+
" test-date show [time_t]...\n"
5+
" test-date parse [date]...\n"
6+
" test-date approxidate [date]...\n";
7+
8+
static void show_dates(char **argv, struct timeval *now)
49
{
5-
int i;
10+
char buf[128];
11+
12+
for (; *argv; argv++) {
13+
time_t t = atoi(*argv);
14+
show_date_relative(t, 0, now, buf, sizeof(buf));
15+
printf("%s -> %s\n", *argv, buf);
16+
}
17+
}
618

7-
for (i = 1; i < argc; i++) {
19+
static void parse_dates(char **argv, struct timeval *now)
20+
{
21+
for (; *argv; argv++) {
822
char result[100];
923
time_t t;
1024

11-
memcpy(result, "bad", 4);
12-
parse_date(argv[i], result, sizeof(result));
25+
result[0] = 0;
26+
parse_date(*argv, result, sizeof(result));
1327
t = strtoul(result, NULL, 0);
14-
printf("%s -> %s -> %s", argv[i], result, ctime(&t));
28+
printf("%s -> %s\n", *argv,
29+
t ? show_date(t, 0, DATE_ISO8601) : "bad");
30+
}
31+
}
1532

16-
t = approxidate(argv[i]);
17-
printf("%s -> %s\n", argv[i], ctime(&t));
33+
static void parse_approxidate(char **argv, struct timeval *now)
34+
{
35+
for (; *argv; argv++) {
36+
time_t t;
37+
t = approxidate_relative(*argv, now);
38+
printf("%s -> %s\n", *argv, show_date(t, 0, DATE_ISO8601));
39+
}
40+
}
41+
42+
int main(int argc, char **argv)
43+
{
44+
struct timeval now;
45+
const char *x;
46+
47+
x = getenv("TEST_DATE_NOW");
48+
if (x) {
49+
now.tv_sec = atoi(x);
50+
now.tv_usec = 0;
1851
}
52+
else
53+
gettimeofday(&now, NULL);
54+
55+
argv++;
56+
if (!*argv)
57+
usage(usage_msg);
58+
if (!strcmp(*argv, "show"))
59+
show_dates(argv+1, &now);
60+
else if (!strcmp(*argv, "parse"))
61+
parse_dates(argv+1, &now);
62+
else if (!strcmp(*argv, "approxidate"))
63+
parse_approxidate(argv+1, &now);
64+
else
65+
usage(usage_msg);
1966
return 0;
2067
}

0 commit comments

Comments
 (0)