-
Notifications
You must be signed in to change notification settings - Fork 0
326 lines (272 loc) · 9.63 KB
/
shell-script-testing.yml
File metadata and controls
326 lines (272 loc) · 9.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
name: Shell Script Testing
on:
push:
branches:
- main
- develop
paths:
- '**.sh'
- 'scripts/**'
- '.github/workflows/shell-script-testing.yml'
pull_request:
paths:
- '**.sh'
- 'scripts/**'
- '.github/workflows/shell-script-testing.yml'
jobs:
shellcheck:
name: ShellCheck Linting
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Run ShellCheck
uses: ludeeus/action-shellcheck@2.0.0
with:
severity: warning
ignore_paths: |
node_modules
vendor
.git
env:
SHELLCHECK_OPTS: -e SC1091 -e SC2148 -e SC2034 -e SC2155
- name: Detailed ShellCheck Analysis
run: |
echo "Running detailed ShellCheck analysis..."
# Find all shell scripts
find . -type f -name "*.sh" \
-not -path "./.git/*" \
-not -path "./node_modules/*" \
-not -path "./vendor/*" > shell-scripts.txt
FAIL_COUNT=0
WARN_COUNT=0
TOTAL_COUNT=0
while IFS= read -r script; do
echo ""
echo "Checking: $script"
TOTAL_COUNT=$((TOTAL_COUNT + 1))
# Run shellcheck and capture output
if shellcheck -f gcc -S warning -e SC1091 -e SC2034 -e SC2155 "$script" > shellcheck-output.txt 2>&1; then
echo " ✅ No issues found"
else
if grep -q "error:" shellcheck-output.txt; then
echo " ❌ Errors found:"
cat shellcheck-output.txt
FAIL_COUNT=$((FAIL_COUNT + 1))
else
echo " ⚠️ Warnings found:"
cat shellcheck-output.txt
WARN_COUNT=$((WARN_COUNT + 1))
fi
fi
done < shell-scripts.txt
echo ""
echo "============================================="
echo "ShellCheck Summary:"
echo "Total scripts: $TOTAL_COUNT"
echo "Errors: $FAIL_COUNT"
echo "Warnings: $WARN_COUNT"
echo "Clean: $((TOTAL_COUNT - FAIL_COUNT - WARN_COUNT))"
echo "============================================="
# Fail if there are errors
if [ $FAIL_COUNT -gt 0 ]; then
echo "❌ Some scripts have errors"
exit 1
fi
syntax-check:
name: Bash Syntax Validation
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Validate Bash syntax
run: |
echo "Validating Bash syntax for all scripts..."
find . -type f -name "*.sh" \
-not -path "./.git/*" \
-not -path "./node_modules/*" \
-not -path "./vendor/*" > shell-scripts.txt
FAIL_COUNT=0
TOTAL_COUNT=0
while IFS= read -r script; do
echo ""
echo "Syntax check: $script"
TOTAL_COUNT=$((TOTAL_COUNT + 1))
if bash -n "$script" 2>&1; then
echo " ✅ Syntax valid"
else
echo " ❌ Syntax error"
FAIL_COUNT=$((FAIL_COUNT + 1))
fi
done < shell-scripts.txt
echo ""
echo "============================================="
echo "Syntax Check Summary:"
echo "Total scripts: $TOTAL_COUNT"
echo "Failed: $FAIL_COUNT"
echo "Passed: $((TOTAL_COUNT - FAIL_COUNT))"
echo "============================================="
if [ $FAIL_COUNT -gt 0 ]; then
exit 1
fi
script-permissions:
name: Check Script Permissions
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Verify executable permissions
run: |
echo "Checking script permissions..."
find scripts/ -type f -name "*.sh" 2>/dev/null > scripts-list.txt || echo "No scripts directory found"
NON_EXEC_COUNT=0
TOTAL_COUNT=0
if [ -s scripts-list.txt ]; then
while IFS= read -r script; do
TOTAL_COUNT=$((TOTAL_COUNT + 1))
if [ -x "$script" ]; then
echo "✅ $script - executable"
else
echo "⚠️ $script - NOT executable"
NON_EXEC_COUNT=$((NON_EXEC_COUNT + 1))
fi
done < scripts-list.txt
echo ""
echo "============================================="
echo "Permission Check Summary:"
echo "Total scripts: $TOTAL_COUNT"
echo "Executable: $((TOTAL_COUNT - NON_EXEC_COUNT))"
echo "Non-executable: $NON_EXEC_COUNT"
echo "============================================="
if [ $NON_EXEC_COUNT -gt 0 ]; then
echo ""
echo "⚠️ Warning: Some scripts are not executable"
echo "Run: chmod +x <script-name>"
fi
else
echo "No scripts found in scripts/ directory"
fi
script-standards:
name: Script Standards Check
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Check script standards
run: |
echo "Checking script standards (shebang, set -e, etc.)..."
find scripts/ -type f -name "*.sh" 2>/dev/null > scripts-list.txt || exit 0
if [ ! -s scripts-list.txt ]; then
echo "No scripts found"
exit 0
fi
NO_SHEBANG=0
NO_SET_E=0
TOTAL_COUNT=0
while IFS= read -r script; do
TOTAL_COUNT=$((TOTAL_COUNT + 1))
echo ""
echo "Checking: $script"
# Check for shebang
if ! head -n 1 "$script" | grep -q "^#!"; then
echo " ⚠️ Missing shebang (#!/bin/bash)"
NO_SHEBANG=$((NO_SHEBANG + 1))
else
echo " ✅ Has shebang"
fi
# Check for set -e or set -euo pipefail
if grep -q "^set -e" "$script" || grep -q "^set -[a-z]*e" "$script"; then
echo " ✅ Has error handling (set -e)"
else
echo " ⚠️ Missing 'set -e' for error handling"
NO_SET_E=$((NO_SET_E + 1))
fi
# Check for license header
if head -n 20 "$script" | grep -qi "copyright\|license"; then
echo " ✅ Has license header"
else
echo " ℹ️ No license header found"
fi
done < scripts-list.txt
echo ""
echo "============================================="
echo "Standards Check Summary:"
echo "Total scripts: $TOTAL_COUNT"
echo "Missing shebang: $NO_SHEBANG"
echo "Missing set -e: $NO_SET_E"
echo "============================================="
# Don't fail on standards issues, just warn
if [ $NO_SHEBANG -gt 0 ] || [ $NO_SET_E -gt 0 ]; then
echo ""
echo "⚠️ Some scripts don't follow best practices"
echo "Consider adding:"
echo " - Shebang: #!/bin/bash"
echo " - Error handling: set -e"
fi
unit-tests:
name: Run Script Unit Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install BATS (Bash Automated Testing System)
run: |
sudo apt-get update
sudo apt-get install -y bats
- name: Check for test files
id: check_tests
run: |
if find . -name "*.bats" -o -name "*test.sh" | grep -q .; then
echo "tests_exist=true" >> $GITHUB_OUTPUT
else
echo "tests_exist=false" >> $GITHUB_OUTPUT
fi
- name: Run unit tests
if: steps.check_tests.outputs.tests_exist == 'true'
run: |
echo "Running BATS tests..."
find . -name "*.bats" -exec bats {} \;
- name: No tests found
if: steps.check_tests.outputs.tests_exist == 'false'
run: |
echo "ℹ️ No unit tests found (.bats or *test.sh files)"
echo "Consider adding tests for critical scripts"
summary:
name: Testing Summary
runs-on: ubuntu-latest
needs: [shellcheck, syntax-check, script-permissions, script-standards, unit-tests]
if: always()
steps:
- name: Check results
run: |
echo "Shell Script Testing Pipeline Complete"
echo "======================================="
# Required checks (must pass)
REQUIRED_PASS=true
if [ "${{ needs.shellcheck.result }}" != "success" ]; then
echo "❌ ShellCheck: FAILED"
REQUIRED_PASS=false
else
echo "✅ ShellCheck: PASSED"
fi
if [ "${{ needs.syntax-check.result }}" != "success" ]; then
echo "❌ Syntax Check: FAILED"
REQUIRED_PASS=false
else
echo "✅ Syntax Check: PASSED"
fi
# Optional checks (warnings only)
echo ""
echo "Additional Checks:"
echo " Script Permissions: ${{ needs.script-permissions.result }}"
echo " Script Standards: ${{ needs.script-standards.result }}"
echo " Unit Tests: ${{ needs.unit-tests.result }}"
if [ "$REQUIRED_PASS" = "false" ]; then
echo ""
echo "❌ Required checks failed"
exit 1
else
echo ""
echo "✅ All required checks passed"
exit 0
fi