Skip to content

Commit 4cc21b0

Browse files
committed
tests/template: Restore GO_SCRIPT_BASH_REPO_URL
This had been hardcoded to the actual repo value in #179. The `create_fake_tarball_if_not_using_real_url` helper function and `TEST_ARCHIVE_URL` environment variable enable the test to run isolated from the network. Also tweaks `GO_SCRIPTS_DIR` to remove unnecessary braces.
1 parent d0ebc17 commit 4cc21b0

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

go-template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ download_go_script_bash_tarball() {
6363
fi
6464
printf "Downloading framework from '%s'...\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL"
6565
if ! tar -xzf <(curl -LfsS "$GO_SCRIPT_BASH_DOWNLOAD_URL") 2>/dev/null ||
66-
[[ ! -d "$unpacked_dir" ]]; then
66+
[[ ! -f "$unpacked_dir/go-core.bash" ]]; then
6767
printf "Failed to download from '%s'.\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL" >&2
6868
return 1
6969
fi

tests/template.bats

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,16 @@ load environment
2121
TEST_USE_REAL_URL="${TEST_USE_REAL_URL:-$TRAVIS}"
2222
GO_CORE_URL="${TEST_USE_REAL_URL:+$_GO_CORE_URL}"
2323
GO_CORE_URL="${GO_CORE_URL:-$_GO_CORE_DIR}"
24+
25+
# Use the same mechanism for testing tarball downloads, since we'll have a
26+
# connection to GitHub in either case.
27+
TEST_ARCHIVE_URL="file://$TEST_GO_ROOTDIR/archive"
28+
GO_ARCHIVE_URL="${TEST_USE_REAL_URL:+$_GO_CORE_URL/archive}"
29+
GO_ARCHIVE_URL="${GO_ARCHIVE_URL:-$TEST_ARCHIVE_URL}"
30+
2431
GO_SCRIPT_BASH_VERSION="$_GO_CORE_VERSION"
25-
GO_SCRIPT_BASH_REPO_URL="https://github.com/mbland/go-script-bash.git"
26-
GO_SCRIPT_BASH_DOWNLOAD_URL="${GO_SCRIPT_BASH_REPO_URL%.git}/archive"
32+
GO_SCRIPT_BASH_REPO_URL="$GO_CORE_URL"
33+
GO_SCRIPT_BASH_DOWNLOAD_URL="$GO_ARCHIVE_URL"
2734

2835
RELEASE_TARBALL="${GO_SCRIPT_BASH_VERSION}.tar.gz"
2936
FULL_DOWNLOAD_URL="$GO_SCRIPT_BASH_DOWNLOAD_URL/$RELEASE_TARBALL"
@@ -55,6 +62,36 @@ assert_go_core_unpacked() {
5562
restore_bats_shell_options "$result"
5663
}
5764

65+
# This mimics the tarball provided by GitHub.
66+
#
67+
# This could probably become a general-purpose utility one day.
68+
create_fake_tarball_if_not_using_real_url() {
69+
# We have to trim the leading 'v' from the version string.
70+
local dirname="go-script-bash-${GO_SCRIPT_BASH_VERSION#v}"
71+
local full_dir="$TEST_GO_ROOTDIR/$dirname"
72+
local tarball="${FULL_DOWNLOAD_URL#file://}"
73+
74+
if [[ -n "$TEST_USE_REAL_URL" ]]; then
75+
return
76+
fi
77+
78+
if ! mkdir -p "${tarball%/*}"; then
79+
printf 'Failed to create fake archive dir %s\n' "$full_dir" >&2
80+
return 1
81+
elif ! mkdir -p "$full_dir"; then
82+
printf 'Failed to create fake content dir %s\n' "$full_dir" >&2
83+
return 1
84+
elif ! tar xf <(tar cf - go-core.bash lib libexec) -C "$full_dir"; then
85+
printf 'Failed to mirror %s to fake tarball dir %s\n' \
86+
"$_GO_ROOTDIR" "$full_dir" >&2
87+
return 1
88+
elif ! tar cfz "$tarball" -C "$TEST_GO_ROOTDIR" "$dirname"; then
89+
printf 'Failed to create fake tarball %s\n from dir %s\n' \
90+
"$tarball" "$full_dir" >&2
91+
return 1
92+
fi
93+
}
94+
5895
# Creates a script in `BATS_TEST_BINDIR` to stand in for a program on `PATH`
5996
#
6097
# This enables a test to use `PATH="$BATS_TEST_BINDIR" run ...` to hide programs
@@ -85,6 +122,7 @@ create_forwarding_script() {
85122
}
86123

87124
@test "$SUITE: download $GO_SCRIPT_BASH_VERSION from $GO_SCRIPT_BASH_REPO_URL" {
125+
create_fake_tarball_if_not_using_real_url
88126
run "$TEST_GO_ROOTDIR/go-template"
89127

90128
# Without a command argument, the script will print the top-level help and
@@ -111,16 +149,15 @@ create_forwarding_script() {
111149

112150
@test "$SUITE: fail to download a nonexistent version" {
113151
local url="$GO_SCRIPT_BASH_DOWNLOAD_URL/vnonexistent.tar.gz"
114-
GO_SCRIPT_BASH_VERSION='vnonexistent' run "$TEST_GO_ROOTDIR/go-template"
115-
assert_failure "Downloading framework from '$url'..." \
116-
"curl: (22) The requested URL returned error: 404 Not Found" \
117-
"Failed to download from '$url'." \
118-
'Using git clone as fallback' \
119-
"Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'..." \
120-
"Cloning into '$TEST_GO_SCRIPTS_DIR/go-script-bash'..." \
121-
"warning: Could not find remote branch vnonexistent to clone." \
122-
"fatal: Remote branch vnonexistent not found in upstream origin" \
123-
"Failed to clone '$GO_SCRIPT_BASH_REPO_URL'; aborting."
152+
local branch='vnonexistent'
153+
GO_SCRIPT_BASH_VERSION="$branch" run "$TEST_GO_ROOTDIR/go-template"
154+
assert_failure
155+
assert_output_matches 'Using git clone as fallback'
156+
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'"
157+
assert_output_matches "Cloning into '$TEST_GO_SCRIPTS_DIR/go-script-bash'"
158+
assert_output_matches "warning: Could not find remote branch $branch to clone"
159+
assert_output_matches "fatal: Remote branch $branch not found in upstream"
160+
assert_output_matches "Failed to clone '$GO_SCRIPT_BASH_REPO_URL'; aborting."
124161
}
125162

126163
@test "$SUITE: fail to find curl uses git clone" {
@@ -167,6 +204,7 @@ create_forwarding_script() {
167204
}
168205

169206
@test "$SUITE: fail to create directory uses git clone" {
207+
create_fake_tarball_if_not_using_real_url
170208
stub_program_in_path mkdir "exit 1"
171209
run "$TEST_GO_ROOTDIR/go-template"
172210
restore_program_in_path mkdir
@@ -197,6 +235,7 @@ create_forwarding_script() {
197235
@test "$SUITE: fail to move extracted directory uses git clone" {
198236
local target="$TEST_GO_SCRIPTS_DIR/go-script-bash"
199237

238+
create_fake_tarball_if_not_using_real_url
200239
stub_program_in_path mv "exit 1"
201240
run "$TEST_GO_ROOTDIR/go-template"
202241
restore_program_in_path mv

0 commit comments

Comments
 (0)