1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#!/usr/bin/env bash
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Global counters
EXECUTED=0
FAILED=0
PYTHON_COMMAND=${PYTHON_COMMAND:=python3}
JSONNET_BIN="${JSONNET_BIN:-../jsonnet}"
JSONNETFMT_BIN="${JSONNETFMT_BIN:-../jsonnetfmt}"
# Display additional information such as successful tests (useful if they are slow).
VERBOSE="${VERBOSE:-false}"
separator() {
echo -e "\n-----------------------------\n"
}
get_temp_dir() {
if [ -z "$TMP_DIR" ]; then
# We use Python to avoid portability problems with `mktemp`.
# See: https://unix.stackexchange.com/questions/30091/fix-or-alternative-for-mktemp-in-os-x
TMP_DIR="`$PYTHON_COMMAND -c "import tempfile; print(tempfile.mkdtemp(prefix='jsonnet_'))"`"
$($VERBOSE) && echo "Created temporary directory $TMP_DIR"
fi
}
remove_temp_dir() {
if [ -n "$TMP_DIR" ]; then
$($VERBOSE) && echo "Removing temporary directory $TMP_DIR"
rm -rf "$TMP_DIR"
TMP_DIR=""
fi
}
init() {
get_temp_dir
}
deinit() {
remove_temp_dir
}
postprocess_output() {
# Redact stdlib line numbers - otherwise many error tests need to change with every stdlib change
# TODO(sbarzowski) Consider moving noise-reducing functionality to Jsonnet interpreter
sed 's/ std\.jsonnet:[0-9]*:[0-9-]*/ std.jsonnet:<stdlib_position_redacted>/'
}
test_eval() {
JSONNET_CMD="$1"
JSONNET_FILE="$2"
EXPECTED_EXIT_CODE="$3"
GOLDEN_OUTPUT="$4"
GOLDEN_KIND="$5"
if [ -n "$DISABLE_ERROR_TESTS" ] && [ "$EXPECTED_EXIT_CODE" != 0 ]; then
return 0
fi
TEST_CMD="$JSONNET_CMD $JSONNET_FILE"
TEST_OUTPUT="$(eval $TEST_CMD 2>&1)"
TEST_EXIT_CODE="$?"
TEST_OUTPUT="$(echo "$TEST_OUTPUT" | postprocess_output)"
EXECUTED=$((EXECUTED + 1))
if [ "$TEST_EXIT_CODE" -ne "$EXPECTED_EXIT_CODE" ] ; then
FAILED=$((FAILED + 1))
if [ -z "$SUMMARY_ONLY" ]; then
printf "\033[31;1mFAIL\033[0m \033[1m(exit code)\033[0m: \033[36m$TEST_CMD\033[0m\n"
echo "This run's output:"
echo "$TEST_OUTPUT"
echo "Actual exit code $TEST_EXIT_CODE, expected $EXPECTED_EXIT_CODE"
separator
fi
return 1
fi
case "$GOLDEN_KIND" in
"REGEX")
if [[ ! "$TEST_OUTPUT" =~ $GOLDEN_OUTPUT ]] ; then
FAILED=$((FAILED + 1))
if [ -z "$SUMMARY_ONLY" ]; then
printf "\033[31;1mFAIL\033[0m \033[1m(regex mismatch)\033[0m: \033[36m$TEST_CMD\033[0m\n"
echo "This run's output:"
echo "$TEST_OUTPUT"
separator
fi
return 1
fi
;;
"PLAIN")
if [ "$TEST_OUTPUT" != "$GOLDEN_OUTPUT" ] ; then
FAILED=$((FAILED + 1))
if [ -z "$SUMMARY_ONLY" ]; then
printf "\033[31;1mFAIL\033[0m \033[1m(output mismatch)\033[0m: \033[36m$TEST_CMD\033[0m\n"
echo "This run's output:"
echo "$TEST_OUTPUT"
echo "Expected:"
echo "$GOLDEN_OUTPUT"
echo "Diff:"
TEST_OUTPUT_FILE="$TMP_DIR/jsonnet_output"
TEST_GOLDEN_FILE="$TMP_DIR/jsonnet_golden"
echo "$TEST_OUTPUT" > "$TEST_OUTPUT_FILE"
echo "$GOLDEN_OUTPUT" > "$TEST_GOLDEN_FILE"
# Using git diff for pretty format and pretty colors
git --no-pager diff --no-index "$TEST_GOLDEN_FILE" "$TEST_OUTPUT_FILE"
# The output is often quite long, let's repeat the filename once more
# to avoid wasting time on looking for it
printf "\nTEST FILE ABOVE: \033[36m$TEST_CMD\033[0m\n"
separator
fi
return 1
fi
;;
*)
echo "Unrecognized GOLDEN_KIND: $GOLDEN_KIND" >&2
exit 1
;;
esac
$($VERBOSE) && printf "\033[32mSUCCESS\033[0m: \033[36m$JSONNET_FILE\033[0m\n"
}
|