From 8c772eaa0dfe227d998e5d5f7b97bd514f3eae3c Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Wed, 16 Aug 2017 16:31:27 +0000 Subject: [PATCH] test: Use small Python script for JSON normalization instead of json.tool json.tool does not sort or otherwise normalize the order of JSON keys in its output, which can result in test failures on some test systems. Instead, use a one-line Python script passed to the interpreter directly on its command line. Use sort_keys=True for json.dump to ensure the key order is normalized. The script works with both Python 2 and 3. * test/test-lib.sh: Update test_expect_equal_json. --- test/test-lib.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/test-lib.sh b/test/test-lib.sh index c505e764..d2b2a47f 100644 --- a/test/test-lib.sh +++ b/test/test-lib.sh @@ -450,9 +450,10 @@ test_expect_equal_json () { # The test suite forces LC_ALL=C, but this causes Python 3 to # decode stdin as ASCII. We need to read JSON in UTF-8, so # override Python's stdio encoding defaults. - output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \ + local script='import json, sys; json.dump(json.load(sys.stdin), sys.stdout, sort_keys=True, indent=4)' + output=$(echo "$1" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -c "$script" \ || echo "$1") - expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -mjson.tool \ + expected=$(echo "$2" | PYTHONIOENCODING=utf-8 $NOTMUCH_PYTHON -c "$script" \ || echo "$2") shift 2 test_expect_equal "$output" "$expected" "$@" -- 2.26.2