Add back the old ENV variabe approach to json_process.

Enable using the old envrionment variable passing of settings for json_process
so that the current autoconf test suite scripts can run nicely.

json_process now takes an optional command line parameter --env which
causes it to use the old method of reading the settings from
environment variables instead of directly from the "env" file.

Also added a --strip command line option, this will be used to
run the stripped tests with CMake as well.
This commit is contained in:
Joakim Söderberg 2013-03-13 00:09:08 +01:00
parent 650707fccc
commit b1b4f307d5
6 changed files with 141 additions and 19 deletions

View File

@ -36,6 +36,7 @@ struct config {
int ensure_ascii;
int sort_keys;
int strip;
int use_env;
} conf;
#define l_isspace(c) ((c) == ' ' || (c) == '\n' || (c) == '\r' || (c) == '\t')
@ -145,8 +146,7 @@ static int cmpfile(const char *str, const char *path, const char *fname)
return ret;
}
int main(int argc, char *argv[])
int use_conf(int argc, char **argv, char *test_path)
{
int ret;
size_t flags = 0;
@ -156,22 +156,13 @@ int main(int argc, char *argv[])
json_t *json;
json_error_t error;
#ifdef HAVE_SETLOCALE
setlocale(LC_ALL, "");
#endif
if (argc != 2) {
fprintf(stderr, "usage: %s test_dir\n", argv[0]);
return 2;
}
sprintf(filename, "%s%cinput", argv[1], dir_sep);
sprintf(filename, "%s%cinput", test_path, dir_sep);
if (!(infile = fopen(filename, "rb"))) {
fprintf(stderr, "Could not open \"%s\"\n", filename);
return 2;
}
sprintf(filename, "%s%cenv", argv[1], dir_sep);
sprintf(filename, "%s%cenv", test_path, dir_sep);
conffile = fopen(filename, "rb");
if (conffile) {
read_conf(conffile);
@ -214,14 +205,144 @@ int main(int argc, char *argv[])
error.line, error.column, error.position,
error.text);
ret = cmpfile(errstr, argv[1], "error");
ret = cmpfile(errstr, test_path, "error");
return ret;
}
buffer = json_dumps(json, flags);
ret = cmpfile(buffer, argv[1], "output");
ret = cmpfile(buffer, test_path, "output");
free(buffer);
json_decref(json);
return ret;
}
static int getenv_int(const char *name)
{
char *value, *end;
long result;
value = getenv(name);
if(!value)
return 0;
result = strtol(value, &end, 10);
if(*end != '\0')
return 0;
return (int)result;
}
int use_env(int argc, char **argv)
{
int indent;
size_t flags = 0;
json_t *json;
json_error_t error;
#ifdef _WIN32
/* On Windows, set stdout and stderr to binary mode to avoid
outputting DOS line terminators */
_setmode(_fileno(stdout), _O_BINARY);
_setmode(_fileno(stderr), _O_BINARY);
#endif
indent = getenv_int("JSON_INDENT");
if(indent < 0 || indent > 255) {
fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
return 2;
}
if(indent > 0)
flags |= JSON_INDENT(indent);
if(getenv_int("JSON_COMPACT") > 0)
flags |= JSON_COMPACT;
if(getenv_int("JSON_ENSURE_ASCII"))
flags |= JSON_ENSURE_ASCII;
if(getenv_int("JSON_PRESERVE_ORDER"))
flags |= JSON_PRESERVE_ORDER;
if(getenv_int("JSON_SORT_KEYS"))
flags |= JSON_SORT_KEYS;
if(getenv_int("STRIP")) {
/* Load to memory, strip leading and trailing whitespace */
size_t size = 0, used = 0;
char *buffer = NULL;
while(1) {
int count;
size = (size == 0 ? 128 : size * 2);
buffer = realloc(buffer, size);
if(!buffer) {
fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
return 1;
}
count = fread(buffer + used, 1, size - used, stdin);
if(count < size - used) {
buffer[used + count] = '\0';
break;
}
used += count;
}
json = json_loads(strip(buffer), 0, &error);
free(buffer);
}
else
json = json_loadf(stdin, 0, &error);
if(!json) {
fprintf(stderr, "%d %d %d\n%s\n",
error.line, error.column,
error.position, error.text);
return 1;
}
json_dumpf(json, stdout, flags);
return 0;
}
int main(int argc, char *argv[])
{
int i;
char *test_path = NULL;
#ifdef HAVE_SETLOCALE
setlocale(LC_ALL, "");
#endif
if (argc < 2) {
goto usage;
}
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--strip"))
conf.strip = 1;
else if (!strcmp(argv[i], "--env"))
conf.use_env = 1;
else
test_path = argv[i];
}
if (conf.use_env)
return use_env(argc, argv);
else
{
if (!test_path)
goto usage;
return use_conf(argc, argv, test_path);
}
usage:
fprintf(stderr, "argc =%d\n", argc);
fprintf(stderr, "usage: %s [--strip] [--env] test_dir\n", argv[0]);
return 2;
}

View File

@ -14,7 +14,7 @@ run_test() {
if [ -f $test_path/env ]; then
. $test_path/env
fi
$json_process <$test_path/input >$test_log/stdout 2>$test_log/stderr
$json_process --env <$test_path/input >$test_log/stdout 2>$test_log/stderr
)
valgrind_check $test_log/stderr || return 1
cmp -s $test_path/output $test_log/stdout

View File

@ -10,7 +10,7 @@ is_test() {
}
run_test() {
$json_process <$test_path/input >$test_log/stdout 2>$test_log/stderr
$json_process --env <$test_path/input >$test_log/stdout 2>$test_log/stderr
valgrind_check $test_log/stderr || return 1
cmp -s $test_path/error $test_log/stderr
}

View File

@ -20,7 +20,7 @@ do_run() {
strip=1
fi
STRIP=$strip $json_process \
STRIP=$strip $json_process --env \
<$test_path/input >$test_log/stdout$s 2>$test_log/stderr$s
valgrind_check $test_log/stderr$s || return 1

View File

@ -0,0 +1 @@
JSON_SORT_KEYS=1

View File

@ -19,7 +19,7 @@ do_run() {
strip=0
[ "$variant" = "strip" ] && strip=1
STRIP=$strip $json_process \
STRIP=$strip $json_process --env \
<$test_path/input >$test_log/stdout$s 2>$test_log/stderr$s
valgrind_check $test_log/stderr$s || return 1