Skip to content

Commit 0c7490b

Browse files
committed
Added tests for upload variable replacement
1 parent 891abcd commit 0c7490b

2 files changed

Lines changed: 174 additions & 6 deletions

File tree

tests/upload-compressed.bats

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
167167

168168
unstub buildkite-agent
169169
unstub tar
170-
170+
171171
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_FROM
172172
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_TO
173173
unset BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED
@@ -193,7 +193,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
193193
assert_output --partial "Compressing file.log to file.zip"
194194
assert_output --partial "uploaded file.zip"
195195
refute_output --partial "uploaded file.log"
196-
196+
197197
unstub buildkite-agent
198198
unstub zip
199199

@@ -224,7 +224,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
224224
assert_output --partial "Compressing file.log to file.tgz"
225225
assert_output --partial "uploaded file.tgz"
226226
refute_output --partial "uploaded file.log"
227-
227+
228228
unstub buildkite-agent
229229
unstub tar
230230

@@ -349,7 +349,7 @@ load "${BATS_PLUGIN_PATH}/load.bash"
349349

350350
unstub buildkite-agent
351351
unstub zip
352-
352+
353353
unset BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_FROM
354354
unset BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0_TO
355355
unset BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_1
@@ -429,4 +429,91 @@ load "${BATS_PLUGIN_PATH}/load.bash"
429429
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0_FROM
430430
unset BUILDKITE_PLUGIN_ARTIFACTS_DOWNLOAD_0_TO
431431
unset BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED
432-
}
432+
}
433+
434+
@test "Post-command does not replace compressed file variables" {
435+
export RANDOM_VAR="random-value"
436+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD="test.log"
437+
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="file-\${RANDOM_VAR}.zip"
438+
439+
touch "test.log"
440+
441+
stub buildkite-agent \
442+
"artifact upload \* : echo uploaded \$3"
443+
444+
stub zip \
445+
"-r \* \* : echo zipped \$3 into \$2"
446+
447+
run "$PWD/hooks/post-command"
448+
449+
assert_success
450+
451+
assert_output --partial "Compressing test.log to file-\${RANDOM_VAR}.zip"
452+
refute_output --partial "Compressing test.log to file-random-value.zip"
453+
454+
unstub buildkite-agent
455+
unstub zip
456+
457+
rm "test.log"
458+
}
459+
460+
@test "Post-command replaces compressed file variables" {
461+
export RANDOM_VAR="random-value"
462+
export OTHER_VAR="other-value"
463+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD="test-\${OTHER_VAR}.log"
464+
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="file-\${RANDOM_VAR}.zip"
465+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_UPLOAD_VARS="true"
466+
467+
touch "test-other-value.log"
468+
469+
stub buildkite-agent \
470+
"artifact upload \* : echo uploaded \$3"
471+
472+
stub zip \
473+
"-r \* \* : echo zipped \$3 into \$2"
474+
475+
run "$PWD/hooks/post-command"
476+
477+
assert_success
478+
479+
assert_output --partial "Compressing test-other-value.log to file-random-value.zip"
480+
481+
unstub buildkite-agent
482+
unstub zip
483+
484+
rm "test-other-value.log"
485+
}
486+
487+
@test "Post-command replaces file variables with multiple files with relocation (sometimes)" {
488+
export RANDOM_VAR="random-value"
489+
export OTHER_VAR="other-value"
490+
export DEST_VAR="dest-value"
491+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0="test-\${OTHER_VAR}.log"
492+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_1_FROM="test2-\${OTHER_VAR}.log"
493+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_1_TO="test-\${DEST_VAR}.log"
494+
export BUILDKITE_PLUGIN_ARTIFACTS_COMPRESSED="file-\${RANDOM_VAR}.zip"
495+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_UPLOAD_VARS="true"
496+
497+
touch "test-other-value.log"
498+
touch "test2-other-value.log"
499+
500+
stub buildkite-agent \
501+
"artifact upload \* : echo uploaded \$3"
502+
503+
stub zip \
504+
"-r \* \* : echo zipped \$3 into \$2"
505+
506+
run "$PWD/hooks/post-command"
507+
508+
assert_success
509+
510+
assert_output --partial "Moving [test2-other-value.log] to [test-dest-value.log]"
511+
assert_output --partial "Compressing test-other-value.log test-dest-value.log to file-random-value.zip"
512+
513+
unstub buildkite-agent
514+
unstub zip
515+
516+
rm -f "test-other-value.log"
517+
rm -f "test2-other-value.log"
518+
rm -f "test-dest-value.log"
519+
}

tests/upload.bats

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,85 @@ load "${BATS_PLUGIN_PATH}/load.bash"
282282
assert_success
283283
refute_output --partial "Uploading artifacts"
284284
assert_output --partial "skipping upload"
285-
}
285+
}
286+
287+
@test "File variables are not replaced" {
288+
export RANDOM_VAR="random-value"
289+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD="test-\${RANDOM_VAR}.log"
290+
291+
stub buildkite-agent \
292+
"artifact upload \* : echo \"uploaded \$3\""
293+
294+
run "$PWD/hooks/post-command"
295+
296+
assert_success
297+
assert_output --partial "Uploading artifacts"
298+
refute_output --partial "uploaded test-random-value.log"
299+
assert_output --partial "uploaded test-\${RANDOM_VAR}.log"
300+
301+
unstub buildkite-agent
302+
}
303+
304+
@test "File variables can be replaced" {
305+
export RANDOM_VAR="random-value"
306+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_UPLOAD_VARS="true"
307+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD="test-\${RANDOM_VAR}.log"
308+
309+
stub buildkite-agent \
310+
"artifact upload \* : echo \"uploaded \$3\""
311+
312+
run "$PWD/hooks/post-command"
313+
314+
assert_success
315+
assert_output --partial "Uploading artifacts"
316+
refute_output --partial "uploaded test-\${RANDOM_VAR}.log"
317+
assert_output --partial "uploaded test-random-value.log"
318+
319+
unstub buildkite-agent
320+
}
321+
322+
@test "File variables can be replaced with relocation" {
323+
export RANDOM_VAR="random-value"
324+
export DEST_VAR="dest-value"
325+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_UPLOAD_VARS="true"
326+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_FROM="test-\${RANDOM_VAR}.log"
327+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_TO="test-\${DEST_VAR}.log"
328+
329+
stub buildkite-agent \
330+
"artifact upload \* : echo \"uploaded \$3\""
331+
332+
run "$PWD/hooks/post-command"
333+
334+
assert_success
335+
assert_output --partial "Uploading artifacts"
336+
refute_output --partial "uploaded test-\${RANDOM_VAR}.log"
337+
refute_output --partial "uploaded test-\${DEST_VAR}.log"
338+
assert_output --partial "uploaded test-dest-value.log"
339+
340+
unstub buildkite-agent
341+
}
342+
343+
@test "Multiple file variables can be replaced with relocation (sometimes)" {
344+
export RANDOM_VAR="random-value"
345+
export DEST_VAR="dest-value"
346+
export BUILDKITE_PLUGIN_ARTIFACTS_EXPAND_UPLOAD_VARS="true"
347+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_0="test.log"
348+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_1_FROM="test-\${DEST_VAR}.log"
349+
export BUILDKITE_PLUGIN_ARTIFACTS_UPLOAD_1_TO="test-\${DEST_VAR}.log"
350+
351+
stub buildkite-agent \
352+
"artifact upload \* : echo \"uploaded \$3\"" \
353+
"artifact upload \* : echo \"uploaded \$3\""
354+
355+
run "$PWD/hooks/post-command"
356+
357+
assert_success
358+
assert_output --partial "Uploading artifacts"
359+
assert_output --partial "uploaded test.log"
360+
361+
refute_output --partial "uploaded test-\${RANDOM_VAR}.log"
362+
refute_output --partial "uploaded test-\${DEST_VAR}.log"
363+
assert_output --partial "uploaded test-dest-value.log"
364+
365+
unstub buildkite-agent
366+
}

0 commit comments

Comments
 (0)