Debian Codegraph @

test_suite/tests.source

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