Skip to content

Commit 3a7b2a5

Browse files
fix jsonnetfmt output when processing stdin with --test
1 parent e094724 commit 3a7b2a5

7 files changed

Lines changed: 32 additions & 29 deletions

cmd/jsonnetfmt.cpp

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ static ArgStatus process_args(int argc, const char **argv, JsonnetConfig *config
123123
} else if (arg == "--") {
124124
// All subsequent args are not options.
125125
while ((++i) < args.size())
126-
remaining_args.push_back(args[i]);
127-
break;
126+
remaining_args.push_back(args[i]);
127+
break;
128128
} else if (arg == "-i" || arg == "--in-place") {
129129
config->fmtInPlace = true;
130130
} else if (arg == "--test") {
@@ -234,10 +234,11 @@ int main(int argc, const char **argv)
234234
bool changed_files = false;
235235
assert(config.inputFiles.size() >= 1);
236236
for (std::string &inputFile : config.inputFiles) {
237+
const bool input_stdin = inputFile == "-";
237238
if (config.fmtInPlace) {
238239
output_file = inputFile;
239240

240-
if (inputFile == "-") {
241+
if (input_stdin) {
241242
std::cerr << "ERROR: cannot use --in-place with stdin" << std::endl;
242243
jsonnet_destroy(vm);
243244
return EXIT_FAILURE;
@@ -265,31 +266,25 @@ int main(int argc, const char **argv)
265266
return EXIT_FAILURE;
266267
}
267268

268-
if (config.fmtTest) {
269-
// Check the output matches the input.
270-
bool ok = output == input;
269+
// Write output Jsonnet only if there is a difference between input and output
270+
bool different = output != input;
271+
if (config.fmtInPlace && different) {
272+
bool successful = write_output_file(output, output_file);
271273
jsonnet_realloc(vm, output, 0);
272-
if (!ok) {
273-
if (inputFile != "-" && !config.filenameIsCode) {
274-
std::cout << inputFile << std::endl;
275-
}
276-
changed_files = true;
274+
if (!successful) {
275+
jsonnet_destroy(vm);
276+
return EXIT_FAILURE;
277277
}
278278
} else {
279-
// Write output Jsonnet only if there is a difference between input and output
280-
bool different = output != input;
281-
if (different) {
282-
bool successful = write_output_file(output, output_file);
283-
jsonnet_realloc(vm, output, 0);
284-
if (!successful) {
285-
jsonnet_destroy(vm);
286-
return EXIT_FAILURE;
287-
}
288-
if (inputFile != "-" && !config.filenameIsCode) {
289-
std::cout << inputFile << std::endl;
290-
}
291-
} else {
292-
jsonnet_realloc(vm, output, 0);
279+
jsonnet_realloc(vm, output, 0);
280+
}
281+
282+
// If we're not processing stdin or some other unnamed thing, then
283+
// print the names of any changed or unclean files.
284+
if (different) {
285+
changed_files = true;
286+
if (!input_stdin && !config.filenameIsCode) {
287+
std::cout << inputFile << std::endl;
293288
}
294289
}
295290
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
test_badfmt.jsonnet

test_cmd/fmt_simple_test4.stdin

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

test_cmd/fmt_simple_test5.golden.stdout

Whitespace-only changes.

test_cmd/fmt_simple_test5.stdin

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}
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ 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-
3230
pushd "${DIR}"
3331

3432
[[ -d "${OUT_DIR}" ]] || {
@@ -179,7 +177,9 @@ do_fmt_test "fmt_double_dash" 0 -e -- -1
179177
do_fmt_test "fmt_simple_test1" 0 --test "test.jsonnet"
180178
do_fmt_test "fmt_simple_test2" 2 --test -e "{a:1,b:2,c:3}"
181179
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"
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"
183183

184184
SED_STDOUT="${SED_REPLACE_OUT_DIR}"
185185
if mkdir -p "${OUT_DIR}/fmt_inplace" && cp "test.jsonnet" "test_badfmt.jsonnet" "${OUT_DIR}/fmt_inplace/"; then

0 commit comments

Comments
 (0)