]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: fixup argument handling
authorEmil Velikov <emil.l.velikov@gmail.com>
Mon, 16 Jun 2025 16:00:42 +0000 (17:00 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 8 Jul 2025 12:17:14 +0000 (07:17 -0500)
Currently we can pass any number of tests as arguments - be that in
oneshot mode or not.

At the same time, oneshot requires only one test while normal mode can
have up-to one.

Fix that up and as a bonus we no longer need the exit() in
test_run_spawned() \o/

While in here, add a few comments around our arg/argc handling.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/376
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
testsuite/testsuite.c
testsuite/testsuite.h

index b8cfdfcf4f9425a42903e60d9ce0b52756b8e026..7388066f887b3160f266445548cf38bd4c700617 100644 (file)
@@ -108,6 +108,18 @@ int test_init(const struct test *start, const struct test *stop, int argc,
                }
        }
 
+       if (oneshot) {
+               if ((optind + 1) != argc) {
+                       ERR("Too many arguments provided. Only one is supported - the testname\n");
+                       return -1;
+               }
+       } else {
+               if (optind != argc && (optind + 1) != argc) {
+                       ERR("Invalid number of arguments provided. One optional can be provided - the testname\n");
+                       return -1;
+               }
+       }
+
        if (isatty(STDOUT_FILENO) == 0) {
                ANSI_HIGHLIGHT_OFF = "";
                ANSI_HIGHLIGHT_RED_ON = "";
@@ -140,9 +152,9 @@ static int test_spawn_test(const struct test *t)
        return EXIT_FAILURE;
 }
 
-static noreturn int test_run_spawned(const struct test *t)
+static int test_run_spawned(const struct test *t)
 {
-       exit(t->func());
+       return t->func();
 }
 
 int test_spawn_prog(const char *prog, const char *const args[])
@@ -1144,7 +1156,7 @@ int test_run(const struct test *t)
        int fdmonitor[2];
 
        if (oneshot)
-               test_run_spawned(t);
+               return test_run_spawned(t);
 
        if (t->output.out != NULL) {
                if (pipe(fdout) != 0) {
index 714af72450c8b18284085898c47ff4ecfe4a5d9e..4ce0c22cb55568f1ed08064792f0797d48bdfe34 100644 (file)
@@ -144,10 +144,12 @@ int test_run(const struct test *t);
                int arg, ret = EXIT_SUCCESS;                                             \
                                                                                          \
                arg = test_init(__start_kmod_tests, __stop_kmod_tests, argc, argv);      \
-               if (arg == 0)                                                            \
-                       return 0;                                                        \
+               /* Invalid arguments */                                                  \
                if (arg < 0)                                                             \
                        return EXIT_FAILURE;                                             \
+               /* Print and exit options - list, help */                                \
+               if (arg == 0)                                                            \
+                       return 0;                                                        \
                                                                                          \
                if (arg < argc) {                                                        \
                        t = test_find(__start_kmod_tests, __stop_kmod_tests, argv[arg]); \