]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: print numeric error value too on failure
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 28 Jun 2025 17:02:50 +0000 (19:02 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 1 Jul 2025 15:51:49 +0000 (17:51 +0200)
The error message is not always meaningful. Also, sometimes we care about the
sign of the value, and we ignore the sign of the error in the printing machinery.

The messages for errno are changed to say "errno" instead of "error". The problem with
the previous formalation is that our errors are always negative and errnos are
positive, so when we print the numerical value, we cannot use the word for both.

Example output:
src/test/test-tests.c:15: Assertion failed: Expected "-1" to succeed, but got error: -1/Operation not permitted
src/test/test-tests.c:16: Assertion failed: Expected "-ENOANO" to succeed, but got error: -55/No anode
src/test/test-tests.c:61: Assertion failed: Expected "0" to fail with error -2/"No such file or directory", but it succeeded
src/test/test-tests.c:62: Assertion failed: Expected "RET_NERRNO(mkdir("/i/will/fail/with/enoent", 666))" to fail with error -55/"No anode", but got the following error: -2/No such file or directory
src/test/test-tests.c:68: Assertion failed: Expected "0" to fail with errno 2/"No such file or directory", but it succeeded
src/test/test-tests.c:70: Assertion failed: Expected "mkdir("/i/will/fail/with/enoent", 666)" to fail with errno 55/"No anode", but got the following errno: 2/No such file or directory

src/shared/tests.h

index 7641b446b0adb09eafddf7cbc25cbdd08bf9c77f..d7c92504875a77e7739cb5bcb3d51205b8ac83b4 100644 (file)
@@ -177,7 +177,8 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
         ({                                                                                                              \
                 typeof(expr) _result = (expr);                                                                          \
                 if (_result < 0)                                                                                        \
-                        log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(_result));     \
+                        log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s",                     \
+                                        #expr, (intmax_t) _result, STRERROR(_result));                                  \
          })
 #endif
 
@@ -189,7 +190,8 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
         ({                                                                                                              \
                 typeof(expr) _result = (expr);                                                                          \
                 if (_result < 0)                                                                                        \
-                        log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(_result));     \
+                        log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s",                     \
+                                        #expr, (intmax_t) _result, STRERROR(_result));                                  \
                 if (_result == 0)                                                                                       \
                         log_test_failed("Expected \"%s\" to be positive, but it is zero.", #expr);                      \
          })
@@ -202,7 +204,8 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
         ({                                                                                                              \
                 typeof(expr) _result = (expr);                                                                          \
                 if (_result < 0)                                                                                        \
-                        log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(_result));     \
+                        log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s",                     \
+                                        #expr, (intmax_t) _result, STRERROR(_result));                                  \
                 if (_result != 0)                                                                                       \
                         log_test_failed("Expected \"%s\" to be zero, but it is %"PRIiMAX".",                            \
                                         #expr, (intmax_t) _result);                                                     \
@@ -217,13 +220,15 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
                 typeof(expr1) _expr1 = (expr1);                                                                         \
                 typeof(expr2) _expr2 = (expr2);                                                                         \
                 if (_expr1 < 0)                                                                                         \
-                        log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr1, STRERROR(_expr1));     \
+                        log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s",                     \
+                                        #expr1, (intmax_t) _expr1, STRERROR(_expr1));                                   \
                 if (_expr1 != _expr2)                                                                                   \
                         log_test_failed("Expected \"%s == %s\", got %"PRIiMAX" != %"PRIiMAX,                            \
                                         #expr1, #expr2, (intmax_t) _expr1, (intmax_t) _expr2);                          \
         })
 #endif
 
+/* For functions that return a boolean on success and set errno on failure. */
 #ifdef __COVERITY__
 #  define ASSERT_OK_ERRNO(expr) __coverity_check__((expr) >= 0)
 #else
@@ -231,7 +236,8 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
         ({                                                                                                              \
                 typeof(expr) _result = (expr);                                                                          \
                 if (_result < 0)                                                                                        \
-                        log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(errno));       \
+                        log_test_failed("Expected \"%s\" to succeed, but got errno: %d/%s",                             \
+                                        #expr, errno, STRERROR(errno));                                                 \
         })
 #endif
 
@@ -242,7 +248,8 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
         ({                                                                                                              \
                 typeof(expr) _result = (expr);                                                                          \
                 if (_result < 0)                                                                                        \
-                        log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr, STRERROR(errno));       \
+                        log_test_failed("Expected \"%s\" to succeed, but got errno: %d/%s",                             \
+                                        #expr, errno, STRERROR(errno));                                                 \
                 if (_result != 0)                                                                                       \
                         log_test_failed("Expected \"%s\" to be zero, but it is %"PRIiMAX".",                            \
                                         #expr, (intmax_t) _result);                                                     \
@@ -257,7 +264,8 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
                 typeof(expr1) _expr1 = (expr1);                                                                         \
                 typeof(expr2) _expr2 = (expr2);                                                                         \
                 if (_expr1 < 0)                                                                                         \
-                        log_test_failed("Expected \"%s\" to succeed, but got error: %s", #expr1, STRERROR(errno));      \
+                        log_test_failed("Expected \"%s\" to succeed, but got errno: %d/%s",                             \
+                                        #expr1, errno, STRERROR(errno));                                                \
                 if (_expr1 != _expr2)                                                                                   \
                         log_test_failed("Expected \"%s == %s\", but %"PRIiMAX" != %"PRIiMAX,                            \
                                         #expr1, #expr2, (intmax_t) _expr1, (intmax_t) _expr2);                          \
@@ -283,11 +291,11 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
                 int _expr1 = (expr1);                                                                                   \
                 int _expr2 = (expr2);                                                                                   \
                 if (_expr1 >= 0)                                                                                        \
-                        log_test_failed("Expected \"%s\" to fail with error \"%s\", but it succeeded",                  \
-                                        #expr1, STRERROR(_expr2));                                                      \
+                        log_test_failed("Expected \"%s\" to fail with error %d/\"%s\", but it succeeded",               \
+                                        #expr1, -_expr2, STRERROR(_expr2));                                             \
                 else if (-_expr1 != _expr2)                                                                             \
-                        log_test_failed("Expected \"%s\" to fail with error \"%s\", but got the following error: %s",   \
-                                        #expr1, STRERROR(_expr2), STRERROR(_expr1));                                    \
+                        log_test_failed("Expected \"%s\" to fail with error %d/\"%s\", but got the following error: %d/%s", \
+                                        #expr1, -_expr2, STRERROR(_expr2), _expr1, STRERROR(_expr1));                   \
         })
 #endif
 
@@ -299,11 +307,11 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char
                 int _expr1 = (expr1);                                                                                   \
                 int _expr2 = (expr2);                                                                                   \
                 if (_expr1 >= 0)                                                                                        \
-                        log_test_failed("Expected \"%s\" to fail with error \"%s\", but it succeeded",                  \
-                                        #expr1, STRERROR(_expr2));                                                      \
+                        log_test_failed("Expected \"%s\" to fail with errno %d/\"%s\", but it succeeded",               \
+                                        #expr1, _expr2, STRERROR(_expr2));                                              \
                 else if (errno != _expr2)                                                                               \
-                        log_test_failed("Expected \"%s\" to fail with error \"%s\", but got the following error: %s",   \
-                                        #expr1, STRERROR(_expr2), STRERROR(errno));                                     \
+                        log_test_failed("Expected \"%s\" to fail with errno %d/\"%s\", but got the following errno: %d/%s", \
+                                        #expr1, _expr2, STRERROR(_expr2), errno, STRERROR(errno));                      \
         })
 #endif