Skip to content

Commit

Permalink
Merge pull request #90 from thoni56/master
Browse files Browse the repository at this point in the history
Change color handling to default to colors if in a tty, introduce -C to force no-colors
  • Loading branch information
thoni56 committed Aug 10, 2016
2 parents 3a6df90 + ed15eb6 commit bead2ca
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 40 deletions.
12 changes: 0 additions & 12 deletions config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,15 @@

/************************** HEADER FILES *************************/

/* Define to 1 if you have the <pty.h> header file. */
#cmakedefine HAVE_PTY_H 1

/*************************** FUNCTIONS ***************************/

/* Define to 1 if you have the `cfmakeraw' function. */
#cmakedefine HAVE_CFMAKERAW 1

/*************************** LIBRARIES ***************************/

/* Define to 1 if you have the `z' library (-lz). */
#cmakedefine HAVE_LIBZ 1

/**************************** OPTIONS ****************************/

/* Define to 1 if you want to enable ZLIB */
#cmakedefine WITH_LIBZ 1

/* Define to 1 if you want to compile for W32API instead of *nix */
#cmakedefine USE_W32API 1

/* Define if building with gcov instrumentation */
#cmakedefine CGREEN_INTERNAL_WITH_GCOV

7 changes: 4 additions & 3 deletions doc/cgreen-guide-en.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -2674,9 +2674,10 @@ and the error messages might not be straight forward to interpret.

|=========================================================
|*Compiler error message* |*Probable cause...*
|`"contextFor<X>" is undeclared here` |You forgot the `BeforeEach()` function
|`undefined reference to 'AfterEach_For_<X>'`|You forgot the `AfterEach()` function
|`CgreenSpec__<X>__<Y>__ is undeclared` |You forgot to specify the test subject/context in the `Ensure` of a BDD style test
|`"contextFor<X>" is undeclared here` | Missing `BeforeEach(<X>)`
|`undefined reference to 'AfterEach_For_<X>'`| Missing `AfterEach(<X>)`
|`CgreenSpec__<X>__<Y>__ is undeclared` | Missing test subject/context in the `Ensure` of a BDD style test
|`use of undeclared identifier 'contextFor<X>'` | Missing `Describe(<X>)`
|=========================================================


Expand Down
17 changes: 11 additions & 6 deletions src/xml_reporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static void xml_reporter_start_suite(TestReporter *reporter, const char *suitena
char filename[PATH_MAX];
int segment_decrementer = reporter->breadcrumb->depth;
XmlMemo *memo = (XmlMemo *)reporter->memo;
FILE *out;

(void)count; /* UNUSED */

Expand All @@ -121,11 +122,15 @@ static void xml_reporter_start_suite(TestReporter *reporter, const char *suitena
add_suite_name(suitename);

snprintf(filename, sizeof(filename), "%s-%s.xml", file_prefix, suite_path);
FILE *out = fopen(filename, "w");
if (!out) {
memo->printer(stderr, "could not open %s: %s\r\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
if (memo->printer == fprintf) {
// If we're really printing to files, then open one...
out = fopen(filename, "w");
if (!out) {
memo->printer(stderr, "could not open %s: %s\r\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
} else
out = stdout;

file_stack[file_stack_p++] = out;
memo->printer(out, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n");
Expand Down Expand Up @@ -156,7 +161,7 @@ static void xml_reporter_start_test(TestReporter *reporter, const char *testname


static char *concat(char *head, const char *tail) {
char *result = realloc(head, strlen(head)+strlen(tail));
char *result = realloc(head, strlen(head)+strlen(tail)+1);
return strcat(result, tail);
}

Expand Down
12 changes: 6 additions & 6 deletions tests/xml_reporter_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ Ensure(XmlReporter, will_report_beginning_of_suite) {


Ensure(XmlReporter, will_report_beginning_and_successful_finishing_of_passing_test) {
va_list arguments;
va_list null_arguments;
memset(&null_arguments, 0, sizeof(null_arguments));

reporter->start_test(reporter, "test_name");
assert_that(output, begins_with_string("<testcase classname=\"\""));
assert_that(output, contains_string("name=\"test_name\""));

clear_output();

memset(&arguments, 0, sizeof(va_list));
reporter->show_pass(reporter, "file", 2, "test_name", arguments);
reporter->show_pass(reporter, "file", 2, "test_name", null_arguments);
assert_that(strlen(output), is_equal_to(0));

reporter->finish_test(reporter, "filename", line, NULL, duration_in_milliseconds);
Expand All @@ -107,11 +107,11 @@ Ensure(XmlReporter, will_report_beginning_and_successful_finishing_of_passing_te


Ensure(XmlReporter, will_report_a_failing_test) {
va_list arguments;
va_list null_arguments;
memset(&null_arguments, 0, sizeof(null_arguments));

reporter->start_test(reporter, "test_name");
memset(&arguments, 0, sizeof(va_list));
reporter->show_fail(reporter, "file", 2, "test_name", arguments);
reporter->show_fail(reporter, "file", 2, "test_name", null_arguments);
reporter->finish_test(reporter, "filename", line, NULL, duration_in_milliseconds);

assert_that(output, contains_string("<failure message=\"test_name\">"));
Expand Down
42 changes: 29 additions & 13 deletions tools/cgreen-runner.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <cgreen/cgreen.h>
#include <cgreen/xml_reporter.h>

#include "utils.h"

#include <unistd.h>
Expand Down Expand Up @@ -32,15 +33,16 @@ static void usage(const char **argv) {
printf("dynamically loadable library.\n\n");
printf("A single test can be run using the form [<context>:]<name> where <context> can\n");
printf("be omitted if there is no context.\n\n");
printf(" -c --colours/colors\tUse colours to emphasis result (requires ANSI-capable terminal)\n");
printf(" -x --xml <prefix>\tInstead of messages on stdout, write results into one XML-file\n");
printf("\t\t\tper suite, compatible with Hudson/Jenkins CI. The filename(s)\n");
printf("\t\t\twill be '<prefix>-<suite>.xml'\n");
printf(" -s --suite <name>\tName the top level suite\n");
printf(" -n --no-run\t\tDon't run the tests\n");
printf(" -v --verbose\t\tShow progress information\n");
printf(" -q --quiet\t\tJust output dots for each test\n");
printf(" --version\t\tShow version information\n");
printf(" -c --colours/colors\t\tUse colours to emphasis result (requires ANSI-capable terminal)\n");
printf(" -C --no-colours/no-colors\tDon't use colours\n");
printf(" -x --xml <prefix>\t\tInstead of messages on stdout, write results into one XML-file\n");
printf("\t\t\t\tper suite, compatible with Hudson/Jenkins CI. The filename(s)\n");
printf("\t\t\t\twill be '<prefix>-<suite>.xml'\n");
printf(" -s --suite <name>\t\tName the top level suite\n");
printf(" -n --no-run\t\t\tDon't run the tests\n");
printf(" -v --verbose\t\t\tShow progress information\n");
printf(" -q --quiet\t\t\tJust output dots for each test\n");
printf(" --version\t\t\tShow version information\n");
}


Expand Down Expand Up @@ -107,6 +109,16 @@ static int initialize_option_handling(int argc, const char **argv) {
gopt_shorts('c'),
gopt_longs("colors")
),
gopt_option('C',
GOPT_NOARG,
gopt_shorts('C'),
gopt_longs("no-colors")
),
gopt_option('C',
GOPT_NOARG,
gopt_shorts('C'),
gopt_longs("no-colours")
),
gopt_option('q',
GOPT_NOARG,
gopt_shorts('q'),
Expand Down Expand Up @@ -174,10 +186,14 @@ int main(int argc, const char **argv) {
if (gopt_arg(options, 'n', &tmp))
no_run = true;

if (gopt_arg(options, 'c', &tmp) && isatty(fileno(stdout)))
reporter_options.use_colours = true;
else
reporter_options.use_colours = false;
reporter_options.use_colours = true;
if (isatty(fileno(stdout))) {
if (gopt_arg(options, 'C', &tmp))
reporter_options.use_colours = false;
} else {
if (!gopt_arg(options, 'c', &tmp))
reporter_options.use_colours = false;
}

if (gopt_arg(options, 'q', &tmp))
reporter_options.quiet_mode = true;
Expand Down

0 comments on commit bead2ca

Please sign in to comment.