Skip to content

Commit

Permalink
feat: add tests result
Browse files Browse the repository at this point in the history
Show tests result in GHA.

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed Jun 4, 2024
1 parent f292767 commit a759991
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 37 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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-$@
Expand Down
107 changes: 74 additions & 33 deletions internal/project/golang/unit_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")).
Expand All @@ -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")).
Expand Down Expand Up @@ -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).
Expand All @@ -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
Expand Down

0 comments on commit a759991

Please sign in to comment.