Commit 24908e18 authored by Rusty Russell's avatar Rusty Russell

ccanlint: use up to three -v to mean more verbosity.

eg. -v dumps percentage of coverage, -vv dumps per-line data, -vvv dumps every command executed.
parent 501e1920
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
#include <ccan/str_talloc/str_talloc.h> #include <ccan/str_talloc/str_talloc.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
static unsigned int verbose = 0; unsigned int verbose = 0;
static LIST_HEAD(compulsory_tests); static LIST_HEAD(compulsory_tests);
static LIST_HEAD(normal_tests); static LIST_HEAD(normal_tests);
static LIST_HEAD(finished_tests); static LIST_HEAD(finished_tests);
...@@ -42,7 +42,7 @@ static unsigned int timeout; ...@@ -42,7 +42,7 @@ static unsigned int timeout;
static void usage(const char *name) static void usage(const char *name)
{ {
fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-t <ms>] [-d <dirname>] [-x <tests>] [-k <test>]*\n" fprintf(stderr, "Usage: %s [-s] [-n] [-v] [-t <ms>] [-d <dirname>] [-x <tests>] [-k <test>]*\n"
" -v: verbose mode\n" " -v: verbose mode (can specify more than once)\n"
" -s: simply give one line summary\n" " -s: simply give one line summary\n"
" -d: use this directory instead of the current one\n" " -d: use this directory instead of the current one\n"
" -n: do not compile anything\n" " -n: do not compile anything\n"
...@@ -106,7 +106,7 @@ static bool run_test(struct ccanlint *i, ...@@ -106,7 +106,7 @@ static bool run_test(struct ccanlint *i,
unsigned int this_score, timeleft; unsigned int this_score, timeleft;
const struct dependent *d; const struct dependent *d;
const char *skip; const char *skip;
bool failed; bool bad, good;
//one less test to run through //one less test to run through
list_for_each(&i->dependencies, d, node) list_for_each(&i->dependencies, d, node)
...@@ -146,10 +146,12 @@ static bool run_test(struct ccanlint *i, ...@@ -146,10 +146,12 @@ static bool run_test(struct ccanlint *i,
else else
this_score = 0; this_score = 0;
failed = (this_score == 0); bad = (this_score == 0);
good = (this_score >= i->total_score);
if (verbose) { if (verbose || (bad && !quiet)) {
printf(" %s: %s", i->name, failed ? "FAIL" : "OK"); printf(" %s: %s", i->name,
bad ? "FAIL" : good ? "PASS" : "PARTIAL");
if (i->total_score) if (i->total_score)
printf(" (+%u/%u)", printf(" (+%u/%u)",
this_score, i->total_score); this_score, i->total_score);
...@@ -157,8 +159,9 @@ static bool run_test(struct ccanlint *i, ...@@ -157,8 +159,9 @@ static bool run_test(struct ccanlint *i,
} }
if (!quiet && result) { if (!quiet && result) {
if (i->describe && (failed || verbose)) const char *desc;
printf(" %s\n", i->describe(m, result)); if (i->describe && (desc = i->describe(m, result)) != NULL)
printf(" %s\n", desc);
if (i->handle) if (i->handle)
i->handle(m, result); i->handle(m, result);
} }
...@@ -171,14 +174,14 @@ static bool run_test(struct ccanlint *i, ...@@ -171,14 +174,14 @@ static bool run_test(struct ccanlint *i,
list_del(&i->list); list_del(&i->list);
list_add_tail(&finished_tests, &i->list); list_add_tail(&finished_tests, &i->list);
if (failed) { if (bad) {
/* Skip any tests which depend on this one. */ /* Skip any tests which depend on this one. */
list_for_each(&i->dependencies, d, node) { list_for_each(&i->dependencies, d, node) {
d->dependent->skip = true; d->dependent->skip = true;
d->dependent->skip_fail = true; d->dependent->skip_fail = true;
} }
} }
return !failed; return good;
} }
static void register_test(struct list_head *h, struct ccanlint *test, ...) static void register_test(struct list_head *h, struct ccanlint *test, ...)
...@@ -387,12 +390,19 @@ int main(int argc, char *argv[]) ...@@ -387,12 +390,19 @@ int main(int argc, char *argv[])
if (optind < argc) if (optind < argc)
usage(argv[0]); usage(argv[0]);
if (verbose >= 3)
tools_verbose = true;
/* We move into temporary directory, so gcov dumps its files there. */ /* We move into temporary directory, so gcov dumps its files there. */
if (chdir(temp_dir(talloc_autofree_context())) != 0) if (chdir(temp_dir(talloc_autofree_context())) != 0)
err(1, "Error changing to %s temporary dir", temp_dir(NULL)); err(1, "Error changing to %s temporary dir", temp_dir(NULL));
m = get_manifest(talloc_autofree_context(), dir); m = get_manifest(talloc_autofree_context(), dir);
/* Create a symlink from temp dir back to src dir's test directory. */
symlink(talloc_asprintf(m, "%s/test", dir),
talloc_asprintf(m, "%s/test", temp_dir(NULL)));
/* If you don't pass the compulsory tests, you don't even get a score */ /* If you don't pass the compulsory tests, you don't even get a score */
if (verbose) if (verbose)
printf("Compulsory tests:\n"); printf("Compulsory tests:\n");
......
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
#define REGISTER_TEST(name, ...) #define REGISTER_TEST(name, ...)
/* 1 == Describe results for partial failures.
2 == Describe gory details.
3 == Describe every action. */
extern unsigned int verbose;
struct manifest { struct manifest {
char *dir; char *dir;
/* The module name, ie. final element of dir name */ /* The module name, ie. final element of dir name */
......
...@@ -51,7 +51,8 @@ static const char *describe_has_tests(struct manifest *m, void *check_result) ...@@ -51,7 +51,8 @@ static const char *describe_has_tests(struct manifest *m, void *check_result)
"warnings, and then run: it is expected to use libtap to report its\n" "warnings, and then run: it is expected to use libtap to report its\n"
"results in a simple and portable format. It should #include the C\n" "results in a simple and portable format. It should #include the C\n"
"files from the module directly (so it can probe the internals): the\n" "files from the module directly (so it can probe the internals): the\n"
"module will not be linked in.\n\n" "module will not be linked in. The test will be run in a temporary\n"
"directory, with the test directory symlinked under test/.\n\n"
"api tests are just like a run test, except it is a guarantee of API\n" "api tests are just like a run test, except it is a guarantee of API\n"
"stability: this test should pass on all future versions of the\n" "stability: this test should pass on all future versions of the\n"
......
...@@ -35,15 +35,6 @@ static void *do_run_tests(struct manifest *m, ...@@ -35,15 +35,6 @@ static void *do_run_tests(struct manifest *m,
struct run_tests_result *res; struct run_tests_result *res;
struct ccan_file *i; struct ccan_file *i;
char *cmdout; char *cmdout;
char *olddir;
/* We run tests in the module directory, so any paths
* referenced can all be module-local. */
olddir = talloc_getcwd(m);
if (!olddir)
err(1, "Could not save cwd");
if (chdir(m->dir) != 0)
err(1, "Could not chdir to %s", m->dir);
list_head_init(list); list_head_init(list);
...@@ -74,9 +65,6 @@ static void *do_run_tests(struct manifest *m, ...@@ -74,9 +65,6 @@ static void *do_run_tests(struct manifest *m,
list = NULL; list = NULL;
} }
if (chdir(olddir) != 0)
err(1, "Could not chdir to %s", olddir);
return list; return list;
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <tools/tools.h> #include <tools/tools.h>
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/str_talloc/str_talloc.h> #include <ccan/str_talloc/str_talloc.h>
#include <ccan/grab_file/grab_file.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -18,7 +19,7 @@ ...@@ -18,7 +19,7 @@
struct coverage_result { struct coverage_result {
float uncovered; float uncovered;
const char *what; const char *what;
const char *output; char *output;
}; };
static bool find_source_file(struct manifest *m, const char *filename) static bool find_source_file(struct manifest *m, const char *filename)
...@@ -38,7 +39,8 @@ static bool find_source_file(struct manifest *m, const char *filename) ...@@ -38,7 +39,8 @@ static bool find_source_file(struct manifest *m, const char *filename)
/* FIXME: Don't know how stable this is. Read cov files directly? */ /* FIXME: Don't know how stable this is. Read cov files directly? */
static void analyze_coverage(struct manifest *m, static void analyze_coverage(struct manifest *m,
struct coverage_result *res, const char *output) struct coverage_result *res, const char *output,
bool full_gcov)
{ {
char **lines = strsplit(res, output, "\n", NULL); char **lines = strsplit(res, output, "\n", NULL);
float covered_lines = 0.0; float covered_lines = 0.0;
...@@ -49,6 +51,7 @@ static void analyze_coverage(struct manifest *m, ...@@ -49,6 +51,7 @@ static void analyze_coverage(struct manifest *m,
Output looks like: Output looks like:
File '../../../ccan/tdb2/private.h' File '../../../ccan/tdb2/private.h'
Lines executed:0.00% of 8 Lines executed:0.00% of 8
/home/ccan/ccan/tdb2/test/run-simple-delete.c:creating 'run-simple-delete.c.gcov'
File '../../../ccan/tdb2/tdb.c' File '../../../ccan/tdb2/tdb.c'
Lines executed:0.00% of 450 Lines executed:0.00% of 450
...@@ -71,6 +74,28 @@ static void analyze_coverage(struct manifest *m, ...@@ -71,6 +74,28 @@ static void analyze_coverage(struct manifest *m,
errx(1, "Could not parse line '%s'", lines[i]); errx(1, "Could not parse line '%s'", lines[i]);
total_lines += of; total_lines += of;
covered_lines += ex / 100.0 * of; covered_lines += ex / 100.0 * of;
} else if (full_gcov && strstr(lines[i], ":creating '")) {
char *file, *filename, *apostrophe;
apostrophe = strchr(lines[i], '\'');
filename = apostrophe + 1;
apostrophe = strchr(filename, '\'');
*apostrophe = '\0';
if (lines_matter) {
file = grab_file(res, filename, NULL);
if (!file) {
res->what = talloc_asprintf(res,
"Reading %s",
filename);
res->output = talloc_strdup(res,
strerror(errno));
return;
}
res->output = talloc_append_string(res->output,
file);
}
if (tools_verbose)
printf("Unlinking %s", filename);
unlink(filename);
} }
} }
...@@ -88,24 +113,18 @@ static void *do_run_coverage_tests(struct manifest *m, ...@@ -88,24 +113,18 @@ static void *do_run_coverage_tests(struct manifest *m,
struct coverage_result *res; struct coverage_result *res;
struct ccan_file *i; struct ccan_file *i;
char *cmdout; char *cmdout;
char *olddir;
char *covcmd; char *covcmd;
bool ok; bool ok;
bool full_gcov = (verbose > 1);
/* We run tests in the module directory, so any paths
* referenced can all be module-local. */
olddir = talloc_getcwd(m);
if (!olddir)
err(1, "Could not save cwd");
if (chdir(m->dir) != 0)
err(1, "Could not chdir to %s", m->dir);
res = talloc(m, struct coverage_result); res = talloc(m, struct coverage_result);
res->what = NULL; res->what = NULL;
res->output = talloc_strdup(res, "");
res->uncovered = 1.0; res->uncovered = 1.0;
/* This tells gcov where we put those .gcno files. */ /* This tells gcov where we put those .gcno files. */
covcmd = talloc_asprintf(m, "gcov -n -o %s", covcmd = talloc_asprintf(m, "gcov %s -o %s",
full_gcov ? "" : "-n",
talloc_dirname(res, m->info_file->compiled)); talloc_dirname(res, m->info_file->compiled));
/* Run them all. */ /* Run them all. */
...@@ -116,7 +135,7 @@ static void *do_run_coverage_tests(struct manifest *m, ...@@ -116,7 +135,7 @@ static void *do_run_coverage_tests(struct manifest *m,
res->output = talloc_steal(res, cmdout); res->output = talloc_steal(res, cmdout);
return res; return res;
} }
covcmd = talloc_asprintf_append(covcmd, " %s", i->name); covcmd = talloc_asprintf_append(covcmd, " %s", i->fullname);
} }
list_for_each(&m->api_tests, i, list) { list_for_each(&m->api_tests, i, list) {
...@@ -126,7 +145,7 @@ static void *do_run_coverage_tests(struct manifest *m, ...@@ -126,7 +145,7 @@ static void *do_run_coverage_tests(struct manifest *m,
res->output = talloc_steal(res, cmdout); res->output = talloc_steal(res, cmdout);
return res; return res;
} }
covcmd = talloc_asprintf_append(covcmd, " %s", i->name); covcmd = talloc_asprintf_append(covcmd, " %s", i->fullname);
} }
/* Now run gcov: we want output even if it succeeds. */ /* Now run gcov: we want output even if it succeeds. */
...@@ -137,7 +156,8 @@ static void *do_run_coverage_tests(struct manifest *m, ...@@ -137,7 +156,8 @@ static void *do_run_coverage_tests(struct manifest *m,
return res; return res;
} }
analyze_coverage(m, res, cmdout); analyze_coverage(m, res, cmdout, full_gcov);
return res; return res;
} }
...@@ -161,12 +181,20 @@ static const char *describe_run_coverage_tests(struct manifest *m, ...@@ -161,12 +181,20 @@ static const char *describe_run_coverage_tests(struct manifest *m,
void *check_result) void *check_result)
{ {
struct coverage_result *res = check_result; struct coverage_result *res = check_result;
bool full_gcov = (verbose > 1);
char *ret;
if (res->what) if (res->what)
return talloc_asprintf(m, "%s: %s", res->what, res->output); return talloc_asprintf(m, "%s: %s", res->what, res->output);
return talloc_asprintf(m, "Tests achieved %0.2f%% coverage", if (!verbose)
(1.0 - res->uncovered) * 100); return NULL;
ret = talloc_asprintf(m, "Tests achieved %0.2f%% coverage",
(1.0 - res->uncovered) * 100);
if (full_gcov)
ret = talloc_asprintf_append(ret, "\n%s", res->output);
return ret;
} }
struct ccanlint run_coverage_tests = { struct ccanlint run_coverage_tests = {
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <ccan/talloc/talloc.h> #include <ccan/talloc/talloc.h>
#include <ccan/str/str.h> #include <ccan/str/str.h>
/* FIXME: only print full analysis if verbose >= 2. */
static char *report_on_trailing_whitespace(const char *line) static char *report_on_trailing_whitespace(const char *line)
{ {
const char *e = strchr(line, 0); const char *e = strchr(line, 0);
...@@ -36,6 +37,8 @@ static void *check_trailing_whitespace(struct manifest *m, ...@@ -36,6 +37,8 @@ static void *check_trailing_whitespace(struct manifest *m,
static const char *describe_trailing_whitespace(struct manifest *m, static const char *describe_trailing_whitespace(struct manifest *m,
void *check_result) void *check_result)
{ {
if (!verbose)
return NULL;
return talloc_asprintf(check_result, return talloc_asprintf(check_result,
"Some source files have trailing whitespace:\n" "Some source files have trailing whitespace:\n"
"%s", (char *)check_result); "%s", (char *)check_result);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment