@@ -60,6 +60,8 @@ jwks_url:=https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-con
6060# Test output files
6161unit_test_json_output ?= ${PWD}/unit-test-results.json
6262integration_test_json_output ?= ${PWD}/integration-test-results.json
63+ unit_coverage_output ?= ${PWD}/coverage-unit.out
64+ integration_coverage_output ?= ${PWD}/coverage-integration.out
6365
6466# Prints a list of useful targets.
6567help :
7476 @echo " make run/docs run swagger and host the api spec"
7577 @echo " make test run unit tests"
7678 @echo " make test-integration run integration tests"
79+ @echo " make coverage-html generate HTML coverage reports"
80+ @echo " make coverage-func show function-level coverage summary"
7781 @echo " make generate generate openapi modules"
7882 @echo " make image build docker image"
7983 @echo " make push push docker image"
@@ -171,7 +175,7 @@ install: check-gopath
171175# Examples:
172176# make test TESTFLAGS="-run TestSomething"
173177test : install
174- OCM_ENV=testing gotestsum --format short-verbose -- -p 1 -v $(TESTFLAGS ) \
178+ OCM_ENV=testing gotestsum --format short-verbose -- -p 1 -v -coverprofile= $( unit_coverage_output ) -coverpkg=./... $(TESTFLAGS ) \
175179 ./pkg/... \
176180 ./cmd/...
177181.PHONY : test
@@ -185,7 +189,7 @@ test: install
185189# make test-unit-json TESTFLAGS="-run TestSomething"
186190ci-test-unit : install
187191 @echo $(db_password ) > ${PWD} /secrets/db.password
188- OCM_ENV=testing gotestsum --jsonfile-timing-events=$(unit_test_json_output ) --format short-verbose -- -p 1 -v $(TESTFLAGS ) \
192+ OCM_ENV=testing gotestsum --jsonfile-timing-events=$(unit_test_json_output ) --format short-verbose -- -p 1 -v -coverprofile= $( unit_coverage_output ) -coverpkg=./... $(TESTFLAGS ) \
189193 ./pkg/... \
190194 ./cmd/...
191195.PHONY : ci-test-unit
@@ -202,7 +206,7 @@ ci-test-unit: install
202206# make test-integration TESTFLAGS="-short" skips long-run tests
203207ci-test-integration : install
204208 @echo $(db_password ) > ${PWD} /secrets/db.password
205- OCM_ENV=testing gotestsum --jsonfile-timing-events=$(integration_test_json_output ) --format $(TEST_SUMMARY_FORMAT ) -- -p 1 -ldflags -s -v -timeout 1h $(TESTFLAGS ) \
209+ OCM_ENV=testing gotestsum --jsonfile-timing-events=$(integration_test_json_output ) --format $(TEST_SUMMARY_FORMAT ) -- -p 1 -ldflags -s -v -timeout 1h -coverprofile= $( integration_coverage_output ) -coverpkg=./... $(TESTFLAGS ) \
206210 ./test/integration
207211.PHONY : ci-test-integration
208212
@@ -218,7 +222,7 @@ ci-test-integration: install
218222# make test-integration TESTFLAGS="-short" skips long-run tests
219223test-integration : install
220224 @echo $(db_password ) > ${PWD} /secrets/db.password
221- OCM_ENV=testing gotestsum --format $(TEST_SUMMARY_FORMAT ) -- -p 1 -ldflags -s -v -timeout 1h $(TESTFLAGS ) \
225+ OCM_ENV=testing gotestsum --format $(TEST_SUMMARY_FORMAT ) -- -p 1 -ldflags -s -v -timeout 1h -coverprofile= $( integration_coverage_output ) -coverpkg=./... $(TESTFLAGS ) \
222226 ./test/integration
223227.PHONY : test-integration
224228
@@ -242,12 +246,37 @@ run/docs:
242246 docker run -d -p 80:8080 -e SWAGGER_JSON=/trex.yaml -v $(PWD ) /openapi/rh-trex.yaml:/trex.yaml swaggerapi/swagger-ui
243247.PHONY : run/docs
244248
249+ # Run coverage reports
250+ coverage-html :
251+ @if [ -f $( unit_coverage_output) ]; then \
252+ go tool cover -html=$(unit_coverage_output ) -o coverage-unit.html; \
253+ echo " Unit test coverage report generated: coverage-unit.html" ; \
254+ fi
255+ @if [ -f $( integration_coverage_output) ]; then \
256+ go tool cover -html=$(integration_coverage_output ) -o coverage-integration.html; \
257+ echo " Integration test coverage report generated: coverage-integration.html" ; \
258+ fi
259+ .PHONY : coverage-html
260+
261+ coverage-func :
262+ @if [ -f $( unit_coverage_output) ]; then \
263+ echo " === Unit Test Coverage ===" ; \
264+ go tool cover -func=$(unit_coverage_output ) ; \
265+ fi
266+ @if [ -f $( integration_coverage_output) ]; then \
267+ echo " === Integration Test Coverage ===" ; \
268+ go tool cover -func=$(integration_coverage_output ) ; \
269+ fi
270+ .PHONY : coverage-func
271+
245272# Delete temporary files
246273clean :
247274 rm -rf \
248275 $(binary ) \
249276 templates/* -template.json \
250277 data/generated/openapi/* .json \
278+ coverage-* .out \
279+ coverage-* .html \
251280.PHONY : clean
252281
253282.PHONY : cmds
0 commit comments