From a759991132f2ace78f45ac58378a1dfb5764c2ed Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Tue, 4 Jun 2024 18:48:39 +0400 Subject: [PATCH] feat: add tests result Show tests result in GHA. Signed-off-by: Andrey Smirnov --- .github/workflows/ci.yaml | 10 ++- Dockerfile | 12 ++- Makefile | 6 +- internal/project/golang/unit_tests.go | 107 ++++++++++++++++++-------- 4 files changed, 98 insertions(+), 37 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 775dcbf1..d0c5d653 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,6 +1,6 @@ # THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. # -# Generated on 2024-05-23T09:16:24Z by kres 1fd6bd5-dirty. +# Generated on 2024-06-04T14:55:06Z by kres 914213a. name: default concurrency: @@ -72,7 +72,13 @@ jobs: make base - name: unit-tests run: | - make unit-tests + make unit-tests-json + - name: unit-tests-results + if: always() + uses: robherley/go-test-action@v0 + with: + fromJSONFile: _out/test-results-unit-tests.json + omit: untested - name: unit-tests-race run: | make unit-tests-race diff --git a/Dockerfile b/Dockerfile index 7d7beea6..3d585a18 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ # THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. # -# Generated on 2024-05-27T15:18:38Z by kres b5844f8-dirty. +# Generated on 2024-06-04T14:35:36Z by kres f292767-dirty. ARG TOOLCHAIN @@ -94,6 +94,12 @@ WORKDIR /src ARG TESTPKGS RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp go test -v -covermode=atomic -coverprofile=coverage.txt -coverpkg=${TESTPKGS} -count 1 ${TESTPKGS} +# runs unit-tests with JSON output +FROM base AS unit-tests-run-json +WORKDIR /src +ARG TESTPKGS +RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp go test -json -covermode=atomic -coverprofile=coverage.txt -coverpkg=${TESTPKGS} -count 1 ${TESTPKGS} > test-results.json + FROM embed-generate AS embed-abbrev-generate WORKDIR /src ARG ABBREV_TAG @@ -103,6 +109,10 @@ RUN echo -n 'undefined' > internal/version/data/sha && \ FROM scratch AS unit-tests COPY --from=unit-tests-run /src/coverage.txt /coverage-unit-tests.txt +FROM scratch AS unit-tests-json +COPY --from=unit-tests-run-json /src/coverage.txt /coverage-unit-tests.txt +COPY --from=unit-tests-run-json /src/test-results.json /test-results-unit-tests.json + # cleaned up specs and compiled versions FROM scratch AS generate COPY --from=embed-abbrev-generate /src/internal/version internal/version diff --git a/Makefile b/Makefile index 4d547a59..8e111692 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. # -# Generated on 2024-06-03T10:17:07Z by kres f249b6c-dirty. +# Generated on 2024-06-04T14:35:36Z by kres f292767-dirty. # common variables @@ -174,6 +174,10 @@ base: ## Prepare base toolchain unit-tests: ## Performs unit tests @$(MAKE) local-$@ DEST=$(ARTIFACTS) +.PHONY: unit-tests-json +unit-tests-json: ## Performs unit tests with JSON output + @$(MAKE) local-$@ DEST=$(ARTIFACTS) + .PHONY: unit-tests-race unit-tests-race: ## Performs unit tests with race detection enabled. @$(MAKE) target-$@ diff --git a/internal/project/golang/unit_tests.go b/internal/project/golang/unit_tests.go index b67ee0f7..716a6957 100644 --- a/internal/project/golang/unit_tests.go +++ b/internal/project/golang/unit_tests.go @@ -52,6 +52,23 @@ func NewUnitTests(meta *meta.Options, packagePath string) *UnitTests { } } +func (tests *UnitTests) addCopySteps(stage *dockerfile.Stage) { + for _, dockerStep := range tests.Docker.Steps { + if dockerStep.Copy != nil { + copyStep := step.Copy(dockerStep.Copy.Src, dockerStep.Copy.Dst) + if dockerStep.Copy.From != "" { + copyStep.From(dockerStep.Copy.From) + } + + if dockerStep.Copy.Platform != "" { + copyStep.Platform(dockerStep.Copy.Platform) + } + + stage.Step(copyStep) + } + } +} + // CompileDockerfile implements dockerfile.Compiler. func (tests *UnitTests) CompileDockerfile(output *dockerfile.Output) error { wrapAsInsecure := func(s *step.RunStep) *step.RunStep { @@ -68,26 +85,15 @@ func (tests *UnitTests) CompileDockerfile(output *dockerfile.Output) error { } workdir := step.WorkDir(filepath.Join("/src", tests.packagePath)) - testRun := tests.Name() + "-run" + testRunName := tests.Name() + "-run" + + // regular unit tests - testRunStage := output.Stage(testRun). + testRunStage := output.Stage(testRunName). Description("runs unit-tests"). From("base") - for _, dockerStep := range tests.Docker.Steps { - if dockerStep.Copy != nil { - copyStep := step.Copy(dockerStep.Copy.Src, dockerStep.Copy.Dst) - if dockerStep.Copy.From != "" { - copyStep.From(dockerStep.Copy.From) - } - - if dockerStep.Copy.Platform != "" { - copyStep.Platform(dockerStep.Copy.Platform) - } - - testRunStage.Step(copyStep) - } - } + tests.addCopySteps(testRunStage) testRunStage.Step(workdir). Step(step.Arg("TESTPKGS")). @@ -103,26 +109,40 @@ func (tests *UnitTests) CompileDockerfile(output *dockerfile.Output) error { output.Stage(tests.Name()). From("scratch"). - Step(step.Copy(filepath.Join("/src", tests.packagePath, "coverage.txt"), fmt.Sprintf("/coverage-%s.txt", tests.Name())).From(testRun)) + Step(step.Copy(filepath.Join("/src", tests.packagePath, "coverage.txt"), fmt.Sprintf("/coverage-%s.txt", tests.Name())).From(testRunName)) - testRunRaceStage := output.Stage(tests.Name() + "-race"). - Description("runs unit-tests with race detector"). + // unit-tests with json output + + testRunJSONStage := output.Stage(testRunName + "-json"). + Description("runs unit-tests with JSON output"). From("base") - for _, dockerStep := range tests.Docker.Steps { - if dockerStep.Copy != nil { - copyStep := step.Copy(dockerStep.Copy.Src, dockerStep.Copy.Dst) - if dockerStep.Copy.From != "" { - copyStep.From(dockerStep.Copy.From) - } + tests.addCopySteps(testRunJSONStage) - if dockerStep.Copy.Platform != "" { - copyStep.Platform(dockerStep.Copy.Platform) - } + testRunJSONStage.Step(workdir). + Step(step.Arg("TESTPKGS")). + Step(wrapAsInsecure( + step.Script( + fmt.Sprintf( + `go test -json -covermode=atomic -coverprofile=coverage.txt -coverpkg=${TESTPKGS} -count 1 %s${TESTPKGS} > test-results.json`, + extraArgs), + ). + MountCache(filepath.Join(tests.meta.CachePath, "go-build")). + MountCache(filepath.Join(tests.meta.GoPath, "pkg")). + MountCache("/tmp"))) - testRunRaceStage.Step(copyStep) - } - } + output.Stage(tests.Name() + "-json"). + From("scratch"). + Step(step.Copy(filepath.Join("/src", tests.packagePath, "coverage.txt"), fmt.Sprintf("/coverage-%s.txt", tests.Name())).From(testRunName + "-json")). + Step(step.Copy(filepath.Join("/src", tests.packagePath, "test-results.json"), fmt.Sprintf("/test-results-%s.json", tests.Name())).From(testRunName + "-json")) + + // unit-tests with race + + testRunRaceStage := output.Stage(tests.Name() + "-race"). + Description("runs unit-tests with race detector"). + From("base") + + tests.addCopySteps(testRunRaceStage) testRunRaceStage.Step(workdir). Step(step.Arg("TESTPKGS")). @@ -157,6 +177,11 @@ func (tests *UnitTests) CompileMakefile(output *makefile.Output) error { Script("@$(MAKE) local-$@ DEST=$(ARTIFACTS)" + scriptExtraArgs). Phony() + output.Target(tests.Name() + "-json"). + Description("Performs unit tests with JSON output"). + Script("@$(MAKE) local-$@ DEST=$(ARTIFACTS)" + scriptExtraArgs). + Phony() + output.Target(tests.Name() + "-race"). Description("Performs unit tests with race detection enabled."). Script("@$(MAKE) target-$@" + scriptExtraArgs). @@ -182,8 +207,24 @@ func (tests *UnitTests) CompileDrone(output *drone.Output) error { func (tests *UnitTests) CompileGitHubWorkflow(output *ghworkflow.Output) error { output.AddStep( "default", - ghworkflow.Step(tests.Name()).SetMakeStep(tests.Name()), - ghworkflow.Step(tests.Name()+"-race").SetMakeStep(tests.Name()+"-race"), + ghworkflow.Step(tests.Name()). + SetMakeStep(tests.Name()+"-json"), + ) + + resultsStep := ghworkflow.Step(tests.Name()+"-results"). + SetUses("robherley/go-test-action@v0"). + SetWith("fromJSONFile", filepath.Join(tests.meta.ArtifactsPath, "test-results-"+tests.Name()+".json")). + SetWith("omit", "untested") + + if err := resultsStep.SetConditions("always"); err != nil { + return err + } + + output.AddStep( + "default", + resultsStep, + ghworkflow.Step(tests.Name()+"-race"). + SetMakeStep(tests.Name()+"-race"), ) return nil