Skip to content

Commit 45fd7be

Browse files
authored
Exit with code 2 on "expected" errors on lint, fmt, and test (#552)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 1869ed9 commit 45fd7be

37 files changed

+49
-40
lines changed

src/command_fmt.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
9696
<< "\n";
9797
}
9898

99-
throw Fail{EXIT_FAILURE};
99+
// Report a different exit code for formatting check failures, to
100+
// distinguish them from other errors
101+
throw Fail{2};
100102
}
101103
}

src/command_lint.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
238238
output << "\n";
239239
}
240240
} else {
241-
result = false;
241+
// Exception was caught - exit immediately with error code 1
242+
throw Fail{EXIT_FAILURE};
242243
}
243244
}
244245
} else {
@@ -263,7 +264,8 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
263264
if (subresult.first) {
264265
return EXIT_SUCCESS;
265266
} else {
266-
return EXIT_FAILURE;
267+
// Return 2 for logical lint failures
268+
return 2;
267269
}
268270
} catch (const sourcemeta::core::SchemaUnknownBaseDialectError &) {
269271
throw FileError<sourcemeta::core::SchemaUnknownBaseDialectError>(
@@ -274,8 +276,12 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
274276
}
275277
});
276278

277-
if (wrapper_result != EXIT_SUCCESS) {
279+
if (wrapper_result == 2) {
280+
// Logical lint failure
278281
result = false;
282+
} else if (wrapper_result != EXIT_SUCCESS) {
283+
// Exception was caught - exit immediately with error code 1
284+
throw Fail{EXIT_FAILURE};
279285
}
280286
}
281287
}
@@ -299,6 +305,8 @@ auto sourcemeta::jsonschema::lint(const sourcemeta::core::Options &options)
299305
}
300306

301307
if (!result) {
302-
throw Fail{EXIT_FAILURE};
308+
// Report a different exit code for linting failures, to
309+
// distinguish them from other errors
310+
throw Fail{2};
303311
}
304312
}

src/command_test.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ auto sourcemeta::jsonschema::test(const sourcemeta::core::Options &options)
250250
}
251251

252252
if (!result) {
253-
throw Fail{EXIT_FAILURE};
253+
// Report a different exit code for test failures, to
254+
// distinguish them from other errors
255+
throw Fail{2};
254256
}
255257
}

test/format/fail_check_many.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ cat << 'EOF' > "$TMP/schemas/3.json"
3030
EOF
3131

3232
"$1" fmt "$TMP/schemas" --check >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?"
33-
test "$CODE" = "1" || exit 1
33+
test "$CODE" = "2" || exit 1
3434

3535
cat << EOF > "$TMP/expected.txt"
3636
fail: $(realpath "$TMP")/schemas/1.json

test/format/fail_check_many_json.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ cat << 'EOF' > "$TMP/schemas/3.json"
3030
EOF
3131

3232
"$1" fmt "$TMP/schemas" --check --json >"$TMP/output.json" 2>&1 && CODE="$?" || CODE="$?"
33-
test "$CODE" = "1" || exit 1
33+
test "$CODE" = "2" || exit 1
3434

3535
cat << EOF > "$TMP/expected.json"
3636
{

test/format/fail_check_single.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cat << 'EOF' > "$TMP/schema.json"
1515
EOF
1616

1717
"$1" fmt "$TMP/schema.json" --check >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?"
18-
test "$CODE" = "1" || exit 1
18+
test "$CODE" = "2" || exit 1
1919

2020
cat << EOF > "$TMP/expected.txt"
2121
fail: $(realpath "$TMP")/schema.json

test/format/fail_check_single_indentation.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cat << 'EOF' > "$TMP/schema.json"
1515
EOF
1616

1717
"$1" fmt "$TMP/schema.json" --indentation 4 --check >"$TMP/output.txt" 2>&1 && CODE="$?" || CODE="$?"
18-
test "$CODE" = "1" || exit 1
18+
test "$CODE" = "2" || exit 1
1919

2020
cat << EOF > "$TMP/expected.txt"
2121
fail: $(realpath "$TMP")/schema.json

test/format/fail_check_single_json.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ EOF
1818

1919
"$1" fmt "$TMP/this/is/a/very/very/very/long/path/schema.json" \
2020
--check --json >"$TMP/output.json" 2>&1 && CODE="$?" || CODE="$?"
21-
test "$CODE" = "1" || exit 1
21+
test "$CODE" = "2" || exit 1
2222

2323
cat << EOF > "$TMP/expected.json"
2424
{

test/format/fail_check_single_json_indentation.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ EOF
1818

1919
"$1" fmt "$TMP/this/is/a/very/very/very/long/path/schema.json" \
2020
--check --json --indentation 4 >"$TMP/output.json" 2>&1 && CODE="$?" || CODE="$?"
21-
test "$CODE" = "1" || exit 1
21+
test "$CODE" = "2" || exit 1
2222

2323
cat << EOF > "$TMP/expected.json"
2424
{

test/lint/fail_lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ EOF
1717

1818
cd "$TMP"
1919
"$1" lint "$TMP/schema.json" >"$TMP/stderr.txt" 2>&1 && CODE="$?" || CODE="$?"
20-
test "$CODE" = "1" || exit 1
20+
test "$CODE" = "2" || exit 1
2121

2222
cat << EOF > "$TMP/expected.txt"
2323
schema.json:4:3:

0 commit comments

Comments
 (0)