@@ -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.
5673func (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 {
182207func (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