Skip to content

Commit c72222e

Browse files
committed
kcov: Add final test cases
The `kcov` lib is now completely covered. In the future, I'll try to extract it into a plugin and support non-Ubuntu/Debian platforms.
1 parent e00b0d9 commit c72222e

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

scripts/lib/kcov

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ run_kcov() {
5454
@go.printf 'Coverage results sent to:\n %s\n' "$coveralls_url"
5555
fi
5656
else
57-
@go.printf 'kcov exited with errors\n'
57+
@go.printf 'kcov exited with errors.\n'
5858
return 1
5959
fi
6060
}

tests/kcov.bats

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@ load assertions
55
load script_helper
66

77
FAKE_BIN_DIR="$TEST_GO_ROOTDIR/fake-bin"
8+
KCOV_DIR='tests/kcov'
9+
KCOV_COVERAGE_DIR='tests/coverage'
10+
KCOV_INCLUDE_PATTERNS='include/,pattern'
11+
KCOV_EXCLUDE_PATTERNS='exclude/,pattern'
12+
KCOV_COVERALLS_URL='https://coveralls.io/github/mbland/go-script-bash'
13+
RUN_KCOV_ARGV=(
14+
"$KCOV_DIR"
15+
"$KCOV_COVERAGE_DIR"
16+
"$KCOV_INCLUDE_PATTERNS"
17+
"$KCOV_EXCLUDE_PATTERNS"
18+
"$KCOV_COVERALLS_URL")
19+
KCOV_PATH="$KCOV_DIR/src/kcov"
20+
KCOV_ARGV_START=(
21+
"$KCOV_PATH"
22+
"--include-pattern=$KCOV_INCLUDE_PATTERNS"
23+
"--exclude-pattern=$KCOV_EXCLUDE_PATTERNS"
24+
)
825

926
setup() {
1027
local fake_binaries=(
@@ -37,6 +54,13 @@ write_kcov_go_script() {
3754
create_test_go_script "${go_script[@]}"
3855
}
3956

57+
write_kcov_dummy() {
58+
local kcov_dummy="$TEST_GO_ROOTDIR/$KCOV_PATH"
59+
mkdir -p "${kcov_dummy%/*}"
60+
printf "#! /usr/bin/env bash\n%s\n" "$*" >"$kcov_dummy"
61+
chmod 700 "$kcov_dummy"
62+
}
63+
4064
@test "$SUITE: check dev packages installed" {
4165
write_kcov_go_script check_kcov_dev_packages_installed
4266
run "$TEST_GO_SCRIPT"
@@ -158,3 +182,125 @@ write_kcov_go_script() {
158182
run env TRAVIS_OS_NAME='linux' "$TEST_GO_SCRIPT"
159183
assert_success 'Building kcov...'
160184
}
185+
186+
@test "$SUITE: fail to run kcov on non-Linux/apt-get platforms" {
187+
# Force the script not to find `apt-get` by forcing an empty `PATH`.
188+
write_kcov_go_script "PATH=; run_kcov ${RUN_KCOV_ARGV[*]}"
189+
run "$TEST_GO_SCRIPT"
190+
assert_failure 'Coverage is only available on Linux platforms with apt-get.'
191+
}
192+
193+
@test "$SUITE: fail to run kcov if coverage dir already exists" {
194+
mkdir -p "$TEST_GO_ROOTDIR/$KCOV_COVERAGE_DIR"
195+
write_kcov_go_script "run_kcov ${RUN_KCOV_ARGV[*]}"
196+
197+
run "$TEST_GO_SCRIPT"
198+
local expected=("The $KCOV_COVERAGE_DIR directory already exists."
199+
'Please move or remove this directory first.')
200+
assert_failure "${expected[*]}"
201+
}
202+
203+
@test "$SUITE: fail to run kcov if not present and can't be built" {
204+
write_kcov_go_script 'clone_and_build_kcov() { echo "KCOV: $*"; return 1; }' \
205+
"run_kcov ${RUN_KCOV_ARGV[*]}"
206+
run "$TEST_GO_SCRIPT"
207+
assert_failure "KCOV: $KCOV_DIR"
208+
}
209+
210+
@test "$SUITE: success when kcov already built" {
211+
write_kcov_dummy "IFS=\$'\\n'; echo \"\$*\""
212+
write_kcov_go_script "run_kcov ${RUN_KCOV_ARGV[*]} foo bar/baz"
213+
214+
local kcov_argv=("${KCOV_ARGV_START[@]}" "$KCOV_COVERAGE_DIR"
215+
"$TEST_GO_SCRIPT" 'test' 'foo' 'bar/baz')
216+
local expected_output=(
217+
'Starting coverage run:'
218+
" ${kcov_argv[*]}"
219+
"${kcov_argv[@]:1}"
220+
'Coverage results located in:'
221+
" $TEST_GO_ROOTDIR/$KCOV_COVERAGE_DIR")
222+
local IFS=$'\n'
223+
224+
run env TRAVIS_JOB_ID= "$TEST_GO_SCRIPT"
225+
assert_success "${expected_output[*]}"
226+
}
227+
228+
@test "$SUITE: success after building kcov" {
229+
local go_script=(
230+
'clone_and_build_kcov() {'
231+
" mkdir -p '${KCOV_PATH%/*}'"
232+
" printf '#! /usr/bin/env bash\n' >'$KCOV_PATH'"
233+
" chmod 700 '$KCOV_PATH'"
234+
'}'
235+
"run_kcov ${RUN_KCOV_ARGV[*]} foo bar/baz")
236+
write_kcov_go_script "${go_script[@]}"
237+
238+
local kcov_argv=("${KCOV_ARGV_START[@]}" "$KCOV_COVERAGE_DIR"
239+
"$TEST_GO_SCRIPT" 'test' 'foo' 'bar/baz')
240+
local expected_output=(
241+
'Starting coverage run:'
242+
" ${kcov_argv[*]}"
243+
'Coverage results located in:'
244+
" $TEST_GO_ROOTDIR/$KCOV_COVERAGE_DIR")
245+
local IFS=$'\n'
246+
247+
run env TRAVIS_JOB_ID= "$TEST_GO_SCRIPT"
248+
assert_success "${expected_output[*]}"
249+
}
250+
251+
@test "$SUITE: failure if kcov returns an error status" {
252+
write_kcov_dummy "printf 'Oh noes!\n' >&2; exit 1"
253+
write_kcov_go_script "run_kcov ${RUN_KCOV_ARGV[*]} foo bar/baz"
254+
255+
local kcov_argv=("${KCOV_ARGV_START[@]}" "$KCOV_COVERAGE_DIR"
256+
"$TEST_GO_SCRIPT" 'test' 'foo' 'bar/baz')
257+
local expected_output=(
258+
'Starting coverage run:'
259+
" ${kcov_argv[*]}"
260+
'kcov exited with errors.')
261+
local IFS=$'\n'
262+
263+
run env TRAVIS_JOB_ID= "$TEST_GO_SCRIPT"
264+
assert_failure "${expected_output[*]}"
265+
}
266+
267+
@test "$SUITE: send results to Coveralls when running on Travis" {
268+
write_kcov_dummy
269+
write_kcov_go_script "run_kcov ${RUN_KCOV_ARGV[*]} foo bar/baz"
270+
271+
local kcov_argv=("${KCOV_ARGV_START[@]}"
272+
"--coveralls-id=666" "$KCOV_COVERAGE_DIR"
273+
"$TEST_GO_SCRIPT" 'test' 'foo' 'bar/baz')
274+
local expected_output=(
275+
'Starting coverage run:'
276+
" ${kcov_argv[*]}"
277+
'Coverage results sent to:'
278+
" $KCOV_COVERALLS_URL")
279+
local IFS=$'\n'
280+
281+
run env TRAVIS_JOB_ID=666 "$TEST_GO_SCRIPT"
282+
assert_success "${expected_output[*]}"
283+
}
284+
285+
@test "$SUITE: don't send to Coveralls when URL is missing" {
286+
local run_kcov_argv=(
287+
"$KCOV_DIR"
288+
"$KCOV_COVERAGE_DIR"
289+
"$KCOV_INCLUDE_PATTERNS"
290+
"$KCOV_EXCLUDE_PATTERNS")
291+
write_kcov_dummy
292+
# Note that the coveage_url argument is the empty string.
293+
write_kcov_go_script "run_kcov ${run_kcov_argv[*]} '' foo bar/baz"
294+
295+
local kcov_argv=("${KCOV_ARGV_START[@]}" "$KCOV_COVERAGE_DIR"
296+
"$TEST_GO_SCRIPT" 'test' 'foo' 'bar/baz')
297+
local expected_output=(
298+
'Starting coverage run:'
299+
" ${kcov_argv[*]}"
300+
'Coverage results located in:'
301+
" $TEST_GO_ROOTDIR/$KCOV_COVERAGE_DIR")
302+
local IFS=$'\n'
303+
304+
run env TRAVIS_JOB_ID=666 "$TEST_GO_SCRIPT"
305+
assert_success "${expected_output[*]}"
306+
}

0 commit comments

Comments
 (0)