From e15990504acea4bd227978c5793c1e31c6c8f74e Mon Sep 17 00:00:00 2001 From: Wouter Scherphof Date: Fri, 17 Jul 2020 17:14:43 +0200 Subject: [PATCH] wip bats --- .gitmodules | 16 +++--- base/plugins/validate/install.sh | 4 ++ base/plugins/validate/validate.bash | 83 +++++++++++++++++++++++++++++ base/test.sh | 12 +++++ base/test_helper/load.bash | 10 ++++ 5 files changed, 115 insertions(+), 10 deletions(-) create mode 100755 base/plugins/validate/install.sh create mode 100644 base/plugins/validate/validate.bash create mode 100755 base/test.sh create mode 100644 base/test_helper/load.bash diff --git a/.gitmodules b/.gitmodules index ab76f928..d1338107 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,13 +1,9 @@ - [submodule "base/test_helper/bats-support"] - path = base/test_helper/bats-support - url = https://github.com/bats-core/bats-support +path = base/test_helper/bats-support +url = https://github.com/bats-core/bats-support [submodule "base/test_helper/bats-assert"] - path = base/test_helper/bats-assert - url = https://github.com/bats-core/bats-assert +path = base/test_helper/bats-assert +url = https://github.com/bats-core/bats-assert [submodule "base/test_helper/bats-file"] - path = base/test_helper/bats-file - url = https://github.com/ztombol/bats-file -[submodule "base/test_helper/bats-mock"] - path = base/test_helper/bats-mock - url = https://github.com/lox/bats-mock +path = base/test_helper/bats-file +url = https://github.com/ztombol/bats-file diff --git a/base/plugins/validate/install.sh b/base/plugins/validate/install.sh new file mode 100755 index 00000000..6b21fc90 --- /dev/null +++ b/base/plugins/validate/install.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +here=$(dirname "$0") +cp "$here"/validate.bash ~/.validate.bash diff --git a/base/plugins/validate/validate.bash b/base/plugins/validate/validate.bash new file mode 100644 index 00000000..6afb5d4d --- /dev/null +++ b/base/plugins/validate/validate.bash @@ -0,0 +1,83 @@ +# To use these input validation functions, +# include in a bash command af follows: +# # shellcheck source=/dev/null +# source ~/.validate.bash +# Default error number is 2. + +ERR_INVALID_INPUT=${1:-2} + +function error() { + echo "ERR_INVALID_INPUT" "$1" + exit "$ERR_INVALID_INPUT" +} + +function exists() { + if ! [ -e "$1" ]; then + error "$1 not found" + fi +} + +function file() { + if ! [ -f "$1" ]; then + error "$1 is not a file" + fi +} + +function directory() { + if ! [ -d "$1" ]; then + error "$1 is not a directory" + fi +} + +function readable() { + if ! [ -r "$1" ]; then + error "$1 is not readable" + fi +} + +function writable() { + if ! [ -w "$1" ]; then + error "$1 is not writable" + fi +} + +function executable() { + if ! [ -x "$1" ]; then + error "$1 is not executable" + fi +} + +function readable_file() { + readable "$1" + file "$1" +} + +function integer() { + local key=$1 + local value=$2 + if ! [ "$value" -eq "$value" ]; then + error "$key: $value is not an integer" + fi +} + +function positive_integer() { + local key=$1 + local value=$2 + if ! [ "$value" -ge 0 ]; then + error "$key: $value is not a positive integer" + fi +} + +function length() { + local key=$1 + local value=$2 + local length=$3 + if ! [ "${#value}" -eq "$length" ]; then + error "$key: $value is not $length characters long" + fi +} + +function positive_integer_length() { + positive_integer "$1" "$2" + length "$1" "$2" "$3" +} diff --git a/base/test.sh b/base/test.sh new file mode 100755 index 00000000..8c134301 --- /dev/null +++ b/base/test.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +if ! command -v bats; then + bats_url=https://github.com/bats-core/bats-core + echo "WARN - Cannot test without [bats]($bats_url); continuing without running any tests now." + echo "Consider npm install -g bats" + exit +fi + +"$DOCKER_BASE"/plugins/validate/install.sh + +time bats -r "${1:-.}" diff --git a/base/test_helper/load.bash b/base/test_helper/load.bash new file mode 100644 index 00000000..b5f408a0 --- /dev/null +++ b/base/test_helper/load.bash @@ -0,0 +1,10 @@ +# Start any bats file with: +# #!/usr/bin/env bats +# load "$DOCKER_BASE"/test_helper/load.bash + +base=$(basename "$BATS_TEST_FILENAME" .bats) +cmd="$BATS_TEST_DIRNAME"/"$base" + +load "$DOCKER_BASE"/test_helper/bats-support/load.bash +load "$DOCKER_BASE"/test_helper/bats-assert/load.bash +load "$DOCKER_BASE"/test_helper/bats-file/load.bash