Skip to content

Commit 0fc5b9f

Browse files
authored
Merge pull request #95 from buildkite-plugins/toote_saves_forces_higher_levels
Save logic updates
2 parents 33f6282 + a0d3564 commit 0fc5b9f

2 files changed

Lines changed: 69 additions & 14 deletions

File tree

hooks/post-command

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,41 @@ if ! validate_compression "${COMPRESS}"; then
2727
exit 1
2828
fi
2929

30-
SAVE_LEVELS=()
31-
if plugin_read_list_into_result SAVE; then
32-
for LEVEL in "${result[@]}"; do
33-
SAVE_LEVELS+=("${LEVEL}")
30+
if ! plugin_read_list_into_result SAVE; then
31+
echo 'Cache not setup for saving'
32+
exit 0
33+
fi
3434

35-
# this validates the level as well
36-
KEY=$(build_key "${LEVEL}" "${CACHE_PATH}" "${COMPRESS}")
35+
ORDERED_LEVELS=(file step branch pipeline all)
36+
SAVE_LEVELS=()
3737

38-
if [ "${LEVEL}" = 'file' ] && [ -z "$(plugin_read_config MANIFEST)" ]; then
39-
echo "+++ 🚨 Missing manifest option in the cache plugin for file-level saving"
40-
exit 1
38+
# array intersection keeping the order of the first array
39+
for O_LEVEL in "${ORDERED_LEVELS[@]}"; do
40+
for R_LEVEL in "${result[@]}"; do
41+
if [ "${O_LEVEL}" = "${R_LEVEL}" ]; then
42+
SAVE_LEVELS+=("${O_LEVEL}")
4143
fi
4244
done
43-
else
44-
echo 'Cache not setup for saving'
45-
exit 0
45+
done
46+
47+
if [ "${#SAVE_LEVELS[@]}" -ne "${#result[@]}" ]; then
48+
echo 'Invalid levels in the save list'
49+
exit 1
4650
fi
4751

4852
ACTUAL_PATH="${CACHE_PATH}"
4953

5054
for LEVEL in "${SAVE_LEVELS[@]}"; do
55+
# this validates the level as well
5156
KEY=$(build_key "${LEVEL}" "${CACHE_PATH}" "${COMPRESS}")
5257

58+
if [ "${LEVEL}" = 'file' ] && [ -z "$(plugin_read_config MANIFEST)" ]; then
59+
echo "+++ 🚨 Missing manifest option in the cache plugin for file-level saving"
60+
exit 1
61+
fi
62+
5363
if [ "$(plugin_read_config FORCE 'false')" != 'false' ] ||
64+
[ "${lower_level_updated:-false}" = 'true' ] ||
5465
! backend_exec exists "${KEY}"; then
5566
echo "Saving ${LEVEL}-level cache of ${CACHE_PATH}"
5667
if compression_active && [ "${already_compressed:-false}" = 'false' ]; then
@@ -59,6 +70,7 @@ for LEVEL in "${SAVE_LEVELS[@]}"; do
5970
already_compressed='true' # avoid re-compressing the files
6071
fi
6172
backend_exec save "${KEY}" "${ACTUAL_PATH}"
73+
lower_level_updated='true' # to force saving higher levels
6274
else
6375
echo "Cache of ${LEVEL} already exists, skipping"
6476
fi

tests/post-command.bats

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ teardown() {
6060
run "$PWD/hooks/post-command"
6161

6262
assert_failure
63-
assert_output --partial 'Invalid cache level'
63+
assert_output --partial 'Invalid levels in the save list'
6464
}
6565

6666
@test "Invalid compression level fails" {
@@ -190,7 +190,7 @@ teardown() {
190190

191191
assert_failure
192192

193-
assert_output --partial 'Invalid cache level unreal'
193+
assert_output --partial 'Invalid levels in the save list'
194194
refute_output --partial 'Saving pipeline-level cache'
195195
}
196196

@@ -208,3 +208,46 @@ teardown() {
208208

209209
unstub cache_dummy
210210
}
211+
212+
@test "Multiple level saving not forced" {
213+
export BUILDKITE_PLUGIN_CACHE_FORCE=false
214+
215+
export BUILDKITE_PLUGIN_CACHE_SAVE_0=all
216+
export BUILDKITE_PLUGIN_CACHE_SAVE_1=pipeline
217+
218+
stub cache_dummy \
219+
"exists \* \* : exit 0" \
220+
"exists \* \* : exit 1" \
221+
"save \* \* : echo saving \$3 in \$2"
222+
223+
run "$PWD/hooks/post-command"
224+
225+
assert_success
226+
assert_output --partial 'Saving all-level cache'
227+
refute_output --partial 'Saving pipeline-level cache'
228+
229+
unstub cache_dummy
230+
}
231+
232+
@test "Multiple level saving lower level change forces higher levels" {
233+
export BUILDKITE_PLUGIN_CACHE_FORCE=false
234+
235+
export BUILDKITE_PLUGIN_CACHE_SAVE_0=all
236+
export BUILDKITE_PLUGIN_CACHE_SAVE_1=pipeline
237+
export BUILDKITE_PLUGIN_CACHE_SAVE_2=step
238+
239+
stub cache_dummy \
240+
"exists \* \* : exit 0" \
241+
"exists \* \* : exit 1" \
242+
"save \* \* : echo saving \$3 in \$2" \
243+
"save \* \* : echo saving \$3 in \$2"
244+
245+
run "$PWD/hooks/post-command"
246+
247+
assert_success
248+
assert_output --partial 'Saving all-level cache'
249+
assert_output --partial 'Saving pipeline-level cache'
250+
refute_output --partial 'Saving step-level cache'
251+
252+
unstub cache_dummy
253+
}

0 commit comments

Comments
 (0)