| 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 |
set -uo pipefail |
| 18 |
DIR="$(dirname $0)" |
| 19 |
|
| 20 |
if [[ ! -z "${BAZEL_TEST:-}" ]]; then |
| 21 |
export JSONNET_BIN=$(rlocation "${JSONNET_BIN}") |
| 22 |
export JSONNETFMT_BIN=$(rlocation "${JSONNETFMT_BIN}") |
| 23 |
fi |
| 24 |
|
| 25 |
source "${DIR}/cmd_tests.source" |
| 26 |
|
| 27 |
OUT_DIR=$(mktemp -d) |
| 28 |
trap "rm -rf -- ${OUT_DIR}" EXIT INT TERM |
| 29 |
|
| 30 |
pushd "${DIR}" |
| 31 |
|
| 32 |
[[ -d "${OUT_DIR}" ]] || { |
| 33 |
>&2 echo "OUT_DIR not set to a valid directory" |
| 34 |
exit 1 |
| 35 |
} |
| 36 |
[[ -x "${JSONNET_BIN}" ]] || { |
| 37 |
>&2 echo "JSONNET_BIN does not point to an existing jsonnet binary" |
| 38 |
exit 1 |
| 39 |
} |
| 40 |
[[ -x "${JSONNETFMT_BIN}" ]] || { |
| 41 |
>&2 echo "JSONNETFMT_BIN does not point to an existing jsonnet binary" |
| 42 |
exit 1 |
| 43 |
} |
| 44 |
|
| 45 |
unset SED_STDOUT SED_STDERR |
| 46 |
# A sed command to replace the OUT_DIR at the start of the line. |
| 47 |
SED_REPLACE_OUT_DIR="s@^${OUT_DIR%/}@out@;p" |
| 48 |
|
| 49 |
# And now the tests: |
| 50 |
|
| 51 |
# do_test <name> <exit_code> <args...> |
| 52 |
|
| 53 |
# Only check_file additional files if the test otherwise succeeded, to avoid double-counting |
| 54 |
# failures. All tests must write into ${OUT_DIR}/${NAME}/ and nowhere else. |
| 55 |
|
| 56 |
do_test "no_args" 1 |
| 57 |
do_test "help" 0 --help |
| 58 |
do_test "version1" 0 -v |
| 59 |
do_test "version2" 0 --version |
| 60 |
do_test "bad_out" 1 "test.jsonnet" -o "" |
| 61 |
do_test "simple1" 0 "test.jsonnet" |
| 62 |
do_test "simple3" 0 - |
| 63 |
do_test "simple4" 1 "nosuchfile.jsonnet" |
| 64 |
do_test "simple5" 1 "test.jsonnet" "test.jsonnet" |
| 65 |
if do_test "simple_out" 0 "test.jsonnet" -o "${OUT_DIR}/simple_out/custom_output"; then |
| 66 |
check_file "simple_out" "${OUT_DIR}/simple_out/custom_output" "simple_out.golden.custom_output" |
| 67 |
fi |
| 68 |
do_test "exec1" 0 -e "{ a: 1, b: 2, c: 3 }" |
| 69 |
if do_test "exec_out" 0 -e "{ a: 1, b: 2, c: 3 }" -o "${OUT_DIR}/exec_out/custom_output"; then |
| 70 |
check_file "exec_out" "${OUT_DIR}/exec_out/custom_output" "exec_out.golden.custom_output" |
| 71 |
fi |
| 72 |
do_test "double_dash" 0 -e -- -1 |
| 73 |
do_test "max_stack1" 1 -s 1 -e 'local x = 1; x' |
| 74 |
do_test "max_stack2" 1 --max-stack 1 -e 'local x = 1; x' |
| 75 |
do_test "max_stack3" 0 --max-stack 2 -e 'local x = 1; x' |
| 76 |
do_test "max_stack4" 0 --max-stack 7 -e 'local f(n, c=0) = if n == 0 then c else f(n - 1, c + n) tailstrict; f(100)' |
| 77 |
do_test "max_stack5" 1 --max-stack 0 -e 'true' |
| 78 |
do_test "max_stack6" 1 --max-stack -1 -e 'true' |
| 79 |
do_test "jpath1" 0 --jpath "lib1" -e 'import "lib1_test.jsonnet"' |
| 80 |
do_test "jpath2" 0 -J "lib1" -e 'import "lib1_test.jsonnet"' |
| 81 |
do_test "jpath3" 0 -J "lib2" -e 'import "lib2_test.jsonnet"' |
| 82 |
do_test "jpath4" 0 -J "lib1" -J "lib2" -e 'import "lib2_test.jsonnet"' |
| 83 |
do_test "jpath5" 0 -J "lib2" -J "lib1" -e 'import "lib2_test.jsonnet"' |
| 84 |
do_test "jpath6" 0 -J "lib2" -J "lib1" -e 'importstr "shared.txt"' |
| 85 |
do_test "jpath7" 0 -J "lib1" -J "lib2" -e 'importstr "shared.txt"' |
| 86 |
do_test "jpath8" 1 -J "" -e 'true' |
| 87 |
do_test "jpath9" 0 -J "lib2" -J "lib1" -e 'importbin "shared.txt"' |
| 88 |
do_test "jpath10" 0 -J "lib1" -J "lib2" -e 'importbin "shared.txt"' |
| 89 |
do_test "ext1" 0 --ext-str x=1 -e 'std.extVar("x")' |
| 90 |
do_test "ext2" 0 -V x=1 -e 'std.extVar("x")' |
| 91 |
do_test "ext3" 1 -V y=1 -e 'std.extVar("x")' |
| 92 |
do_test "ext4" 0 --ext-code x=1+1 -e 'std.extVar("x")' |
| 93 |
do_test "ext5" 0 --ext-str-file "x=test.txt" -e 'std.extVar("x")' |
| 94 |
do_test "ext6" 0 --ext-code-file "x=test.jsonnet" -e 'std.extVar("x")' |
| 95 |
do_test "ext7" 0 --ext-code-file "x=lib1/lib3_test.jsonnet" -e 'std.extVar("x")' |
| 96 |
do_test "tla1" 0 --tla-str x=1 -e 'function(x) x' |
| 97 |
do_test "tla2" 0 -A x=1 -e 'function(x) x' |
| 98 |
do_test "tla3" 1 -A y=1 -e 'function(x) x' |
| 99 |
do_test "tla4" 0 --tla-code x=1+1 -e 'function(x) x' |
| 100 |
do_test "tla5" 0 --tla-str-file "x=test.txt" -e 'function(x) x' |
| 101 |
do_test "tla6" 0 --tla-code-file "x=test.jsonnet" -e 'function(x) x' |
| 102 |
do_test "tla7" 1 -A x=val -e 'function() 3' |
| 103 |
do_test "tla8" 0 -A x=val -e '3' |
| 104 |
do_test "tla9" 0 -e 'function() 3' |
| 105 |
do_test "tla10" 0 --tla-code-file "x=lib1/lib3_test.jsonnet" -e 'function(x) x' |
| 106 |
do_test "max_trace1" 1 -t 10 -e 'local f(n, c=0) = if n == 0 then error "whee" else f(n - 1, c + n); f(100)' |
| 107 |
do_test "max_trace2" 1 --max-trace 10 -e 'local f(n, c=0) = if n == 0 then error "whee" else f(n - 1, c + n); f(100)' |
| 108 |
do_test "max_trace3" 1 -t 20 -e 'local f(n, c=0) = if n == 0 then error "whee" else f(n - 1, c + n); f(100)' |
| 109 |
do_test "max_trace4" 1 -t 0 -e 'local f(n, c=0) = if n == 0 then error "whee" else f(n - 1, c + n); f(100)' |
| 110 |
do_test "max_trace5" 1 -t -1 -e 'true' |
| 111 |
|
| 112 |
SED_STDOUT="${SED_REPLACE_OUT_DIR}" |
| 113 |
if do_test "multi1" 0 -m "${OUT_DIR}/multi1" -e '{ file1: "file1", file2: "file2" }'; then |
| 114 |
check_file "multi1" "${OUT_DIR}/multi1/file1" "multi1.golden.file1" |
| 115 |
check_file "multi1" "${OUT_DIR}/multi1/file2" "multi1.golden.file2" |
| 116 |
fi |
| 117 |
if do_test "multi2" 0 --multi "${OUT_DIR}/multi2" -e '{ file1: "file1", file2: "file2" }'; then |
| 118 |
check_file "multi2" "${OUT_DIR}/multi2/file1" "multi2.golden.file1" |
| 119 |
check_file "multi2" "${OUT_DIR}/multi2/file2" "multi2.golden.file2" |
| 120 |
fi |
| 121 |
unset -v SED_STDOUT |
| 122 |
if do_test "multi3" 0 -m "${OUT_DIR}/multi3" -o "${OUT_DIR}/multi3/list" -e '{ file1: "file1", file2: "file2" }'; then |
| 123 |
check_file "multi3" "${OUT_DIR}/multi3/file1" "multi3.golden.file1" |
| 124 |
check_file "multi3" "${OUT_DIR}/multi3/file2" "multi3.golden.file2" |
| 125 |
check_file_sed "multi3" "${OUT_DIR}/multi3/list" "multi3.golden.list" "${SED_REPLACE_OUT_DIR}" |
| 126 |
fi |
| 127 |
|
| 128 |
do_test "nonewline1" 0 --no-trailing-newline -e '42' |
| 129 |
do_test "nonewline2" 0 --no-trailing-newline test.jsonnet |
| 130 |
do_test "nonewline3" 0 --no-trailing-newline --string -e '"hello"' |
| 131 |
SED_STDOUT="${SED_REPLACE_OUT_DIR}" |
| 132 |
if do_test "nonewline_multi1" 0 --no-trailing-newline --multi "${OUT_DIR}/nonewline_multi1" -e '{ file1: "file1", file2: "file2" }'; then |
| 133 |
check_file "nonewline_multi1" "${OUT_DIR}/nonewline_multi1/file1" "nonewline_multi1.golden.file1" |
| 134 |
check_file "nonewline_multi1" "${OUT_DIR}/nonewline_multi1/file2" "nonewline_multi1.golden.file2" |
| 135 |
fi |
| 136 |
if do_test "nonewline_multi2" 0 --no-trailing-newline --multi "${OUT_DIR}/nonewline_multi2" --string -e '{ file1: "file1", file2: "file2" }'; then |
| 137 |
check_file "nonewline_multi2" "${OUT_DIR}/nonewline_multi2/file1" "nonewline_multi2.golden.file1" |
| 138 |
check_file "nonewline_multi2" "${OUT_DIR}/nonewline_multi2/file2" "nonewline_multi2.golden.file2" |
| 139 |
fi |
| 140 |
unset -v SED_STDOUT |
| 141 |
do_test "nonewline_yaml1" 1 --no-trailing-newline --yaml-stream -e '[{a:1},{b:2},{c:3}]' |
| 142 |
|
| 143 |
do_test "multi4" 1 -m -- -e 'null' |
| 144 |
do_test "yaml1" 0 -y -e '[1,2,3]' |
| 145 |
do_test "yaml2" 1 -y -e 'null' |
| 146 |
if do_test "yaml3" 0 -y -o "${OUT_DIR}/yaml3/stream" -e '[1,2,3]'; then |
| 147 |
check_file "yaml3" "${OUT_DIR}/yaml3/stream" "yaml3.golden.stream" |
| 148 |
fi |
| 149 |
do_test "string1" 0 -S -e '"A long\nparagraph."' |
| 150 |
do_test "string2" 1 -S -e 'null' |
| 151 |
|
| 152 |
export JSONNET_PATH=lib1:lib2 |
| 153 |
do_test "jsonnet_path1" 0 -e '[importstr "shared.txt", importbin "shared.txt"]' |
| 154 |
export JSONNET_PATH=lib2:lib1 |
| 155 |
do_test "jsonnet_path2" 0 -e '[importstr "shared.txt", importbin "shared.txt"]' |
| 156 |
|
| 157 |
if [ -z "${DISABLE_FMT_TESTS:-}" ]; then |
| 158 |
|
| 159 |
do_fmt_test "fmt_no_args" 1 |
| 160 |
do_fmt_test "fmt_help" 0 --help |
| 161 |
do_fmt_test "fmt_version1" 0 -v |
| 162 |
do_fmt_test "fmt_version2" 0 --version |
| 163 |
do_fmt_test "fmt_bad_out" 1 "test.jsonnet" -o "" |
| 164 |
do_fmt_test "fmt_simple1" 0 "test.jsonnet" |
| 165 |
do_fmt_test "fmt_simple3" 0 - |
| 166 |
do_fmt_test "fmt_simple4" 1 "nosuchfile.jsonnet" |
| 167 |
do_fmt_test "fmt_simple5" 1 "test.jsonnet" "test.jsonnet" |
| 168 |
if do_fmt_test "fmt_simple_out" 0 "test.jsonnet" -o "${OUT_DIR}/fmt_simple_out/custom_output"; then |
| 169 |
check_file "fmt_simple_out" "${OUT_DIR}/fmt_simple_out/custom_output" "fmt_simple_out.golden.custom_output" |
| 170 |
fi |
| 171 |
do_fmt_test "fmt1" 0 -e "{ a: 1, b: 2, c: 3 }" |
| 172 |
if do_fmt_test "fmt_out" 0 -e "{ a: 1, b: 2, c: 3 }" -o "${OUT_DIR}/fmt_out/custom_output"; then |
| 173 |
check_file "fmt_out" "${OUT_DIR}/fmt_out/custom_output" "fmt_out.golden.custom_output" |
| 174 |
fi |
| 175 |
do_fmt_test "fmt_double_dash" 0 -e -- -1 |
| 176 |
|
| 177 |
do_fmt_test "fmt_simple_test1" 0 --test "test.jsonnet" |
| 178 |
do_fmt_test "fmt_simple_test2" 2 --test -e "{a:1,b:2,c:3}" |
| 179 |
do_fmt_test "fmt_simple_test3" 0 --test -e $'42\n' |
| 180 |
do_fmt_test "fmt_simple_test4" 0 --test - |
| 181 |
do_fmt_test "fmt_simple_test5" 2 --test - |
| 182 |
do_fmt_test "fmt_simple_test6" 2 --test "test.jsonnet" "test_badfmt.jsonnet" |
| 183 |
|
| 184 |
SED_STDOUT="${SED_REPLACE_OUT_DIR}" |
| 185 |
if mkdir -p "${OUT_DIR}/fmt_inplace" && cp "test.jsonnet" "test_badfmt.jsonnet" "${OUT_DIR}/fmt_inplace/"; then |
| 186 |
# Test jsonnetfmt in-place modifications |
| 187 |
check_file "fmt_inplace" "${OUT_DIR}/fmt_inplace/test.jsonnet" "fmt_simple_out.golden.custom_output" |
| 188 |
# Verify that running jsonnetfmt on an already formatted file does not change timesetamps |
| 189 |
touch -m -t 199108252057.08 "${OUT_DIR}/fmt_inplace/test.jsonnet" |
| 190 |
touch -m -t 199108252057.08 "${OUT_DIR}/fmt_inplace/test_badfmt.jsonnet" |
| 191 |
stat -c '%y' "${OUT_DIR}/fmt_inplace/test.jsonnet" > "${OUT_DIR}/fmt_inplace/stat_mod_time_before.txt" |
| 192 |
do_fmt_test "fmt_inplace" 0 -i \ |
| 193 |
"${OUT_DIR}/fmt_inplace/test.jsonnet" "${OUT_DIR}/fmt_inplace/test_badfmt.jsonnet" |
| 194 |
stat -c '%y' "${OUT_DIR}/fmt_inplace/test.jsonnet" > "${OUT_DIR}/fmt_inplace/stat_mod_time_after.txt" |
| 195 |
check_file "fmt_inplace" "${OUT_DIR}/fmt_inplace/stat_mod_time_before.txt" "${OUT_DIR}/fmt_inplace/stat_mod_time_after.txt" |
| 196 |
fi |
| 197 |
unset -v SED_STDOUT |
| 198 |
|
| 199 |
fi |
| 200 |
|
| 201 |
|
| 202 |
popd |
| 203 |
|
| 204 |
# Bean counting: |
| 205 |
|
| 206 |
if [ $FAILED -eq 0 ] ; then |
| 207 |
echo "$0: All $EXECUTED test scripts pass." |
| 208 |
else |
| 209 |
echo "$0: FAILED: $FAILED / $EXECUTED" |
| 210 |
exit 1 |
| 211 |
fi |