瀏覽代碼

use go 1.20 -cover support

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 2 年之前
父節點
當前提交
85ddfde5d6
共有 6 個文件被更改,包括 35 次插入7 次删除
  1. 4 4
      .github/workflows/ci.yml
  2. 2 0
      .gitignore
  3. 1 0
      Dockerfile
  4. 10 2
      Makefile
  5. 10 0
      docker-bake.hcl
  6. 8 1
      pkg/e2e/framework.go

+ 4 - 4
.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:

+ 2 - 0
.gitignore

@@ -1,3 +1,5 @@
 bin/
 /.vscode/
 coverage.out
+covdatafiles/
+.DS_Store

+ 1 - 0
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 \

+ 10 - 2
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

+ 10 - 0
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"

+ 8 - 1
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