From 4a5add08ab795f0d7ee810ebfda8578038031855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 5 Apr 2020 20:25:24 +0100 Subject: [PATCH] fix(tests): test main() function --- Makefile | 12 ++++++++++-- cmd/karma/runmain_test.go | 9 +++++++++ go.mod | 1 + go.sum | 2 ++ scripts/gocover.sh | 30 ------------------------------ scripts/gocovmerge.sh | 9 +++++++++ scripts/test-main.sh | 38 ++++++++++++++++++++++++++++++++++++++ scripts/test-unit.sh | 23 +++++++++++++++++++++++ tools.go | 1 + 9 files changed, 93 insertions(+), 32 deletions(-) create mode 100644 cmd/karma/runmain_test.go delete mode 100755 scripts/gocover.sh create mode 100755 scripts/gocovmerge.sh create mode 100755 scripts/test-main.sh create mode 100755 scripts/test-unit.sh diff --git a/Makefile b/Makefile index f1baa90ea..514e00b20 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/cmd/karma/runmain_test.go b/cmd/karma/runmain_test.go new file mode 100644 index 000000000..e2a6aee45 --- /dev/null +++ b/cmd/karma/runmain_test.go @@ -0,0 +1,9 @@ +// +build testrunmain + +package main + +import "testing" + +func TestRunMain(t *testing.T) { + main() +} diff --git a/go.mod b/go.mod index 5f2e1a0f6..31c907eb1 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b00680b36..9248e5d55 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/scripts/gocover.sh b/scripts/gocover.sh deleted file mode 100755 index 35bc62948..000000000 --- a/scripts/gocover.sh +++ /dev/null @@ -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}' diff --git a/scripts/gocovmerge.sh b/scripts/gocovmerge.sh new file mode 100755 index 000000000..6789adb9e --- /dev/null +++ b/scripts/gocovmerge.sh @@ -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}' diff --git a/scripts/test-main.sh b/scripts/test-main.sh new file mode 100755 index 000000000..c728ec6ac --- /dev/null +++ b/scripts/test-main.sh @@ -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 diff --git a/scripts/test-unit.sh b/scripts/test-unit.sh new file mode 100755 index 000000000..95c52351d --- /dev/null +++ b/scripts/test-unit.sh @@ -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 diff --git a/tools.go b/tools.go index cb6ac3bec..bc6677108 100644 --- a/tools.go +++ b/tools.go @@ -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" )