Skip to contentSkip to conversation

tests/libqtest: Print error instead of aborting when env variable is missing

PATCH Superseded Thomas Huth 2 participants 2 messages May 19, 2017 – May 22, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/libqtest: Print error instead of aborting when env variable is missing

When you currently try to run a test directly from the command line without setting the QTEST_QEMU_BINARY environment variable first, you are presented with an unhelpful assertion message like this:

ERROR:tests/libqtest.c:163:qtest_init_without_qmp_handshake:
assertion failed: (qemu_binary != NULL)
Aborted (core dumped)

Let's replace the assert() with a more user friendly error message instead.

---
 tests/libqtest.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tests/libqtest.c +5 -1
index 84ecbd2..f0fd682 100644
@@ -160,7 +160,11 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
     const char *qemu_binary;
 
     qemu_binary = getenv("QTEST_QEMU_BINARY");
-    g_assert(qemu_binary != NULL);
+    if (!qemu_binary) {
+        fprintf(stderr, "The environment variable QTEST_QEMU_BINARY has to be "
+                        "set before running the test.\n");
+        exit(1);
+    }
 
     s = g_malloc(sizeof(*s));
 
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Thomas Huth

When you currently try to run a test directly from the command line without setting the QTEST_QEMU_BINARY environment variable first, you are presented with an unhelpful assertion message like this:

ERROR:tests/libqtest.c:163:qtest_init_without_qmp_handshake:
assertion failed: (qemu_binary != NULL)
Aborted (core dumped)

Let's replace the assert() with a more user friendly error message instead.

---
 tests/libqtest.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tests/libqtest.c +0 -0
index 84ecbd2..f0fd682 100644

tests/libqtest.c QTestState *qtest_init_without_qmp_handshake(const char *extra_args) · line 165

Quoted 7 lines
tests/libqtest.c +3 -1
@@ -160,7 +160,11 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
     const char *qemu_binary;
 
     qemu_binary = getenv("QTEST_QEMU_BINARY");
-    g_assert(qemu_binary != NULL);
+    if (!qemu_binary) {
+        fprintf(stderr, "The environment variable QTEST_QEMU_BINARY has to be "
+                        "set before running the test.\n");

Nitpick: error messages usually don't end with a period.

I'm fine with the message itself, although I'd have written somehing more laconic like "Environment variable QTEST_QEMU_BINARY required". Your choice.

tests/libqtest.c QTestState *qtest_init_without_qmp_handshake(const char *extra_args) · line 169

Quoted 4 lines
tests/libqtest.c +2 -0
+        exit(1);
+    }
 
     s = g_malloc(sizeof(*s));