From 9a92d29111af74365a7694467847df2afd086f9f Mon Sep 17 00:00:00 2001 From: Otto Sabart Date: Tue, 16 Aug 2022 21:00:00 +0200 Subject: [PATCH] tests: add shellcheck --- .cirrus.yml | 4 ++++ test/run-tests.sh | 8 +++++++- test/shellcheck.sh | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100755 test/shellcheck.sh diff --git a/.cirrus.yml b/.cirrus.yml index 93a699b..3b56692 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -36,3 +36,7 @@ task: build_script: - nix flake check - ./test/nixos-search/ci-test.sh + + - name: shellcheck + build_script: + - nix shell --inputs-from . nixpkgs#{shellcheck,findutils,gnugrep} -c ./test/shellcheck.sh diff --git a/test/run-tests.sh b/test/run-tests.sh index 9824a16..0d8ceae 100755 --- a/test/run-tests.sh +++ b/test/run-tests.sh @@ -312,8 +312,14 @@ examples() { (cd "$scriptDir/../examples" && nix-shell --run "$script") } +shellcheck() { + if ! checkFlakeSupport "shellcheck"; then return; fi + nix shell --inputs-from "$scriptDir/.." nixpkgs#shellcheck -c "$scriptDir/shellcheck.sh" +} + all() { - buildable + buildable "$@" + shellcheck examples flake nixosSearch diff --git a/test/shellcheck.sh b/test/shellcheck.sh new file mode 100755 index 0000000..822f5d9 --- /dev/null +++ b/test/shellcheck.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "${BASH_SOURCE[0]%/*}/.." +{ + # Skip .git dir in all find commands + find . -type f ! -path './.git/*' -name '*.sh' + # Find files without extensions that have a shell shebang + find . -type f ! -path './.git/*' ! -name "*.*" -exec grep -lP '\A^#! */usr/bin/env (?:nix-shell|bash)' {} \; +} | while IFS= read -r path; do + echo "$path" + file=${path##*/} + dir=${path%/*} + # Switch working directory so that shellcheck can access external sources + # (via arg `--external-sources`) + pushd "$dir" > /dev/null + shellcheck --external-sources --shell bash "$file" + popd > /dev/null +done