Skip to content

Commit e094724

Browse files
print names of unclean files in jsonnetfmt --test and --in-place
And add test_cmd test cases for it Fixes #365 Go-jsonnet feature request for it: google/go-jsonnet#768
1 parent 9dfe1f5 commit e094724

8 files changed

Lines changed: 31 additions & 5 deletions

cmd/jsonnetfmt.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ int main(int argc, const char **argv)
231231
std::string output_file = config.outputFile;
232232

233233
if (config.fmtInPlace || config.fmtTest) {
234+
bool changed_files = false;
234235
assert(config.inputFiles.size() >= 1);
235236
for (std::string &inputFile : config.inputFiles) {
236237
if (config.fmtInPlace) {
@@ -269,8 +270,10 @@ int main(int argc, const char **argv)
269270
bool ok = output == input;
270271
jsonnet_realloc(vm, output, 0);
271272
if (!ok) {
272-
jsonnet_destroy(vm);
273-
return 2;
273+
if (inputFile != "-" && !config.filenameIsCode) {
274+
std::cout << inputFile << std::endl;
275+
}
276+
changed_files = true;
274277
}
275278
} else {
276279
// Write output Jsonnet only if there is a difference between input and output
@@ -282,11 +285,19 @@ int main(int argc, const char **argv)
282285
jsonnet_destroy(vm);
283286
return EXIT_FAILURE;
284287
}
288+
if (inputFile != "-" && !config.filenameIsCode) {
289+
std::cout << inputFile << std::endl;
290+
}
285291
} else {
286292
jsonnet_realloc(vm, output, 0);
287293
}
288294
}
289295
}
296+
297+
if (config.fmtTest && changed_files) {
298+
jsonnet_destroy(vm);
299+
return 2;
300+
}
290301
} else {
291302
assert(config.inputFiles.size() == 1);
292303
// Read input file.

test_cmd/fmt_inplace.golden.stdout

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out/fmt_inplace/test_badfmt.jsonnet

test_cmd/fmt_simple_test1.golden.stdout

Whitespace-only changes.

test_cmd/fmt_simple_test2.golden.stdout

Whitespace-only changes.

test_cmd/fmt_simple_test3.golden.stdout

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test_badfmt.jsonnet

test_cmd/run_cmd_tests.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ source "${DIR}/cmd_tests.source"
2727
OUT_DIR=$(mktemp -d)
2828
trap "rm -rf -- ${OUT_DIR}" EXIT INT TERM
2929

30+
>&2 echo "output dir: ${OUT_DIR}"
31+
3032
pushd "${DIR}"
3133

3234
[[ -d "${OUT_DIR}" ]] || {
@@ -174,17 +176,25 @@ if do_fmt_test "fmt_out" 0 -e "{ a: 1, b: 2, c: 3 }" -o "${OUT_DIR}/fmt_out/cust
174176
fi
175177
do_fmt_test "fmt_double_dash" 0 -e -- -1
176178

177-
if mkdir -p "${OUT_DIR}/fmt_inplace" && cp "test.jsonnet" "${OUT_DIR}/fmt_inplace/test.jsonnet"; then
179+
do_fmt_test "fmt_simple_test1" 0 --test "test.jsonnet"
180+
do_fmt_test "fmt_simple_test2" 2 --test -e "{a:1,b:2,c:3}"
181+
do_fmt_test "fmt_simple_test3" 0 --test -e $'42\n'
182+
do_fmt_test "fmt_simple_test4" 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
178186
# Test jsonnetfmt in-place modifications
179-
do_fmt_test "fmt_inplace" 0 -i "${OUT_DIR}/fmt_inplace/test.jsonnet"
180187
check_file "fmt_inplace" "${OUT_DIR}/fmt_inplace/test.jsonnet" "fmt_simple_out.golden.custom_output"
181188
# Verify that running jsonnetfmt on an already formatted file does not change timesetamps
182189
touch -m -t 199108252057.08 "${OUT_DIR}/fmt_inplace/test.jsonnet"
190+
touch -m -t 199108252057.08 "${OUT_DIR}/fmt_inplace/test_badfmt.jsonnet"
183191
stat -c '%y' "${OUT_DIR}/fmt_inplace/test.jsonnet" > "${OUT_DIR}/fmt_inplace/stat_mod_time_before.txt"
184-
do_fmt_test "fmt_inplace" 0 -i "${OUT_DIR}/fmt_inplace/test.jsonnet"
192+
do_fmt_test "fmt_inplace" 0 -i \
193+
"${OUT_DIR}/fmt_inplace/test.jsonnet" "${OUT_DIR}/fmt_inplace/test_badfmt.jsonnet"
185194
stat -c '%y' "${OUT_DIR}/fmt_inplace/test.jsonnet" > "${OUT_DIR}/fmt_inplace/stat_mod_time_after.txt"
186195
check_file "fmt_inplace" "${OUT_DIR}/fmt_inplace/stat_mod_time_before.txt" "${OUT_DIR}/fmt_inplace/stat_mod_time_after.txt"
187196
fi
197+
unset -v SED_STDOUT
188198

189199
fi
190200

test_cmd/test_badfmt.jsonnet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{a:1, b:
2+
3+
2,['c']:3}

0 commit comments

Comments
 (0)