diff --git a/lib/functions.bash b/lib/functions.bash index 7801348..2101856 100755 --- a/lib/functions.bash +++ b/lib/functions.bash @@ -96,12 +96,14 @@ function s3Exists { function s3Upload { local s3_path local localPaths + local tempFile s3_path=$(s3Path "$1") # shellcheck disable=SC2206 localPaths=($2) + tempFile=$(makeTempFile) set +e # shellcheck disable=SC2068 - if ! (tar --ignore-failed-read -cz ${localPaths[@]} | aws "${aws_cli_args[@]}" s3 cp - "$s3_path"); then + if ! (tar -czf "$tempFile" ${localPaths[@]} && aws "${aws_cli_args[@]}" s3 cp "$tempFile" "$s3_path" --quiet); then echo "false" else echo "true" diff --git a/tests/functions.bats b/tests/functions.bats index feb911f..455b29f 100644 --- a/tests/functions.bats +++ b/tests/functions.bats @@ -324,4 +324,24 @@ setup() { export BUILDKITE_PLUGIN_S3_CACHE_PIPELINE_NAME="another-pipeline" run -0 s3ObjectKey file assert_output "my-org/another-pipeline/file.tar.gz" + unset BUILDKITE_PLUGIN_S3_CACHE_PIPELINE_NAME +} + +@test "s3Upload with existing and non-existing paths" { + makeTempFile() { echo /tmp/file.tar.gz; } + export -f makeTempFile + stub aws \ + "s3 cp /tmp/file.tar.gz s3://bucket/my-org/my-pipeline/s3_path.tar.gz --quiet : :" + stub tar \ + "-czf /tmp/file.tar.gz local_paths : :" \ + "-czf /tmp/file.tar.gz local_paths : exit 1" + + run -0 s3Upload s3_path local_paths + assert_output "true" + run -0 s3Upload s3_path local_paths + assert_output "false" + + unset makeTempFile + unstub aws + unstub tar } diff --git a/tests/post-command.bats b/tests/post-command.bats index fe3d5ab..c0f49e3 100755 --- a/tests/post-command.bats +++ b/tests/post-command.bats @@ -11,6 +11,7 @@ function cleanup { trap cleanup EXIT setup() { + bats_require_minimum_version 1.5.0 export BUILDKITE_BUILD_CHECKOUT_PATH=$tmp_dir export BUILDKITE_BUILD_ID=1 export BUILDKITE_JOB_ID=0 @@ -21,6 +22,5 @@ setup() { @test "Post-command succeeds" { export BUILDKITE_PLUGIN_S3_CACHE_SAVE_0_KEY=v1-node-modules - run "$post_command_hook" - assert_success + run -0 "$post_command_hook" } diff --git a/tests/pre-command.bats b/tests/pre-command.bats index fae7b0e..6057d78 100644 --- a/tests/pre-command.bats +++ b/tests/pre-command.bats @@ -11,6 +11,7 @@ function cleanup { trap cleanup EXIT setup() { + bats_require_minimum_version 1.5.0 export BUILDKITE_BUILD_CHECKOUT_PATH=$tmp_dir export BUILDKITE_BUILD_ID=1 export BUILDKITE_JOB_ID=0 @@ -35,7 +36,6 @@ function teardown() { run "$pre_command_hook" - assert_success assert_output --partial "Successfully restored v1-cache-key" } @@ -50,9 +50,8 @@ function teardown() { "-xz : echo true" \ "-xz : echo true" - run "$pre_command_hook" + run -0 "$pre_command_hook" - assert_success assert_output --partial "Failed to restore cache-key-missing" assert_output --partial "Successfully restored cache-key-exists" }