From 85ddfde5d6a4f7e1f48e3bc9dde6346a527bc9d7 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Fri, 10 Mar 2023 09:08:24 +0000 Subject: [PATCH] use go 1.20 -cover support Signed-off-by: Nicolas De Loof --- .github/workflows/ci.yml | 8 ++++---- .gitignore | 2 ++ Dockerfile | 1 + Makefile | 12 ++++++++++-- docker-bake.hcl | 10 ++++++++++ pkg/e2e/framework.go | 9 ++++++++- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f9a6ecfd3..334179b0c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -158,7 +158,7 @@ jobs: name: Build uses: docker/bake-action@v2 with: - targets: binary + targets: binary-with-coverage set: | *.cache-from=type=gha,scope=binary-linux-amd64 *.cache-from=type=gha,scope=binary-e2e-${{ matrix.mode }} @@ -177,9 +177,6 @@ jobs: if: ${{ matrix.mode == 'plugin' }} run: | make e2e-compose - - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - name: Test standalone mode if: ${{ matrix.mode == 'standalone' }} @@ -192,6 +189,9 @@ jobs: if: ${{ matrix.mode == 'cucumber'}} run: | make test-cucumber + - + name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 release: permissions: diff --git a/.gitignore b/.gitignore index 3cb333cac..7bfe97f71 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ bin/ /.vscode/ coverage.out +covdatafiles/ +.DS_Store diff --git a/Dockerfile b/Dockerfile index cd62b4b13..75be9c10a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -76,6 +76,7 @@ EOT FROM build-base AS build ARG BUILD_TAGS +ARG BUILD_FLAGS ARG TARGETPLATFORM RUN --mount=type=bind,target=. \ --mount=type=cache,target=/root/.cache \ diff --git a/Makefile b/Makefile index d3fc2cecc..fb9f0c0f2 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,7 @@ ifneq ($(DETECTED_OS),Windows) # https://github.com/golang/go/issues/27089 TEST_COVERAGE_FLAGS += -race endif +BUILD_FLAGS?= TEST_FLAGS?= E2E_TEST?= ifeq ($(E2E_TEST),) @@ -53,12 +54,16 @@ all: build .PHONY: build ## Build the compose cli-plugin build: - GO111MODULE=on go build -trimpath -tags "$(GO_BUILDTAGS)" -ldflags "$(GO_LDFLAGS)" -o "$(DESTDIR)/docker-compose$(BINARY_EXT)" ./cmd + GO111MODULE=on go build $(BUILD_FLAGS) -trimpath -tags "$(GO_BUILDTAGS)" -ldflags "$(GO_LDFLAGS)" -o "$(DESTDIR)/docker-compose$(BINARY_EXT)" ./cmd .PHONY: binary binary: $(BUILDX_CMD) bake binary +.PHONY: binary-with-coverage +binary-with-coverage: + $(BUILDX_CMD) bake binary-with-coverage + .PHONY: install install: binary mkdir -p ~/.docker/cli-plugins @@ -66,7 +71,10 @@ install: binary .PHONY: e2e-compose e2e-compose: ## Run end to end local tests in plugin mode. Set E2E_TEST=TestName to run a single test - go test $(TEST_FLAGS) $(TEST_COVERAGE_FLAGS) -count=1 ./pkg/e2e + rm -rf covdatafiles + mkdir covdatafiles + GOCOVERDIR=covdatafiles go test $(TEST_FLAGS) -count=1 ./pkg/e2e + go tool covdata textfmt -i=covdatafiles -o=coverage.out .PHONY: e2e-compose-standalone e2e-compose-standalone: ## Run End to end local tests in standalone mode. Set E2E_TEST=TestName to run a single test diff --git a/docker-bake.hcl b/docker-bake.hcl index 09794f7b1..ef75a7a1f 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -83,6 +83,16 @@ target "test" { output = [bindir("coverage")] } +target "binary-with-coverage" { + inherits = ["_common"] + target = "binary" + args = { + BUILD_FLAGS = "-cover" + } + output = [bindir("build")] + platforms = ["local"] +} + target "binary" { inherits = ["_common"] target = "binary" diff --git a/pkg/e2e/framework.go b/pkg/e2e/framework.go index fd0b1e587..7ef45f48d 100644 --- a/pkg/e2e/framework.go +++ b/pkg/e2e/framework.go @@ -214,12 +214,19 @@ func CopyFile(t testing.TB, sourceFile string, destinationFile string) { // BaseEnvironment provides the minimal environment variables used across all // Docker / Compose commands. func (c *CLI) BaseEnvironment() []string { - return []string{ + env := []string{ "HOME=" + c.HomeDir, "USER=" + os.Getenv("USER"), "DOCKER_CONFIG=" + c.ConfigDir, "KUBECONFIG=invalid", } + if coverdir, ok := os.LookupEnv("GOCOVERDIR"); ok { + _, filename, _, _ := runtime.Caller(0) + root := filepath.Join(filepath.Dir(filename), "..", "..") + coverdir = filepath.Join(root, coverdir) + env = append(env, fmt.Sprintf("GOCOVERDIR=%s", coverdir)) + } + return env } // NewCmd creates a cmd object configured with the test environment set