From 6b60354656169bea426062753c728657e84e6797 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 1 Jul 2025 17:43:45 +0200 Subject: [PATCH] tests: print errno name rather than the message The tests are written and consumed by developers. Errno descriptions are good for users, but for developers the errno "name" is actually more useful, and we need to always map the description back to the name to compare with the code. Let's make things simpler for ourselves by printing the errno names directly. Example output: src/test/test-tests.c:15: Assertion failed: Expected "-1" to succeed, but got error: -1/EPERM src/test/test-tests.c:16: Assertion failed: Expected "-ENOANO" to succeed, but got error: -55/ENOANO src/test/test-tests.c:20: Assertion failed: Expected "0" to be positive, but it is zero. src/test/test-tests.c:62: Assertion failed: Expected "RET_NERRNO(mkdir("/i/will/fail/with/enoent", 666))" to fail with error -55/ENOANO, but got -2/ENOENT src/test/test-tests.c:68: Assertion failed: Expected "0" to fail with errno 2/ENOENT, but it succeeded src/test/test-tests.c:70: Assertion failed: Expected "mkdir("/i/will/fail/with/enoent", 666)" to fail with errno 55/ENOANO, but got 2/ENOENT --- src/shared/tests.h | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/shared/tests.h b/src/shared/tests.h index 5d1dccba9b1..46c82f444c6 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #pragma once +#include "errno-list.h" #include "errno-util.h" #include "forward.h" #include "log.h" @@ -178,7 +179,7 @@ _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: %"PRIiMAX"/%s", \ - #expr, (intmax_t) _result, STRERROR(_result)); \ + #expr, (intmax_t) _result, ERRNO_NAME_FULL(_result)); \ }) #endif @@ -194,7 +195,7 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char typeof(expr) _result = (expr); \ if (_result < 0 && !IN_SET(_result, 0, __VA_ARGS__)) \ log_test_failed("\"%s\" failed with unexpected error: %"PRIiMAX"/%s", \ - #expr, (intmax_t) _result, STRERROR(_result)); \ + #expr, (intmax_t) _result, ERRNO_NAME_FULL(_result)); \ }) #endif @@ -207,7 +208,7 @@ _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: %"PRIiMAX"/%s", \ - #expr, (intmax_t) _result, STRERROR(_result)); \ + #expr, (intmax_t) _result, ERRNO_NAME_FULL(_result)); \ if (_result == 0) \ log_test_failed("Expected \"%s\" to be positive, but it is zero.", #expr); \ }) @@ -221,7 +222,7 @@ _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: %"PRIiMAX"/%s", \ - #expr, (intmax_t) _result, STRERROR(_result)); \ + #expr, (intmax_t) _result, ERRNO_NAME_FULL(_result)); \ if (_result != 0) \ log_test_failed("Expected \"%s\" to be zero, but it is %"PRIiMAX".", \ #expr, (intmax_t) _result); \ @@ -237,7 +238,7 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char typeof(expr2) _expr2 = (expr2); \ if (_expr1 < 0) \ log_test_failed("Expected \"%s\" to succeed, but got error: %"PRIiMAX"/%s", \ - #expr1, (intmax_t) _expr1, STRERROR(_expr1)); \ + #expr1, (intmax_t) _expr1, ERRNO_NAME_FULL(_expr1)); \ if (_expr1 != _expr2) \ log_test_failed("Expected \"%s == %s\", got %"PRIiMAX" != %"PRIiMAX, \ #expr1, #expr2, (intmax_t) _expr1, (intmax_t) _expr2); \ @@ -253,7 +254,7 @@ _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 errno: %d/%s", \ - #expr, errno, STRERROR(errno)); \ + #expr, errno, ERRNO_NAME_FULL(errno)); \ }) #endif @@ -265,7 +266,7 @@ _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 errno: %d/%s", \ - #expr, errno, STRERROR(errno)); \ + #expr, errno, ERRNO_NAME_FULL(errno)); \ if (_result != 0) \ log_test_failed("Expected \"%s\" to be zero, but it is %"PRIiMAX".", \ #expr, (intmax_t) _result); \ @@ -281,7 +282,7 @@ _noreturn_ void log_test_failed_internal(const char *file, int line, const char typeof(expr2) _expr2 = (expr2); \ if (_expr1 < 0) \ log_test_failed("Expected \"%s\" to succeed, but got errno: %d/%s", \ - #expr1, errno, STRERROR(errno)); \ + #expr1, errno, ERRNO_NAME_FULL(errno)); \ if (_expr1 != _expr2) \ log_test_failed("Expected \"%s == %s\", but %"PRIiMAX" != %"PRIiMAX, \ #expr1, #expr2, (intmax_t) _expr1, (intmax_t) _expr2); \ @@ -307,11 +308,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 %d/\"%s\", but it succeeded", \ - #expr1, -_expr2, STRERROR(_expr2)); \ + log_test_failed("Expected \"%s\" to fail with error %d/%s, but it succeeded", \ + #expr1, -_expr2, ERRNO_NAME_FULL(_expr2)); \ else if (-_expr1 != _expr2) \ - 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)); \ + log_test_failed("Expected \"%s\" to fail with error %d/%s, but got %d/%s", \ + #expr1, -_expr2, ERRNO_NAME_FULL(_expr2), _expr1, ERRNO_NAME_FULL(_expr1)); \ }) #endif @@ -323,11 +324,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 errno %d/\"%s\", but it succeeded", \ - #expr1, _expr2, STRERROR(_expr2)); \ + log_test_failed("Expected \"%s\" to fail with errno %d/%s, but it succeeded", \ + #expr1, _expr2, ERRNO_NAME_FULL(_expr2)); \ else if (errno != _expr2) \ - 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)); \ + log_test_failed("Expected \"%s\" to fail with errno %d/%s, but got %d/%s", \ + #expr1, _expr2, ERRNO_NAME_FULL(_expr2), errno, ERRNO_NAME_FULL(errno)); \ }) #endif -- 2.47.3