Skip to content

Commit 8b82085

Browse files
authored
Merge pull request #158 from mbland/template-coverage
go-template: Fix test coverage, improve comments
2 parents 3c1aa6e + 7fd3d53 commit 8b82085

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

go-template

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,44 @@
1616
# your project or check in a versioned copy of the sources. See the "How to use
1717
# this framework" section of `README.md` for details.
1818
#
19-
# Make sure the `_GO_STANDALONE`, `SCRIPTS_DIR`, and `GO_SCRIPT_BASH_VERSION`
20-
# variables within this script are configured as necessary for your program.
21-
# You can also add any other initialization or configuration logic between:
19+
# Make sure the variables within this script are configured as necessary for
20+
# your program. You can add any other initialization or configuration between:
2221
#
23-
# . "$_GO_SCRIPT_BASH_CORE" "${SCRIPTS_DIR##*/}"`
22+
# . "$_GO_SCRIPT_BASH_CORE_DIR/go-core.bash" "$GO_SCRIPTS_DIR"`
2423
# `@go "$@"`
2524

26-
# Set `_GO_STANDALONE='true'` if your program isn't a project-specific script.
27-
readonly _GO_STANDALONE=
25+
# Set to 'true' if your script is a standalone program, i.e. not bound to
26+
# execute only from the directory in which it resides. See the "Standalone mode"
27+
# section in README.md.
28+
export _GO_STANDALONE=
2829

29-
# Set SCRIPTS_DIR to the path where your command scripts reside.
30+
# The path where your command scripts reside
3031
#
3132
# For `_GO_STANDALONE` programs and plugins containing command scripts, you may
32-
# wish to set SCRIPTS_DIR to end with `bin` and have a separate `./go` script to
33+
# wish to set GO_SCRIPTS_DIR to `bin` and have a separate `./go` script to
3334
# manage project tasks that finds its command scripts in `scripts`.
34-
readonly SCRIPTS_DIR="${0%/*}/scripts"
35+
declare GO_SCRIPTS_DIR="${GO_SCRIPTS_DIR:-scripts}"
3536

36-
# Set this to the `GO_SCRIPT_BASH_REPO_URL` tag or branch you wish to use.
37-
readonly GO_SCRIPT_BASH_VERSION='v1.3.0'
37+
# The `GO_SCRIPT_BASH_REPO_URL` tag or branch you wish to use
38+
declare GO_SCRIPT_BASH_VERSION="${GO_SCRIPT_BASH_VERSION:-v1.3.0}"
3839

39-
readonly GO_SCRIPT_BASH_CORE="$SCRIPTS_DIR/go-script-bash/go-core.bash"
40-
readonly GO_SCRIPT_BASH_REPO_URL='https://github.com/mbland/go-script-bash.git'
40+
# The go-script-bash installation directory within your project
41+
declare GO_SCRIPT_BASH_CORE_DIR="${GO_SCRIPT_BASH_CORE_DIR:-${0%/*}/$GO_SCRIPTS_DIR/go-script-bash}"
4142

42-
if [[ ! -e "$GO_SCRIPT_BASH_CORE" ]]; then
43+
# The URL of the go-script-bash framework sources
44+
declare GO_SCRIPT_BASH_REPO_URL="${GO_SCRIPT_BASH_REPO_URL:-https://github.com/mbland/go-script-bash.git}"
45+
46+
if [[ ! -e "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" ]]; then
4347
printf "Cloning framework from '%s'...\n" "$GO_SCRIPT_BASH_REPO_URL"
4448
if ! git clone --depth 1 -c advice.detachedHead=false \
4549
-b "$GO_SCRIPT_BASH_VERSION" "$GO_SCRIPT_BASH_REPO_URL" \
46-
"${GO_SCRIPT_BASH_CORE%/*}"; then
50+
"$GO_SCRIPT_BASH_CORE_DIR"; then
4751
printf "Failed to clone '%s'; aborting.\n" "$GO_SCRIPT_BASH_REPO_URL" >&2
4852
exit 1
4953
fi
5054
printf "Clone of '%s' successful.\n\n" "$GO_SCRIPT_BASH_REPO_URL"
5155
fi
5256

53-
. "$GO_SCRIPT_BASH_CORE" "${SCRIPTS_DIR##*/}"
57+
. "$GO_SCRIPT_BASH_CORE_DIR/go-core.bash" "$GO_SCRIPTS_DIR"
5458
# Add any other configuration or initialization steps here.
5559
@go "$@"

tests/template.bats

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,27 @@ GO_CORE_URL="${GO_CORE_URL:-$_GO_CORE_DIR}"
2424

2525
setup() {
2626
test_filter
27-
mkdir "$TEST_GO_ROOTDIR"
28-
29-
if [[ -e "$GO_CORE_URL/.git/shallow" ]]; then
30-
skip "Can't clone shallow repositories"
31-
fi
27+
export GO_SCRIPT_BASH_VERSION="$_GO_CORE_VERSION"
28+
export GO_SCRIPTS_DIR="$_GO_TEST_DIR/tmp/go-template-test-scripts"
29+
export GO_SCRIPT_BASH_REPO_URL="$GO_CORE_URL"
3230
}
3331

3432
teardown() {
35-
@go.remove_test_go_rootdir
33+
rm -rf "$_GO_ROOTDIR/$GO_SCRIPTS_DIR"
3634
}
3735

38-
create_template_script() {
39-
local repo_url="$1"
40-
local version="$2"
41-
local template="$(< "$_GO_CORE_DIR/go-template")"
42-
local replacement
43-
44-
if [[ "$template" =~ GO_SCRIPT_BASH_REPO_URL=[^$'\n']+ ]]; then
45-
replacement="GO_SCRIPT_BASH_REPO_URL='$repo_url'"
46-
template="${template/${BASH_REMATCH[0]}/$replacement}"
47-
fi
48-
if [[ "$template" =~ GO_SCRIPT_BASH_VERSION=[^$'\n']+ ]]; then
49-
replacement="GO_SCRIPT_BASH_VERSION='$version'"
50-
template="${template/${BASH_REMATCH[0]}/$replacement}"
51-
fi
52-
printf '%s\n' "$template" > "$TEST_GO_ROOTDIR/go-template"
53-
chmod 700 "$TEST_GO_ROOTDIR/go-template"
36+
@test "$SUITE: successfully run 'help' from its own directory" {
37+
GO_SCRIPT_BASH_CORE_DIR="$_GO_CORE_DIR" GO_SCRIPTS_DIR='scripts' \
38+
run "$_GO_CORE_DIR/go-template" 'help'
39+
assert_success
40+
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
5441
}
5542

5643
@test "$SUITE: clone the go-script-bash repository from $GO_CORE_URL" {
57-
create_template_script "$GO_CORE_URL" "$_GO_CORE_VERSION"
58-
run "$TEST_GO_ROOTDIR/go-template"
44+
if [[ -e "$GO_CORE_URL/.git/shallow" ]]; then
45+
skip "Can't clone shallow repositories"
46+
fi
47+
run "$_GO_CORE_DIR/go-template"
5948

6049
# Without a command argument, the script will print the top-level help and
6150
# return an error, but the core repo should exist as expected.
@@ -64,20 +53,20 @@ create_template_script() {
6453

6554
# Use `.*/scripts/go-script-bash` to account for the fact that `git clone` on
6655
# MSYS2 will output `C:/Users/<user>/AppData/Local/Temp/` in place of `/tmp`.
67-
assert_output_matches "Cloning into '.*/scripts/go-script-bash'\.\.\."
56+
assert_output_matches "Cloning into '.*/$GO_SCRIPTS_DIR/go-script-bash'\.\.\."
6857
assert_output_matches "Clone of '$GO_CORE_URL' successful\."$'\n\n'
69-
assert_output_matches "Usage: $TEST_GO_ROOTDIR/go-template <command>"
70-
[[ -f "$TEST_GO_ROOTDIR/scripts/go-script-bash/go-core.bash" ]]
58+
assert_output_matches "Usage: $_GO_CORE_DIR/go-template <command>"
59+
[[ -f "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash/go-core.bash" ]]
7160

72-
cd "$TEST_GO_ROOTDIR/scripts/go-script-bash"
61+
cd "$_GO_ROOTDIR/$GO_SCRIPTS_DIR/go-script-bash"
7362
run git log --oneline -n 1
7463
assert_success
7564
assert_output_matches "go-script-bash $_GO_CORE_VERSION"
7665
}
7766

7867
@test "$SUITE: fail to clone a nonexistent repo" {
79-
create_template_script 'bogus-repo-that-does-not-exist'
80-
run "$TEST_GO_ROOTDIR/go-template"
68+
GO_SCRIPT_BASH_REPO_URL='bogus-repo-that-does-not-exist' \
69+
run "$_GO_CORE_DIR/go-template"
8170
assert_failure "Cloning framework from 'bogus-repo-that-does-not-exist'..." \
8271
"fatal: repository 'bogus-repo-that-does-not-exist' does not exist" \
8372
"Failed to clone 'bogus-repo-that-does-not-exist'; aborting."

0 commit comments

Comments
 (0)