feat: Add local_target variable and tests

This commit is contained in:
ge 2022-05-14 04:55:36 +03:00
parent d07ff8ca43
commit 0d355c468b
4 changed files with 30 additions and 5 deletions

View File

@ -37,7 +37,7 @@ validate_sources() {
}
validate_targets() {
# Check targets array.
# Check targets array and set local_target variable.
#
# Usage: validate_targets ARRAY
@ -46,24 +46,28 @@ validate_targets() {
local array=("$@")
local scheme=
local file_scheme=0
local file_targets=()
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
file_targets+=("$uri")
fi
;;
*) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
esac
done
if [ "$file_scheme" -eq 0 ]; then
if [ "${#file_targets[@]}" -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
else
# Set local_target. This variable contains path to save local backups.
# Files to additional targets will be coped from this directory.
local_target="${file_targets[0]}"
fi
}

2
tests/files/basic.plan Normal file
View File

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

View File

@ -0,0 +1,7 @@
sources=('file:/home/user')
targets=(
'file:///home/backups'
'file:/var/backup'
'file:/backups'
's3://bucket'
)

View File

@ -39,7 +39,7 @@ setup() {
assert_output --partial 'Error: targets array is not set'
}
@test "No targets with file URI scheme" {
@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."
@ -56,3 +56,15 @@ setup() {
run source_script $DIR/files/unsupported_target_scheme.plan
assert_output --partial 'Error: Unsupported URI scheme: scp'
}
@test "Set local target" {
. script.sh
source_script $DIR/files/basic.plan
[ "$local_target" == 'file:/var/backup' ]
}
@test "Set local target from multiple 'file' targets" {
. script.sh
source_script $DIR/files/multiple_file_targets.plan
[ "$local_target" == 'file:///home/backups' ]
}