Skip to content

Commit 8d1fa10

Browse files
committed
tests/template: Skip tests if programs are missing
Most of the test cases will now be skipped if any of the required programs are missing, rather than failing and requiring the user to investigate. Whereas `skip_if_system_missing` will skip a test case if any of the specified programs are missing, `skip_if_none_present_on_system` will skip a test case only if all of the programs are missing. I'll move it to `lib/bats/helpers` in a future commit.
1 parent b1cab8c commit 8d1fa10

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

tests/template.bats

Lines changed: 43 additions & 1 deletion
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
@@ -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'
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'
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)