From 21eee5a0ca4a0f0d710d5af8198f3c0a1cf818d2 Mon Sep 17 00:00:00 2001 From: diginc Date: Sat, 8 Oct 2016 14:56:06 -0500 Subject: [PATCH] Shellcheck added to py.test and fixes - Various minor quote fixes - Optimized repeated echos into php conf for alpine --- .travis.yml | 2 +- autotest | 2 +- common_start.sh | 24 +++++++++++++----------- docker_dev.sh | 3 ++- docker_run.sh | 4 ++-- make_symlinks.sh | 6 +++--- test/test_shellcheck.py | 13 +++++++++++++ update.sh | 2 +- 8 files changed, 36 insertions(+), 20 deletions(-) create mode 100644 test/test_shellcheck.py diff --git a/.travis.yml b/.travis.yml index 655f84e..2ca3b2d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,4 +7,4 @@ python: install: - pip install -r requirements.txt -script: py.test -v +script: py.test -vv diff --git a/autotest b/autotest index 3747cc0..fc67fc6 100755 --- a/autotest +++ b/autotest @@ -1 +1 @@ -py.test -v -f test/ +py.test -vv -f test/ diff --git a/common_start.sh b/common_start.sh index cf44941..c0eb391 100644 --- a/common_start.sh +++ b/common_start.sh @@ -39,31 +39,33 @@ setup_php_env() { } setup_php_env_debian() { - sed -i "/bin-environment/ a\\\t\t\t\"ServerIP\" => \"${ServerIP}\"," $PHP_ENV_CONFIG - sed -i "/bin-environment/ a\\\t\t\t\"PHP_ERROR_LOG\" => \"${PHP_ERROR_LOG}\"," $PHP_ENV_CONFIG + sed -i "/bin-environment/ a\\\t\t\t\"ServerIP\" => \"${ServerIP}\"," "$PHP_ENV_CONFIG" + sed -i "/bin-environment/ a\\\t\t\t\"PHP_ERROR_LOG\" => \"${PHP_ERROR_LOG}\"," "$PHP_ENV_CONFIG" if [ -z "$VIRTUAL_HOST" ] ; then VIRTUAL_HOST="$ServerIP" fi; - sed -i "/bin-environment/ a\\\t\t\t\"VIRTUAL_HOST\" => \"${VIRTUAL_HOST}\"," $PHP_ENV_CONFIG + sed -i "/bin-environment/ a\\\t\t\t\"VIRTUAL_HOST\" => \"${VIRTUAL_HOST}\"," "$PHP_ENV_CONFIG" echo "Added ENV to php:" - grep -E '(VIRTUAL_HOST|ServerIP)' $PHP_ENV_CONFIG + grep -E '(VIRTUAL_HOST|ServerIP)' "$PHP_ENV_CONFIG" } setup_php_env_alpine() { - echo "[www]" > $PHP_ENV_CONFIG; - echo "env[PATH] = ${PATH}" >> $PHP_ENV_CONFIG; - echo "env[PHP_ERROR_LOG] = ${PHP_ERROR_LOG}" >> $PHP_ENV_CONFIG; - echo "env[ServerIP] = ${ServerIP}" >> $PHP_ENV_CONFIG; + cat <<-EOF > "$PHP_ENV_CONFIG" + [www] + env[PATH] = ${PATH} + env[PHP_ERROR_LOG] = ${PHP_ERROR_LOG} + env[ServerIP] = ${ServerIP} + EOF if [ -z "$VIRTUAL_HOST" ] ; then VIRTUAL_HOST="$ServerIP" fi; - echo "env[VIRTUAL_HOST] = ${VIRTUAL_HOST}" >> $PHP_ENV_CONFIG; + echo "env[VIRTUAL_HOST] = ${VIRTUAL_HOST}" >> "$PHP_ENV_CONFIG"; echo "Added ENV to php:" - cat $PHP_ENV_CONFIG + cat "$PHP_ENV_CONFIG" } test_configs() { @@ -96,5 +98,5 @@ test_configs_alpine() { } test_framework_stubbing() { - if [ -n "$PYTEST" ] ; then sed -i 's/^gravity_spinup/#donotcurl/g' `which gravity.sh`; fi; + if [ -n "$PYTEST" ] ; then sed -i 's/^gravity_spinup/#donotcurl/g' "$(which gravity.sh)"; fi; } diff --git a/docker_dev.sh b/docker_dev.sh index 23a4997..7116bc8 100755 --- a/docker_dev.sh +++ b/docker_dev.sh @@ -6,6 +6,7 @@ docker build -f debian.docker -t diginc/pi-hole:debian . IP=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1) # Alternative ports to not conflict with my real instance +# shellcheck disable=SC2068 docker run -it --rm --cap-add=NET_ADMIN \ -p 5053:53/tcp \ -p 5053:53/udp \ @@ -13,5 +14,5 @@ docker run -it --rm --cap-add=NET_ADMIN \ -e ServerIP="$IP" \ -e VIRTUAL_HOST='pihole.diginc.lan:5080' \ $@ \ - diginc/pi-hole:${image:-alpine} + diginc/pi-hole:"${image:-alpine}" diff --git a/docker_run.sh b/docker_run.sh index ea4d6e1..2a6799c 100755 --- a/docker_run.sh +++ b/docker_run.sh @@ -1,11 +1,11 @@ #!/bin/bash IMAGE=${1:-'diginc/pi-hole:alpine'} NIC=${2:-'eth0'} -IP=$(ip addr show $NIC | grep "inet\b" | awk '{print $2}' | cut -d/ -f1) +IP=$(ip addr show "$NIC" | grep "inet\b" | awk '{print $2}' | cut -d/ -f1) # Default ports + daemonized docker container docker run -p 53:53/tcp -p 53:53/udp -p 80:80 \ --cap-add=NET_ADMIN \ -e ServerIP="$IP" \ --name pihole \ - -d $IMAGE + -d "$IMAGE" diff --git a/make_symlinks.sh b/make_symlinks.sh index c3b1930..cf4f913 100755 --- a/make_symlinks.sh +++ b/make_symlinks.sh @@ -1,11 +1,11 @@ #!/bin/sh -e supportedTags='^(alpine|debian)$' -if ! (echo $1 | grep -Pq "$supportedTags") ; then +if ! (echo "$1" | grep -Pq "$supportedTags") ; then echo "$1 is not a supported tag"; exit 1; fi unlink docker-compose.yml unlink Dockerfile -ln -s doco-${1}.yml docker-compose.yml -ln -s ${1}.docker Dockerfile +ln -s "doco-${1}.yml" docker-compose.yml +ln -s "${1}.docker" Dockerfile diff --git a/test/test_shellcheck.py b/test/test_shellcheck.py new file mode 100644 index 0000000..eb20194 --- /dev/null +++ b/test/test_shellcheck.py @@ -0,0 +1,13 @@ +import pytest +import testinfra + +run_local = testinfra.get_backend( + "local://" +).get_module("Command").run + +def test_scripts_pass_shellcheck(): + ''' Make sure shellcheck does not find anything wrong with our shell scripts ''' + shellcheck = "find . ! -path './pi-hole/*' -name '*.sh' -a ! -name 'gravity.sh' | while read file; do shellcheck $file; done;" + results = run_local(shellcheck) + print results.stdout + assert '' == results.stdout diff --git a/update.sh b/update.sh index 666d812..019c73e 100755 --- a/update.sh +++ b/update.sh @@ -14,7 +14,7 @@ sed -i 's|service dnsmasq start|dnsmasq -7 /etc/dnsmasq.d|g' alpine/gravity.sh cron='./docker-pi-hole.cron' cp -f pi-hole/advanced/pihole.cron ${cron}; sed -i '/Update the ad sources/ i\# Your container name goes here:\nDOCKER_NAME=pihole\nPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin\n' ${cron}; -sed -i 's|/usr/local/bin/|docker exec $DOCKER_NAME |g' ${cron}; +sed -i "s|/usr/local/bin/|docker exec \$DOCKER_NAME |g" ${cron}; sed -i '/docker exec/ s|$| > /dev/null|g' ${cron}; # docker-pi-hole users update their docker images, not git code