From 9d64d78c320cfcd89e46d1de53f6c6d8e9487b4f Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 28 Aug 2021 21:19:45 -0700 Subject: [PATCH] Allow running this repo as a pre-commit hook --- .dockerignore | 9 +++------ .pre-commit-hooks.yaml | 6 ++++++ Dockerfile | 18 ++++++++++++++++++ test/distrotest | 30 ++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 .pre-commit-hooks.yaml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore index 39d8893..49f90c9 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,3 @@ -* -!LICENSE -!Setup.hs -!ShellCheck.cabal -!shellcheck.hs -!src +dist +dist-newstyle +.git diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..9c5b4bc --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,6 @@ +- id: shellcheck + name: shellcheck + description: Static analysis tool for shell scripts + types: [shell] + language: docker + entry: shellcheck diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0f8e07c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# This file builds ShellCheck for pre-commit. +# +# It may also be useful for local development, but it is notably NOT +# used to build the official ShellCheck docker images. + +FROM ubuntu:20.04 +RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y cabal-install +RUN cabal update + +# Install dependencies separately for more efficient iteration +COPY ShellCheck.cabal /build/ +RUN cd /build && cabal install --dependencies-only + +# Now build the rest +COPY . /build/ +RUN cd /build && cabal build shellcheck +RUN find /build -type f -name shellcheck -perm /111 -exec cp -f {} /bin \; +ENTRYPOINT ["/bin/shellcheck"] diff --git a/test/distrotest b/test/distrotest index 346a706..8aaa84d 100755 --- a/test/distrotest +++ b/test/distrotest @@ -1,6 +1,9 @@ #!/bin/bash # This script runs 'buildtest' on each of several distros # via Docker. + +# shellcheck disable=SC2016 + set -o pipefail exec 3>&1 4>&2 @@ -32,6 +35,33 @@ echo "Logging to $log" >&3 exec >> "$log" 2>&1 final=0 + +echo "Trying to build pre-commit docker image" +if ! docker build --tag precommit . +then + final=1 + echo "pre-commit image failed to build" +else + if printf '%s\n' '#!/bin/sh' 'echo $1' | docker run -i precommit - + then + final=1 + echo "pre-commit image succeeds with incorrect example" + fi + + if ! printf '%s\n' '#!/bin/sh' 'echo "$1"' | docker run -i precommit - + then + final=1 + echo "pre-commit image fails with correct example" + fi +fi + +if [[ $final -ne 0 ]] +then + echo >&3 "pre-commit image failure, see log" +else + echo >&3 "pre-commit image succeeded" +fi + while read -r distro setup do [[ "$distro" = "#"* || -z "$distro" ]] && continue