use go 1.20 -cover support

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-03-10 09:08:24 +00:00 committed by Nicolas De loof
parent 6a0398d786
commit 85ddfde5d6
6 changed files with 35 additions and 7 deletions

View File

@ -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
.gitignore vendored
View File

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

View File

@ -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 \

View File

@ -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

View File

@ -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"

View File

@ -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