Skip to content

Commit bf32bca

Browse files
committed
Allow passing arguments through to shellcheck
This can be used to exclude certain tests. Signed-off-by: dann frazier <dann.frazier@chainguard.dev>
1 parent af9d964 commit bf32bca

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

pre_commit_hooks/shellcheck_run_steps.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
def do_shellcheck(
1818
melange_cfg: Mapping[str, Any],
1919
shellcheck: list[str],
20+
shellcheck_args: list[str],
2021
) -> None:
2122
if melange_cfg == {}:
2223
return
@@ -53,6 +54,7 @@ def do_shellcheck(
5354
shfile.close()
5455
subprocess.check_call(
5556
["/usr/bin/shellcheck"]
57+
+ shellcheck_args
5658
+ ["--shell=busybox", "--"]
5759
+ [os.path.basename(f.name) for _, f in all_steps],
5860
cwd=os.getcwd(),
@@ -61,7 +63,12 @@ def do_shellcheck(
6163

6264
def main(argv: Sequence[str] | None = None) -> int:
6365
parser = argparse.ArgumentParser()
64-
parser.add_argument("filenames", nargs="*", help="Filenames to check.")
66+
parser.add_argument(
67+
"filenames",
68+
nargs="*",
69+
help="Filenames to check. You can also pass "
70+
"arguments to shellcheck before a '--' separator.",
71+
)
6572
parser.add_argument(
6673
"--shellcheck",
6774
default=[
@@ -76,9 +83,16 @@ def main(argv: Sequence[str] | None = None) -> int:
7683
help="shellcheck command",
7784
)
7885
args = parser.parse_args(argv)
86+
try:
87+
idx = args.filenames.index("--")
88+
shellcheck_args = args.filenames[:idx]
89+
filenames = args.filenames[idx + 1 :]
90+
except ValueError:
91+
shellcheck_args = []
92+
filenames = args.filenames
7993

8094
melange_cfg = {}
81-
for filename in args.filenames:
95+
for filename in filenames:
8296
with tempfile.NamedTemporaryFile(
8397
"w",
8498
delete_on_close=False,
@@ -97,7 +111,11 @@ def main(argv: Sequence[str] | None = None) -> int:
97111
try:
98112
with open(compiled_out.name) as compiled_in:
99113
melange_cfg = yaml.load(compiled_in)
100-
do_shellcheck(melange_cfg, args.shellcheck)
114+
do_shellcheck(
115+
melange_cfg,
116+
args.shellcheck,
117+
shellcheck_args,
118+
)
101119
except ruamel.yaml.YAMLError as exc:
102120
print(exc)
103121
return 1

0 commit comments

Comments
 (0)