Skip to content

Commit 6a5479d

Browse files
austin987SolidEva
authored andcommitted
tests/shellcheck: add basic shellcheck tests
Currently we're ignoring SC2068 and style checks. However since it integrates with github actions, it allows prevent new problems from getting in to begin with, so I think it's worth going on in as is then improving later.
1 parent 5c180e0 commit 6a5479d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

.github/workflows/shellcheck.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: shellcheck
2+
3+
on: [push, pull_request]
4+
jobs:
5+
build:
6+
name: build
7+
runs-on: ubuntu-20.04
8+
steps:
9+
- name: checkout project
10+
uses: actions/checkout@v2
11+
- name: run shellcheck
12+
run: ./tests/shellcheck

tests/shellcheck

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/bin/bash
2+
# Wrapper around shellcheck
3+
#
4+
# Copyright (C) 2020 Austin English
5+
#
6+
# This software comes with ABSOLUTELY NO WARRANTY.
7+
#
8+
# This is free software, placed under the terms of the GNU Lesser
9+
# Public License version 2.1 (or later), as published by the Free
10+
# Software Foundation. Please see the file COPYING for details.
11+
12+
set -e
13+
set -x
14+
15+
###################################################################################################
16+
# Helpers
17+
###################################################################################################
18+
19+
w_die() {
20+
echo "$* failed"
21+
exit 1
22+
}
23+
24+
w_try() {
25+
"$@"
26+
status=$?
27+
if test ${status} -ne 0; then
28+
w_die "Note: command $* returned status ${status}. Aborting."
29+
fi
30+
}
31+
32+
###################################################################################################
33+
# Setup
34+
###################################################################################################
35+
36+
if [ ! -f makefile ] ; then
37+
w_die "$0 should be run from the top of the source tree"
38+
fi
39+
40+
temp="$(mktemp -d)"
41+
42+
trap 'rm -fr "$temp"' EXIT
43+
44+
###################################################################################################
45+
# Test functions
46+
###################################################################################################
47+
48+
# tests using shellcheck
49+
test_shellcheck() {
50+
shellcheck="$(command -v shellcheck)"
51+
52+
if [ -z "$shellcheck" ]; then
53+
apt-get -y install shellcheck
54+
fi
55+
56+
echo "======================== Begin shellcheck version info ==========================="
57+
"${shellcheck}" --version > /dev/null || w_die "shellcheck must be installed!"
58+
"${shellcheck}" --version
59+
echo "======================== End shellcheck version info ==========================="
60+
61+
echo "Checking ${shellscript} with shellcheck:"
62+
# FIXME: just the errors, for now:
63+
# FIXME: the SC2068 errors are likely legit (or should be explicitly silenced), but they count as errors
64+
# so ignoring for now
65+
w_try "${shellcheck}" -S error -e SC2068 "${shellscript}"
66+
}
67+
68+
###################################################################################################
69+
70+
# Test wrapper
71+
main() {
72+
# Use git ls-files if available, this prevents 'finding' scripts that aren't checked into git.
73+
# E.g., if patching foo fails, then foo.orig would also be 'found'.
74+
# The find fallback is for non git users, e.g., distros packaging shellcheck or end users
75+
# running shell-checks from a tarball download.
76+
if [ -d .git ] ; then
77+
files_to_check="$(git ls-files | xargs file | grep -e 'shell script' | cut -d : -f1)"
78+
else
79+
files_to_check="$(find . -type f -exec file {} \; | grep -e 'shell script' | cut -d : -f1)"
80+
fi
81+
82+
# Generic shellscript checks:
83+
for shellscript in ${files_to_check}; do
84+
test_shellcheck
85+
done
86+
}
87+
88+
main
89+
90+
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

0 commit comments

Comments
 (0)