Permalink
Browse files

Allow current time to be overriden during tests.

List output changes depending on the current date: the year is shown
instead of the time for files older than 6 months. For repeatability,
use a fixed value for the current time while running tests.
  • Loading branch information...
fragglet committed Jun 2, 2012
1 parent 729d5d0 commit 3604141420991469a77133869380a320b1d47bc0
Showing with 35 additions and 2 deletions.
  1. +24 −1 src/list.c
  2. +0 −1 src/main.c
  3. +11 −0 test/test_common.sh
View
@@ -244,6 +244,29 @@ static ListColumn method_crc_column = {
method_crc_column_print
};
+// Get the current time.
+
+static time_t get_now_time(void)
+{
+ // For test builds, allow the current time to be overridden using
+ // an environment variable. This is because the list output can
+ // change depending on the current date.
+
+#ifdef TEST_BUILD
+ char *env_val;
+ unsigned int result;
+
+ env_val = getenv("TEST_NOW_TIME");
+
+ if (env_val != NULL
+ && sscanf(env_val, "%u", &result) == 1) {
+ return (time_t) result;
+ }
+#endif
+
+ return time(NULL);
+}
+
// File timestamp
static void output_timestamp(unsigned int timestamp)
@@ -270,7 +293,7 @@ static void output_timestamp(unsigned int timestamp)
// If this is an old time (more than 6 months), print the year.
// For recent timestamps, print the time.
- tmp = time(NULL);
+ tmp = get_now_time();
if ((time_t) timestamp > tmp - 6 * 30 * 24 * 60 * 60) {
printf("%02i:%02i", ts->tm_hour, ts->tm_min);
View
@@ -22,7 +22,6 @@ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h>
#include <string.h>
#include <errno.h>
-#include <time.h>
#include "lib/lha_arch.h"
#include "lha_reader.h"
View
@@ -32,6 +32,17 @@ set -eu
TZ=Europe/London
export TZ
+# When listing files, the list output (eg. from 'lha l') can vary
+# depending on the date. Files less than 6 months old show the time in
+# addition to the date, whereas after 6 months this is replaced by the
+# year instead for disambiguation reasons.
+# Unfortunately this functionality means that some tests can break as
+# the date changes. So run the tests using a fixed date (2012-05-01)
+# for repeatability.
+
+TEST_NOW_TIME=1335830400
+export TEST_NOW_TIME
+
# Expected result of invoking the lha command.
SUCCESS_EXPECTED=true

0 comments on commit 3604141

Please sign in to comment.