Skip to content

Commit

Permalink
wip bats
Browse files Browse the repository at this point in the history
  • Loading branch information
wscherphof committed Jul 17, 2020
1 parent 3453072 commit e159905
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 10 deletions.
16 changes: 6 additions & 10 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions base/plugins/validate/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

here=$(dirname "$0")
cp "$here"/validate.bash ~/.validate.bash
83 changes: 83 additions & 0 deletions base/plugins/validate/validate.bash
Original file line number Diff line number Diff line change
@@ -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"
}
12 changes: 12 additions & 0 deletions base/test.sh
Original file line number Diff line number Diff line change
@@ -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:-.}"
10 changes: 10 additions & 0 deletions base/test_helper/load.bash
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e159905

Please sign in to comment.