Skip to content

Commit 8f31198

Browse files
committed
shellcheck_run_steps: shellcheck errors should not be exceptions
The stack trace is obnoxious. This refactoring also means we see errors in all files instead of just the first one that failed. Signed-off-by: dann frazier <dann.frazier@chainguard.dev>
1 parent 0a5467f commit 8f31198

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

pre_commit_hooks/shellcheck_run_steps.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
DefaultShellCheckImage = "koalaman/shellcheck@sha256:652a5a714dc2f5f97e36f565d4f7d2322fea376734f3ec1b04ed54ce2a0b124f"
1919

2020

21+
# Returns False if shellcheck reports issues
2122
def do_shellcheck(
2223
melange_cfg: Mapping[str, Any],
2324
shellcheck: list[str],
2425
shellcheck_args: list[str],
25-
) -> None:
26+
) -> bool:
2627
if melange_cfg == {}:
27-
return
28+
return True
2829

2930
pkgs = [melange_cfg]
3031
pkgs.extend(melange_cfg.get("subpackages", []))
@@ -54,17 +55,22 @@ def do_shellcheck(
5455
),
5556
)
5657
if len(all_steps) == 0:
57-
return
58+
return True
5859
for step, shfile in all_steps:
5960
shfile.write(step["runs"])
6061
shfile.close()
61-
subprocess.check_call(
62-
shellcheck
63-
+ shellcheck_args
64-
+ ["--shell=busybox", "--"]
65-
+ [os.path.basename(f.name) for _, f in all_steps],
66-
cwd=os.getcwd(),
67-
)
62+
try:
63+
subprocess.check_call(
64+
shellcheck
65+
+ shellcheck_args
66+
+ ["--shell=busybox", "--"]
67+
+ [os.path.basename(f.name) for _, f in all_steps],
68+
cwd=os.getcwd(),
69+
)
70+
except subprocess.CalledProcessError:
71+
return False
72+
73+
return True
6874

6975

7076
def main(argv: Sequence[str] | None = None) -> int:
@@ -95,6 +101,7 @@ def main(argv: Sequence[str] | None = None) -> int:
95101
shellcheck_args = []
96102
filenames = args.filenames
97103

104+
fail_cnt = 0
98105
melange_cfg = {}
99106
for filename in filenames:
100107
with tempfile.NamedTemporaryFile(
@@ -115,17 +122,23 @@ def main(argv: Sequence[str] | None = None) -> int:
115122
try:
116123
with open(compiled_out.name) as compiled_in:
117124
melange_cfg = yaml.load(compiled_in)
118-
do_shellcheck(
125+
if not do_shellcheck(
119126
melange_cfg,
120127
args.shellcheck,
121128
shellcheck_args,
122-
)
129+
):
130+
fail_cnt += 1
123131
except ruamel.yaml.YAMLError as exc:
124132
print(exc)
125-
return 1
133+
fail_cnt += 1
126134

127-
return 0
135+
return fail_cnt
128136

129137

130138
if __name__ == "__main__":
131-
raise SystemExit(main())
139+
fail_cnt = main()
140+
exit_code = 0
141+
if fail_cnt != 0:
142+
exit_code = 1
143+
144+
raise SystemExit(exit_code)

0 commit comments

Comments
 (0)