From ca4009c9824b26af9c2785fc847dfe7f9f14bc1d Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 13 Nov 2018 19:55:06 -0800 Subject: [PATCH] GUACAMOLE-662: Log test output in TAP format. --- util/generate-test-runner.pl | 56 +++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/util/generate-test-runner.pl b/util/generate-test-runner.pl index c32ec0c4..a121a071 100755 --- a/util/generate-test-runner.pl +++ b/util/generate-test-runner.pl @@ -39,15 +39,23 @@ use strict; -# Parse all test declarations from given file +my $num_tests = 0; my %test_suites = (); + +# Parse all test declarations from given file while (<>) { if ((my $suite_name, my $test_name) = m/^void\s+test_(\w+)__(\w+)/) { + $num_tests++; $test_suites{$suite_name} //= (); push @{$test_suites{$suite_name}}, $test_name; } } +# Bail out if there's nothing to write +if ($num_tests == 0) { + die "No unit tests... :(\n"; +} + # # Common test runner header # @@ -73,7 +81,44 @@ print <<'END'; */ #include -#include +#include + +/** + * The current test number, as required by the TAP format. This value is + * automatically incremented by tap_log_test_completed() after each test is + * run. + */ +int tap_test_number = 1; + +/** + * Logs the status of a CUnit test which just completed. This implementation + * logs test completion in TAP format. + * + * @param test + * The CUnit test which just completed. + * + * @param suite + * The CUnit test suite associated with the test. + * + * @param failure + * The head element of the test failure list, or NULL if the test passed. + */ +static void tap_log_test_completed(const CU_pTest test, + const CU_pSuite suite, const CU_pFailureRecord failure) { + + /* Log success/failure in TAP format */ + if (failure == NULL) + printf("ok %i - [%s] %s: OK\n", + tap_test_number, suite->pName, test->pName); + else + printf("not ok %i - [%s] %s: Assertion failed on %s:%i: %s\n", + tap_test_number, suite->pName, test->pName, + failure->strFileName, failure->uiLineNumber, + failure->strCondition); + + tap_test_number++; + +} END # @@ -132,9 +177,12 @@ while ((my $suite_name, my $test_names) = each (%test_suites)) { print <<"END"; + /* Write TAP header */ + printf("1..$num_tests\\n"); + /* Run all tests in all suites */ - CU_basic_set_mode(CU_BRM_VERBOSE); - CU_basic_run_tests(); + CU_set_test_complete_handler(tap_log_test_completed); + CU_run_all_tests(); cleanup: /* Tests complete */