Skip to content

Commit 2628d11

Browse files
authored
Merge pull request #185 from mbland/skip-template-test
tests/template: Make fake tarball with BusyBox, and skip tests if programs are missing
2 parents de7ce6c + 8045210 commit 2628d11

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

go-template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ download_go_script_bash_tarball() {
8484
fi
8585
printf "Downloading framework from '%s'...\n" "$url"
8686

87-
if ! tar -xzf <("${download_cmd[@]}") || [[ ! -f "$unpacked_core" ]]; then
87+
if ! tar -xzf - < <("${download_cmd[@]}") || [[ ! -f "$unpacked_core" ]]; then
8888
printf "Failed to download from '%s'.\n" "$url" >&2
8989
return 1
9090
elif [[ ! -d "$core_dir_parent" ]] && ! mkdir -p "$core_dir_parent" ; then

tests/template.bats

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ assert_go_core_unpacked() {
7575
restore_bats_shell_options "$result"
7676
}
7777

78+
# Skips the current test if zero of the listed system programs are present.
79+
#
80+
# Arguments:
81+
# $@: System programs of which at least one must be present to run the test
82+
skip_if_none_present_on_system() {
83+
local missing
84+
85+
if ! command -v "$@" >/dev/null; then
86+
if [[ "$#" -eq '1' ]]; then
87+
skip "$1 isn't installed on the system"
88+
elif [[ "$#" -eq '2' ]]; then
89+
skip "Neither $1 nor $2 are installed on the system"
90+
else
91+
printf -v missing '%s, ' "${@:1:$(($# - 1))}"
92+
skip "None of ${missing% } or ${@:$#} are installed on the system"
93+
fi
94+
fi
95+
}
96+
7897
# Converts a Unix path or 'file://' URL to a Git for Windows native path.
7998
#
8099
# This is useful when passing file paths or URLs to native programs on Git for
@@ -123,11 +142,11 @@ create_fake_tarball_if_not_using_real_url() {
123142
elif ! mkdir -p "$full_dir"; then
124143
printf 'Failed to create fake content dir %s\n' "$full_dir" >&2
125144
result='1'
126-
elif ! tar xf <(tar cf - go-core.bash lib libexec) -C "$full_dir"; then
145+
elif ! tar -xf - -C "$full_dir" < <(tar -cf - go-core.bash lib libexec); then
127146
printf 'Failed to mirror %s to fake tarball dir %s\n' \
128147
"$_GO_ROOTDIR" "$full_dir" >&2
129148
result='1'
130-
elif ! tar cfz "$tarball" -C "$TEST_GO_ROOTDIR" "$dirname"; then
149+
elif ! tar -czf "$tarball" -C "$TEST_GO_ROOTDIR" "$dirname"; then
131150
printf 'Failed to create fake tarball %s\n from dir %s\n' \
132151
"$tarball" "$full_dir" >&2
133152
result='1'
@@ -202,6 +221,8 @@ run_with_download_program() {
202221
}
203222

204223
@test "$SUITE: download $GO_SCRIPT_BASH_VERSION from $FULL_DOWNLOAD_URL" {
224+
skip_if_none_present_on_system 'curl' 'fetch' 'wget'
225+
skip_if_system_missing 'tar'
205226
create_fake_tarball_if_not_using_real_url
206227
run "$TEST_GO_ROOTDIR/go-template"
207228

@@ -215,6 +236,7 @@ run_with_download_program() {
215236
}
216237

217238
@test "$SUITE: download locally using curl" {
239+
skip_if_system_missing 'tar'
218240
run_with_download_program 'curl'
219241
assert_output_matches "Downloading framework from '$NATIVE_LOCAL_URL'\.\.\."
220242
assert_output_matches "Download of '$NATIVE_LOCAL_URL' successful."$'\n\n'
@@ -223,6 +245,7 @@ run_with_download_program() {
223245
}
224246

225247
@test "$SUITE: download locally using fetch" {
248+
skip_if_system_missing 'tar'
226249
run_with_download_program 'fetch'
227250
assert_output_matches "Downloading framework from '$NATIVE_LOCAL_URL'\.\.\."
228251
assert_output_matches "Download of '$NATIVE_LOCAL_URL' successful."$'\n\n'
@@ -231,6 +254,7 @@ run_with_download_program() {
231254
}
232255

233256
@test "$SUITE: download locally using cat" {
257+
skip_if_system_missing 'tar'
234258
# We'll actually use `cat` with `file://` URLs, since `wget` only supports
235259
# HTTP, HTTPS, and FTP.
236260
run_with_download_program 'cat'
@@ -241,6 +265,7 @@ run_with_download_program() {
241265
}
242266

243267
@test "$SUITE: download locally using wget" {
268+
skip_if_system_missing 'tar'
244269
# As mentioned in the above test case, we'll actually use `cat` with `file://`
245270
# URLs, but we're simulating `wget` by pretending `cat` doesn't exist.
246271
run_with_download_program 'wget'
@@ -252,6 +277,8 @@ run_with_download_program() {
252277

253278
@test "$SUITE: download into nonstandard GO_SCRIPTS_DIR" {
254279
local core_dir="$TEST_GO_ROOTDIR/foobar"
280+
skip_if_none_present_on_system 'curl' 'fetch' 'wget'
281+
skip_if_system_missing 'tar'
255282
create_fake_tarball_if_not_using_real_url
256283

257284
# Create a command script in the normal `TEST_GO_SCRIPTS_DIR`.
@@ -265,6 +292,8 @@ run_with_download_program() {
265292
}
266293

267294
@test "$SUITE: download uses existing GO_SCRIPTS_DIR" {
295+
skip_if_none_present_on_system 'curl' 'fetch' 'wget'
296+
skip_if_system_missing 'tar'
268297
create_fake_tarball_if_not_using_real_url
269298
mkdir -p "$TEST_GO_SCRIPTS_DIR"
270299
stub_program_in_path mkdir "exit 1"
@@ -280,6 +309,9 @@ run_with_download_program() {
280309
@test "$SUITE: fail to download a nonexistent repo" {
281310
local url='https://bogus-url-that-does-not-exist'
282311
local repo='bogus-repo-that-does-not-exist'
312+
313+
skip_if_none_present_on_system 'curl' 'fetch' 'wget'
314+
skip_if_system_missing 'git' 'tar'
283315
GO_SCRIPT_BASH_DOWNLOAD_URL="$url" GO_SCRIPT_BASH_REPO_URL="$repo" \
284316
run "$TEST_GO_ROOTDIR/go-template"
285317
assert_failure
@@ -294,6 +326,9 @@ run_with_download_program() {
294326
@test "$SUITE: fail to download a nonexistent version" {
295327
local url="$GO_SCRIPT_BASH_DOWNLOAD_URL/vnonexistent.tar.gz"
296328
local branch='vnonexistent'
329+
330+
skip_if_none_present_on_system 'curl' 'fetch' 'wget'
331+
skip_if_system_missing 'git' 'tar'
297332
GO_SCRIPT_BASH_VERSION="$branch" run "$TEST_GO_ROOTDIR/go-template"
298333
assert_failure
299334
assert_output_matches 'Using git clone as fallback'
@@ -307,6 +342,7 @@ run_with_download_program() {
307342
@test "$SUITE: use git clone if GO_SCRIPT_BASH_DOWNLOAD_URL lacks a protocol" {
308343
local url='bogus-url-with-no-protocol'
309344

345+
skip_if_system_missing 'git'
310346
GO_SCRIPT_BASH_DOWNLOAD_URL="$url" run "$TEST_GO_ROOTDIR/go-template"
311347

312348
assert_output_matches "GO_SCRIPT_BASH_DOWNLOAD_URL has no protocol: $url"
@@ -325,6 +361,7 @@ run_with_download_program() {
325361
}
326362

327363
@test "$SUITE: fail to find download program uses git clone" {
364+
skip_if_system_missing 'git'
328365
PATH="$BATS_TEST_BINDIR" run "$BASH" "$TEST_GO_ROOTDIR/go-template"
329366

330367
assert_output_matches "Failed to find cURL, wget, or fetch"
@@ -335,7 +372,8 @@ run_with_download_program() {
335372
}
336373

337374
@test "$SUITE: fail to find tar uses git clone" {
338-
# If none of these are installed on the system, the test will fail.
375+
skip_if_none_present_on_system 'curl' 'fetch' 'wget'
376+
skip_if_system_missing 'git'
339377
create_forwarding_script 'curl'
340378
create_forwarding_script 'wget'
341379
create_forwarding_script 'fetch'
@@ -350,6 +388,8 @@ run_with_download_program() {
350388
}
351389

352390
@test "$SUITE: fail to create directory uses git clone" {
391+
skip_if_none_present_on_system 'curl' 'fetch' 'wget'
392+
skip_if_system_missing 'git' 'tar'
353393
create_fake_tarball_if_not_using_real_url
354394
stub_program_in_path mkdir "exit 1"
355395
run "$TEST_GO_ROOTDIR/go-template"
@@ -371,6 +411,8 @@ run_with_download_program() {
371411
@test "$SUITE: fail to move extracted directory uses git clone" {
372412
local target="$TEST_GO_SCRIPTS_DIR/go-script-bash"
373413

414+
skip_if_none_present_on_system 'curl' 'fetch' 'wget'
415+
skip_if_system_missing 'git' 'tar'
374416
create_fake_tarball_if_not_using_real_url
375417
stub_program_in_path mv "exit 1"
376418
run "$TEST_GO_ROOTDIR/go-template"

0 commit comments

Comments
 (0)