Skip to content

Commit 75c701c

Browse files
committed
linting: add shelcheck for shell script lint
Add linting for shell scripts in the scipts directory as well as any *.sh file accross the project using shellcheck. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
1 parent 0e242f8 commit 75c701c

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

nix/util.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ rec {
8888

8989
inherit (pkgs)
9090
nixpkgs-fmt
91-
shfmt;
91+
shfmt
92+
shellcheck;
9293

9394
inherit (pkgs.python3Packages)
9495
mpmath sympy black pyparsing pyyaml;

scripts/format

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ if ! command -v shfmt 2>&1 >/dev/null; then
4141
error "shfmt not found. Are you running in a nix shell? See BUILDING.md."
4242
exit 1
4343
fi
44-
shfmt -s -w -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/))
44+
45+
# Find all scripts in the repo
46+
ALL_FILES=$(git grep -l '' :/)
47+
SHELL_SCRIPTS=$(echo "$ALL_FILES" | xargs shfmt -f)
48+
shfmt -s -l -i 2 -ci -fn $SHELL_SCRIPTS
4549

4650
info "Formatting python scripts"
4751
if ! command -v black 2>&1 >/dev/null; then

scripts/lint

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ fi
2020
# Standard color definitions
2121
GREEN="\033[32m"
2222
RED="\033[31m"
23-
BLUE="\033[94m"
24-
BOLD="\033[1m"
2523
NORMAL="\033[0m"
2624

2725
info()
@@ -106,15 +104,36 @@ gh_error_simple()
106104
fi
107105
}
108106

107+
run-shellcheck()
108+
{
109+
if ! command -v shellcheck >/dev/null; then
110+
gh_error_simple "Shellcheck missing" "shellcheck is not installed"
111+
error "Lint shellcheck"
112+
SUCCESS=false
113+
gh_summary_failure "Lint shellcheck"
114+
return 0
115+
fi
116+
117+
checkerr "Lint shellcheck" "$(shellcheck --severity=warning $SHELL_SCRIPTS)"
118+
}
119+
109120
# Formatting
110121
SUCCESS=true
111122

123+
# Get list of shell scripts for linting
124+
ALL_FILES=$(git grep -l '' :/)
125+
SHELL_SCRIPTS=$(echo "$ALL_FILES" | xargs shfmt -f)
126+
112127
gh_group_start "Linting nix files with nixpkgs-fmt"
113128
checkerr "Lint nix" "$(nixpkgs-fmt --check "$ROOT")"
114129
gh_group_end
115130

116131
gh_group_start "Linting shell scripts with shfmt"
117-
checkerr "Lint shell" "$(shfmt -s -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)))"
132+
checkerr "Lint shell" "$(echo $SHELL_SCRIPTS | xargs shfmt -s -l -i 2 -ci -fn)"
133+
gh_group_end
134+
135+
gh_group_start "Linting shell scripts with shellcheck"
136+
run-shellcheck
118137
gh_group_end
119138

120139
gh_group_start "Linting python scripts with black"
@@ -130,7 +149,7 @@ fi
130149
gh_group_end
131150

132151
gh_group_start "Linting c files with clang-format"
133-
checkerr "Lint C" "$(clang-format $(git ls-files ":/*.c" ":/*.h") --Werror --dry-run 2>&1 | grep "error:" | cut -d ':' -f 1,2 | tr ':' ' ')"
152+
checkerr "Lint C" "$(clang-format "$(git ls-files ":/*.c" ":/*.h")" --Werror --dry-run 2>&1 | grep "error:" | cut -d ':' -f 1,2 | tr ':' ' ')"
134153
gh_group_end
135154

136155
check-eol-dry-run()
@@ -152,22 +171,22 @@ check-spdx()
152171
local success=true
153172
for file in $(git ls-files -- ":/" ":/!:*.json" ":/!:*.png" ":/!:*LICENSE*" ":/!:.git*" ":/!:flake.lock"); do
154173
# Ignore symlinks
155-
if [[ ! -L $file && $(grep "SPDX-License-Identifier:" $file | wc -l) == 0 ]]; then
174+
if [[ ! -L $file && $(grep "SPDX-License-Identifier:" "$file" | wc -l) == 0 ]]; then
156175
gh_error "$file" "${line:-1}" "Missing license header error" "$file is missing SPDX License header"
157176
success=false
158177
fi
159178
done
160179
for file in $(git ls-files -- "*.[chsS]" "*.py" "*.mk" "*.yml" "**/Makefile*" ":/!proofs/cbmc/*.py" ":/!examples/bring_your_own_fips202/custom_fips202/tiny_sha3/*"); do
161180
# Ignore symlinks
162-
if [[ ! -L $file && $(grep "Copyright (c) The mldsa-native project authors" $file | wc -l) == 0 ]]; then
181+
if [[ ! -L $file && $(grep "Copyright (c) The mldsa-native project authors" "$file" | wc -l) == 0 ]]; then
163182
gh_error "$file" "${line:-1}" "Missing copyright header error" "$file is missing copyright header"
164183
success=false
165184
fi
166185
done
167186
# For source files in dev/* and mldsa/*, we enforce `Apache-2.0 OR ISC OR MIT`
168187
for file in $(git ls-files -- "*.[chsSi]" | grep "^dev/\|^mldsa/"); do
169188
# Ignore symlinks
170-
if [[ ! -L $file && $(grep "SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT" $file | wc -l) == 0 ]]; then
189+
if [[ ! -L $file && $(grep "SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT" "$file" | wc -l) == 0 ]]; then
171190
gh_error "$file" "${line:-1}" "Missing license header error" "$file is not licensed under 'Apache-2.0 OR ISC OR MIT'"
172191
success=false
173192
fi
@@ -188,7 +207,7 @@ gh_group_end
188207

189208
check-autogenerated-files()
190209
{
191-
if python3 $ROOT/scripts/autogen --dry-run; then
210+
if python3 "$ROOT"/scripts/autogen --dry-run; then
192211
info "Check native auto-generated files"
193212
gh_summary_success "Check native auto-generated files"
194213
else
@@ -204,7 +223,7 @@ gh_group_end
204223

205224
check-magic()
206225
{
207-
if python3 $ROOT/scripts/check-magic >/dev/null; then
226+
if python3 "$ROOT"/scripts/check-magic >/dev/null; then
208227
info "Check magic constants"
209228
gh_summary_success "Check magic constants"
210229
else
@@ -224,7 +243,7 @@ fi
224243

225244
check-contracts()
226245
{
227-
if python3 $ROOT/scripts/check-contracts >/dev/null; then
246+
if python3 "$ROOT"/scripts/check-contracts >/dev/null; then
228247
info "Check contracts"
229248
gh_summary_success "Check contracts"
230249
else

0 commit comments

Comments
 (0)