From 6068d3f6d1dd4bd8be95e6bf4ed3e9148291b408 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sat, 4 Jul 2020 18:56:15 +0000 Subject: [PATCH] Change the strerror_override handling to check $_JSON_C_STRERROR_OVERRIDE instead of using a variable, so we don't need to export it. --- strerror_override.c | 5 ++++- tests/CMakeLists.txt | 4 ++++ tests/test_basic.test | 2 ++ tests/test_json_pointer.c | 5 +---- tests/test_util_file.c | 2 -- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/strerror_override.c b/strerror_override.c index a93be38f..7a262f7b 100644 --- a/strerror_override.c +++ b/strerror_override.c @@ -59,7 +59,8 @@ static struct /* clang-format on */ // Enabled during tests -int _json_c_strerror_enable = 0; +static int _json_c_strerror_enable = 0; +extern char *getenv(const char *name); // Avoid including stdlib.h #define PREFIX "ERRNO=" static char errno_buf[128] = PREFIX; @@ -70,6 +71,8 @@ char *_json_c_strerror(int errno_in) int ii, jj; if (!_json_c_strerror_enable) + _json_c_strerror_enable = (getenv("_JSON_C_STRERROR_ENABLE") == NULL) ? -1 : 1; + if (_json_c_strerror_enable == -1) return strerror(errno_in); // Avoid standard functions, so we don't need to include any diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a8715739..125f6157 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -37,6 +37,10 @@ foreach(TESTNAME test_object_iterator) add_executable(${TESTNAME} ${TESTNAME}.c) +if(${TESTNAME} STREQUAL test_util_file) +# For output consistency, we need _json_c_strerror() in some tests: +target_sources(${TESTNAME} PRIVATE ../strerror_override.c) +endif() add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test) # XXX using the non-target_ versions of these doesn't work :( diff --git a/tests/test_basic.test b/tests/test_basic.test index 154e036d..474e7a82 100755 --- a/tests/test_basic.test +++ b/tests/test_basic.test @@ -1,5 +1,7 @@ #!/bin/sh +export _JSON_C_STRERROR_ENABLE=1 + # Common definitions if test -z "$srcdir"; then srcdir="${0%/*}" diff --git a/tests/test_json_pointer.c b/tests/test_json_pointer.c index 34fa2026..3174c87e 100644 --- a/tests/test_json_pointer.c +++ b/tests/test_json_pointer.c @@ -1,9 +1,8 @@ -#include "strerror_override.h" -#include "strerror_override_private.h" #ifdef NDEBUG #undef NDEBUG #endif #include +#include #include #include @@ -320,8 +319,6 @@ static void test_wrong_inputs_set(void) int main(int argc, char **argv) { - _json_c_strerror_enable = 1; - test_example_get(); test_recursion_get(); test_wrong_inputs_get(); diff --git a/tests/test_util_file.c b/tests/test_util_file.c index 9d506637..9150586e 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -127,8 +127,6 @@ int main(int argc, char **argv) // json_object_to_file(file, obj); // json_object_to_file_ext(file, obj, flags); - _json_c_strerror_enable = 1; - const char *testdir; if (argc < 2) { -- 2.39.5