#!/bin/bash # # Purpose : coredesktop-dev check: lint and test the working copy, including # scripts not committed yet, which CoreDesktop's make lint skips check_usage() { cat << EOF Usage: coredesktop-dev check [--fast] Runs make lint and make test in the working copy, then shellcheck on the scripts git doesn't track yet. --fast runs only the shellcheck steps. EOF } # Print each file named on stdin (NUL-separated) that starts with a bash or sh # shebang, as the Makefile picks them, or is a .sh file shell_scripts() { local file while IFS= read -r -d '' file; do [ -f "$file" ] && [ ! -L "$file" ] || continue if [[ "$file" == *.sh ]] || head -n 1 "$file" | grep -qE '^#!.*[/ ](ba)?sh'; then echo "$file" fi done } cmd_check() { local fast=0 case "${1:-}" in -h | --help) check_usage; return 0 ;; --fast) fast=1 ;; "") ;; *) check_usage >&2; return 1 ;; esac require_working_copy cd "$WORKING_COPY" || return 1 local status=0 tracked untracked lint="ok" bats="skipped (--fast)" mapfile -t tracked < <(git ls-files -z | shell_scripts) mapfile -t untracked < <(git ls-files -z --others --exclude-standard | shell_scripts) echo "== make lint" make --no-print-directory lint || { lint="FAILED"; status=1; } if (( ${#untracked[@]} > 0 )); then echo "== shellcheck on untracked scripts: ${untracked[*]}" shellcheck --severity=warning "${untracked[@]}" || { lint="FAILED"; status=1; } fi if (( fast == 0 )); then echo "== make test" if make --no-print-directory test; then bats="ok ($(bats --count tests 2> /dev/null) tests)" else bats="FAILED" status=1 fi fi echo echo "shellcheck: $lint (${#tracked[@]} files, ${#untracked[@]} untracked) bats: $bats" return $status }