Merge pull request #1597 from prymitive/more-tests

fix(tests): test main() function
This commit is contained in:
Łukasz Mierzwa
2020-04-05 20:56:56 +01:00
committed by GitHub
9 changed files with 93 additions and 32 deletions

View File

@@ -22,6 +22,11 @@ endif
GO111MODULE=on go install github.com/elazarl/go-bindata-assetfs/...
touch $@
.build/deps-test-go.ok: go.mod go.sum
@mkdir -p .build
GO111MODULE=on go install github.com/wadey/gocovmerge
touch $@
.build/deps-lint-go.ok: go.mod go.sum
@mkdir -p .build
GO111MODULE=on go install github.com/golangci/golangci-lint/cmd/golangci-lint
@@ -133,8 +138,11 @@ benchmark-go:
GO111MODULE=on go test -run=NONE -bench=. -benchmem ./...
.PHONY: test-go
test-go:
GO111MODULE=on scripts/gocover.sh
test-go: .build/deps-test-go.ok
@rm -f profile.*
GO111MODULE=on ./scripts/test-unit.sh
GO111MODULE=on ./scripts/test-main.sh
GO111MODULE=on ./scripts/gocovmerge.sh
.PHONY: test-js
test-js: .build/deps-build-node.ok

View File

@@ -0,0 +1,9 @@
// +build testrunmain
package main
import "testing"
func TestRunMain(t *testing.T) {
main()
}

1
go.mod
View File

@@ -34,6 +34,7 @@ require (
github.com/sirupsen/logrus v1.5.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.6.2 // indirect
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad
github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1 // indirect
gopkg.in/go-playground/colors.v1 v1.2.0
gopkg.in/yaml.v2 v2.2.8

2
go.sum
View File

@@ -476,6 +476,8 @@ github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk
github.com/valyala/quicktemplate v1.2.0/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOVRUAfrukLPuGJ4=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw=
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad h1:W0LEBv82YCGEtcmPA3uNZBI33/qF//HAAs3MawDjRa0=
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad/go.mod h1:Hy8o65+MXnS6EwGElrSRjUzQDLXreJlzYLlWiHtt8hM=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1 h1:j2hhcujLRHAg872RWAV5yaUrEjHEObwDv3aImCaNLek=
github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1/go.mod h1:QcJo0QPSfTONNIgpN5RA8prR7fF8nkF6cTWTcNerRO8=

View File

@@ -1,30 +0,0 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
trap cleanup INT
function cleanup() {
rm -f coverage.txt coverage.out profile.out
exit
}
echo "" > coverage.txt
PKGS=$(go list ./... | grep -vE 'prymitive/karma/internal/mapper/v017/(client|models)')
COVERPKG=$(echo "$PKGS" | tr '\n' ',')
for d in $PKGS; do
(go test -coverprofile=profile.out -coverpkg="$COVERPKG" $d 2>&1 || exit 2) \
| grep -v 'warning: no packages being tested depend on matches for pattern' \
| sed s/'of statements in .*'/''/g
if [ -f profile.out ]; then
cat profile.out >> coverage.txt
rm profile.out
fi
done
echo "mode: set" > coverage.out
cat coverage.txt | grep -v "mode: set" | grep -vE '^$' | grep -vE '^github.com/prymitive/karma/cmd/karma/bindata_assetfs.go:' >> coverage.out
mv coverage.out coverage.txt
go tool cover -func coverage.txt | tail -n 1 | awk '{print $3}'

9
scripts/gocovmerge.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
gocovmerge profile.* | grep -vE '^github.com/prymitive/karma/cmd/karma/bindata_assetfs.go:' > coverage.txt
rm -f profile.*
go tool cover -func coverage.txt | tail -n 1 | awk '{print $3}'

38
scripts/test-main.sh Executable file
View File

@@ -0,0 +1,38 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
trap cleanup INT
function cleanup() {
rm -f profile.*
exit
}
PKGS=$(go list ./... | grep -vE 'prymitive/karma/internal/mapper/v017/(client|models)')
COVERPKG=$(echo "$PKGS" | tr '\n' ',')
go test \
-coverpkg="$COVERPKG" \
-c \
-tags testrunmain \
./cmd/karma 2>&1 \
| (grep -v 'warning: no packages being tested depend on matches for pattern' || true)
(
ALERTMANAGER_URI=http://localhost \
ALERTMANAGER_INTERVAL=1s \
LISTEN_ADDRESS=127.0.0.1 \
LOG_LEVEL=fatal \
LOG_CONFIG=false \
./karma.test \
-test.run "^TestRunMain$" \
-test.coverprofile=profile.main.1 2>&1 \
| grep -v 'warning: no packages being tested depend on matches for pattern' \
| sed s/'of statements in .*'/''/g &)
sleep 5
killall karma.test
rm karma.test

23
scripts/test-unit.sh Executable file
View File

@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
trap cleanup INT
function cleanup() {
rm -f profile.*
exit
}
PKGS=$(go list ./... | grep -vE 'prymitive/karma/internal/mapper/v017/(client|models)')
COVERPKG=$(echo "$PKGS" | tr '\n' ',')
I=0
for d in $PKGS; do
I=$((I+1))
COVFILE="profile.test.${I}"
(go test -coverprofile="${COVFILE}" -coverpkg="$COVERPKG" $d 2>&1 || exit 2) \
| grep -v 'warning: no packages being tested depend on matches for pattern' \
| sed s/'of statements in .*'/''/g
done

View File

@@ -10,4 +10,5 @@ import (
_ "github.com/elazarl/go-bindata-assetfs"
_ "github.com/go-bindata/go-bindata/v3"
_ "github.com/golangci/golangci-lint/pkg/commands"
_ "github.com/wadey/gocovmerge"
)