From 72222dddf9cfe424c3b4cb8ffd87bc5dbed1a56c Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sun, 6 Sep 2015 23:25:07 -0400 Subject: [PATCH] self-contained "testing" infrastructure --- Makefile | 20 +++++++++---- tests/setup.sh | 16 +++++++++++ tests/unit/service.bats | 33 +--------------------- tests/unit/test_helper.bash | 56 +++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 37 deletions(-) create mode 100644 tests/setup.sh create mode 100644 tests/unit/test_helper.bash diff --git a/Makefile b/Makefile index 5341517..faaf231 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,24 @@ shellcheck: ifeq ($(shell shellcheck > /dev/null 2>&1 ; echo $$?),127) ifeq ($(shell uname),Darwin) - brew install shellcheck + brew install shellcheck else - sudo add-apt-repository 'deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse' - sudo apt-get update && sudo apt-get install -y shellcheck + sudo add-apt-repository 'deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse' + sudo apt-get update -qq && sudo apt-get install -qq -y shellcheck endif endif bats: +ifeq ($(shell bats > /dev/null 2>&1 ; echo $$?),127) +ifeq ($(shell uname),Darwin) git clone https://github.com/sstephenson/bats.git /tmp/bats cd /tmp/bats && sudo ./install.sh /usr/local rm -rf /tmp/bats +else + sudo add-apt-repository ppa:duggan/bats --yes + sudo apt-get update -qq && sudo apt-get install -qq -y bats +endif +endif ci-dependencies: shellcheck bats @@ -21,10 +28,13 @@ lint: # SC2068: Double quote array expansions, otherwise they're like $* and break on spaces. - https://github.com/koalaman/shellcheck/wiki/SC2068 # SC2086: Double quote to prevent globbing and word splitting - https://github.com/koalaman/shellcheck/wiki/SC2086 @echo linting... - @$(QUIET) find . -not -path '*/\.*' | xargs file | egrep "shell|bash" | awk '{ print $$1 }' | sed 's/://g' | xargs shellcheck -e SC2046,SC2068,SC2086 + @$(QUIET) find ./ -maxdepth 1 -not -path '*/\.*' | xargs file | egrep "shell|bash" | awk '{ print $$1 }' | sed 's/://g' | xargs shellcheck -e SC2046,SC2068,SC2086 unit-tests: @echo running unit tests... @$(QUIET) bats tests/unit -test: ci-dependencies lint unit-tests +setup: + bash tests/setup.sh + +test: setup ci-dependencies lint unit-tests diff --git a/tests/setup.sh b/tests/setup.sh new file mode 100644 index 0000000..e2c53c3 --- /dev/null +++ b/tests/setup.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +source "$(dirname $0)/unit/test_helper.bash" + +if [[ ! -d $DOKKU_ROOT ]]; then + git clone https://github.com/progrium/dokku.git $DOKKU_ROOT > /dev/null +fi + +cd $DOKKU_ROOT +echo "Dokku version $DOKKU_VERSION" +git checkout $DOKKU_VERSION > /dev/null +cd - + +rm -rf $DOKKU_ROOT/plugins/service +mkdir -p $DOKKU_ROOT/plugins/service +find ./ -maxdepth 1 -type f -exec cp '{}' $DOKKU_ROOT/plugins/service \; diff --git a/tests/unit/service.bats b/tests/unit/service.bats index 671cca6..a622319 100644 --- a/tests/unit/service.bats +++ b/tests/unit/service.bats @@ -1,37 +1,6 @@ #!/usr/bin/env bats -export SERVICE=redis -flunk() { - { if [ "$#" -eq 0 ]; then cat - - else echo "$*" - fi - } - return 1 -} - -assert_success() { - if [ "$status" -ne 0 ]; then - flunk "command failed with exit status $status" - elif [ "$#" -gt 0 ]; then - assert_output "$1" - fi -} - -assert_equal() { - if [ "$1" != "$2" ]; then - { echo "expected: $1" - echo "actual: $2" - } | flunk - fi -} - -assert_output() { - local expected - if [ $# -eq 0 ]; then expected="$(cat -)" - else expected="$1" - fi - assert_equal "$expected" "$output" -} +load test_helper @test "(service) dokku" { dokku $SERVICE:create l diff --git a/tests/unit/test_helper.bash b/tests/unit/test_helper.bash new file mode 100644 index 0000000..004fa37 --- /dev/null +++ b/tests/unit/test_helper.bash @@ -0,0 +1,56 @@ +#!/usr/bin/env bash + +export DOKKU_QUIET_OUTPUT=1 +export DOKKU_ROOT="tests/dokku" +export DOKKU_VERSION=${DOKKU_VERSION:-"master"} +export PATH="$(dirname $0)/../dokku:$PATH" +export PLUGIN_PATH="$DOKKU_ROOT/plugins" +export SERVICE=redis + +flunk() { + { if [ "$#" -eq 0 ]; then cat - + else echo "$*" + fi + } + return 1 +} + +assert_equal() { + if [ "$1" != "$2" ]; then + { echo "expected: $1" + echo "actual: $2" + } | flunk + fi +} + +assert_exit_status() { + assert_equal "$status" "$1" +} + +assert_success() { + if [ "$status" -ne 0 ]; then + flunk "command failed with exit status $status" + elif [ "$#" -gt 0 ]; then + assert_output "$1" + fi +} + +assert_exists() { + if [ ! -f "$1" ]; then + flunk "expected file to exist: $1" + fi +} + +assert_contains() { + if [[ "$1" != *"$2"* ]]; then + flunk "expected $2 to be in: $1" + fi +} + +assert_output() { + local expected + if [ $# -eq 0 ]; then expected="$(cat -)" + else expected="$1" + fi + assert_equal "$expected" "$output" +}