Skip to content

Commit

Permalink
feat: Add gotest target to mage catalog (#91)
Browse files Browse the repository at this point in the history
* feat: Add gotest target to mage catalog

Signed-off-by: Mikołaj Baranowski <mikolajb@gmail.com>
  • Loading branch information
mikolajb committed Mar 31, 2023
1 parent 9849258 commit 8f27330
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 37 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ jobs:
go-version: '>=1.20.0'

- name: Run unit tests
run: ./hack/mage test:go
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./hack/mage test:gounit

- name: Install goveralls
run: go install github.com/mattn/goveralls@latest

- name: Send coverage
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: goveralls -coverprofile=.output/coverage.txt -service=github
run: goveralls -coverprofile=.reports/coverage.txt -service=github
72 changes: 72 additions & 0 deletions catalog/gotest/mage.go
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
}
38 changes: 3 additions & 35 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,9 @@
package main

import (
"context"

"github.com/magefile/mage/mg"

"github.com/mesosphere/daggers/catalog/golang"
"github.com/mesosphere/daggers/daggers"

// mage:import precommit
_ "github.com/mesosphere/daggers/catalog/precommit"
)

// Test is a collection of test targets.
//
//goland:noinspection GoUnusedExportedType // used by mage
type Test mg.Namespace

func (Test) Go(ctx context.Context) error {
runtime, err := daggers.NewRuntime(ctx, daggers.WithVerbose(true))

args := []string{"test", "-v", "-race", "-coverprofile", "coverage.txt", "-covermode", "atomic", "./..."}

_, dir, err := golang.RunCommand(
ctx,
runtime,
golang.WithArgs(args...),
golang.WithEnv(map[string]string{"GOPRIVATE": "github.com/mesosphere"}),
)
if err != nil {
return err
}

_, err = dir.File("coverage.txt").Export(ctx, ".output/coverage.txt")
if err != nil {
return err
}

return nil
}
// mage:import test
_ "github.com/mesosphere/daggers/catalog/gotest"
)

0 comments on commit 8f27330

Please sign in to comment.