Skip to content

Commit f888f8d

Browse files
authored
CI - add job for shellcheck (#5948)
- Add check/shellcheck for syntax checking of shell scripts - Add CI job to run shellcheck on our shell scripts Resolves #5923.
1 parent c35ccff commit f888f8d

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ jobs:
123123
run: pip install -r dev_tools/requirements/deps/tensorflow-docs.txt
124124
- name: Doc check
125125
run: check/nbformat
126+
shellcheck:
127+
name: Shell check
128+
runs-on: ubuntu-20.04
129+
steps:
130+
- uses: actions/checkout@v2
131+
with:
132+
fetch-depth: 0
133+
- name: Run shellcheck
134+
run: check/shellcheck
126135
isolated-modules:
127136
name: Isolated pytest Ubuntu
128137
runs-on: ubuntu-20.04

check/shellcheck

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env bash
2+
3+
###############################################################################
4+
# Execute shellcheck for all shell scripts in this repository
5+
#
6+
# Usage:
7+
# check/shellcheck [--dry-run] [shellcheck-arguments]
8+
#
9+
# Use the '--dry-run' option to print out the shellcheck command line without
10+
# running it. This displays all shell script files found in the repository.
11+
#
12+
################################################################################
13+
14+
# Change working directory to the repository root.
15+
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
16+
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
17+
cd "${topdir}" || exit $?
18+
19+
# Process command line arguments
20+
opt_dry_run=0
21+
typeset -a shellcheck_options
22+
typeset -a our_shell_scripts
23+
24+
for arg in "$@"; do
25+
if [[ "${arg}" == "--dry-run" ]]; then
26+
opt_dry_run=1
27+
else
28+
shellcheck_options+=( "${arg}" )
29+
fi
30+
done
31+
32+
# Find all shell scripts in this repository.
33+
IFS=$'\n' read -r -d '' -a our_shell_scripts < \
34+
<(git ls-files -z -- \
35+
':(exclude)*.'{py,json,json_inward,repr,repr_inward,ipynb,txt,md} \
36+
':(exclude)*.'{yaml,ts,tst,rst,pyi,cfg} | \
37+
xargs -0 file | grep -i 'shell script' | cut -d: -f1)
38+
39+
# Verify our_shell_scripts array - require it must contain files below.
40+
typeset -a required_shell_scripts
41+
required_shell_scripts=(
42+
# items below must be sorted
43+
check/all
44+
check/doctest
45+
check/format-incremental
46+
check/mypy
47+
check/nbformat
48+
check/pylint
49+
check/pytest
50+
dev_tools/pypath
51+
)
52+
53+
scripts_not_found="$(comm -13 \
54+
<(printf "%s\n" "${our_shell_scripts[@]}") \
55+
<(printf "%s\n" "${required_shell_scripts[@]}") )"
56+
57+
if [[ -n "${scripts_not_found}" ]]; then
58+
echo "Identification of shell scripts failed - files not found:" >&2
59+
printf "\n%s\n\n" "${scripts_not_found}" >&2
60+
echo "Please fix $0." >&2
61+
exit 2
62+
fi
63+
64+
# Ready to run here.
65+
if (( opt_dry_run )); then
66+
printf '%s ' '>>' 'shellcheck' "${shellcheck_options[@]}"
67+
printf '\\\n %s ' "${our_shell_scripts[@]}"
68+
printf '\\\n;\n'
69+
else
70+
shellcheck "${shellcheck_options[@]}" "${our_shell_scripts[@]}"
71+
fi

0 commit comments

Comments
 (0)