Skip to content

Commit 044e64d

Browse files
committed
feat: add tests result
Show tests result in GHA. Signed-off-by: Andrey Smirnov <[email protected]>
1 parent f292767 commit 044e64d

File tree

5 files changed

+99
-38
lines changed

5 files changed

+99
-38
lines changed

.github/workflows/ci.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
22
#
3-
# Generated on 2024-05-23T09:16:24Z by kres 1fd6bd5-dirty.
3+
# Generated on 2024-06-04T14:55:06Z by kres 914213a.
44

55
name: default
66
concurrency:
@@ -72,7 +72,13 @@ jobs:
7272
make base
7373
- name: unit-tests
7474
run: |
75-
make unit-tests
75+
make unit-tests-json
76+
- name: unit-tests-results
77+
if: always()
78+
uses: robherley/go-test-action@v0
79+
with:
80+
fromJSONFile: _out/test-results-unit-tests.json
81+
omit: untested
7682
- name: unit-tests-race
7783
run: |
7884
make unit-tests-race

Dockerfile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
44
#
5-
# Generated on 2024-05-27T15:18:38Z by kres b5844f8-dirty.
5+
# Generated on 2024-06-04T14:35:36Z by kres f292767-dirty.
66

77
ARG TOOLCHAIN
88

@@ -94,6 +94,12 @@ WORKDIR /src
9494
ARG TESTPKGS
9595
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}
9696

97+
# runs unit-tests with JSON output
98+
FROM base AS unit-tests-run-json
99+
WORKDIR /src
100+
ARG TESTPKGS
101+
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
102+
97103
FROM embed-generate AS embed-abbrev-generate
98104
WORKDIR /src
99105
ARG ABBREV_TAG
@@ -103,6 +109,10 @@ RUN echo -n 'undefined' > internal/version/data/sha && \
103109
FROM scratch AS unit-tests
104110
COPY --from=unit-tests-run /src/coverage.txt /coverage-unit-tests.txt
105111

112+
FROM scratch AS unit-tests-json
113+
COPY --from=unit-tests-run-json /src/coverage.txt /coverage-unit-tests.txt
114+
COPY --from=unit-tests-run-json /src/test-results.json /test-results-unit-tests.json
115+
106116
# cleaned up specs and compiled versions
107117
FROM scratch AS generate
108118
COPY --from=embed-abbrev-generate /src/internal/version internal/version

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
22
#
3-
# Generated on 2024-06-03T10:17:07Z by kres f249b6c-dirty.
3+
# Generated on 2024-06-04T14:35:36Z by kres f292767-dirty.
44

55
# common variables
66

@@ -174,6 +174,10 @@ base: ## Prepare base toolchain
174174
unit-tests: ## Performs unit tests
175175
@$(MAKE) local-$@ DEST=$(ARTIFACTS)
176176

177+
.PHONY: unit-tests-json
178+
unit-tests-json: ## Performs unit tests with JSON output
179+
@$(MAKE) local-$@ DEST=$(ARTIFACTS)
180+
177181
.PHONY: unit-tests-race
178182
unit-tests-race: ## Performs unit tests with race detection enabled.
179183
@$(MAKE) target-$@

internal/config/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestLoad(t *testing.T) {
4545
require.NoError(t, err)
4646

4747
assert.Equal(t, "xyz", foo.Contents)
48-
assert.Equal(t, 5, foo.Len)
48+
assert.Equal(t, 6, foo.Len)
4949
assert.Equal(t, "same", foo.Extra)
5050

5151
bad := Foo{

internal/project/golang/unit_tests.go

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ func NewUnitTests(meta *meta.Options, packagePath string) *UnitTests {
5252
}
5353
}
5454

55+
func (tests *UnitTests) addCopySteps(stage *dockerfile.Stage) {
56+
for _, dockerStep := range tests.Docker.Steps {
57+
if dockerStep.Copy != nil {
58+
copyStep := step.Copy(dockerStep.Copy.Src, dockerStep.Copy.Dst)
59+
if dockerStep.Copy.From != "" {
60+
copyStep.From(dockerStep.Copy.From)
61+
}
62+
63+
if dockerStep.Copy.Platform != "" {
64+
copyStep.Platform(dockerStep.Copy.Platform)
65+
}
66+
67+
stage.Step(copyStep)
68+
}
69+
}
70+
}
71+
5572
// CompileDockerfile implements dockerfile.Compiler.
5673
func (tests *UnitTests) CompileDockerfile(output *dockerfile.Output) error {
5774
wrapAsInsecure := func(s *step.RunStep) *step.RunStep {
@@ -68,26 +85,15 @@ func (tests *UnitTests) CompileDockerfile(output *dockerfile.Output) error {
6885
}
6986

7087
workdir := step.WorkDir(filepath.Join("/src", tests.packagePath))
71-
testRun := tests.Name() + "-run"
88+
testRunName := tests.Name() + "-run"
89+
90+
// regular unit tests
7291

73-
testRunStage := output.Stage(testRun).
92+
testRunStage := output.Stage(testRunName).
7493
Description("runs unit-tests").
7594
From("base")
7695

77-
for _, dockerStep := range tests.Docker.Steps {
78-
if dockerStep.Copy != nil {
79-
copyStep := step.Copy(dockerStep.Copy.Src, dockerStep.Copy.Dst)
80-
if dockerStep.Copy.From != "" {
81-
copyStep.From(dockerStep.Copy.From)
82-
}
83-
84-
if dockerStep.Copy.Platform != "" {
85-
copyStep.Platform(dockerStep.Copy.Platform)
86-
}
87-
88-
testRunStage.Step(copyStep)
89-
}
90-
}
96+
tests.addCopySteps(testRunStage)
9197

9298
testRunStage.Step(workdir).
9399
Step(step.Arg("TESTPKGS")).
@@ -103,26 +109,40 @@ func (tests *UnitTests) CompileDockerfile(output *dockerfile.Output) error {
103109

104110
output.Stage(tests.Name()).
105111
From("scratch").
106-
Step(step.Copy(filepath.Join("/src", tests.packagePath, "coverage.txt"), fmt.Sprintf("/coverage-%s.txt", tests.Name())).From(testRun))
112+
Step(step.Copy(filepath.Join("/src", tests.packagePath, "coverage.txt"), fmt.Sprintf("/coverage-%s.txt", tests.Name())).From(testRunName))
107113

108-
testRunRaceStage := output.Stage(tests.Name() + "-race").
109-
Description("runs unit-tests with race detector").
114+
// unit-tests with json output
115+
116+
testRunJSONStage := output.Stage(testRunName + "-json").
117+
Description("runs unit-tests with JSON output").
110118
From("base")
111119

112-
for _, dockerStep := range tests.Docker.Steps {
113-
if dockerStep.Copy != nil {
114-
copyStep := step.Copy(dockerStep.Copy.Src, dockerStep.Copy.Dst)
115-
if dockerStep.Copy.From != "" {
116-
copyStep.From(dockerStep.Copy.From)
117-
}
120+
tests.addCopySteps(testRunJSONStage)
118121

119-
if dockerStep.Copy.Platform != "" {
120-
copyStep.Platform(dockerStep.Copy.Platform)
121-
}
122+
testRunJSONStage.Step(workdir).
123+
Step(step.Arg("TESTPKGS")).
124+
Step(wrapAsInsecure(
125+
step.Script(
126+
fmt.Sprintf(
127+
`go test -json -covermode=atomic -coverprofile=coverage.txt -coverpkg=${TESTPKGS} -count 1 %s${TESTPKGS} > test-results.json`,
128+
extraArgs),
129+
).
130+
MountCache(filepath.Join(tests.meta.CachePath, "go-build")).
131+
MountCache(filepath.Join(tests.meta.GoPath, "pkg")).
132+
MountCache("/tmp")))
122133

123-
testRunRaceStage.Step(copyStep)
124-
}
125-
}
134+
output.Stage(tests.Name() + "-json").
135+
From("scratch").
136+
Step(step.Copy(filepath.Join("/src", tests.packagePath, "coverage.txt"), fmt.Sprintf("/coverage-%s.txt", tests.Name())).From(testRunName + "-json")).
137+
Step(step.Copy(filepath.Join("/src", tests.packagePath, "test-results.json"), fmt.Sprintf("/test-results-%s.json", tests.Name())).From(testRunName + "-json"))
138+
139+
// unit-tests with race
140+
141+
testRunRaceStage := output.Stage(tests.Name() + "-race").
142+
Description("runs unit-tests with race detector").
143+
From("base")
144+
145+
tests.addCopySteps(testRunRaceStage)
126146

127147
testRunRaceStage.Step(workdir).
128148
Step(step.Arg("TESTPKGS")).
@@ -157,6 +177,11 @@ func (tests *UnitTests) CompileMakefile(output *makefile.Output) error {
157177
Script("@$(MAKE) local-$@ DEST=$(ARTIFACTS)" + scriptExtraArgs).
158178
Phony()
159179

180+
output.Target(tests.Name() + "-json").
181+
Description("Performs unit tests with JSON output").
182+
Script("@$(MAKE) local-$@ DEST=$(ARTIFACTS)" + scriptExtraArgs).
183+
Phony()
184+
160185
output.Target(tests.Name() + "-race").
161186
Description("Performs unit tests with race detection enabled.").
162187
Script("@$(MAKE) target-$@" + scriptExtraArgs).
@@ -182,8 +207,24 @@ func (tests *UnitTests) CompileDrone(output *drone.Output) error {
182207
func (tests *UnitTests) CompileGitHubWorkflow(output *ghworkflow.Output) error {
183208
output.AddStep(
184209
"default",
185-
ghworkflow.Step(tests.Name()).SetMakeStep(tests.Name()),
186-
ghworkflow.Step(tests.Name()+"-race").SetMakeStep(tests.Name()+"-race"),
210+
ghworkflow.Step(tests.Name()).
211+
SetMakeStep(tests.Name()+"-json"),
212+
)
213+
214+
resultsStep := ghworkflow.Step(tests.Name()+"-results").
215+
SetUses("robherley/go-test-action@v0").
216+
SetWith("fromJSONFile", filepath.Join(tests.meta.ArtifactsPath, "test-results-"+tests.Name()+".json")).
217+
SetWith("omit", "untested")
218+
219+
if err := resultsStep.SetConditions("always"); err != nil {
220+
return err
221+
}
222+
223+
output.AddStep(
224+
"default",
225+
resultsStep,
226+
ghworkflow.Step(tests.Name()+"-race").
227+
SetMakeStep(tests.Name()+"-race"),
187228
)
188229

189230
return nil

0 commit comments

Comments
 (0)