From 0d355c468b073979597dfff9647e2c01f7c072c2 Mon Sep 17 00:00:00 2001 From: ge Date: Sat, 14 May 2022 04:55:36 +0300 Subject: [PATCH] feat: Add local_target variable and tests --- src/lib/script.sh | 12 ++++++++---- tests/files/basic.plan | 2 ++ tests/files/multiple_file_targets.plan | 7 +++++++ tests/source_script.bats | 14 +++++++++++++- 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 tests/files/basic.plan create mode 100644 tests/files/multiple_file_targets.plan diff --git a/src/lib/script.sh b/src/lib/script.sh index 1894c36..8599ab0 100644 --- a/src/lib/script.sh +++ b/src/lib/script.sh @@ -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 } diff --git a/tests/files/basic.plan b/tests/files/basic.plan new file mode 100644 index 0000000..0ac9c6b --- /dev/null +++ b/tests/files/basic.plan @@ -0,0 +1,2 @@ +sources=('file:/home/user') +targets=('file:/var/backup') diff --git a/tests/files/multiple_file_targets.plan b/tests/files/multiple_file_targets.plan new file mode 100644 index 0000000..974e1a5 --- /dev/null +++ b/tests/files/multiple_file_targets.plan @@ -0,0 +1,7 @@ +sources=('file:/home/user') +targets=( + 'file:///home/backups' + 'file:/var/backup' + 'file:/backups' + 's3://bucket' +) diff --git a/tests/source_script.bats b/tests/source_script.bats index 2814add..ae2ced2 100644 --- a/tests/source_script.bats +++ b/tests/source_script.bats @@ -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' ] +}