git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/4] refactor test-date interface

From
Jeff King <peff@peff.net>
Date
Aug 31, 2009, 02:26 UTC
Message-ID
<20090831022646.GB5507@coredump.intra.peff.net>
In-Reply-To
<20090831022228.GA4833@coredump.intra.peff.net>

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>
---
Same as earlier 2/3.
 test-date.c |   86 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 66 insertions(+), 20 deletions(-)
 rewrite test-date.c (63%)
diff --git a/test-date.c b/test-date.c
dissimilarity index 63%
index 62e8f23..5b0a220 100644
--- a/test-date.c
+++ b/test-date.c
@@ -1,20 +1,66 @@
-#include "cache.h"
-
-int main(int argc, char **argv)
-{
-	int i;
-
-	for (i = 1; i < argc; i++) {
-		char result[100];
-		time_t t;
-
-		memcpy(result, "bad", 4);
-		parse_date(argv[i], result, sizeof(result));
-		t = strtoul(result, NULL, 0);
-		printf("%s -> %s -> %s", argv[i], result, ctime(&t));
-
-		t = approxidate(argv[i]);
-		printf("%s -> %s\n", argv[i], ctime(&t));
-	}
-	return 0;
-}
+#include "cache.h"
+
+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)
+{
+	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);
+	}
+}
+
+static void parse_dates(char **argv, struct timeval *now)
+{
+	for (; *argv; argv++) {
+		char result[100];
+		time_t t;
+
+		parse_date(*argv, result, sizeof(result));
+		t = strtoul(result, NULL, 0);
+		printf("%s -> %s\n", *argv,
+			t ? show_date(t, 0, DATE_ISO8601) : "bad");
+	}
+}
+
+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;
+}
-- 
1.6.4.2.373.g5881fd
Previous: Alex RiesenNext: Jeff King
Message 42 of 47 in “Round-down years in "years+months" relative date view”
  1. Round-down years in "years+months" relative date viewDavid Reiss, Aug 27, 2009
  2. Jeff KingAug 28, 2009
  3. Alex RiesenAug 28, 2009
  4. Jeff KingAug 28, 2009
  5. Alex RiesenAug 28, 2009
  6. Jeff KingAug 28, 2009
  7. Alex RiesenAug 28, 2009
  8. A Large Angry SCMAug 28, 2009
  9. Nicolas PitreAug 28, 2009
  10. Jeff KingAug 28, 2009
  11. Alex RiesenAug 28, 2009
  12. Jeff KingAug 28, 2009
  13. Alex RiesenAug 28, 2009
  14. Alex RiesenAug 28, 2009
  15. Nicolas PitreAug 28, 2009
  16. Alex RiesenAug 28, 2009
  17. Nicolas PitreAug 28, 2009
  18. Alex RiesenAug 28, 2009
  19. Nicolas PitreAug 28, 2009
  20. Alex RiesenAug 28, 2009
  21. Jeff KingAug 28, 2009
  22. Alex RiesenAug 28, 2009
  23. Alex RiesenAug 28, 2009
  24. Allow testing of _relative family of time formatting and parsing functionsAlex Riesen, Aug 28, 2009
  25. Alex RiesenAug 28, 2009
  26. Junio C HamanoAug 29, 2009
  27. Alex RiesenAug 30, 2009
  28. Jeff KingAug 30, 2009
  29. Alex RiesenAug 30, 2009
  30. 1/2 Add date formatting and parsing functions relative to a given timeAlex Riesen, Aug 30, 2009
  31. 2/2 Allow testing of _relative family of time formatting and parsing functionsAlex Riesen, Aug 30, 2009
  32. Jeff KingAug 30, 2009
  33. Jeff KingAug 30, 2009
  34. Alex RiesenAug 30, 2009
  35. Jeff KingAug 30, 2009
  36. Alex RiesenAug 30, 2009
  37. 1/3 Add date formatting and parsing functions relative to a given timeJeff King, Aug 30, 2009
  38. Jeff KingAug 30, 2009
  39. Jeff KingAug 31, 2009
  40. 1/4 Add date formatting and parsing functions relative to a given timeJeff King, Aug 31, 2009
  41. Alex RiesenAug 31, 2009
  42. 2/4 refactor test-date interfaceJeff King, Aug 31, 2009
  43. 3/4 tests: add date printing and parsing testsJeff King, Aug 31, 2009
  44. Jeff KingSep 1, 2009
  45. 4/4 fix approxidate parsing of relative months and yearsJeff King, Aug 31, 2009
  46. 2/3 refactor test-date interfaceJeff King, Aug 30, 2009
  47. 3/3 tests: add date printing and parsing testsJeff King, Aug 30, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.