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 CB996431FC0
\r
6 for <notmuch@notmuchmail.org>; Sun, 21 Oct 2012 14:22:50 -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 B9hjXVpsoJLg for <notmuch@notmuchmail.org>;
\r
17 Sun, 21 Oct 2012 14:22:47 -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 9DDAA431FD0
\r
22 for <notmuch@notmuchmail.org>; Sun, 21 Oct 2012 14:22:46 -0700 (PDT)
\r
23 Received: by mail-lb0-f181.google.com with SMTP id gg6so1515820lbb.26
\r
24 for <notmuch@notmuchmail.org>; Sun, 21 Oct 2012 14:22:46 -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=fozFlLFWYRPIq5jUxgriYDkFz2iAlH550DL/Poy0uz8=;
\r
31 b=PVVF0NdjhipnClVHeJ8KfkrcsFtnoT8BjIWz2vtySKta3LdIsLxPENKzcolFJfwiTS
\r
32 SZFhkGFnRu2th/P+nYypDmMkLY+VBaojd1vCtQjUQnEzFHzNStPKulKb7N09mNiFB97I
\r
33 rm6Dp4cqChOlFrny0Iuj9T7KFO52zyvLPGLLl+WloIAlFY9YOi/jO8jpwHK9Bu+d8Yvk
\r
34 Lx3kIuWrJ6ampwAtIlH6rf1B1Mwsc3t9uvVLE9BRgEW8/aIToSztUqxfRxycld4STAJD
\r
35 hODHAGpQgCqco4+U+iL8tNiZNCgpYY22exSk7mLAyb0duzr3JkRsxB2ht7eGsResC7Uw
\r
37 Received: by 10.112.101.10 with SMTP id fc10mr2912074lbb.82.1350854566216;
\r
38 Sun, 21 Oct 2012 14:22:46 -0700 (PDT)
\r
39 Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.
\r
41 by mx.google.com with ESMTPS id oj5sm2417681lab.8.2012.10.21.14.22.44
\r
42 (version=SSLv3 cipher=OTHER); Sun, 21 Oct 2012 14:22:45 -0700 (PDT)
\r
43 From: Jani Nikula <jani@nikula.org>
\r
44 To: notmuch@notmuchmail.org
\r
46 [PATCH v5 3/9] test: add new test tool parse-time for date/time parser
\r
47 Date: Mon, 22 Oct 2012 00:22:26 +0300
\r
49 <75a8f129d5e0d824b3e04ddfc1816c45fa0ec70d.1350854171.git.jani@nikula.org>
\r
50 X-Mailer: git-send-email 1.7.10.4
\r
51 In-Reply-To: <cover.1350854171.git.jani@nikula.org>
\r
52 References: <cover.1350854171.git.jani@nikula.org>
\r
53 In-Reply-To: <cover.1350854171.git.jani@nikula.org>
\r
54 References: <cover.1350854171.git.jani@nikula.org>
\r
56 Content-Type: text/plain; charset=UTF-8
\r
57 Content-Transfer-Encoding: 8bit
\r
59 ALoCoQmOzixYrVfDjuQ7k4hg2i7RF4hyj/xUZhAGUulVcnYqHlCx3j16hV2RTX8+aR/6lWvhTXsV
\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: Sun, 21 Oct 2012 21:22:51 -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 | 281 +++++++++++++++++++++++++++++++++++++++++++++++++++
\r
83 3 files changed, 288 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..5f73b85
\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
162 + * concat argv[start]...argv[end - 1], separating them by a single
\r
163 + * space, to a malloced string
\r
166 +concat_args (int start, int end, char *argv[])
\r
172 + for (i = start; i < end; i++)
\r
173 + len += strlen (argv[i]) + 1;
\r
175 + p = malloc (len);
\r
181 + for (i = start; i < end; i++) {
\r
184 + strcat (p, argv[i]);
\r
190 +#define DEFAULT_FORMAT "%a %b %d %T %z %Y"
\r
193 +usage (const char *name)
\r
195 + printf ("Usage: %s [options ...] [<date/time>]\n\n", name);
\r
197 + "Parse <date/time> and display it in given format. If <date/time> is\n"
\r
198 + "not given, parse each line in stdin according to:\n\n"
\r
199 + " <date/time> [(==>|==_>|==^>|==^^>)<ignored>] [#<comment>]\n\n"
\r
200 + "and produce output:\n\n"
\r
201 + " <date/time> (==>|==_>|==^>|==^^>) <time in --format=FMT> [#<comment>]\n\n"
\r
202 + "preserving whitespace and comment in input. The operators ==>, ==_>,\n"
\r
203 + "==^>, and ==^^> define rounding as no rounding, round down, round up\n"
\r
204 + "inclusive, and round up, respectively.\n\n"
\r
206 + " -f, --format=FMT output format, FMT according to strftime(3)\n"
\r
207 + " (default: \"%s\")\n"
\r
208 + " -r, --ref=N use N seconds since epoch as reference time\n"
\r
209 + " (default: now)\n"
\r
210 + " -u, --^ round result up inclusive (default: no rounding)\n"
\r
211 + " -U, --^^ round result up (default: no rounding)\n"
\r
212 + " -d, --_ round result down (default: no rounding)\n"
\r
213 + " -h, --help print this help\n",
\r
218 + const char *operator;
\r
221 + { "==>", PARSE_TIME_NO_ROUND },
\r
222 + { "==_>", PARSE_TIME_ROUND_DOWN },
\r
223 + { "==^>", PARSE_TIME_ROUND_UP_INCLUSIVE },
\r
224 + { "==^^>", PARSE_TIME_ROUND_UP },
\r
227 +static const char *
\r
228 +find_operator_in_string (char *str, char **ptr, int *round)
\r
230 + const char *oper = NULL;
\r
233 + for (i = 0; i < ARRAY_SIZE (operators); i++) {
\r
234 + char *p = strstr (str, operators[i].operator);
\r
237 + *round = operators[i].round;
\r
241 + oper = operators[i].operator;
\r
249 +static const char *
\r
250 +get_operator (int round)
\r
252 + const char *oper = NULL;
\r
255 + for (i = 0; i < ARRAY_SIZE(operators); i++) {
\r
256 + if (round == operators[i].round) {
\r
257 + oper = operators[i].operator;
\r
266 +parse_stdin (FILE *infile, time_t *ref, int round, const char *format)
\r
268 + char *input = NULL;
\r
269 + char result[1024];
\r
270 + size_t inputsize;
\r
276 + while ((len = getline (&input, &inputsize, infile)) != -1) {
\r
277 + const char *oper;
\r
278 + char *trail, *tmp;
\r
280 + /* trail is trailing whitespace and (optional) comment */
\r
281 + trail = strchr (input, '#');
\r
283 + trail = input + len;
\r
285 + while (trail > input && isspace ((unsigned char) *(trail-1)))
\r
288 + if (trail == input) {
\r
289 + printf ("%s", input);
\r
293 + tmp = strdup (trail);
\r
295 + fprintf (stderr, "strdup() failed\n");
\r
302 + oper = find_operator_in_string (input, &tmp, &round);
\r
306 + oper = get_operator (round);
\r
310 + r = parse_time_string (input, &t, ref, round);
\r
312 + if (!localtime_r (&t, &tm)) {
\r
313 + fprintf (stderr, "localtime_r() failed\n");
\r
318 + strftime (result, sizeof (result), format, &tm);
\r
320 + snprintf (result, sizeof (result), "ERROR: %d", r);
\r
323 + printf ("%s%s %s%s", input, oper, result, trail);
\r
333 +main (int argc, char *argv[])
\r
339 + time_t *nowp = NULL;
\r
341 + int round = PARSE_TIME_NO_ROUND;
\r
343 + const char *format = DEFAULT_FORMAT;
\r
344 + struct option options[] = {
\r
345 + { "help", no_argument, NULL, 'h' },
\r
346 + { "^", no_argument, NULL, 'u' },
\r
347 + { "^^", no_argument, NULL, 'U' },
\r
348 + { "_", no_argument, NULL, 'd' },
\r
349 + { "format", required_argument, NULL, 'f' },
\r
350 + { "ref", required_argument, NULL, 'r' },
\r
351 + { NULL, 0, NULL, 0 },
\r
357 + c = getopt_long (argc, argv, "huUdf:r:", options, NULL);
\r
363 + /* output format */
\r
367 + round = PARSE_TIME_ROUND_UP_INCLUSIVE;
\r
370 + round = PARSE_TIME_ROUND_UP;
\r
373 + round = PARSE_TIME_ROUND_DOWN;
\r
376 + /* specify now in seconds since epoch */
\r
377 + now = (time_t) strtol (optarg, NULL, 10);
\r
378 + if (now >= (time_t) 0)
\r
389 + if (optind == argc)
\r
390 + return parse_stdin (stdin, nowp, round, format);
\r
392 + argstr = concat_args (optind, argc, argv);
\r
396 + r = parse_time_string (argstr, &result, nowp, round);
\r
403 + if (!localtime_r (&result, &tm))
\r
406 + strftime (buf, sizeof (buf), format, &tm);
\r
407 + printf ("%s\n", buf);
\r