1/*-
2 * Copyright (c) 2012 Dag-Erling Sm��rgrav
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $Id: t_main.c 651 2013-03-05 18:11:59Z des $
30 */
31
32#ifdef HAVE_CONFIG_H
33# include "config.h"
34#endif
35
36#include <err.h>
37#include <stdarg.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <syslog.h>
42#include <unistd.h>
43
44#include "t.h"
45
46const char *t_progname;
47
48static int verbose;
49
50void
51t_verbose(const char *fmt, ...)
52{
53	va_list ap;
54
55	if (verbose) {
56		va_start(ap, fmt);
57		vfprintf(stderr, fmt, ap);
58		va_end(ap);
59	}
60}
61
62static void
63usage(void)
64{
65
66	fprintf(stderr, "usage: %s [-v]\n", t_progname);
67	exit(1);
68}
69
70int
71main(int argc, char *argv[])
72{
73	const struct t_test **t_plan;
74	const char *desc;
75	int n, pass, fail;
76	int opt;
77
78#ifdef HAVE_SETLOGMASK
79	/* suppress openpam_log() */
80	setlogmask(LOG_UPTO(0));
81#endif
82
83	/* clean up temp files in case of premature exit */
84	atexit(t_fcloseall);
85
86	if ((t_progname = strrchr(argv[0], '/')) != NULL)
87		t_progname++; /* one past the slash */
88	else
89		t_progname = argv[0];
90
91	while ((opt = getopt(argc, argv, "v")) != -1)
92		switch (opt) {
93		case 'v':
94			verbose = 1;
95			break;
96		default:
97			usage();
98		}
99
100	argc -= optind;
101	argv += optind;
102
103	/* prepare the test plan */
104	if ((t_plan = t_prepare(argc, argv)) == NULL)
105		errx(1, "no plan\n");
106
107	/* count the tests */
108	for (n = 0; t_plan[n] != NULL; ++n)
109		/* nothing */;
110	printf("1..%d\n", n);
111
112	/* run the tests */
113	for (n = pass = fail = 0; t_plan[n] != NULL; ++n) {
114		desc = t_plan[n]->desc ? t_plan[n]->desc : "no description";
115		if ((*t_plan[n]->func)(t_plan[n]->arg)) {
116			printf("ok %d - %s\n", n + 1, desc);
117			++pass;
118		} else {
119			printf("not ok %d - %s\n", n + 1, desc);
120			++fail;
121		}
122	}
123
124	/* clean up and exit */
125	t_cleanup();
126	exit(fail > 0 ? 1 : 0);
127}
128