diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..0db76a85 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,81 @@ +name: Tests / Code Coverage +on: + pull_request: + types: [opened, synchronize, reopened, labeled] + merge_group: + types: [checks_requested] + +permissions: + contents: read + +concurrency: + group: ci-${{ github.ref }}-tests + cancel-in-progress: true + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + arch: [amd64, arm64] + targetos: [darwin, linux] + name: sedad ${{ matrix.targetos }}-${{ matrix.arch }} + steps: + - uses: actions/checkout@v4 + - name: Cache binaries + id: cache-binaries + uses: actions/cache@v3 + with: + path: ./cmd/sedad/sedad + key: sedad-${{ matrix.targetos }}-${{ matrix.arch }} + - uses: technote-space/get-diff-action@v6.1.2 + with: + PATTERNS: | + **/**.go + go.mod + go.sum + - name: Setup go + if: steps.cache-binaries.outputs.cache-hit != 'true' && env.GIT_DIFF + uses: actions/setup-go@v4 + with: + go-version: "1.19" + cache: true + env: + GOOS: ${{ matrix.targetos }} + GOARCH: ${{ matrix.arch }} + - name: Compile + if: steps.cache-binaries.outputs.cache-hit != 'true' && env.GIT_DIFF + run: | + go mod download + make build + + tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v4 + with: + go-version: "1.19" + check-latest: true + cache: true + cache-dependency-path: go.sum + - uses: technote-space/get-diff-action@v6.1.2 + id: git_diff + with: + PATTERNS: | + **/*.go + go.mod + go.sum + **/go.mod + **/go.sum + **/Makefile + Makefile + - name: test & coverage report creation + if: env.GIT_DIFF + run: make test-unit-cover + - uses: actions/upload-artifact@v3 + if: env.GIT_DIFF + with: + name: "${{ github.sha }}-${{ matrix.part }}-coverage" + path: ./${{ matrix.part }}profile.out + diff --git a/Makefile b/Makefile index a5e74a59..ced4c52d 100644 --- a/Makefile +++ b/Makefile @@ -131,3 +131,45 @@ proto-update-deps: $(DOCKER) run --rm -v $(CURDIR)/proto:/workspace --workdir /workspace $(protoImageName) buf mod update .PHONY: proto-gen proto-lint proto-update-deps + +############################################################################### +## Tests ## +############################################################################### + +PACKAGES_UNIT=$(shell go list ./...) +TEST_PACKAGES=./... +TEST_TARGETS := test-unit test-unit-cover test-race +TEST_COVERAGE_PROFILE=coverage.txt + +UNIT_TEST_TAGS = norace +TEST_RACE_TAGS = "" + +ifeq ($(EXPERIMENTAL),true) + UNIT_TEST_TAGS += experimental + TEST_RACE_TAGS += experimental +endif + +test-unit: ARGS=-timeout=10m -tags='$(UNIT_TEST_TAGS)' +test-unit: TEST_PACKAGES=$(PACKAGES_UNIT) +test-unit-cover: ARGS=-timeout=10m -tags='$(UNIT_TEST_TAGS)' -coverprofile=$(TEST_COVERAGE_PROFILE) -covermode=atomic +test-unit-cover: TEST_PACKAGES=$(PACKAGES_UNIT) +test-race: ARGS=-timeout=10m -race -tags='$(TEST_RACE_TAGS)' +test-race: TEST_PACKAGES=$(PACKAGES_UNIT) +$(TEST_TARGETS): run-tests + +run-tests: +ifneq (,$(shell which tparse 2>/dev/null)) + @echo "--> Running tests" + @go test -mod=readonly -json $(ARGS) $(TEST_PACKAGES) | tparse +else + @echo "--> Running tests" + @go test -mod=readonly $(ARGS) $(TEST_PACKAGES) +endif + +cover-html: test-unit-cover + @echo "--> Opening in the browser" + @go tool cover -html=$(TEST_COVERAGE_PROFILE) + +.PHONY: cover-html run-tests $(TEST_TARGETS) + +