From 3b888662886a52f36c68906074538c476e1d2249 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Fri, 2 Jun 2023 07:34:09 -0700 Subject: [PATCH] [chore] enable coverage for unit tests (#22966) This re-enables unit test coverage for the repository. Coverage will not immediately include integration tests. Fixes #8394 --------- Signed-off-by: Alex Boten --- .github/workflows/build-and-test.yml | 22 ++++++++++++++++++++++ .gitignore | 5 ++--- Makefile | 12 ++++++------ Makefile.Common | 7 ++++++- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 016ab329c521..cb88004bf00a 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -254,7 +254,16 @@ jobs: path: ~/.cache/go-build key: go-test-build-${{ runner.os }}-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }} - name: Run Unit Tests + if: ${{ matrix.go-version == '1.19' }} run: make gotest GROUP=${{ matrix.group }} + - name: Run Unit Tests With Coverage + if: ${{ matrix.go-version == '1.20' }} # only run coverage on one version + run: make gotest-with-cover GROUP=${{ matrix.group }} + - uses: actions/upload-artifact@v3 + if: ${{ matrix.go-version == '1.20' }} # only run coverage on one version + with: + name: coverage-artifacts + path: ${{ matrix.group }}-coverage.txt unittest: if: ${{ github.actor != 'dependabot[bot]' && always() }} strategy: @@ -274,6 +283,19 @@ jobs: echo "One or more matrix jobs failed." false fi + coverage: + runs-on: ubuntu-latest + needs: [unittest] + steps: + - uses: actions/checkout@v3 + - uses: actions/download-artifact@v3 + with: + name: coverage-artifacts + - name: Upload coverage report + uses: codecov/codecov-action@v3 + with: + fail_ci_if_error: true + verbose: true integration-tests: runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index da76a7fd2fde..7518e3156227 100644 --- a/.gitignore +++ b/.gitignore @@ -27,9 +27,8 @@ dist/ *.DS_Store # Coverage -coverage.txt -integration-coverage.txt -coverage.html +coverage/* +*-coverage.txt integration-coverage.html # Wix diff --git a/Makefile b/Makefile index bb7cb1a7260f..5485ae6cd25d 100644 --- a/Makefile +++ b/Makefile @@ -73,12 +73,6 @@ all-common: e2e-test: otelcontribcol oteltestbedcol $(MAKE) -C testbed run-tests -.PHONY: unit-tests-with-cover -unit-tests-with-cover: - @echo Verifying that all packages have test files to count in coverage - @internal/buildscripts/check-test-files.sh $(subst github.com/open-telemetry/opentelemetry-collector-contrib/,./,$(ALL_PKGS)) - @$(MAKE) $(FOR_GROUP_TARGET) TARGET="do-unit-tests-with-cover" - TARGET="do-integration-tests-with-cover" .PHONY: integration-tests-with-cover integration-tests-with-cover: $(INTEGRATION_MODS) @@ -101,6 +95,11 @@ gomoddownload: gotest: $(MAKE) $(FOR_GROUP_TARGET) TARGET="test" +.PHONY: gotest-with-cover +gotest-with-cover: + @$(MAKE) $(FOR_GROUP_TARGET) TARGET="test-with-cover" + $(GOCMD) tool covdata textfmt -i=./coverage/unit -o ./$(GROUP)-coverage.txt + .PHONY: gofmt gofmt: $(MAKE) $(FOR_GROUP_TARGET) TARGET="fmt" @@ -385,6 +384,7 @@ clean: @echo "Removing coverage files" find . -type f -name 'coverage.txt' -delete find . -type f -name 'coverage.html' -delete + find . -type f -name 'coverage.out' -delete find . -type f -name 'integration-coverage.txt' -delete find . -type f -name 'integration-coverage.html' -delete diff --git a/Makefile.Common b/Makefile.Common index dcb9b294c619..7144da77ce49 100644 --- a/Makefile.Common +++ b/Makefile.Common @@ -96,7 +96,12 @@ common: lint .PHONY: test test: $(GOTEST) $(GOTEST_OPT) ./... - + +.PHONY: test-with-cover +test-with-cover: + mkdir -p $(PWD)/coverage/unit + $(GOTEST) -cover ./... -covermode=atomic -args -test.gocoverdir="$(PWD)/coverage/unit" + .PHONY: do-unit-tests-with-cover do-unit-tests-with-cover: @echo "running $(GOCMD) unit test ./... + coverage in `pwd`"