@@ -21,9 +21,16 @@ load environment
21
21
TEST_USE_REAL_URL=" ${TEST_USE_REAL_URL:- $TRAVIS } "
22
22
GO_CORE_URL=" ${TEST_USE_REAL_URL: +$_GO_CORE_URL } "
23
23
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
+
24
31
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 "
27
34
28
35
RELEASE_TARBALL=" ${GO_SCRIPT_BASH_VERSION} .tar.gz"
29
36
FULL_DOWNLOAD_URL=" $GO_SCRIPT_BASH_DOWNLOAD_URL /$RELEASE_TARBALL "
@@ -55,6 +62,36 @@ assert_go_core_unpacked() {
55
62
restore_bats_shell_options " $result "
56
63
}
57
64
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
+
58
95
# Creates a script in `BATS_TEST_BINDIR` to stand in for a program on `PATH`
59
96
#
60
97
# This enables a test to use `PATH="$BATS_TEST_BINDIR" run ...` to hide programs
@@ -85,6 +122,7 @@ create_forwarding_script() {
85
122
}
86
123
87
124
@test " $SUITE : download $GO_SCRIPT_BASH_VERSION from $GO_SCRIPT_BASH_REPO_URL " {
125
+ create_fake_tarball_if_not_using_real_url
88
126
run " $TEST_GO_ROOTDIR /go-template"
89
127
90
128
# Without a command argument, the script will print the top-level help and
@@ -111,16 +149,15 @@ create_forwarding_script() {
111
149
112
150
@test " $SUITE : fail to download a nonexistent version" {
113
151
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."
124
161
}
125
162
126
163
@test " $SUITE : fail to find curl uses git clone" {
@@ -167,6 +204,7 @@ create_forwarding_script() {
167
204
}
168
205
169
206
@test " $SUITE : fail to create directory uses git clone" {
207
+ create_fake_tarball_if_not_using_real_url
170
208
stub_program_in_path mkdir " exit 1"
171
209
run " $TEST_GO_ROOTDIR /go-template"
172
210
restore_program_in_path mkdir
@@ -197,6 +235,7 @@ create_forwarding_script() {
197
235
@test " $SUITE : fail to move extracted directory uses git clone" {
198
236
local target=" $TEST_GO_SCRIPTS_DIR /go-script-bash"
199
237
238
+ create_fake_tarball_if_not_using_real_url
200
239
stub_program_in_path mv " exit 1"
201
240
run " $TEST_GO_ROOTDIR /go-template"
202
241
restore_program_in_path mv
0 commit comments