improve test system
authorNiki Roo <niki@nikiroo.be>
Thu, 3 Mar 2022 19:42:13 +0000 (20:42 +0100)
committerNiki Roo <niki@nikiroo.be>
Thu, 3 Mar 2022 19:42:13 +0000 (20:42 +0100)
src/tests/launcher.c
src/tests/launcher.h

index 6d3135843b29dab002a2cfb4f62610a8647c7df1..2fdd21c11e60b437b1a1b27997203f3f71b5513e 100644 (file)
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-#include <check.h>
-#include <string.h>
+#include "launcher.h"
+
+#include <asm-generic/ioctls.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/ioctl.h>
-#include "launcher.h"
+
+// Shared globals
+int launcher_color = -1;
+
+// Private functions
+static int has_colour();
+static char *waiting_color();
+static char *passed_color();
+static char *failed_color();
+static char *stop_color();
+
+// Private vars
+// static char BLEU[] = { (char) 27, '[', '3', '4', 'm', '\0' };
+// static char TEAL[] = { (char) 27, '[', '3', '6', 'm', '\0' };
+// static char GRIS[] = { (char) 27, '[', '3', '7', 'm', '\0' };
+static char VERT[] = { (char) 27, '[', '3', '2', 'm', '\0' };
+static char ROUGE[] = { (char) 27, '[', '3', '1', 'm', '\0' };
+static char ORANGE[] = { (char) 27, '[', '3', '3', 'm', '\0' };
+static char STOP[] = { (char) 27, '[', '0', 'm', '\0' };
+static char bs9[] = { 8, 8, 8, 8, 8, 8, 8, 8, 8, '\0' };
 
 // CK_ENV : Gets the print mode from the environment variable CK_VERBOSITY,
 // which can have the values "silent", "minimal", "normal", "verbose". If the
@@ -48,7 +69,7 @@
 
 char *tests_name = NULL;
 
-void test_init(const char * const name) {
+void test_init(const char name[]) {
        int i;
        int cols;
        struct winsize ws;
@@ -69,41 +90,27 @@ void test_init(const char * const name) {
 
        strcpy(tests_name + 4, name);
        tests_name[strlen(name) + 4] = ' ';
-
-       strcpy(tests_name + cols - 6 - 1 - 4, " [ ?? ]    ");
+       tests_name[cols - 6 - 1 - 4] = '\0';
 
        fprintf(stderr, "%s", tests_name);
+       fprintf(stderr, "%s[%s%s%s]    ", " ", waiting_color(), " ?? ",
+                       stop_color());
 }
 
 void test_success() {
-       fprintf(stderr, "%s", "\b\b\b\b\b\b\b\b OK ]    ");
+       fprintf(stderr, "%s[%s%s%s]    ", bs9, passed_color(), " OK ",
+                       stop_color());
 }
 
 void test_failure() {
-       fprintf(stderr, "%s", "\b\b\b\b\b\b\b\bFAIL]    ");
+       fprintf(stderr, "%s[%s%s%s]    ", bs9, failed_color(), "FAIL",
+                       stop_color());
 }
 
-int main(int argc, char **argv) {
+int test_start(int more) {
        int failed;
        SRunner *runner;
-       int more;
 
-       if (argc > 1 && !strcmp("--name", argv[1])) {
-               test_init(argv[2]);
-               return 0;
-       }
-       if (argc > 1 && !strcmp("--passed", argv[1])) {
-               test_success();
-               return 0;
-       }
-       if (argc > 1 && !strcmp("--failed", argv[1])) {
-               test_failure();
-               return 0;
-       }
-
-       more = 0;
-       if (argc > 1 && !strcmp("--more", argv[1]))
-               more = 1;
        runner = get_tests(more);
 
        failed = 0;
@@ -119,3 +126,71 @@ int main(int argc, char **argv) {
        return failed;
 }
 
+int main(int argc, char **argv) {
+       int more = 0;
+       int cont = 1;
+
+       for (int i = 1; i < argc; i++) {
+               if (!strcmp("--name", argv[i])) {
+                       if ((i + 1) >= argc)
+                               return 1;
+                       test_init(argv[++i]);
+                       cont = 0;
+               } else if (!strcmp("--passed", argv[i])) {
+                       test_success();
+                       cont = 0;
+               } else if (!strcmp("--failed", argv[i])) {
+                       if ((i + 1) >= argc)
+                               return 1;
+                       test_failure();
+                       cont = 0;
+               } else if (!strcmp("--more", argv[i])) {
+                       if ((i + 1) >= argc)
+                               return 1;
+                       more = 1;
+               } else if (!strcmp("--color", argv[i])) {
+                       launcher_color = 1;
+               } else if (!strcmp("--no-color", argv[i])) {
+                       launcher_color = 0;
+               }
+       }
+
+       if (!cont)
+               return 0;
+
+       return test_start(more);
+}
+
+static int has_colour() {
+       if (launcher_color == -1) {
+               // TODO: detect if terminal supports colour mode
+               // for instance, check if $COLORTERM is not empty?
+               launcher_color = 1;
+       }
+
+       return launcher_color;
+}
+
+static char *waiting_color() {
+       if (has_colour())
+               return ORANGE;
+       return "";
+}
+
+static char *passed_color() {
+       if (has_colour())
+               return VERT;
+       return "";
+}
+
+static char *failed_color() {
+       if (has_colour())
+               return ROUGE;
+       return "";
+}
+
+static char *stop_color() {
+       if (has_colour())
+               return STOP;
+       return "";
+}
index 9a05a6bfdb628d3d3b27c8dac6a0342bfa5015a8..89a6ac41fd76a42f96ad830b1fe0eaf787fd5315 100644 (file)
@@ -30,9 +30,13 @@ ck_abort_msg("%s\n\tExpected: %lld\n\tReceived: %lld", title, (long long)expecte
 ck_abort_msg("%s\n\tExpected: %zu\n\tReceived: %zu", title, expected, received); \
 }
 
+extern int launcher_color;
+
 SRunner *get_tests(int more);
 
-void test_init(const char * const name);
+void test_init(const char name[]);
+
+int test_start(int more);
 
 void test_success();