Skip to content

Commit 891abcd

Browse files
committed
Added tests for download var expansion
1 parent 182ed03 commit 891abcd

2 files changed

Lines changed: 187 additions & 7 deletions

File tree

tests/download-compressed.bats

Lines changed: 118 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
88
@test "Invalid compressed format" {
99
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD="file.log"
1010
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="file.rar"
11-
11+
1212
run "$PWD/hooks/pre-command"
1313

1414
assert_failure
@@ -22,7 +22,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
2222
@test "Single value zip" {
2323
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD="*.log"
2424
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="file.zip"
25-
25+
2626
stub buildkite-agent \
2727
"artifact download \* \* : echo downloaded \$3 to \$4"
2828

@@ -46,7 +46,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
4646
@test "Single value tgz" {
4747
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD="*.log"
4848
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="file.tgz"
49-
49+
5050
stub buildkite-agent \
5151
"artifact download \* \* : echo downloaded \$3 to \$4"
5252

@@ -230,7 +230,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
230230

231231
unstub buildkite-agent
232232
unstub tar
233-
233+
234234
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD
235235
unset BUILDKITE_PLUGIN_ARTIFACTS_BUILD
236236
unset BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED
@@ -402,4 +402,117 @@ load "${BATS_PLUGIN_PATH}/load.bash"
402402
unset BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_TO
403403
unset BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_FROM
404404
unset BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_TO
405-
}
405+
}
406+
407+
@test "Pre-command does not replace compressed file variables" {
408+
export RANDOM_VAR="random-value"
409+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD="bar.log"
410+
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="FILE-\${RANDOM_VAR}.zip"
411+
412+
stub buildkite-agent \
413+
"artifact download \* \* : echo downloaded artifact \$3 to \$4"
414+
415+
run "$PWD/hooks/pre-command"
416+
417+
assert_failure
418+
refute_output --partial "downloaded artifact FILE-random-value.zip"
419+
assert_output --partial "downloaded artifact FILE-\${RANDOM_VAR}.zip"
420+
421+
unstub buildkite-agent
422+
423+
unset BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED
424+
unset BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_UPLOAD_VARS
425+
}
426+
427+
@test "Pre-command replaces compressed file variables" {
428+
export RANDOM_VAR="random-value"
429+
export OTHER_VAR="other-value"
430+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD="bar-\${OTHER_VAR}.log"
431+
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="FILE-\${RANDOM_VAR}.zip"
432+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_DOWNLOAD_VARS="true"
433+
434+
stub buildkite-agent \
435+
"artifact download \* \* : echo downloaded artifact \$3 to \$4"
436+
437+
stub unzip \
438+
"\* \* : echo extracted \$2 from \$1; touch \$2"
439+
440+
run "$PWD/hooks/pre-command"
441+
442+
assert_success
443+
assert_output --partial "downloaded artifact FILE-random-value.zip"
444+
assert_output --partial "Uncompressing bar-other-value.log from FILE-random-value.zip"
445+
446+
unstub buildkite-agent
447+
unstub unzip
448+
449+
unset BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED
450+
unset BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_UPLOAD_VARS
451+
}
452+
453+
@test "Pre-command replaces file variables with relocation" {
454+
export RANDOM_VAR="random-value"
455+
export OTHER_VAR="other-value"
456+
export DEST_VAR="dest-value"
457+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_FROM="/tmp/bar-\${OTHER_VAR}.log"
458+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO="/tmp/baz-\${DEST_VAR}.log"
459+
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="FILE-\${RANDOM_VAR}.zip"
460+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_DOWNLOAD_VARS="true"
461+
462+
stub buildkite-agent \
463+
"artifact download \* \* : echo downloaded artifact \$3 to \$4"
464+
465+
stub unzip \
466+
"\* \* : echo extracted \$2 from \$1; touch \$2"
467+
468+
run "$PWD/hooks/pre-command"
469+
470+
assert_success
471+
assert_output --partial "downloaded artifact FILE-random-value.zip"
472+
assert_output --partial "Uncompressing /tmp/bar-other-value.log from FILE-random-value.zip"
473+
assert_output --partial "Moving [/tmp/bar-other-value.log] to [/tmp/baz-dest-value.log]"
474+
475+
assert [ ! -e /tmp/bar-other-value.log ]
476+
assert [ -e /tmp/baz-dest-value.log ]
477+
rm /tmp/baz-dest-value.log
478+
479+
unstub buildkite-agent
480+
unstub unzip
481+
482+
unset BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED
483+
unset BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_UPLOAD_VARS
484+
}
485+
486+
@test "Pre-command replaces file variables with multiple files with relocation sometimes" {
487+
export RANDOM_VAR="random-value"
488+
export OTHER_VAR="other-value"
489+
export DEST_VAR="dest-value"
490+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0="/tmp/bar.log"
491+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_1_FROM="/tmp/bar-\${OTHER_VAR}.log"
492+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_1_TO="/tmp/baz-\${DEST_VAR}.log"
493+
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="FILE-\${RANDOM_VAR}.zip"
494+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_DOWNLOAD_VARS="true"
495+
496+
stub buildkite-agent \
497+
"artifact download \* \* : echo downloaded artifact \$3 to \$4"
498+
499+
stub unzip \
500+
"\* \* \* : echo extracted \$2 and \$3 from \$1; touch \$2 \$3"
501+
502+
run "$PWD/hooks/pre-command"
503+
504+
assert_success
505+
assert_output --partial "downloaded artifact FILE-random-value.zip"
506+
assert_output --partial "Uncompressing /tmp/bar.log /tmp/bar-other-value.log from FILE-random-value.zip"
507+
assert_output --partial "Moving [/tmp/bar-other-value.log] to [/tmp/baz-dest-value.log]"
508+
509+
assert [ ! -e /tmp/bar-other-value.log ]
510+
assert [ -e /tmp/baz-dest-value.log ]
511+
rm /tmp/baz-dest-value.log
512+
513+
unstub buildkite-agent
514+
unstub unzip
515+
516+
unset BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED
517+
unset BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_UPLOAD_VARS
518+
}

tests/download.bats

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
263263

264264
@test "Pre-command does nothing if there is no download-specific vars setup" {
265265
run "$PWD/hooks/pre-command"
266-
266+
267267
assert_success
268268
refute_output --partial "Downloading artifacts"
269269
}
@@ -287,4 +287,71 @@ load "${BATS_PLUGIN_PATH}/load.bash"
287287
unset BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_TO
288288
unset BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_FROM
289289
unset BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_TO
290-
}
290+
}
291+
292+
@test "Pre-command does not expand variables by default" {
293+
export RANDOM_VAR="random-var"
294+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD="file-\${RANDOM_VAR}.log"
295+
296+
stub buildkite-agent \
297+
"artifact download \* \* : echo can not download artifact \$3 to \$4; exit 1"
298+
299+
run "$PWD/hooks/pre-command"
300+
301+
assert_failure
302+
assert_output --partial "Error in download of file-\${RANDOM_VAR}.log"
303+
refute_output --partial "file-random-var.log to ."
304+
305+
unstub buildkite-agent
306+
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD
307+
unset BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_DOWNLOAD_VARS
308+
}
309+
310+
@test "Pre-command downloads artifacts with expansion" {
311+
export RANDOM_VAR="random-var"
312+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD="\${RANDOM_VAR}.log"
313+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_DOWNLOAD_VARS="true"
314+
315+
stub buildkite-agent \
316+
"artifact download \* \* : echo downloaded artifact \$3 to \$4"
317+
318+
run "$PWD/hooks/pre-command"
319+
320+
assert_success
321+
assert_output --partial "Downloading artifacts"
322+
assert_output --partial "downloaded artifact random-var.log to ."
323+
324+
unstub buildkite-agent
325+
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD
326+
unset BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_DOWNLOAD_VARS
327+
}
328+
329+
@test "Pre-command downloads multiple artifacts with expansion and relocation" {
330+
export RANDOM_VAR="random-var"
331+
export DEST_VAR="dest-var"
332+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_DOWNLOAD_VARS="true"
333+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0="file.log"
334+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_1_FROM="file-\${RANDOM_VAR}.log"
335+
export BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_1_TO="dest-\${DEST_VAR}.log"
336+
337+
stub buildkite-agent \
338+
"artifact download \* \* : echo downloaded artifact \$3 to \$4; touch \$3" \
339+
"artifact download \* \* : echo downloaded artifact \$3 to \$4; touch \$3"
340+
341+
run "$PWD/hooks/pre-command"
342+
343+
assert_success
344+
assert_output --partial "Downloading artifacts"
345+
assert_output --partial "downloaded artifact file-random-var.log to ."
346+
assert_output --partial "Moving [file-random-var.log] to [dest-dest-var.log]"
347+
348+
assert [ -e file.log ]
349+
assert [ -e dest-dest-var.log ]
350+
assert [ ! -e file-random-var.log ]
351+
352+
rm file.log dest-dest-var.log
353+
354+
unstub buildkite-agent
355+
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD
356+
unset BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_DOWNLOAD_VARS
357+
}

0 commit comments

Comments
 (0)