Merge r368,496,625,626 from libarchive.googlecode.com: A number of

style and portability tweaks to the test harness.  Most significantly,
don't use getopt().
This commit is contained in:
Tim Kientzle 2009-03-08 05:38:45 +00:00
parent 97c382f46b
commit 503c6aa1ca
2 changed files with 83 additions and 36 deletions

View File

@ -56,7 +56,11 @@ __FBSDID("$FreeBSD$");
*/
#undef DEFINE_TEST
#define DEFINE_TEST(name) void name(void);
#ifdef LIST_H
#include LIST_H
#else
#include "list.h"
#endif
/* Interix doesn't define these in a standard header. */
#if __INTERIX__
@ -80,7 +84,7 @@ static int skips = 0;
static int assertions = 0;
/* Directory where uuencoded reference files can be found. */
static char *refdir;
static const char *refdir;
/*
* My own implementation of the standard assert() macro emits the
@ -476,7 +480,9 @@ test_assert_empty_file(const char *f1fmt, ...)
va_end(ap);
if (stat(f1, &st) != 0) {
fprintf(stderr, "%s:%d: Could not stat: %s\n", test_filename, test_line, f1);
fprintf(stderr, "%s:%d: Could not stat: %s\n",
test_filename, test_line, f1);
failures ++;
report_failure(NULL);
return (0);
}
@ -519,6 +525,7 @@ test_assert_non_empty_file(const char *f1fmt, ...)
fprintf(stderr, "%s:%d: Could not stat: %s\n",
test_filename, test_line, f1);
report_failure(NULL);
failures++;
return (0);
}
if (st.st_size != 0)
@ -630,6 +637,15 @@ test_assert_file_contents(const void *buff, int s, const char *fpattern, ...)
va_end(ap);
fd = open(f, O_RDONLY);
if (fd < 0) {
failures ++;
if (!previous_failures(test_filename, test_line)) {
fprintf(stderr, "%s:%d: File doesn't exist: %s\n",
test_filename, test_line, f);
report_failure(test_extra);
}
return (0);
}
contents = malloc(s * 2);
n = read(fd, contents, s * 2);
if (n == s && memcmp(buff, contents, s) == 0) {
@ -731,7 +747,11 @@ slurpfile(size_t * sizep, const char *fmt, ...)
#undef DEFINE_TEST
#define DEFINE_TEST(n) { n, #n },
struct { void (*func)(void); const char *name; } tests[] = {
#ifdef LIST_H
#include LIST_H
#else
#include "list.h"
#endif
};
/*
@ -881,10 +901,12 @@ int main(int argc, char **argv)
int i, tests_run = 0, tests_failed = 0, opt;
time_t now;
char *refdir_alloc = NULL;
char *progname, *p;
const char *opt_arg, *progname, *p;
char tmpdir[256];
char tmpdir_timestamp[256];
(void)argc; /* UNUSED */
/*
* Name of this program, used to build root of our temp directory
* tree.
@ -909,39 +931,62 @@ int main(int argc, char **argv)
refdir = getenv(ENVBASE "_TEST_FILES");
/*
* Parse options.
* Parse options, without using getopt(), which isn't available
* on all platforms.
*/
while ((opt = getopt(argc, argv, "dkp:qr:v")) != -1) {
switch (opt) {
case 'd':
dump_on_failure = 1;
++argv; /* Skip program name */
while (*argv != NULL) {
if (**argv != '-')
break;
case 'k':
keep_temp_files = 1;
break;
case 'p':
p = *argv++;
++p; /* Skip '-' */
while (*p != '\0') {
opt = *p++;
opt_arg = NULL;
/* If 'opt' takes an argument, parse that. */
if (opt == 'p' || opt == 'r') {
if (*p != '\0')
opt_arg = p;
else if (*argv == NULL) {
fprintf(stderr,
"Option -%c requires argument.\n",
opt);
usage(progname);
} else
opt_arg = *argv++;
p = ""; /* End of this option word. */
}
/* Now, handle the option. */
switch (opt) {
case 'd':
dump_on_failure = 1;
break;
case 'k':
keep_temp_files = 1;
break;
case 'p':
#ifdef PROGRAM
testprog = optarg;
testprog = opt_arg;
#else
usage(progname);
usage(progname);
#endif
break;
case 'q':
quiet_flag++;
break;
case 'r':
refdir = optarg;
break;
case 'v':
verbose = 1;
break;
case '?':
default:
usage(progname);
break;
case 'q':
quiet_flag++;
break;
case 'r':
refdir = opt_arg;
break;
case 'v':
verbose = 1;
break;
case '?':
default:
usage(progname);
}
}
}
argc -= optind;
argv += optind;
/*
* Sanity-check that our options make sense.
@ -976,12 +1021,14 @@ int main(int argc, char **argv)
* reference files, use the current directory for that.
*/
if (refdir == NULL) {
char *q;
systemf("/bin/pwd > %s/refdir", tmpdir);
refdir = refdir_alloc = slurpfile(NULL, "%s/refdir", tmpdir);
p = refdir + strlen(refdir);
while (p[-1] == '\n') {
--p;
*p = '\0';
q = slurpfile(NULL, "%s/refdir", tmpdir);
refdir = refdir_alloc = q;
q += strlen(refdir);
while (q[-1] == '\n') {
--q;
*q = '\0';
}
systemf("rm %s/refdir", tmpdir);
}
@ -1003,7 +1050,7 @@ int main(int argc, char **argv)
/*
* Run some or all of the individual tests.
*/
if (argc == 0) {
if (*argv == NULL) {
/* Default: Run all tests. */
for (i = 0; i < limit; i++) {
if (test_run(i, tmpdir))

View File

@ -151,4 +151,4 @@ void extract_reference_file(const char *);
*/
/* Pathname of exe to be tested. */
char *testprog;
const char *testprog;