summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Haszlakiewicz <erh+git@nimenees.com>2020-07-04 18:56:15 +0000
committerEric Haszlakiewicz <erh+git@nimenees.com>2020-07-04 18:58:00 +0000
commit6068d3f6d1dd4bd8be95e6bf4ed3e9148291b408 (patch)
treeacb72e207ff341497aa968a595de447a386262d9
parentb01d50b8dc73ef678b71dc6fdbb16dcc517653bf (diff)
downloadjson-c-6068d3f6d1dd4bd8be95e6bf4ed3e9148291b408.tar.gz
Change the strerror_override handling to check $_JSON_C_STRERROR_OVERRIDE instead of using a variable, so we don't need to export it.
-rw-r--r--strerror_override.c5
-rw-r--r--tests/CMakeLists.txt4
-rwxr-xr-xtests/test_basic.test2
-rw-r--r--tests/test_json_pointer.c5
-rw-r--r--tests/test_util_file.c2
5 files changed, 11 insertions, 7 deletions
diff --git a/strerror_override.c b/strerror_override.c
index a93be38..7a262f7 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 a871573..125f615 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 154e036..474e7a8 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 34fa202..3174c87 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 <assert.h>
+#include <errno.h>
#include <stdio.h>
#include <string.h>
@@ -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 9d50663..9150586 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)
{