Skip to content

Commit ba2ab94

Browse files
authored
Add syntax checkers for Bash and Python (#1469)
1 parent 6d2d239 commit ba2ab94

File tree

2 files changed

+95
-37
lines changed

2 files changed

+95
-37
lines changed

.github/workflows/check-json-syntax.yml

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
name: Validate syntax
3+
4+
'on':
5+
- push
6+
- pull_request
7+
8+
permissions: {}
9+
10+
jobs:
11+
json-syntax:
12+
name: JSON
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Validate syntax for JSON files
20+
run: |
21+
error=0
22+
readarray -d '' json_files < \
23+
<(find . \( -path ./.git -or -path ./DATA/testing/private \) -prune -false -or -type f -name '*.json' -print0)
24+
for jsonf in "${json_files[@]}"; do
25+
echo "::debug::Checking $jsonf..."
26+
if ! errmsg=$(jq . "$jsonf" 2>&1 >/dev/null); then
27+
error=1
28+
echo "Invalid JSON syntax found in $jsonf:" >&2
29+
printf '::error file=%s,title=%s::%s\n' "$jsonf" 'Invalid JSON syntax' "$errmsg"
30+
fi
31+
done
32+
exit "$error"
33+
34+
bash-syntax:
35+
name: Bash
36+
runs-on: ubuntu-latest
37+
38+
steps:
39+
- name: Checkout code
40+
uses: actions/checkout@v4
41+
42+
- name: Validate syntax with bash -n
43+
run: |
44+
error=0
45+
readarray -d '' files < \
46+
<(find . -path ./.git -prune -false -or -type f -name '*.sh' -print0)
47+
for bashf in "${files[@]}"; do
48+
echo "::debug::Checking $bashf..."
49+
if ! errmsg=$(bash -n "$bashf" 2>&1 >/dev/null); then
50+
error=1
51+
echo "Invalid Bash syntax found in $bashf:" >&2
52+
printf '::error file=%s,title=%s::%s\n' "$bashf" 'Invalid syntax' "$errmsg"
53+
fi
54+
done
55+
exit "$error"
56+
57+
shellcheck:
58+
name: Shellcheck
59+
runs-on: ubuntu-latest
60+
61+
steps:
62+
- name: Checkout code
63+
uses: actions/checkout@v4
64+
65+
- name: Run Shellcheck to find errors
66+
run: |
67+
error=0
68+
find . -path ./.git -prune -false -or -type f -name '*.sh' -print0 |
69+
xargs -0 shellcheck -xf json1 -S error -s bash > errors.json || error=$?
70+
# Produce code annotations in GitHub's format.
71+
jq -r '.comments[] | "Error found in \(.file) line \(.line):\n::error file=\(.file),line=\(.line),endLine=\(.endLine),col=\(.column),endColumn=\(.endColumn)::\(.message)"' errors.json
72+
exit "$error"
73+
74+
pylint:
75+
name: Pylint
76+
runs-on: ubuntu-latest
77+
78+
steps:
79+
- name: Checkout code
80+
uses: actions/checkout@v4
81+
82+
- name: Install prerequisites
83+
run: |
84+
sudo apt update -y
85+
sudo apt install -y pylint
86+
87+
- name: Run Pylint to find errors
88+
run: |
89+
error=0
90+
find . -path ./.git -prune -false -or -type f -name '*.py' -print0 |
91+
# "import-errors" are shown for valid modules like ROOT, so ignore them.
92+
xargs -0 pylint -E -f json --disable import-error > errors.json || error=$?
93+
# Produce code annotations in GitHub's format.
94+
jq -r '.[] | "Error found in \(.path) line \(.line):\n::error file=\(.path),line=\(.line),endLine=\(.endLine),col=\(.column),endColumn=\(.endColumn),title=Pylint \(.type) \(.symbol)::\(.message)"' errors.json
95+
exit "$error"

0 commit comments

Comments
 (0)