[Spice-devel] [PATCH] server/tests: use getopt_long

Alon Levy alevy at redhat.com
Tue Feb 14 07:31:32 PST 2012


---
 server/tests/test_display_base.c      |   47 +++++++++++++++++++++------------
 server/tests/test_display_base.h      |    3 +-
 server/tests/test_display_streaming.c |    4 +--
 3 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/server/tests/test_display_base.c b/server/tests/test_display_base.c
index 48f59d2..811e901 100644
--- a/server/tests/test_display_base.c
+++ b/server/tests/test_display_base.c
@@ -1,4 +1,3 @@
-
 #include <config.h>
 #include <stdlib.h>
 #include <math.h>
@@ -9,7 +8,10 @@
 #include <wait.h>
 #include <sys/select.h>
 #include <sys/types.h>
+#include <getopt.h>
+
 #include <spice/qxl_dev.h>
+
 #include "test_display_base.h"
 #include "red_channel.h"
 #include "test_util.h"
@@ -649,32 +651,43 @@ SpiceServer* test_init(SpiceCoreInterface *core)
     return server;
 }
 
-void check_automated(int argc, char **argv)
+void init_automated()
 {
     struct sigaction sa;
 
-    if (argc == 1) {
-        return;
-    }
-
-    if (argc > 2) {
-        goto invalid_option;
-    }
-
-    if (strcmp(argv[1], "--automated-tests") != 0) {
-        goto invalid_option;
-    }
-
-    has_automated_tests = 1;
-
     memset(&sa, 0, sizeof sa);
     sa.sa_handler = &sigchld_handler;
     sigaction(SIGCHLD, &sa, NULL);
+}
+
+void spice_test_config_parse_args(int argc, char **argv)
+{
+    struct option options[] = {
+#ifdef AUTOMATED_TESTS
+        {"automated-tests", no_argument, &has_automated_tests, 1},
+#endif
+        {NULL, 0, NULL, 0},
+    };
+    int option_index;
+    int val;
+
+    while ((val = getopt_long(argc, argv, "", options, &option_index)) != -1) {
+        switch (val) {
+        case '?':
+            printf("unrecognized option %s", argv[optind]);
+            goto invalid_option;
+        case 0:
+            break;
+        }
+    }
 
+    if (has_automated_tests) {
+        init_automated();
+    }
     return;
 
 invalid_option:
     printf("Invalid option!\n"
-           "Please, check README before run tests!\n" );
+           "usage: %s [--automated-tests]\n", argv[0]);
     exit(0);
 }
diff --git a/server/tests/test_display_base.h b/server/tests/test_display_base.h
index 4aeaf5f..0ed41cb 100644
--- a/server/tests/test_display_base.h
+++ b/server/tests/test_display_base.h
@@ -10,8 +10,7 @@ void test_set_simple_command_list(int* commands, int num_commands);
 void test_add_display_interface(SpiceServer *server);
 SpiceServer* test_init(SpiceCoreInterface* core);
 
-/* Used for automated tests */
-void check_automated(int argc, char **argv);
+void spice_test_config_parse_args(int argc, char **argv);
 
 // simple queue for commands
 enum {
diff --git a/server/tests/test_display_streaming.c b/server/tests/test_display_streaming.c
index 5052991..1b81d76 100644
--- a/server/tests/test_display_streaming.c
+++ b/server/tests/test_display_streaming.c
@@ -20,9 +20,7 @@ SpiceServer *server;
 
 int main(int argc, char **argv)
 {
-#ifdef AUTOMATED_TESTS
-    check_automated(argc, argv);
-#endif
+    spice_test_config_parse_args(argc, argv);
     core = basic_event_loop_init();
     server = test_init(core);
     spice_server_set_streaming_video(server, SPICE_STREAM_VIDEO_ALL);
-- 
1.7.9



More information about the Spice-devel mailing list