-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add gotest target to mage catalog (#91)
* feat: Add gotest target to mage catalog Signed-off-by: Mikołaj Baranowski <mikolajb@gmail.com>
- Loading branch information
Showing
3 changed files
with
79 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package gotest | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"dagger.io/dagger" | ||
"github.com/magefile/mage/mg" | ||
|
||
"github.com/mesosphere/daggers/catalog/golang" | ||
"github.com/mesosphere/daggers/daggers" | ||
"github.com/mesosphere/daggers/daggers/containers" | ||
) | ||
|
||
const ( | ||
// EnvGowork env variable name for go.work. | ||
EnvGowork = "GOWORK" | ||
// EnvGoPrivate env variable name for GOPRIVATE. | ||
EnvGoPrivate = "GOPRIVATE" | ||
) | ||
|
||
// Gounit runs unit tests. | ||
func Gounit(ctx context.Context) error { | ||
verbose := mg.Verbose() || mg.Debug() | ||
|
||
runtime, err := daggers.NewRuntime(ctx, daggers.WithVerbose(verbose)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// golang container customizer options | ||
customizers := golang.WithContainerCustomizers( | ||
containers.WithGithubAuth(ctx), | ||
containers.WithEnvVariables(map[string]string{ | ||
EnvGowork: "off", | ||
EnvGoPrivate: os.Getenv(EnvGoPrivate), | ||
}), | ||
) | ||
|
||
// create a golang container | ||
container, err := golang.GetContainer(ctx, runtime, customizers) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// execute the unit tests | ||
return runUnitTests(ctx, container) | ||
} | ||
|
||
// runUnitTests runs the unit tests in the container and exports the test results to .output directory. | ||
func runUnitTests(ctx context.Context, container *dagger.Container) error { | ||
testContainer := container. | ||
WithExec([]string{"test", "-v", "-race", "-coverprofile", "coverage.txt", "-covermode", "atomic", "./..."}). | ||
WithExec([]string{"tool", "cover", "-html=coverage.txt", "-o", "coverage.html"}) | ||
|
||
_, err := testContainer.ExitCode(ctx) // execute all steps and return the exit code | ||
if err != nil { | ||
return err | ||
} | ||
|
||
srcDir := testContainer.Directory("/src") | ||
|
||
if _, err := srcDir.File("coverage.txt").Export(ctx, ".reports/coverage.txt"); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := srcDir.File("coverage.html").Export(ctx, ".reports/coverage.html"); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters