From b0f78f935492e149658660cac6a027385287b058 Mon Sep 17 00:00:00 2001 From: Karl Fischer Date: Mon, 15 Jun 2020 00:16:24 +0200 Subject: [PATCH] Fix dir with trailing '/' (#39) --- CHANGELOG.md | 7 ++++ cli/command.go | 4 +- cli/cp.go | 3 +- test/command-tests/cp.bats | 83 ++++++++++++++++++++++++++++++++++++-- test/command-tests/mv.bats | 60 ++++++++++++++++++++++++--- test/util/util.bash | 3 +- 6 files changed, 148 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0fe46b3..b5e9af50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## v0.6.1 (June 15, 2020) + +BUG FIXES: + +* Properly handle suffix '/' in source path ([#39](https://github.com/fishi0x01/vsh/pull/39)) + + ## v0.6.0 (June 13, 2020) ENHANCEMENTS: diff --git a/cli/command.go b/cli/command.go index 2a6d2c8b..04814d59 100644 --- a/cli/command.go +++ b/cli/command.go @@ -1,9 +1,10 @@ package cli import ( - "github.com/fishi0x01/vsh/client" "path/filepath" "strings" + + "github.com/fishi0x01/vsh/client" ) // Command interface to describe a command structure @@ -31,6 +32,7 @@ func cmdPath(pwd string, arg string) (result string) { } func runCommandWithTraverseTwoPaths(client *client.Client, source string, target string, f func(string, string) error) { + source = filepath.Clean(source) // remove potential trailing '/' for _, path := range client.Traverse(source) { target := strings.Replace(path, source, target, 1) err := f(path, target) diff --git a/cli/cp.go b/cli/cp.go index 4107db89..b4764e86 100644 --- a/cli/cp.go +++ b/cli/cp.go @@ -2,8 +2,9 @@ package cli import ( "fmt" - "github.com/fishi0x01/vsh/client" "io" + + "github.com/fishi0x01/vsh/client" ) // CopyCommand container for all 'cp' parameters diff --git a/test/command-tests/cp.bats b/test/command-tests/cp.bats index 10293fb0..1560b423 100644 --- a/test/command-tests/cp.bats +++ b/test/command-tests/cp.bats @@ -25,7 +25,7 @@ load ../bin/plugins/bats-assert/load assert_output "test" ####################################### - echo "==== case: copy single directory ====" + echo "==== case: copy single directory without trailing '/' ====" run ${APP_BIN} -c "cp ${KV_BACKEND}/src/dev ${KV_BACKEND}/dest/dev" assert_success @@ -33,11 +33,9 @@ load ../bin/plugins/bats-assert/load run get_vault_value "value" "${KV_BACKEND}/dest/dev/1" assert_success assert_output "1" - run get_vault_value "value" "${KV_BACKEND}/dest/dev/2" assert_success assert_output "2" - run get_vault_value "value" "${KV_BACKEND}/dest/dev/3" assert_success assert_output "3" @@ -46,11 +44,90 @@ load ../bin/plugins/bats-assert/load run get_vault_value "value" "${KV_BACKEND}/src/dev/1" assert_success assert_output "1" + run get_vault_value "value" "${KV_BACKEND}/src/dev/2" + assert_success + assert_output "2" + run get_vault_value "value" "${KV_BACKEND}/src/dev/3" + assert_success + assert_output "3" + + ####################################### + echo "==== case: copy single directory with trailing '/' ====" + run ${APP_BIN} -c "cp ${KV_BACKEND}/src/dev/ ${KV_BACKEND}/dest/dev.copy" + assert_success + + echo "ensure the directory got copied to destination" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy/1" + assert_success + assert_output "1" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy/2" + assert_success + assert_output "2" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy/3" + assert_success + assert_output "3" + + echo "ensure the src directory still exists" + run get_vault_value "value" "${KV_BACKEND}/src/dev/1" + assert_success + assert_output "1" + run get_vault_value "value" "${KV_BACKEND}/src/dev/2" + assert_success + assert_output "2" + run get_vault_value "value" "${KV_BACKEND}/src/dev/3" + assert_success + assert_output "3" + + ###################################### + echo "==== case: copy single directory with dest trailing '/' ====" + run ${APP_BIN} -c "cp ${KV_BACKEND}/src/dev ${KV_BACKEND}/dest/dev.copy2/" + assert_success + + echo "ensure the directory got copied to destination" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy2/1" + assert_success + assert_output "1" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy2/2" + assert_success + assert_output "2" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy2/3" + assert_success + assert_output "3" + echo "ensure the src directory still exists" + run get_vault_value "value" "${KV_BACKEND}/src/dev/1" + assert_success + assert_output "1" run get_vault_value "value" "${KV_BACKEND}/src/dev/2" assert_success assert_output "2" + run get_vault_value "value" "${KV_BACKEND}/src/dev/3" + assert_success + assert_output "3" + + ###################################### + echo "==== case: copy single directory with dsrc and est trailing '/' ====" + run ${APP_BIN} -c "cp ${KV_BACKEND}/src/dev/ ${KV_BACKEND}/dest/dev.copy3/" + assert_success + + echo "ensure the directory got copied to destination" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy3/1" + assert_success + assert_output "1" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy3/2" + assert_success + assert_output "2" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy3/3" + assert_success + assert_output "3" + echo "ensure the src directory still exists" + run get_vault_value "value" "${KV_BACKEND}/src/dev/1" + assert_success + assert_output "1" + run get_vault_value "value" "${KV_BACKEND}/src/dev/2" + assert_success + assert_output "2" run get_vault_value "value" "${KV_BACKEND}/src/dev/3" assert_success assert_output "3" diff --git a/test/command-tests/mv.bats b/test/command-tests/mv.bats index 5e8d75d7..5bb535a3 100644 --- a/test/command-tests/mv.bats +++ b/test/command-tests/mv.bats @@ -22,7 +22,7 @@ load ../bin/plugins/bats-assert/load assert_output --partial "${NO_VALUE_FOUND}" ####################################### - echo "==== case: move single directory ====" + echo "==== case: move single directory without trailing '/' ====" run ${APP_BIN} -c "mv ${KV_BACKEND}/src/dev ${KV_BACKEND}/dest/dev" assert_success @@ -30,11 +30,9 @@ load ../bin/plugins/bats-assert/load run get_vault_value "value" "${KV_BACKEND}/dest/dev/1" assert_success assert_output "1" - run get_vault_value "value" "${KV_BACKEND}/dest/dev/2" assert_success assert_output "2" - run get_vault_value "value" "${KV_BACKEND}/dest/dev/3" assert_success assert_output "3" @@ -43,15 +41,67 @@ load ../bin/plugins/bats-assert/load run get_vault_value "value" "${KV_BACKEND}/src/dev/1" assert_success assert_output --partial "${NO_VALUE_FOUND}" - run get_vault_value "value" "${KV_BACKEND}/src/dev/2" assert_success assert_output --partial "${NO_VALUE_FOUND}" - run get_vault_value "value" "${KV_BACKEND}/src/dev/3" assert_success assert_output --partial "${NO_VALUE_FOUND}" + ####################################### + echo "==== case: move single directory with trailing '/' ====" + run ${APP_BIN} -c "mv ${KV_BACKEND}/dest/dev/ ${KV_BACKEND}/dest/dev.copy" + assert_success + + echo "ensure the directory got moved to destination" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy/1" + assert_success + assert_output "1" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy/2" + assert_success + assert_output "2" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy/3" + assert_success + assert_output "3" + + echo "ensure the src directory got removed" + run get_vault_value "value" "${KV_BACKEND}/dest/dev/1" + assert_success + assert_output --partial "${NO_VALUE_FOUND}" + run get_vault_value "value" "${KV_BACKEND}/dest/dev/2" + assert_success + assert_output --partial "${NO_VALUE_FOUND}" + run get_vault_value "value" "${KV_BACKEND}/dest/dev/3" + assert_success + assert_output --partial "${NO_VALUE_FOUND}" + + ####################################### + echo "==== case: move single directory with dest trailing '/' ====" + run ${APP_BIN} -c "mv ${KV_BACKEND}/dest/dev.copy ${KV_BACKEND}/dest/dev/" + assert_success + + echo "ensure the directory got moved to destination" + run get_vault_value "value" "${KV_BACKEND}/dest/dev/1" + assert_success + assert_output "1" + run get_vault_value "value" "${KV_BACKEND}/dest/dev/2" + assert_success + assert_output "2" + run get_vault_value "value" "${KV_BACKEND}/dest/dev/3" + assert_success + assert_output "3" + + echo "ensure the src directory got removed" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy/1" + assert_success + assert_output --partial "${NO_VALUE_FOUND}" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy/2" + assert_success + assert_output --partial "${NO_VALUE_FOUND}" + run get_vault_value "value" "${KV_BACKEND}/dest/dev.copy/3" + assert_success + assert_output --partial "${NO_VALUE_FOUND}" + ####################################### echo "==== TODO case: move ambigious file ====" diff --git a/test/util/util.bash b/test/util/util.bash index 5d72553f..204f5afb 100644 --- a/test/util/util.bash +++ b/test/util/util.bash @@ -1,8 +1,7 @@ #!/bin/bash -export KV_VERSION="${KV_VERSION:-"2"}" - export VAULT_VERSION=${VAULT_VERSION:-"1.3.4"} +export KV_BACKEND=${KV_BACKEND:-"KV2"} export VAULT_CONTAINER_NAME="vsh-integration-test-vault" export VAULT_HOST_PORT=${VAULT_HOST_PORT:-"8888"}