Skip to content

Commit ee65e49

Browse files
authored
Merge pull request #103 from sp-aaflalo/patch-1
Improve zstd compression implementation by relying on tar directly
2 parents 808a356 + 408aa13 commit ee65e49

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

compression/zstd_wrapper

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,26 @@ is_absolute_path() {
88
[ "${1:0:1}" = "/" ]
99
}
1010

11-
if ! command -v zstd &> /dev/null; then
12-
echo "zstd is not installed"
13-
if [[ "$OSTYPE" == "darwin"* ]]; then
14-
echo "Try 'brew install zstd'"
15-
else
16-
echo "Try 'apt-get install zstd'"
17-
fi
11+
if ! tar --help | grep -q -- "--zstd"; then
12+
echo "tar does not support zstd compression"
13+
echo "You need a version of tar with zstd support (tar 1.31 or higher)"
1814
exit 1
1915
fi
2016

2117
if [ "${OPERATION}" = "compress" ]; then
22-
TAR_OPTS=('-c' '-f' '-')
23-
if is_absolute_path "${SOURCE}"; then
24-
TAR_OPTS+=('-P')
25-
fi
26-
tar "${TAR_OPTS[@]}" "${SOURCE}" | zstd -o "${TARGET}"
18+
TAR_OPTS=('-c' '--zstd')
19+
if is_absolute_path "${SOURCE}"; then
20+
TAR_OPTS+=('-P')
21+
fi
22+
23+
tar "${TAR_OPTS[@]}" -f "${TARGET}" "${SOURCE}"
2724
elif [ "${OPERATION}" = "decompress" ]; then
28-
mkdir -p "${TARGET}"
29-
TAR_OPTS=('-x' '-f' '-')
30-
if is_absolute_path "${TARGET}"; then
31-
TAR_OPTS+=('-P')
32-
zstd -d -c "${SOURCE}" | tar "${TAR_OPTS[@]}"
33-
else
34-
zstd -d -c "${SOURCE}" | tar "${TAR_OPTS[@]}" -C "${TARGET}"
35-
fi
25+
TAR_OPTS=('-x' '--zstd')
26+
if is_absolute_path "${TARGET}"; then
27+
TAR_OPTS+=('-P')
28+
fi
29+
30+
tar "${TAR_OPTS[@]}" -f "${SOURCE}" "${TARGET}"
3631
else
3732
echo "Invalid operation"
3833
exit 1

tests/post-command-zstd.bats

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# To debug stubs, uncomment these lines:
44
# export CACHE_DUMMY_STUB_DEBUG=/dev/tty
5-
# export ZSTD_STUB_DEBUG=/dev/tty
5+
# export TAR_STUB_DEBUG=/dev/tty
66

77
setup() {
88
load "${BATS_PLUGIN_PATH}/load.bash"
@@ -26,19 +26,19 @@ setup() {
2626
export BUILDKITE_ORGANIZATION_SLUG="bk-cache-test"
2727
export BUILDKITE_PIPELINE_SLUG="cache-pipeline"
2828

29-
# stubs are the same for every test
30-
stub zstd \
31-
"\* \* : echo compressed stdin into \$2 with options \$1"
32-
3329
stub cache_dummy \
3430
"save \* \* : echo saving \$3 in \$2"
31+
32+
stub tar \
33+
"--help : echo '--zstd'" \
34+
"echo called tar with options \$@"
3535
}
3636

3737
teardown() {
3838
rm -rf tests/data
3939

4040
unstub cache_dummy
41-
unstub zstd
41+
unstub tar
4242
}
4343

4444
@test "File-level saving with compression" {
@@ -50,7 +50,7 @@ teardown() {
5050
assert_success
5151
assert_output --partial 'Saving file-level cache'
5252
assert_output --partial 'Compressing tests/data/my_files with zstd'
53-
assert_output --partial "with options -o"
53+
assert_output --partial "with options -c --zstd"
5454
}
5555

5656
@test "Step-level saving" {
@@ -119,7 +119,7 @@ teardown() {
119119
assert_success
120120
assert_output --partial 'Saving pipeline-level cache'
121121
assert_output --partial "Compressing ${BUILDKITE_PLUGIN_CACHE_PATH} with zstd..."
122-
assert_output --partial "with options -o"
122+
assert_output --partial "with options -c --zstd -P"
123123

124124
rm -rf "${BUILDKITE_PLUGIN_CACHE_PATH}"
125125
}

tests/pre-command-zstd.bats

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# To debug stubs, uncomment these lines:
44
# export CACHE_DUMMY_STUB_DEBUG=/dev/tty
55
# export TAR_STUB_DEBUG=/dev/tty
6-
# export ZSTD_STUB_DEBUG=/dev/tty
76

87
setup() {
98
load "${BATS_PLUGIN_PATH}/load.bash"
@@ -23,18 +22,14 @@ setup() {
2322
export BUILDKITE_ORGANIZATION_SLUG="bk-cache-test"
2423
export BUILDKITE_PIPELINE_SLUG="cache-pipeline"
2524

26-
# stub is the same for all tests
27-
stub zstd \
28-
"-d \* \* : echo uncompressed \$2 into \$3"
29-
30-
stub tar "echo called tar with options \$@ and input; cat"
31-
25+
stub tar \
26+
"--help : echo '--zstd'" \
27+
"echo called tar with options \$@"
3228
}
3329

3430
teardown() {
3531
rm -rf tests/data
3632

37-
unstub zstd
3833
unstub tar
3934
}
4035

0 commit comments

Comments
 (0)