Skip to content

Commit 305efc7

Browse files
committed
go-template: Test in TEST_GO_ROOTDIR, fix quoting
Don't know why the test wasn't originally written using `TEST_GO_ROOTDIR`. As intended, it exposed some missing quotes from variables in `go-template` introduced in #179. I've removed several instances of the following comment, as I'm not sure it applies anymore. If it does, I'll make sure to restore it as needed: # Use `.*/scripts/go-script-bash` to account for the fact that `git clone` on # MSYS2 will output `C:/Users/<user>/AppData/Local/Temp/` in place of `/tmp`.
1 parent 28cd65a commit 305efc7

File tree

2 files changed

+51
-54
lines changed

2 files changed

+51
-54
lines changed

go-template

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ if [[ ! -e "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" ]]; then
6868
printf "Failed to download from '%s'.\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL" >&2
6969
return 1
7070
fi
71-
if ! mkdir -p $GO_SCRIPTS_DIR ; then
72-
printf "Failed to create scripts dir '%s'\n" $GO_SCRIPTS_DIR >&2
73-
rm -rf go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER
71+
if ! mkdir -p "$GO_SCRIPTS_DIR" ; then
72+
printf "Failed to create scripts dir '%s'\n" "$GO_SCRIPTS_DIR" >&2
73+
rm -rf "go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER"
7474
return 1
7575
fi
76-
if ! mv go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER $GO_SCRIPT_BASH_CORE_DIR; then
77-
printf "Failed to install downloaded directory in '%s'\n" $GO_SCRIPT_BASH_CORE_DIR >&2
78-
rm -rf go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER
76+
if ! mv "go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER" "$GO_SCRIPT_BASH_CORE_DIR"; then
77+
printf "Failed to install downloaded directory in '%s'\n" \
78+
"$GO_SCRIPT_BASH_CORE_DIR" >&2
79+
rm -rf "go-script-bash-$_GO_SCRIPT_BASH_VERSION_NUMBER"
7980
return 1
8081
fi
8182
printf "Download of '%s' successful.\n\n" "$GO_SCRIPT_BASH_DOWNLOAD_URL"

tests/template.bats

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,23 @@ GO_CORE_URL="${GO_CORE_URL:-$_GO_CORE_DIR}"
2525
setup() {
2626
test_filter
2727
export GO_SCRIPT_BASH_VERSION="$_GO_CORE_VERSION"
28-
export GO_SCRIPTS_DIR="$_GO_TEST_DIR/tmp/go-template-test-scripts"
2928
export GO_SCRIPT_BASH_REPO_URL="https://github.com/mbland/go-script-bash.git"
3029
export GO_SCRIPT_BASH_DOWNLOAD_URL="${GO_SCRIPT_BASH_REPO_URL%.git}/archive"
30+
31+
# Set up the template to run from `TEST_GO_ROOTDIR`. Add a dummy script to
32+
# ensure it doesn't return nonzero due to no scripts being present. This will
33+
# also create `TEST_GO_ROOTDIR` and `TEST_GO_ROOTDIR/scripts`.
34+
@go.create_test_command_script 'foo' 'printf "%s\n" "Hello, World!"'
35+
cp "$_GO_CORE_DIR/go-template" "$TEST_GO_ROOTDIR"
3136
}
3237

3338
teardown() {
34-
rm -rf "$_GO_ROOTDIR/$GO_SCRIPTS_DIR"
39+
@go.remove_test_go_rootdir
3540
}
3641

3742
assert_go_core_unpacked() {
3843
set "$DISABLE_BATS_SHELL_OPTIONS"
39-
local go_core="$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash/go-core.bash"
44+
local go_core="$TEST_GO_SCRIPTS_DIR/go-script-bash/go-core.bash"
4045
local result='0'
4146

4247
if [[ ! -f "$go_core" ]]; then
@@ -47,31 +52,29 @@ assert_go_core_unpacked() {
4752
}
4853

4954
@test "$SUITE: successfully run 'help' from its own directory" {
50-
GO_SCRIPT_BASH_CORE_DIR="$_GO_CORE_DIR" GO_SCRIPTS_DIR='scripts' \
51-
run "$_GO_CORE_DIR/go-template" 'help'
55+
# Use `_GO_CORE_DIR` to avoid the download attempt in this test.
56+
GO_SCRIPT_BASH_CORE_DIR="$_GO_CORE_DIR" \
57+
run "$TEST_GO_ROOTDIR/go-template" 'help'
5258
assert_success
53-
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
59+
assert_output_matches "Usage: $TEST_GO_ROOTDIR/go-template <command>"
5460
}
5561

5662
@test "$SUITE: download the go-script-bash release from $GO_SCRIPT_BASH_REPO_URL" {
57-
run "$_GO_CORE_DIR/go-template"
63+
run "$TEST_GO_ROOTDIR/go-template"
5864

5965
# Without a command argument, the script will print the top-level help and
6066
# return an error, but the core repo should exist as expected.
6167
assert_failure
6268
assert_output_matches "Downloading framework from '${GO_SCRIPT_BASH_REPO_URL%.git}.*.tar.gz'\.\.\."
63-
64-
# Use `.*/scripts/go-script-bash` to account for the fact that `git clone` on
65-
# MSYS2 will output `C:/Users/<user>/AppData/Local/Temp/` in place of `/tmp`.
6669
assert_output_matches "Download of '${GO_SCRIPT_BASH_REPO_URL%.git}.*.tar.gz' successful."
67-
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
70+
assert_output_matches "Usage: $TEST_GO_ROOTDIR/go-template <command>"
6871
assert_go_core_unpacked
6972
}
7073

7174
@test "$SUITE: fail to download a nonexistent repo" {
7275
GO_SCRIPT_BASH_REPO_URL='bogus-repo-that-does-not-exist' \
7376
GO_SCRIPT_BASH_DOWNLOAD_URL='bogus-url-that-does-not-exist' \
74-
run "$_GO_CORE_DIR/go-template"
77+
run "$TEST_GO_ROOTDIR/go-template"
7578
assert_failure "Downloading framework from 'bogus-url-that-does-not-exist/$GO_SCRIPT_BASH_VERSION.tar.gz'..." \
7679
"curl: (6) Could not resolve host: bogus-url-that-does-not-exist" \
7780
"Failed to download from 'bogus-url-that-does-not-exist/$GO_SCRIPT_BASH_VERSION.tar.gz'." \
@@ -83,13 +86,13 @@ assert_go_core_unpacked() {
8386

8487
@test "$SUITE: fail to download a nonexistent version" {
8588
GO_SCRIPT_BASH_VERSION='vnonexistent' \
86-
run "$_GO_CORE_DIR/go-template"
89+
run "$TEST_GO_ROOTDIR/go-template"
8790
assert_failure "Downloading framework from 'https://github.com/mbland/go-script-bash/archive/vnonexistent.tar.gz'..." \
8891
"curl: (22) The requested URL returned error: 404 Not Found" \
8992
"Failed to download from 'https://github.com/mbland/go-script-bash/archive/vnonexistent.tar.gz'." \
9093
"Using git clone as fallback" \
9194
"Cloning framework from 'https://github.com/mbland/go-script-bash.git'..." \
92-
"Cloning into '$PWD/$GO_SCRIPTS_DIR/go-script-bash'..." \
95+
"Cloning into '$TEST_GO_SCRIPTS_DIR/go-script-bash'..." \
9396
"warning: Could not find remote branch vnonexistent to clone." \
9497
"fatal: Remote branch vnonexistent not found in upstream origin" \
9598
"Failed to clone 'https://github.com/mbland/go-script-bash.git'; aborting."
@@ -98,23 +101,20 @@ assert_go_core_unpacked() {
98101
@test "$SUITE: fail to find curl uses git clone" {
99102
PATH="$BATS_TEST_BINDIR:$PATH"
100103
stub_program_in_path curl "exit 1"
101-
run "$_GO_CORE_DIR/go-template"
104+
run "$TEST_GO_ROOTDIR/go-template"
102105
restore_program_in_path curl
103106

104107
# Without a command argument, the script will print the top-level help and
105108
# return an error, but the core repo should exist as expected.
106109
assert_output_matches "Failed to find cURL or tar"
107110
assert_output_matches "Using git clone as fallback"
108-
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'\.\.\."
109-
110-
# Use `.*/scripts/go-script-bash` to account for the fact that `git clone` on
111-
# MSYS2 will output `C:/Users/<user>/AppData/Local/Temp/` in place of `/tmp`.
112-
assert_output_matches "Cloning into '.*/$GO_SCRIPTS_DIR/go-script-bash'\.\.\."
111+
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'"
112+
assert_output_matches "Cloning into '$TEST_GO_SCRIPTS_DIR/go-script-bash'"
113113
assert_output_matches "Clone of '$GO_SCRIPT_BASH_REPO_URL' successful\."$'\n\n'
114-
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
114+
assert_output_matches "Usage: $TEST_GO_ROOTDIR/go-template <command>"
115115
assert_go_core_unpacked
116116

117-
cd "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash"
117+
cd "$TEST_GO_SCRIPTS_DIR/go-script-bash"
118118
run git log --oneline -n 1
119119
assert_success
120120
assert_output_matches "go-script-bash $_GO_CORE_VERSION"
@@ -123,23 +123,20 @@ assert_go_core_unpacked() {
123123
@test "$SUITE: fail to find tar uses git clone" {
124124
PATH="$BATS_TEST_BINDIR:$PATH"
125125
stub_program_in_path tar "exit 1"
126-
run "$_GO_CORE_DIR/go-template"
126+
run "$TEST_GO_ROOTDIR/go-template"
127127
restore_program_in_path tar
128128

129129
# Without a command argument, the script will print the top-level help and
130130
# return an error, but the core repo should exist as expected.
131131
assert_output_matches "Failed to find cURL or tar"
132132
assert_output_matches "Using git clone as fallback"
133-
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'\.\.\."
134-
135-
# Use `.*/scripts/go-script-bash` to account for the fact that `git clone` on
136-
# MSYS2 will output `C:/Users/<user>/AppData/Local/Temp/` in place of `/tmp`.
137-
assert_output_matches "Cloning into '.*/$GO_SCRIPTS_DIR/go-script-bash'\.\.\."
133+
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'"
134+
assert_output_matches "Cloning into '$TEST_GO_SCRIPTS_DIR/go-script-bash'"
138135
assert_output_matches "Clone of '$GO_SCRIPT_BASH_REPO_URL' successful\."$'\n\n'
139-
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
136+
assert_output_matches "Usage: $TEST_GO_ROOTDIR/go-template <command>"
140137
assert_go_core_unpacked
141138

142-
cd "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash"
139+
cd "$TEST_GO_SCRIPTS_DIR/go-script-bash"
143140
run git log --oneline -n 1
144141
assert_success
145142
assert_output_matches "go-script-bash $_GO_CORE_VERSION"
@@ -148,24 +145,26 @@ assert_go_core_unpacked() {
148145
@test "$SUITE: fail to create directory uses git clone" {
149146
PATH="$BATS_TEST_BINDIR:$PATH"
150147
stub_program_in_path mkdir "exit 1"
151-
run "$_GO_CORE_DIR/go-template"
148+
run "$TEST_GO_ROOTDIR/go-template"
152149
restore_program_in_path mkdir
153150

154151
# Without a command argument, the script will print the top-level help and
155152
# return an error, but the core repo should exist as expected.
156153
assert_output_matches "Downloading framework from '${GO_SCRIPT_BASH_REPO_URL%.git}.*.tar.gz'\.\.\."
157-
assert_output_matches "Failed to create scripts dir '$GO_SCRIPTS_DIR'"
158-
assert_output_matches "Using git clone as fallback"
159-
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'\.\.\."
160154

161-
# Use `.*/scripts/go-script-bash` to account for the fact that `git clone` on
162-
# MSYS2 will output `C:/Users/<user>/AppData/Local/Temp/` in place of `/tmp`.
163-
assert_output_matches "Cloning into '.*/$GO_SCRIPTS_DIR/go-script-bash'\.\.\."
155+
# Note that the go-template defines `GO_SCRIPTS_DIR`, but the framework's own
156+
# `go` script doesn't. Hence, we use `TEST_GO_SCRIPTS_RELATIVE_DIR` below,
157+
# which should always match the default `GO_SCRIPTS_DIR` in the template.
158+
assert_output_matches \
159+
"Failed to create scripts dir '$TEST_GO_SCRIPTS_RELATIVE_DIR'"
160+
assert_output_matches "Using git clone as fallback"
161+
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'"
162+
assert_output_matches "Cloning into '$TEST_GO_SCRIPTS_DIR/go-script-bash'."
164163
assert_output_matches "Clone of '$GO_SCRIPT_BASH_REPO_URL' successful\."$'\n\n'
165-
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
164+
assert_output_matches "Usage: $TEST_GO_ROOTDIR/go-template <command>"
166165
assert_go_core_unpacked
167166

168-
cd "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash"
167+
cd "$TEST_GO_SCRIPTS_DIR/go-script-bash"
169168
run git log --oneline -n 1
170169
assert_success
171170
assert_output_matches "go-script-bash $_GO_CORE_VERSION"
@@ -174,24 +173,21 @@ assert_go_core_unpacked() {
174173
@test "$SUITE: fail to move extracted directory uses git clone" {
175174
PATH="$BATS_TEST_BINDIR:$PATH"
176175
stub_program_in_path mv "exit 1"
177-
run "$_GO_CORE_DIR/go-template"
176+
run "$TEST_GO_ROOTDIR/go-template"
178177
restore_program_in_path mv
179178

180179
# Without a command argument, the script will print the top-level help and
181180
# return an error, but the core repo should exist as expected.
182181
assert_output_matches "Downloading framework from '${GO_SCRIPT_BASH_REPO_URL%.git}.*.tar.gz'\.\.\."
183-
assert_output_matches "Failed to install downloaded directory in '.*/$GO_SCRIPTS_DIR/go-script-bash'"
182+
assert_output_matches "Failed to install downloaded directory in '$TEST_GO_SCRIPTS_DIR/go-script-bash'"
184183
assert_output_matches "Using git clone as fallback"
185-
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'\.\.\."
186-
187-
# Use `.*/scripts/go-script-bash` to account for the fact that `git clone` on
188-
# MSYS2 will output `C:/Users/<user>/AppData/Local/Temp/` in place of `/tmp`.
189-
assert_output_matches "Cloning into '.*/$GO_SCRIPTS_DIR/go-script-bash'\.\.\."
184+
assert_output_matches "Cloning framework from '$GO_SCRIPT_BASH_REPO_URL'"
185+
assert_output_matches "Cloning into '$TEST_GO_SCRIPTS_DIR/go-script-bash'"
190186
assert_output_matches "Clone of '$GO_SCRIPT_BASH_REPO_URL' successful\."$'\n\n'
191-
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
187+
assert_output_matches "Usage: $TEST_GO_ROOTDIR/go-template <command>"
192188
assert_go_core_unpacked
193189

194-
cd "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash"
190+
cd "$TEST_GO_SCRIPTS_DIR/go-script-bash"
195191
run git log --oneline -n 1
196192
assert_success
197193
assert_output_matches "go-script-bash $_GO_CORE_VERSION"

0 commit comments

Comments
 (0)