Skip to content

Commit 545af13

Browse files
committed
test: add copyright checking and shell liting
Signed-off-by: willieyz <[email protected]>
1 parent 1736882 commit 545af13

File tree

1 file changed

+112
-29
lines changed

1 file changed

+112
-29
lines changed

scripts/lint

Lines changed: 112 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@ set -o pipefail
1111
ROOT="$(realpath "$(dirname "$0")"/../)"
1212
GITHUB_STEP_SUMMARY=${GITHUB_STEP_SUMMARY:-/dev/stdout}
1313

14+
# Check if we're in GitHub context
15+
IN_GITHUB_CONTEXT=false
16+
if [[ $GITHUB_STEP_SUMMARY != "/dev/stdout" ]]; then
17+
IN_GITHUB_CONTEXT=true
18+
fi
19+
20+
# Standard color definitions
21+
GREEN="\033[32m"
22+
RED="\033[31m"
23+
BLUE="\033[94m"
24+
BOLD="\033[1m"
25+
NORMAL="\033[0m"
26+
27+
info()
28+
{
29+
printf "%b %b\n" "${GREEN}" "${NORMAL}${*}"
30+
}
31+
32+
error()
33+
{
34+
printf "%b %b\n" "${RED}" "${NORMAL}${*}"
35+
}
36+
1437
checkerr()
1538
{
1639
local code=$?
@@ -22,44 +45,92 @@ checkerr()
2245
fi
2346

2447
if [[ ${#out} != 0 ]]; then
25-
echo "$out" | while read -r file line; do
26-
echo "::error file=$file,line=${line:-1},title=Format error::$file require to be formatted"
27-
done
48+
if $IN_GITHUB_CONTEXT; then
49+
echo "$out" | while read -r file line; do
50+
echo "::error file=$file,line=${line:-1},title=Format error::$file require to be formatted"
51+
done
52+
fi
2853
success=false
2954
fi
3055

3156
if $success; then
32-
echo ":white_check_mark: $title" >>"$GITHUB_STEP_SUMMARY"
57+
info "$title"
58+
gh_summary_success "$title"
3359
else
60+
error "$title"
3461
SUCCESS=false
35-
echo ":x: $title" >>"$GITHUB_STEP_SUMMARY"
62+
gh_summary_failure "$title"
63+
fi
64+
}
65+
66+
gh_group_start()
67+
{
68+
if $IN_GITHUB_CONTEXT; then
69+
echo "::group::$1"
70+
fi
71+
}
72+
73+
gh_group_end()
74+
{
75+
if $IN_GITHUB_CONTEXT; then
76+
echo "::endgroup::"
77+
fi
78+
}
79+
80+
gh_summary_success()
81+
{
82+
if $IN_GITHUB_CONTEXT; then
83+
echo ":white_check_mark: $1" >>"$GITHUB_STEP_SUMMARY"
84+
fi
85+
}
86+
87+
gh_summary_failure()
88+
{
89+
if $IN_GITHUB_CONTEXT; then
90+
echo ":x: $1" >>"$GITHUB_STEP_SUMMARY"
91+
fi
92+
}
93+
94+
gh_error()
95+
{
96+
if $IN_GITHUB_CONTEXT; then
97+
echo "::error file=$1,line=${2:-1},title=$3::$4"
98+
fi
99+
}
100+
101+
gh_error_simple()
102+
{
103+
if $IN_GITHUB_CONTEXT; then
104+
echo "::error title=$1::$2"
36105
fi
37106
}
38107

39108
# Formatting
40109
SUCCESS=true
41110

42-
echo "::group::Linting nix files with nixpkgs-fmt"
111+
gh_group_start "Linting nix files with nixpkgs-fmt"
43112
checkerr "Lint nix" "$(nixpkgs-fmt --check "$ROOT")"
44-
echo "::endgroup::"
113+
gh_group_end
45114

46-
#echo "::group::Linting shell scripts with shfmt"
47-
#checkerr "Lint shell" "$(shfmt -s -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)))"
48-
#echo "::endgroup::"
115+
gh_group_start "Linting shell scripts with shfmt"
116+
checkerr "Lint shell" "$(shfmt -s -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)))"
117+
gh_group_end
49118

50-
echo "::group::Linting python scripts with black"
119+
gh_group_start "Linting python scripts with black"
51120
if ! diff=$(black --check --diff -q --include "(scripts/tests|scripts/simpasm|scripts/autogen|scripts/check-namespace|\.py$)" "$ROOT"); then
52-
echo "::error title=Format error::$diff"
121+
gh_error_simple "Format error" "$diff"
122+
error "Lint python"
53123
SUCCESS=false
54-
echo ":x: Lint python" >>"$GITHUB_STEP_SUMMARY"
124+
gh_summary_failure "Lint python"
55125
else
56-
echo ":white_check_mark: Lint Python" >>"$GITHUB_STEP_SUMMARY"
126+
info "Lint Python"
127+
gh_summary_success "Lint Python"
57128
fi
58-
echo "::endgroup::"
129+
gh_group_end
59130

60-
echo "::group::Linting c files with clang-format"
131+
gh_group_start "Linting c files with clang-format"
61132
checkerr "Lint C" "$(clang-format $(git ls-files ":/*.c" ":/*.h") --Werror --dry-run 2>&1 | grep "error:" | cut -d ':' -f 1,2 | tr ':' ' ')"
62-
echo "::endgroup::"
133+
gh_group_end
63134

64135
check-eol-dry-run()
65136
{
@@ -71,52 +142,64 @@ check-eol-dry-run()
71142
fi
72143
done
73144
}
74-
echo "::group::Checking eol"
145+
gh_group_start "Checking eol"
75146
checkerr "Check eol" "$(check-eol-dry-run)"
76-
echo "::endgroup::"
147+
gh_group_end
77148

78149
check-spdx()
79150
{
80151
local success=true
81152
for file in $(git ls-files -- ":/" ":/!:*.json" ":/!:*.png" ":/!:*LICENSE*" ":/!:.git*" ":/!:flake.lock"); do
82153
# Ignore symlinks
83154
if [[ ! -L $file && $(grep "SPDX-License-Identifier:" $file | wc -l) == 0 ]]; then
84-
echo "::error file=$file,line=${line:-1},title=Missing license header error::$file is missing SPDX License header"
155+
gh_error "$file" "${line:-1}" "Missing license header error" "$file is missing SPDX License header"
85156
success=false
86157
fi
87158
done
88159
for file in $(git ls-files -- "*.[chsS]" "*.py" ":/!proofs/cbmc/*.py"); do
89160
# Ignore symlinks
90161
if [[ ! -L $file && $(grep "Copyright (c) The mldsa-native project authors" $file | wc -l) == 0 ]]; then
91-
echo "::error file=$file,line=${line:-1},title=Missing copyright header error::$file is missing copyright header"
162+
gh_error "$file" "${line:-1}" "Missing copyright header error" "$file is missing copyright header"
163+
success=false
164+
fi
165+
done
166+
# For source files in dev/* and mldsa/*, we enforce `Apache-2.0 OR ISC OR MIT`
167+
for file in $(git ls-files -- "*.[chsSi]" | grep "^dev/\|^mlkem/"); do
168+
# Ignore symlinks
169+
if [[ ! -L $file && $(grep "SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT" $file | wc -l) == 0 ]]; then
170+
gh_error "$file" "${line:-1}" "Missing license header error" "$file is not licensed under 'Apache-2.0 OR ISC OR MIT'"
92171
success=false
93172
fi
94173
done
95174

96175
if $success; then
97-
echo ":white_check_mark: Check SPDX + Copyright" >>"$GITHUB_STEP_SUMMARY"
176+
info "Check SPDX + Copyright"
177+
gh_summary_success "Check SPDX + Copyright"
98178
else
179+
error "Check SPDX + Copyright"
99180
SUCCESS=false
100-
echo ":x: Check SPDX + Copyright" >>"$GITHUB_STEP_SUMMARY"
181+
gh_summary_failure "Check SPDX + Copyright"
101182
fi
102183
}
103-
echo "::group::Checking SPDX + Copyright headers"
184+
gh_group_start "Checking SPDX + Copyright headers"
104185
check-spdx
105-
echo "::endgroup::"
186+
gh_group_end
106187

107188
check-autogenerated-files()
108189
{
109190
if python3 $ROOT/scripts/autogen --dry-run; then
110-
echo ":white_check_mark: Check native auto-generated files" >>"$GITHUB_STEP_SUMMARY"
191+
info "Check native auto-generated files"
192+
gh_summary_success "Check native auto-generated files"
111193
else
112-
echo ":x: Check native auto-generated files" >>"$GITHUB_STEP_SUMMARY"
194+
error "Check native auto-generated files"
195+
gh_summary_failure "Check native auto-generated files"
113196
SUCCESS=false
114197
fi
115198
}
116199

117-
echo "::group::Check native auto-generated files"
200+
gh_group_start "Check native auto-generated files"
118201
check-autogenerated-files
119-
echo "::endgroup::"
202+
gh_group_end
120203

121204
if ! $SUCCESS; then
122205
exit 1

0 commit comments

Comments
 (0)