1 Return-Path: <jani@nikula.org>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 9E5C6431FD9
\r
6 for <notmuch@notmuchmail.org>; Tue, 30 Oct 2012 13:33:15 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"
\r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5
\r
13 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id 8PnWgvqyyK22 for <notmuch@notmuchmail.org>;
\r
17 Tue, 30 Oct 2012 13:33:12 -0700 (PDT)
\r
18 Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com
\r
19 [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id 81F8C431FDC
\r
22 for <notmuch@notmuchmail.org>; Tue, 30 Oct 2012 13:32:56 -0700 (PDT)
\r
23 Received: by mail-lb0-f181.google.com with SMTP id gg6so567600lbb.26
\r
24 for <notmuch@notmuchmail.org>; Tue, 30 Oct 2012 13:32:56 -0700 (PDT)
\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
\r
26 d=google.com; s=20120113;
\r
27 h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references
\r
28 :in-reply-to:references:mime-version:content-type
\r
29 :content-transfer-encoding:x-gm-message-state;
\r
30 bh=vMILaKO6smR7vl2i4sM9jBz5pOCabcqzPmPfd6V5VVo=;
\r
31 b=RHK+BGp5teXvSQFmYinkRREeL/kiCl5w7i6W4rmmlLSi4dNnbYs5mqLa4n1WV4tnE+
\r
32 a2XPwhExvdsQIrelXS+yH69QIIDQs1MerHUE+U24ZomDINw3E2MLDC19EAKlzkG33+rm
\r
33 sEDQA/HKurUdAHuFu23Qpe+z0XdLPICsADhOD3GtRHOlhflfaXLD+ya0JVLtOcmf1JEL
\r
34 wGF1494vKs2jLH3QpBT/pYYiX+6Z7eYrPLhxZCRW2kigT2hgDV25xmbIdDnXFuoQBZpd
\r
35 NfuTwkNqcE2ZfpIabVG3ly6G1Ka8BuN1G+e9VBcKVBUBp4G9XjXKjLG+3hPFgrZRjuTn
\r
37 Received: by 10.152.104.148 with SMTP id ge20mr30985253lab.51.1351629175980;
\r
38 Tue, 30 Oct 2012 13:32:55 -0700 (PDT)
\r
39 Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.
\r
41 by mx.google.com with ESMTPS id jk8sm665831lab.7.2012.10.30.13.32.54
\r
42 (version=SSLv3 cipher=OTHER); Tue, 30 Oct 2012 13:32:55 -0700 (PDT)
\r
43 From: Jani Nikula <jani@nikula.org>
\r
44 To: notmuch@notmuchmail.org
\r
46 [PATCH v6 3/9] test: add new test tool parse-time for date/time parser
\r
47 Date: Tue, 30 Oct 2012 22:32:34 +0200
\r
49 <fe26c805b9e26bba9d93f705539faf13c12eb019.1351626272.git.jani@nikula.org>
\r
50 X-Mailer: git-send-email 1.7.10.4
\r
51 In-Reply-To: <cover.1351626272.git.jani@nikula.org>
\r
52 References: <cover.1351626272.git.jani@nikula.org>
\r
53 In-Reply-To: <cover.1351626272.git.jani@nikula.org>
\r
54 References: <cover.1351626272.git.jani@nikula.org>
\r
56 Content-Type: text/plain; charset=UTF-8
\r
57 Content-Transfer-Encoding: 8bit
\r
59 ALoCoQlbgj4AxEGxVu2A+zxoD7Paw61REWkLR5YurUqXGNjrRYj/NYbWbgSjqzT2UJ7KHYgsEViN
\r
60 X-BeenThere: notmuch@notmuchmail.org
\r
61 X-Mailman-Version: 2.1.13
\r
63 List-Id: "Use and development of the notmuch mail system."
\r
64 <notmuch.notmuchmail.org>
\r
65 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
66 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
67 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
68 List-Post: <mailto:notmuch@notmuchmail.org>
\r
69 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
70 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
71 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
72 X-List-Received-Date: Tue, 30 Oct 2012 20:33:16 -0000
\r
74 Add a smoke testing tool to support testing the date/time parser
\r
75 module directly and independent of the rest of notmuch.
\r
77 Credits to Michal Sojka <sojkam1@fel.cvut.cz> for the stdin parsing
\r
78 idea and consequent massive improvement in testability.
\r
80 test/Makefile.local | 7 +-
\r
82 test/parse-time.c | 314 +++++++++++++++++++++++++++++++++++++++++++++++++++
\r
83 3 files changed, 321 insertions(+), 2 deletions(-)
\r
84 create mode 100644 test/parse-time.c
\r
86 diff --git a/test/Makefile.local b/test/Makefile.local
\r
87 index 45df4c7..9ae130a 100644
\r
88 --- a/test/Makefile.local
\r
89 +++ b/test/Makefile.local
\r
90 @@ -19,9 +19,13 @@ $(dir)/smtp-dummy: $(smtp_dummy_modules)
\r
91 $(dir)/symbol-test: $(dir)/symbol-test.o
\r
92 $(call quiet,CXX) $^ -o $@ -Llib -lnotmuch $(XAPIAN_LDFLAGS)
\r
94 +$(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o
\r
95 + $(call quiet,CC) $^ -o $@
\r
99 -test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test
\r
100 +test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test \
\r
101 + $(dir)/parse-time
\r
103 test: all test-binaries
\r
104 @${dir}/notmuch-test $(OPTIONS)
\r
105 @@ -32,4 +36,5 @@ SRCS := $(SRCS) $(smtp_dummy_srcs)
\r
106 CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \
\r
107 $(dir)/symbol-test $(dir)/symbol-test.o \
\r
108 $(dir)/arg-test $(dir)/arg-test.o \
\r
109 + $(dir)/parse-time $(dir)/parse-time.o \
\r
110 $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.*
\r
111 diff --git a/test/basic b/test/basic
\r
112 index 3b635c8..c47197c 100755
\r
115 @@ -54,7 +54,7 @@ test_begin_subtest 'Ensure that all available tests will be run by notmuch-test'
\r
116 eval $(sed -n -e '/^TESTS="$/,/^"$/p' $TEST_DIRECTORY/notmuch-test)
\r
117 tests_in_suite=$(for i in $TESTS; do echo $i; done | sort)
\r
118 available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111 | \
\r
119 - sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test)$/d" | \
\r
120 + sed -r -e "s,.*/,," -e "/^(aggregate-results.sh|notmuch-test|smtp-dummy|test-verbose|symbol-test|arg-test|parse-time)$/d" | \
\r
122 test_expect_equal "$tests_in_suite" "$available"
\r
124 diff --git a/test/parse-time.c b/test/parse-time.c
\r
125 new file mode 100644
\r
126 index 0000000..901a4dd
\r
128 +++ b/test/parse-time.c
\r
131 + * parse time string - user friendly date and time parser
\r
132 + * Copyright © 2012 Jani Nikula
\r
134 + * This program is free software: you can redistribute it and/or modify
\r
135 + * it under the terms of the GNU General Public License as published by
\r
136 + * the Free Software Foundation, either version 2 of the License, or
\r
137 + * (at your option) any later version.
\r
139 + * This program is distributed in the hope that it will be useful,
\r
140 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
141 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
142 + * GNU General Public License for more details.
\r
144 + * You should have received a copy of the GNU General Public License
\r
145 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
147 + * Author: Jani Nikula <jani@nikula.org>
\r
150 +#include <assert.h>
\r
151 +#include <ctype.h>
\r
152 +#include <getopt.h>
\r
153 +#include <stdio.h>
\r
154 +#include <stdlib.h>
\r
155 +#include <string.h>
\r
157 +#include "parse-time-string.h"
\r
159 +#define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
\r
161 +static const char *parse_time_error_strings[] = {
\r
162 + [PARSE_TIME_OK] = "OK",
\r
163 + [PARSE_TIME_ERR] = "ERR",
\r
164 + [PARSE_TIME_ERR_LIB] = "LIB",
\r
165 + [PARSE_TIME_ERR_ALREADYSET] = "ALREADYSET",
\r
166 + [PARSE_TIME_ERR_FORMAT] = "FORMAT",
\r
167 + [PARSE_TIME_ERR_DATEFORMAT] = "DATEFORMAT",
\r
168 + [PARSE_TIME_ERR_TIMEFORMAT] = "TIMEFORMAT",
\r
169 + [PARSE_TIME_ERR_INVALIDDATE] = "INVALIDDATE",
\r
170 + [PARSE_TIME_ERR_INVALIDTIME] = "INVALIDTIME",
\r
171 + [PARSE_TIME_ERR_KEYWORD] = "KEYWORD",
\r
174 +static const char *
\r
175 +parse_time_strerror (unsigned int errnum)
\r
177 + if (errnum < ARRAY_SIZE (parse_time_error_strings))
\r
178 + return parse_time_error_strings[errnum];
\r
184 + * concat argv[start]...argv[end - 1], separating them by a single
\r
185 + * space, to a malloced string
\r
188 +concat_args (int start, int end, char *argv[])
\r
194 + for (i = start; i < end; i++)
\r
195 + len += strlen (argv[i]) + 1;
\r
197 + p = malloc (len);
\r
203 + for (i = start; i < end; i++) {
\r
206 + strcat (p, argv[i]);
\r
212 +#define DEFAULT_FORMAT "%a %b %d %T %z %Y"
\r
215 +usage (const char *name)
\r
217 + printf ("Usage: %s [options ...] [<date/time>]\n\n", name);
\r
219 + "Parse <date/time> and display it in given format. If <date/time> is\n"
\r
220 + "not given, parse each line in stdin according to:\n\n"
\r
221 + " <date/time> [(==>|==_>|==^>|==^^>)<ignored>] [#<comment>]\n\n"
\r
222 + "and produce output:\n\n"
\r
223 + " <date/time> (==>|==_>|==^>|==^^>) <time in --format=FMT> [#<comment>]\n\n"
\r
224 + "preserving whitespace and comment in input. The operators ==>, ==_>,\n"
\r
225 + "==^>, and ==^^> define rounding as no rounding, round down, round up\n"
\r
226 + "inclusive, and round up, respectively.\n\n"
\r
228 + " -f, --format=FMT output format, FMT according to strftime(3)\n"
\r
229 + " (default: \"%s\")\n"
\r
230 + " -r, --ref=N use N seconds since epoch as reference time\n"
\r
231 + " (default: now)\n"
\r
232 + " -u, --^ round result up inclusive (default: no rounding)\n"
\r
233 + " -U, --^^ round result up (default: no rounding)\n"
\r
234 + " -d, --_ round result down (default: no rounding)\n"
\r
235 + " -h, --help print this help\n",
\r
240 + const char *operator;
\r
243 + { "==>", PARSE_TIME_NO_ROUND },
\r
244 + { "==_>", PARSE_TIME_ROUND_DOWN },
\r
245 + { "==^>", PARSE_TIME_ROUND_UP_INCLUSIVE },
\r
246 + { "==^^>", PARSE_TIME_ROUND_UP },
\r
249 +static const char *
\r
250 +find_operator_in_string (char *str, char **ptr, int *round)
\r
252 + const char *oper = NULL;
\r
255 + for (i = 0; i < ARRAY_SIZE (operators); i++) {
\r
256 + char *p = strstr (str, operators[i].operator);
\r
259 + *round = operators[i].round;
\r
263 + oper = operators[i].operator;
\r
271 +static const char *
\r
272 +get_operator (int round)
\r
274 + const char *oper = NULL;
\r
277 + for (i = 0; i < ARRAY_SIZE(operators); i++) {
\r
278 + if (round == operators[i].round) {
\r
279 + oper = operators[i].operator;
\r
288 +parse_stdin (FILE *infile, time_t *ref, int round, const char *format)
\r
290 + char *input = NULL;
\r
291 + char result[1024];
\r
292 + size_t inputsize;
\r
298 + while ((len = getline (&input, &inputsize, infile)) != -1) {
\r
299 + const char *oper;
\r
300 + char *trail, *tmp;
\r
302 + /* trail is trailing whitespace and (optional) comment */
\r
303 + trail = strchr (input, '#');
\r
305 + trail = input + len;
\r
307 + while (trail > input && isspace ((unsigned char) *(trail-1)))
\r
310 + if (trail == input) {
\r
311 + printf ("%s", input);
\r
315 + tmp = strdup (trail);
\r
317 + fprintf (stderr, "strdup() failed\n");
\r
324 + oper = find_operator_in_string (input, &tmp, &round);
\r
328 + oper = get_operator (round);
\r
332 + r = parse_time_string (input, &t, ref, round);
\r
334 + if (!localtime_r (&t, &tm)) {
\r
335 + fprintf (stderr, "localtime_r() failed\n");
\r
340 + strftime (result, sizeof (result), format, &tm);
\r
342 + const char *errstr = parse_time_strerror (r);
\r
344 + snprintf (result, sizeof (result), "ERROR: %s", errstr);
\r
346 + snprintf (result, sizeof (result), "ERROR: %d", r);
\r
349 + printf ("%s%s %s%s", input, oper, result, trail);
\r
359 +main (int argc, char *argv[])
\r
365 + time_t *nowp = NULL;
\r
367 + int round = PARSE_TIME_NO_ROUND;
\r
369 + const char *format = DEFAULT_FORMAT;
\r
370 + struct option options[] = {
\r
371 + { "help", no_argument, NULL, 'h' },
\r
372 + { "^", no_argument, NULL, 'u' },
\r
373 + { "^^", no_argument, NULL, 'U' },
\r
374 + { "_", no_argument, NULL, 'd' },
\r
375 + { "format", required_argument, NULL, 'f' },
\r
376 + { "ref", required_argument, NULL, 'r' },
\r
377 + { NULL, 0, NULL, 0 },
\r
383 + c = getopt_long (argc, argv, "huUdf:r:", options, NULL);
\r
389 + /* output format */
\r
393 + round = PARSE_TIME_ROUND_UP_INCLUSIVE;
\r
396 + round = PARSE_TIME_ROUND_UP;
\r
399 + round = PARSE_TIME_ROUND_DOWN;
\r
402 + /* specify now in seconds since epoch */
\r
403 + now = (time_t) strtol (optarg, NULL, 10);
\r
404 + if (now >= (time_t) 0)
\r
415 + if (optind == argc)
\r
416 + return parse_stdin (stdin, nowp, round, format);
\r
418 + argstr = concat_args (optind, argc, argv);
\r
422 + r = parse_time_string (argstr, &result, nowp, round);
\r
427 + const char *errstr = parse_time_strerror (r);
\r
429 + fprintf (stderr, "ERROR: %s\n", errstr);
\r
431 + fprintf (stderr, "ERROR: %d\n", r);
\r
436 + if (!localtime_r (&result, &tm))
\r
439 + strftime (buf, sizeof (buf), format, &tm);
\r
440 + printf ("%s\n", buf);
\r