hub/script/test

66 строки
1.4 KiB
Plaintext
Исходник Обычный вид История

2014-01-08 02:22:14 +04:00
#!/usr/bin/env bash
# Usage: script/test [--coverage [<MIN>]] [<FEATURES>...]
2014-01-08 02:22:14 +04:00
#
# Run Go and Cucumber test suites for hub.
set -e
Measure code coverage between tests Go has code coverage tooling for test mode, which temporarily rewrites the source code to insert annotations which will activate during the test run and track progress of executed code. Then, upon process completion, that information is dumped into a coverage report. We can't use this approach for hub, at least not without substantial changes. First of all, hub's test coverage is mostly "from the outside", utilizing Cucumber to invoke the binary with different arguments and inspect the outputs and result. There are some tests in go, but they are minimal compared to the cukes. Second, hub frequently aborts the process on errors via `os.Exit(1)`, and those scenarios need to be tested too. However, if the process exits prematurely, the code coverage report will never be generated. To work around this, I first used the go tool that annotates the source: go tool cover -mode=set -var=LiveCoverage myfile.go This injects `LiveCoverage.Count[pos] = 1` lines at appropriate places all over the source code, and generates a mapping of line/column positions in the original source. Then I rewrite those lines to become a method invocation: coverage.Record(LiveCoverage, pos) The new `Record` method will immediately append the information to a code coverage report file as soon as it's invoked. This ensures that there is coverage information even if the process gets aborted. This approach works the same for go tests as well as for cukes. They all append to the same file. Finally, the rest of Go tooling is used to generate an HTML report of code coverage: go tool cover -html=cover.out
2016-09-12 22:18:52 +03:00
while [ $# -gt 0 ]; do
case "$1" in
--coverage )
export HUB_COVERAGE="$PWD/tmp/cover.out"
if [ "${2%.*}" -gt 0 ] 2>/dev/null; then
Measure code coverage between tests Go has code coverage tooling for test mode, which temporarily rewrites the source code to insert annotations which will activate during the test run and track progress of executed code. Then, upon process completion, that information is dumped into a coverage report. We can't use this approach for hub, at least not without substantial changes. First of all, hub's test coverage is mostly "from the outside", utilizing Cucumber to invoke the binary with different arguments and inspect the outputs and result. There are some tests in go, but they are minimal compared to the cukes. Second, hub frequently aborts the process on errors via `os.Exit(1)`, and those scenarios need to be tested too. However, if the process exits prematurely, the code coverage report will never be generated. To work around this, I first used the go tool that annotates the source: go tool cover -mode=set -var=LiveCoverage myfile.go This injects `LiveCoverage.Count[pos] = 1` lines at appropriate places all over the source code, and generates a mapping of line/column positions in the original source. Then I rewrite those lines to become a method invocation: coverage.Record(LiveCoverage, pos) The new `Record` method will immediately append the information to a code coverage report file as soon as it's invoked. This ensures that there is coverage information even if the process gets aborted. This approach works the same for go tests as well as for cukes. They all append to the same file. Finally, the rest of Go tooling is used to generate an HTML report of code coverage: go tool cover -html=cover.out
2016-09-12 22:18:52 +03:00
min_coverage="$2"
shift 2
else
min_coverage=1
shift 1
fi
;;
-h | --help )
sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' < "$0"
exit
;;
* )
break
;;
Measure code coverage between tests Go has code coverage tooling for test mode, which temporarily rewrites the source code to insert annotations which will activate during the test run and track progress of executed code. Then, upon process completion, that information is dumped into a coverage report. We can't use this approach for hub, at least not without substantial changes. First of all, hub's test coverage is mostly "from the outside", utilizing Cucumber to invoke the binary with different arguments and inspect the outputs and result. There are some tests in go, but they are minimal compared to the cukes. Second, hub frequently aborts the process on errors via `os.Exit(1)`, and those scenarios need to be tested too. However, if the process exits prematurely, the code coverage report will never be generated. To work around this, I first used the go tool that annotates the source: go tool cover -mode=set -var=LiveCoverage myfile.go This injects `LiveCoverage.Count[pos] = 1` lines at appropriate places all over the source code, and generates a mapping of line/column positions in the original source. Then I rewrite those lines to become a method invocation: coverage.Record(LiveCoverage, pos) The new `Record` method will immediately append the information to a code coverage report file as soon as it's invoked. This ensures that there is coverage information even if the process gets aborted. This approach works the same for go tests as well as for cukes. They all append to the same file. Finally, the rest of Go tooling is used to generate an HTML report of code coverage: go tool cover -html=cover.out
2016-09-12 22:18:52 +03:00
esac
done
STATUS=0
trap "exit 1" INT
check_formatting() {
[[ "$(go version)" != *" go1.8."* ]] || return 0
make fmt >/dev/null
if ! git diff -U1 --exit-code; then
echo
echo "Some go code was not formatted properly." >&2
echo "Run \`make fmt' locally to fix these errors." >&2
return 1
fi
}
2018-06-14 13:04:31 +03:00
install_test() {
touch share/man/man1/hub.1
DESTDIR="$PWD/tmp/destdir" prefix=/my/prefix bash < script/install.sh
test -x tmp/destdir/my/prefix/bin/hub
test -e tmp/destdir/my/prefix/share/man/man1/hub.1
test ! -x tmp/destdir/my/prefix/share/man/man1/hub.1
rm share/man/man1/hub.1
}
[ -z "$HUB_COVERAGE" ] || script/coverage prepare
script/build
go test ./... || STATUS="$?"
script/ruby-test "$@" || STATUS="$?"
[ -z "$HUB_COVERAGE" ] || script/coverage summarize "$min_coverage" || STATUS="$?"
if [ -n "$CI" ]; then
check_formatting || STATUS="$?"
2018-06-14 13:04:31 +03:00
install_test || STATUS="$?"
fi
exit "$STATUS"