Browse Source

Unit test: use different log files for different tests

bel2125 7 years ago
parent
commit
fb0a5a664a
3 changed files with 42 additions and 8 deletions
  1. 4 4
      .travis.yml
  2. 3 1
      appveyor.yml
  3. 35 3
      unittest/main.c

+ 4 - 4
.travis.yml

@@ -100,13 +100,13 @@ script:
   - if [ "${MACOSX_PACKAGE}" == "1" ]; then
       cd "${TRAVIS_BUILD_DIR}";
       make -f Makefile.osx package;
+      pwd;
+      ls -la unittest;
+      cat unittest/test-*.log;
+      cat unittest/test-*.xml;
     else
       CTEST_OUTPUT_ON_FAILURE=1 make all test;
     fi
-  - pwd
-  - ls
-  - ls unittest
-  - cat unittest/test.log
 
 # Coveralls options: https://github.com/eddyxu/cpp-coveralls/blob/master/README.md
 after_success:

+ 3 - 1
appveyor.yml

@@ -364,7 +364,9 @@ test_script:
   - set CTEST_OUTPUT_ON_FAILURE=1
   - cmd /c "%test%" & set "test_ret=%ERRORLEVEL%"
   - echo "Test returned %test_ret%"
-  - type "%source_path%\output\build\unittest\test.log"
+  - dir "%source_path%\output\build\unittest\"
+  - type "%source_path%\output\build\unittest\test-*.log"
+  - type "%source_path%\output\build\unittest\test-*.xml"
   - cd "%source_path%"
   - set "output_path=%source_path%\output"
   - set "build_path=%output_path%\build"

+ 35 - 3
unittest/main.c

@@ -54,11 +54,15 @@ main(const int argc, char *argv[])
 	const size_t test_case_arg_size = strlen(test_case_arg);
 	const char *const test_dir_arg = "--test-dir=";
 	const size_t test_dir_arg_size = strlen(test_dir_arg);
-	const char *const help_arg = "--help";
+    const char *const test_log_arg = "--test-log=";
+    const size_t test_log_arg_size = strlen(test_log_arg);
+    const char *const help_arg = "--help";
 
 	SRunner *srunner;
 	int number_run = 0;
 	int number_failed = 0;
+    const char *test_log_prefix = NULL;
+    char test_log_name[128];
 
 	int i;
 
@@ -72,6 +76,13 @@ main(const int argc, char *argv[])
 		} else if (0 == strncmp(test_dir_arg, argv[i], test_dir_arg_size)
 		           && (strlen(argv[i]) > test_dir_arg_size)) {
 			set_test_directory(&argv[i][test_dir_arg_size]);
+        } else if (0 == strncmp(test_log_arg, argv[i], test_log_arg_size)
+                   && (strlen(argv[i]) > test_log_arg_size)) {
+            test_log_prefix = &argv[i][test_log_arg_size];
+            if (strlen(test_log_prefix) > (sizeof(test_log_name)-16)) {
+                fprintf(stderr, "Argument too long: %s\n", argv[i]);
+                exit(EXIT_FAILURE);
+            }
 		} else if (0 == strcmp(help_arg, argv[i])) {
 			printf(
 			    "Usage: %s [options]\n"
@@ -96,8 +107,29 @@ main(const int argc, char *argv[])
 	srunner_add_suite(srunner, make_timertest_suite());
 
 	/* Write test logs to a file */
-	srunner_set_log(srunner, "test.log");
-	srunner_set_xml(srunner, "test.xml");
+    if (test_log_prefix != NULL) {
+        /* Find the next free log name */
+        FILE *f;
+        for (i=1;;i++) {
+            sprintf(test_log_name, "log-%i.log", i);
+            f = fopen(test_log_name, "r");
+            if (f) {
+                /* already exists */
+                fclose(f);
+                continue;
+            }
+            srunner_set_log(srunner, test_log_name);
+            sprintf(test_log_name, "log-%i.xml", i);
+            srunner_set_xml(srunner, test_log_name);
+            break;
+        }
+    } else {
+        /* We got a test log name from the command line */
+        sprintf(test_log_name, "%s.log", test_log_prefix);
+        srunner_set_log(srunner, test_log_name);
+        sprintf(test_log_name, "%s.xml", test_log_prefix);
+        srunner_set_xml(srunner, test_log_name);
+    }
 
 	/* Run tests, using log level CK_VERBOSE, since CK_NORMAL
 	 * offers not enough diagnosis to analyze failed tests.