-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathcheck.sh
More file actions
executable file
·258 lines (230 loc) · 8.63 KB
/
Copy pathcheck.sh
File metadata and controls
executable file
·258 lines (230 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
# Code quality checks for Mole.
# Auto-formats code, then runs lint and syntax checks.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
MODE="all"
usage() {
cat << 'EOF'
Usage: ./scripts/check.sh [--format|--no-format]
Options:
--format Apply formatting fixes only, shfmt, gofmt
--no-format Skip formatting and run checks only
--help Show this help
EOF
}
# Mole-Diagnose.command is support-only: a private reply hands it over as one
# pasted command, and it must never be linked from a public surface such as
# the README, docs, or issue templates (maintainer 2026-09-21, mirrored from
# the Mole Mac repo). Maintainer guidance may name it; public pages may not.
check_diagnostic_placement() {
local file
local status=0
for file in "$@"; do
[[ -f "$file" ]] || continue
if grep -n 'Mole-Diagnose' "$file" | sed "s|^|$file:|; s|\$| (support-only diagnostic script on a public surface)|"; then
status=1
fi
done
return "$status"
}
while [[ $# -gt 0 ]]; do
case "$1" in
--format)
MODE="format"
shift
;;
--no-format)
MODE="check"
shift
;;
--help | -h)
usage
exit 0
;;
*)
echo "Unknown option: $1"
usage
exit 1
;;
esac
done
cd "$PROJECT_ROOT"
# Honor https://no-color.org: any non-empty NO_COLOR disables ANSI escapes.
if [[ -n "${NO_COLOR:-}" ]]; then
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
else
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
fi
readonly ICON_SUCCESS="✓"
readonly ICON_ERROR="☻"
readonly ICON_WARNING="●"
readonly ICON_LIST="•"
echo -e "${BLUE}=== Mole Check, ${MODE} ===${NC}\n"
SHELL_FILES=$(find . -type f \( -name "*.sh" -o -name "mole" \) \
-not -path "./.git/*" \
-not -path "*/node_modules/*" \
-not -path "*/tests/tmp-*/*" \
-not -path "*/.*" \
2> /dev/null)
if [[ "$MODE" == "format" ]]; then
echo -e "${YELLOW}Formatting shell scripts...${NC}"
if command -v shfmt > /dev/null 2>&1; then
echo "$SHELL_FILES" | xargs shfmt -i 4 -ci -sr -w
echo -e "${GREEN}${ICON_SUCCESS} Shell formatting complete${NC}\n"
else
echo -e "${RED}${ICON_ERROR} shfmt not installed${NC}"
exit 1
fi
if command -v goimports > /dev/null 2>&1; then
echo -e "${YELLOW}Formatting Go code, goimports...${NC}"
goimports -w -local github.com/tw93/mole ./cmd ./internal
echo -e "${GREEN}${ICON_SUCCESS} Go formatting complete${NC}\n"
elif command -v go > /dev/null 2>&1; then
echo -e "${YELLOW}Formatting Go code, gofmt...${NC}"
gofmt -w ./cmd ./internal
echo -e "${GREEN}${ICON_SUCCESS} Go formatting complete${NC}\n"
else
echo -e "${YELLOW}${ICON_WARNING} go not installed, skipping gofmt${NC}\n"
fi
echo -e "${GREEN}=== Format Completed ===${NC}"
exit 0
fi
if [[ "$MODE" != "check" ]]; then
echo -e "${YELLOW}1. Formatting shell scripts...${NC}"
if command -v shfmt > /dev/null 2>&1; then
echo "$SHELL_FILES" | xargs shfmt -i 4 -ci -sr -w
echo -e "${GREEN}${ICON_SUCCESS} Shell formatting applied${NC}\n"
else
echo -e "${YELLOW}${ICON_WARNING} shfmt not installed, skipping${NC}\n"
fi
if command -v goimports > /dev/null 2>&1; then
echo -e "${YELLOW}2. Formatting Go code, goimports...${NC}"
goimports -w -local github.com/tw93/mole ./cmd ./internal
echo -e "${GREEN}${ICON_SUCCESS} Go formatting applied${NC}\n"
elif command -v go > /dev/null 2>&1; then
echo -e "${YELLOW}2. Formatting Go code, gofmt...${NC}"
gofmt -w ./cmd ./internal
echo -e "${GREEN}${ICON_SUCCESS} Go formatting applied${NC}\n"
fi
fi
echo -e "${YELLOW}3. Running Go linters...${NC}"
if command -v golangci-lint > /dev/null 2>&1; then
if ! golangci-lint config verify; then
echo -e "${RED}${ICON_ERROR} golangci-lint config invalid${NC}\n"
exit 1
fi
if golangci-lint run ./...; then
echo -e "${GREEN}${ICON_SUCCESS} golangci-lint passed${NC}\n"
else
echo -e "${RED}${ICON_ERROR} golangci-lint failed${NC}\n"
echo -e "${YELLOW}If the output points to deleted temporary worktrees or non-existent paths, run:${NC}"
echo -e "${YELLOW} golangci-lint cache clean && golangci-lint run ./...${NC}\n"
exit 1
fi
elif command -v go > /dev/null 2>&1; then
echo -e "${YELLOW}${ICON_WARNING} golangci-lint not installed, falling back to go vet${NC}"
if go vet ./...; then
echo -e "${GREEN}${ICON_SUCCESS} go vet passed${NC}\n"
else
echo -e "${RED}${ICON_ERROR} go vet failed${NC}\n"
exit 1
fi
else
echo -e "${YELLOW}${ICON_WARNING} Go not installed, skipping Go checks${NC}\n"
fi
echo -e "${YELLOW}4. Running ShellCheck...${NC}"
if command -v shellcheck > /dev/null 2>&1; then
if shellcheck mole install.sh bin/*.sh lib/*/*.sh scripts/*.sh; then
echo -e "${GREEN}${ICON_SUCCESS} ShellCheck passed${NC}\n"
else
echo -e "${RED}${ICON_ERROR} ShellCheck failed${NC}\n"
exit 1
fi
else
echo -e "${YELLOW}${ICON_WARNING} shellcheck not installed, skipping${NC}\n"
fi
echo -e "${YELLOW}5. Running syntax check...${NC}"
if ! bash -n mole; then
echo -e "${RED}${ICON_ERROR} Syntax check failed, mole${NC}\n"
exit 1
fi
if ! bash -n install.sh; then
echo -e "${RED}${ICON_ERROR} Syntax check failed, install.sh${NC}\n"
exit 1
fi
for script in bin/*.sh; do
if ! bash -n "$script"; then
echo -e "${RED}${ICON_ERROR} Syntax check failed, $script${NC}\n"
exit 1
fi
done
for script in scripts/*.sh; do
if ! bash -n "$script"; then
echo -e "${RED}${ICON_ERROR} Syntax check failed, $script${NC}\n"
exit 1
fi
done
find lib -name "*.sh" | while read -r script; do
if ! bash -n "$script"; then
echo -e "${RED}${ICON_ERROR} Syntax check failed, $script${NC}\n"
exit 1
fi
done
echo -e "${GREEN}${ICON_SUCCESS} Syntax check passed${NC}\n"
# Same body, different name. Grep cannot see this class once the variables have
# been renamed, and review reads the copies as separate helpers, so it needs a
# gate rather than a habit. Run with --list to inspect every group.
#
# python3 is a hard requirement here, not an optional tool like shfmt: six Bats
# files already fail without it, so a developer who can run the suite can run
# this. Mole itself never shells out to python3, so this stays a dev-only need.
if ! command -v python3 > /dev/null 2>&1; then
echo -e "${RED}${ICON_ERROR} python3 not installed; it is required by the test suite and this check${NC}\n"
exit 1
fi
if ! duplication_output=$(python3 "$SCRIPT_DIR/audit_function_duplication.py" 2>&1); then
printf '%s\n' "$duplication_output"
echo -e "${RED}${ICON_ERROR} Duplicate shell function bodies found${NC}\n"
exit 1
fi
printf '%s\n' "$duplication_output"
echo -e "${GREEN}${ICON_SUCCESS} Function duplication check passed${NC}\n"
if ! destructive_output=$(python3 "$SCRIPT_DIR/audit_destructive_sinks.py" 2>&1); then
printf '%s\n' "$destructive_output"
echo -e "${RED}${ICON_ERROR} Destructive sink annotation check failed${NC}\n"
exit 1
fi
printf '%s\n' "$destructive_output"
echo -e "${GREEN}${ICON_SUCCESS} Destructive sink annotation check passed${NC}\n"
if ! bats_assertion_output=$(python3 "$SCRIPT_DIR/audit_bats_assertions.py" 2>&1); then
printf '%s\n' "$bats_assertion_output"
echo -e "${RED}${ICON_ERROR} Bats assertions that cannot fail found${NC}\n"
exit 1
fi
printf '%s\n' "$bats_assertion_output"
echo -e "${GREEN}${ICON_SUCCESS} Bats assertion check passed${NC}\n"
if ! timeout_status_output=$(python3 "$SCRIPT_DIR/audit_timeout_status.py" 2>&1); then
printf '%s\n' "$timeout_status_output"
echo -e "${RED}${ICON_ERROR} Raw timeout status comparisons found${NC}\n"
exit 1
fi
printf '%s\n' "$timeout_status_output"
echo -e "${GREEN}${ICON_SUCCESS} Timeout status check passed${NC}\n"
diagnostic_public_files=(README.md CONTRIBUTING.md docs/*.md .github/ISSUE_TEMPLATE/*)
if ! diagnostic_placement_output=$(check_diagnostic_placement "${diagnostic_public_files[@]}"); then
[[ -n "$diagnostic_placement_output" ]] && printf '%s\n' "$diagnostic_placement_output"
echo -e "${RED}${ICON_ERROR} The diagnostic script must stay off public pages and issue templates${NC}\n"
exit 1
fi
echo -e "${GREEN}${ICON_SUCCESS} Diagnostic placement check passed${NC}\n"
echo -e "${GREEN}=== Checks Completed ===${NC}"