Skip to content

Commit c92d0e3

Browse files
authored
Merge pull request #115 from mbland/tweaks
`@go.restore_stubbed_core_modules`, `lib/kcov-ubuntu`, and test tweaks
2 parents 2c4f9c0 + 00925fe commit c92d0e3

File tree

10 files changed

+55
-48
lines changed

10 files changed

+55
-48
lines changed

lib/kcov-ubuntu

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Builds kcov from scratch on Ubuntu to support Bash test coverage
44
#
55
# Exports:
6-
#
76
# run_kcov
87
# Downloads and compiles kcov if necessary, then runs tests under kcov
98
#
@@ -20,6 +19,7 @@ readonly __KCOV_DEV_PACKAGES=(
2019
'zlib1g-dev'
2120
)
2221
readonly __KCOV_URL='https://github.com/SimonKagstrom/kcov'
22+
readonly __KCOV_VERSION='master'
2323

2424
# Downloads and compiles kcov if necessary, then runs tests under kcov
2525
#
@@ -103,8 +103,7 @@ __clone_and_build_kcov() {
103103
if [[ ! -d "$kcov_dir" ]]; then
104104
@go.printf 'Cloning kcov repository from %s...\n' "$__KCOV_URL"
105105

106-
if ! git clone "$__KCOV_URL" "$kcov_dir"; then
107-
@go.printf "Failed to clone $__KCOV_URL into $kcov_dir." >&2
106+
if ! @go get git-repo "$__KCOV_URL" "$__KCOV_VERSION" "$kcov_dir"; then
108107
return 1
109108
fi
110109
fi

lib/testing/stubbing

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
# YOU MUST CALL THIS FROM TEARDOWN IF YOU USE `@go.create_core_module_stub`!
3434
@go.restore_stubbed_core_modules() {
3535
local module
36+
local stubbed_modules=("$_GO_CORE_DIR/lib"/*.stubbed)
3637

37-
for module in "$_GO_CORE_DIR/lib"/*.stubbed; do
38-
mv "$module" "${module%.stubbed}"
39-
done
38+
if [[ "${stubbed_modules#$_GO_CORE_DIR/lib/}" != '*.stubbed' ]]; then
39+
for module in "${stubbed_modules[@]}"; do
40+
mv "$module" "${module%.stubbed}"
41+
done
42+
fi
4043
}

tests/aliases.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ load environment
9999
# Setting _GO_CMD will trick the script into thinking the shell function is
100100
# running it.
101101

102-
run env _GO_CMD='test-go' ./go aliases --help cd
102+
_GO_CMD='test-go' run ./go aliases --help cd
103103
[ "$status" -eq '0' ]
104104

105105
local expected=("test-go cd - Shell alias that will execute in $_GO_ROOTDIR")
@@ -109,7 +109,7 @@ load environment
109109
[ "${lines[1]}" == "${expected[1]}" ]
110110
[ -z "${lines[2]}" ]
111111

112-
run env _GO_CMD='test-go' ./go aliases --help pushd
112+
_GO_CMD='test-go' run ./go aliases --help pushd
113113
[ "$status" -eq '0' ]
114114

115115
expected[0]="${expected[0]/test-go cd/test-go pushd}"

tests/core.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ load environment
2121
}
2222

2323
@test "$SUITE: invoke editor on edit command" {
24-
run env EDITOR=echo ./go edit 'editor invoked'
24+
EDITOR=echo run ./go edit 'editor invoked'
2525
assert_success 'editor invoked'
2626
}
2727

tests/core/columns.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ teardown() {
2828
}
2929

3030
@test "$SUITE: set COLUMNS if unset" {
31-
run env COLUMNS= "$TEST_GO_SCRIPT"
31+
COLUMNS= run "$TEST_GO_SCRIPT"
3232
assert_success
3333
[ -n "$output" ]
3434
}
3535

3636
@test "$SUITE: honor COLUMNS if already set" {
37-
run env COLUMNS="19700918" "$TEST_GO_SCRIPT"
37+
COLUMNS="19700918" run "$TEST_GO_SCRIPT"
3838
assert_success '19700918'
3939
}
4040

4141
@test "$SUITE: default COLUMNS to 80 if actual columns can't be determined" {
42-
run env COLUMNS= PATH= "$BASH" "$TEST_GO_SCRIPT"
42+
COLUMNS= PATH= run "$BASH" "$TEST_GO_SCRIPT"
4343
assert_success '80'
4444
}
4545

@@ -60,7 +60,7 @@ teardown() {
6060
local expected_cols="$(env COLUMNS= tput cols)"
6161
exec 1>&27 27>&- 2>&1
6262

63-
run env COLUMNS= "$TEST_GO_SCRIPT"
63+
COLUMNS= run "$TEST_GO_SCRIPT"
6464
assert_success "$expected_cols"
6565
}
6666

@@ -73,6 +73,6 @@ teardown() {
7373

7474
# One way to cause tput to fail is to set `$TERM` to null. On Travis it's set
7575
# to 'dumb', but tput fails anyway. The code now defaults to 80 on all errors.
76-
run env COLUMNS= TERM= "$TEST_GO_SCRIPT"
76+
COLUMNS= TERM= run "$TEST_GO_SCRIPT"
7777
assert_success "$expected_cols"
7878
}

tests/edit.bats

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
load environment
44

55
@test "$SUITE: open file with EDITOR" {
6-
run env EDITOR='echo' ./go edit foo/bar/baz
6+
EDITOR='echo' run ./go edit foo/bar/baz
77
assert_success 'foo/bar/baz'
88
}
99

1010
@test "$SUITE: error if EDITOR not defined" {
11-
run env EDITOR= ./go edit foo/bar/baz
11+
EDITOR= run ./go edit foo/bar/baz
1212
assert_failure 'Cannot edit foo/bar/baz: $EDITOR not defined.'
1313
}

tests/env.bats

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ teardown() {
2626

2727
@test "$SUITE: error if no implementation available for SHELL" {
2828
local shell='nonexistent-sh'
29-
run env SHELL="$shell" "$TEST_GO_SCRIPT" env
29+
SHELL="$shell" run "$TEST_GO_SCRIPT" env
3030

3131
assert_failure
3232
assert_line_equals 0 "The $shell shell currently isn't supported."
@@ -37,7 +37,7 @@ teardown() {
3737
local go_script="$TEST_GO_ROOTDIR/go script"
3838
mv "$TEST_GO_SCRIPT" "$go_script"
3939

40-
run env SHELL='bash' "$go_script" env
40+
SHELL='bash' run "$go_script" env
4141
assert_failure
4242

4343
local expected="ERROR: the \"${go_script#$TEST_GO_ROOTDIR/}\" script "
@@ -49,7 +49,7 @@ teardown() {
4949
local go_script="$TEST_GO_ROOTDIR/my-go"
5050
mv "$TEST_GO_SCRIPT" "$go_script"
5151

52-
run env SHELL='bash' "$go_script" env
52+
SHELL='bash' run "$go_script" env
5353
assert_success
5454
assert_line_matches 0 "Define the \"${go_script#$TEST_GO_ROOTDIR/}\" function"
5555
assert_line_equals 2 "eval \"\$($go_script env -)\""
@@ -58,7 +58,7 @@ teardown() {
5858
@test "$SUITE: error if shell impl doesn't contain eval line" {
5959
echo '' > "$_GO_ROOTDIR/lib/internal/env/badsh"
6060

61-
run env SHELL='badsh' "$TEST_GO_SCRIPT" env
61+
SHELL='badsh' run "$TEST_GO_SCRIPT" env
6262
rm "$_GO_ROOTDIR/lib/internal/env/badsh"
6363

6464
assert_failure
@@ -68,7 +68,7 @@ teardown() {
6868
}
6969

7070
@test "$SUITE: error if function name contains spaces" {
71-
run env SHELL='bash' "$TEST_GO_SCRIPT" env 'foo bar'
71+
SHELL='bash' run "$TEST_GO_SCRIPT" env 'foo bar'
7272
assert_failure
7373
assert_output_matches 'ERROR: "foo bar" must not contain spaces'
7474
}
@@ -78,7 +78,7 @@ teardown() {
7878
local go_script="$TEST_GO_ROOTDIR/$script_name"
7979
mv "$TEST_GO_SCRIPT" "$go_script"
8080

81-
run env SHELL='bash' "$go_script" env -
81+
SHELL='bash' run "$go_script" env -
8282
assert_success
8383

8484
! command -v "_$script_name"
@@ -110,7 +110,7 @@ teardown() {
110110

111111
@test "$SUITE: generate functions using specified name" {
112112
local func_name='never-collide-with-test-environment-go'
113-
run env SHELL='bash' "$TEST_GO_SCRIPT" env "$func_name"
113+
SHELL='bash' run "$TEST_GO_SCRIPT" env "$func_name"
114114
assert_success
115115

116116
! command -v "_$func_name"

tests/kcov.bats

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,20 @@ write_kcov_dummy() {
8282
@test "$SUITE: clone and build" {
8383
write_kcov_go_script '__check_kcov_dev_packages_installed() { return 1; }' \
8484
'__clone_and_build_kcov tests/kcov'
85-
echo 'mkdir -p "$3"' >> "$BATS_TEST_BINDIR/git"
8685

87-
run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT"
86+
# Use `mkdir` to create the target directory of `git clone`.
87+
echo 'mkdir -p "${@:$(($# - 1))}"' >> "$BATS_TEST_BINDIR/git"
88+
89+
TRAVIS_OS_NAME= run "$TEST_GO_SCRIPT"
8890
. 'lib/kcov-ubuntu'
89-
local expected_output=(
90-
"Cloning kcov repository from $__KCOV_URL..."
91-
'Installing dev packages to build kcov...'
92-
'Building kcov...')
93-
local IFS=$'\n'
94-
assert_success "${expected_output[*]}"
91+
assert_success
92+
assert_lines_match "Cloning kcov repository from $__KCOV_URL..." \
93+
"Successfully cloned \"$__KCOV_URL\" reference \"$__KCOV_VERSION\" into " \
94+
'Installing dev packages to build kcov...' \
95+
'Building kcov...'
9596

96-
assert_file_equals "$BATS_TEST_BINDIR/git.out" "clone $__KCOV_URL tests/kcov"
97+
assert_file_matches "$BATS_TEST_BINDIR/git.out" \
98+
"clone .* -b $__KCOV_VERSION $__KCOV_URL tests/kcov"
9799

98100
IFS=' '
99101
assert_file_equals "$BATS_TEST_BINDIR/sudo.out" \
@@ -106,13 +108,11 @@ write_kcov_dummy() {
106108
write_kcov_go_script '__clone_and_build_kcov tests/kcov'
107109
echo 'exit 1' >> "$BATS_TEST_BINDIR/git"
108110

109-
run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT"
111+
TRAVIS_OS_NAME= run "$TEST_GO_SCRIPT"
110112
. 'lib/kcov-ubuntu'
111-
local expected_output=(
112-
"Cloning kcov repository from $__KCOV_URL..."
113-
"Failed to clone $__KCOV_URL into tests/kcov.")
114-
local IFS=$'\n'
115-
assert_failure "${expected_output[*]}"
113+
assert_failure
114+
assert_lines_match "Cloning kcov repository from $__KCOV_URL\.\.\." \
115+
"Failed to clone \"$__KCOV_URL\" .* into \"tests/kcov\"\."
116116
}
117117

118118
@test "$SUITE: clone and build fails if install fails" {
@@ -121,7 +121,7 @@ write_kcov_dummy() {
121121
echo 'exit 1' >> "$BATS_TEST_BINDIR/sudo"
122122
mkdir -p "$TEST_GO_ROOTDIR/tests/kcov"
123123

124-
run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT"
124+
TRAVIS_OS_NAME= run "$TEST_GO_SCRIPT"
125125
. 'lib/kcov-ubuntu'
126126
local expected_output=(
127127
'Installing dev packages to build kcov...'
@@ -135,7 +135,7 @@ write_kcov_dummy() {
135135
mkdir -p "$TEST_GO_ROOTDIR/tests/kcov"
136136
echo 'exit 1' >> "$BATS_TEST_BINDIR/cmake"
137137

138-
run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT"
138+
TRAVIS_OS_NAME= run "$TEST_GO_SCRIPT"
139139
. 'lib/kcov-ubuntu'
140140
local expected_output=(
141141
'Building kcov...'
@@ -149,7 +149,7 @@ write_kcov_dummy() {
149149
mkdir -p "$TEST_GO_ROOTDIR/tests/kcov"
150150
echo 'exit 1' >> "$BATS_TEST_BINDIR/make"
151151

152-
run env TRAVIS_OS_NAME= "$TEST_GO_SCRIPT"
152+
TRAVIS_OS_NAME= run "$TEST_GO_SCRIPT"
153153
. 'lib/kcov-ubuntu'
154154
local expected_output=(
155155
'Building kcov...'
@@ -163,7 +163,7 @@ write_kcov_dummy() {
163163
'__clone_and_build_kcov tests/kcov'
164164

165165
mkdir -p "$TEST_GO_ROOTDIR/tests/kcov"
166-
run env TRAVIS_OS_NAME='linux' "$TEST_GO_SCRIPT"
166+
TRAVIS_OS_NAME='linux' run "$TEST_GO_SCRIPT"
167167
assert_success 'Building kcov...'
168168
}
169169

@@ -206,7 +206,7 @@ write_kcov_dummy() {
206206
'Coverage results located in:'
207207
" $TEST_GO_ROOTDIR/$KCOV_COVERAGE_DIR")
208208

209-
run env TRAVIS_JOB_ID= "$TEST_GO_SCRIPT"
209+
TRAVIS_JOB_ID= run "$TEST_GO_SCRIPT"
210210
local IFS=$'\n'
211211
assert_success "${expected_output[*]}"
212212
}
@@ -228,7 +228,7 @@ write_kcov_dummy() {
228228
'Coverage results located in:'
229229
" $TEST_GO_ROOTDIR/$KCOV_COVERAGE_DIR")
230230

231-
run env TRAVIS_JOB_ID= "$TEST_GO_SCRIPT"
231+
TRAVIS_JOB_ID= run "$TEST_GO_SCRIPT"
232232
local IFS=$'\n'
233233
assert_success "${expected_output[*]}"
234234
}
@@ -245,7 +245,7 @@ write_kcov_dummy() {
245245
" ${kcov_argv[*]}"
246246
'kcov exited with errors.')
247247

248-
run env TRAVIS_JOB_ID= "$TEST_GO_SCRIPT"
248+
TRAVIS_JOB_ID= run "$TEST_GO_SCRIPT"
249249
local IFS=$'\n'
250250
assert_failure "${expected_output[*]}"
251251
}
@@ -264,7 +264,7 @@ write_kcov_dummy() {
264264
'Coverage results sent to:'
265265
" $KCOV_COVERALLS_URL")
266266

267-
run env TRAVIS_JOB_ID=666 "$TEST_GO_SCRIPT"
267+
TRAVIS_JOB_ID=666 run "$TEST_GO_SCRIPT"
268268
local IFS=$'\n'
269269
assert_success "${expected_output[*]}"
270270
}
@@ -288,7 +288,7 @@ write_kcov_dummy() {
288288
'Coverage results located in:'
289289
" $TEST_GO_ROOTDIR/$KCOV_COVERAGE_DIR")
290290

291-
run env TRAVIS_JOB_ID=666 "$TEST_GO_SCRIPT"
291+
TRAVIS_JOB_ID=666 run "$TEST_GO_SCRIPT"
292292
local IFS=$'\n'
293293
assert_success "${expected_output[*]}"
294294
}

tests/test.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ _trim_expected() {
7070
}
7171

7272
@test "$SUITE: open EDITOR on --edit" {
73-
run env EDITOR='echo' ./go test --edit test aliases 'builtins*'
73+
EDITOR='echo' run ./go test --edit test aliases 'builtins*'
7474
local expected=(
7575
'tests/test.bats' 'tests/aliases.bats' 'tests/builtins.bats'
7676
tests/builtins/*)

tests/testing/stubbing.bats

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ teardown() {
2727
assert_success 'Hello, World!'
2828
}
2929

30+
@test "$SUITE: restore_stubbed_core_modules does nothing if no stubs exist" {
31+
run @go.restore_stubbed_core_modules
32+
assert_success ''
33+
}
34+
3035
@test "$SUITE: create_core_module_stub aborts if module unknown" {
3136
[ ! -e "$_GO_CORE_DIR/lib/foobar" ]
3237
run @go.create_core_module_stub 'foobar' 'echo Hello, World!'

0 commit comments

Comments
 (0)