|
| 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