Skip to content

Commit a0d3564

Browse files
committed
Corrected bug with save level order
1 parent 19319f2 commit a0d3564

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

hooks/post-command

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,39 @@ 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' ] ||
5464
[ "${lower_level_updated:-false}" = 'true' ] ||
5565
! backend_exec exists "${KEY}"; then

tests/post-command.bats

Lines changed: 2 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

0 commit comments

Comments
 (0)