feat: Add script.sh with tests

This commit is contained in:
ge 2022-05-14 03:36:13 +03:00
parent 68b4def813
commit c950273168
9 changed files with 171 additions and 0 deletions

101
src/lib/script.sh Normal file
View File

@ -0,0 +1,101 @@
#! /usr/bin/env bash
# script.sh - utilitary functions.
# Copyright (c) 2022 ge <https://nixhacks.net/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
validate_sources() {
# Check sources array.
#
# Usage: validate_sources ARRAY
# Allowed URI schemes: file, mysql postgres, sqlite
# No required schemes.
local array=("$@")
local scheme=
for uri in "${array[@]}"; do
scheme="${uri%%:*}"
case "$scheme" in
file|mysql|postgres|sqlite) : ;; # do nothing, this is OK
*) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
esac
done
}
validate_targets() {
# Check targets array.
#
# Usage: validate_targets ARRAY
# Allowed URI schemes: file, ftp, sftp, rsync, s3, swift, sj, dav, davs
# Required schemes (one or more times): file
local array=("$@")
local scheme=
local file_scheme=0
for uri in "${array[@]}"; do
scheme="${uri%%:*}"
case "$scheme" in
file|ftp|sftp|rsync|s3|swift|sj|dav|davs)
if [[ "$scheme" == file ]]; then
(( file_scheme++ )) || true
fi
;;
*) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
esac
done
if [ "$file_scheme" -eq 0 ]; then
echo "Error: 'file' scheme is not set in targets." \
"You must provide one or more targets with 'file' scheme." >&2
exit 1
fi
}
source_script() {
# Safely as possible source backup script.
#
# Usage: source_script SCRIPT
local script="$1"
if ! test -f "$script"; then
echo "Error: No such file: $script" >&2; exit 1
fi
# Dry run script, check syntax. See set(1p)
if ! bash -n "$script"; then
echo Error: Please check your syntax >&2; exit 1
fi
# Source script
. "$@"
# Check required variables
if [[ "$sources" ]]; then
validate_sources "${sources[@]}"
else
echo Error: sources array is not set >&2; exit 1
fi
if [[ "$targets" ]]; then
validate_targets "${targets[@]}"
else
echo Error: targets array is not set >&2; exit 1
fi
}

View File

@ -0,0 +1,2 @@
sources= ('file:/home/user')
targets=('file:/var/backup')

View File

View File

@ -0,0 +1,2 @@
sources=()
targets=('file:/var/backup')

View File

@ -0,0 +1,2 @@
sources=('file:/home/user')
targets=()

View File

@ -0,0 +1,2 @@
sources=(file:///)
targets=(ftp://user:pass@host/path)

View File

@ -0,0 +1,2 @@
sources=('mongo:/user:password@localhost/database')
targets=('file:/var/backup')

View File

@ -0,0 +1,2 @@
sources=('file:/home/user')
targets=(scp://user:pass@host/path)

58
tests/source_script.bats Normal file
View File

@ -0,0 +1,58 @@
#! /usr/bin/env bats
# source_script() from lib/script.sh tests.
# See: https://bats-core.readthedocs.io/en/latest/index.html
setup() {
# Bats setup
load 'helpers/bats-support/load'
load 'helpers/bats-assert/load'
DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
PATH="$DIR/../src/lib:$PATH"
}
# ------------------------------ #
# Do tests! #
# ------------------------------ #
@test "Bad script syntax" {
. script.sh
run source_script $DIR/files/bad_syntax.plan
assert_output --partial 'Error: Please check your syntax'
}
@test "Empty script" {
. script.sh
run source_script $DIR/files/empty_script.plan
assert_output --partial 'Error: sources array is not set'
}
@test "Empty sources array" {
. script.sh
run source_script $DIR/files/empty_sources.plan
assert_output --partial 'Error: sources array is not set'
}
@test "Empty targets array" {
. script.sh
run source_script $DIR/files/empty_targets.plan
assert_output --partial 'Error: targets array is not set'
}
@test "No targets with file URI scheme" {
. script.sh
run source_script $DIR/files/no_file_target.plan
assert_output --partial "Error: 'file' scheme is not set in targets. You must provide one or more targets with 'file' scheme."
}
@test "Unsuported source scheme" {
. script.sh
run source_script $DIR/files/unsupported_source_scheme.plan
assert_output --partial 'Error: Unsupported URI scheme: mongo'
}
@test "Unsuported target scheme" {
. script.sh
run source_script $DIR/files/unsupported_target_scheme.plan
assert_output --partial 'Error: Unsupported URI scheme: scp'
}