golang

package
v1.5.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

package golang provides util functions for managing Go project with Go.

Index

Constants

View Source
const (
	IntegrationTestPkg          = "./integrationtests"
	TestBinDir                  = "./target/tests/bin/"
	UnitTestCoverDir            = "./target/tests/cover/unit/"
	UnitTestCoverProfile        = "./target/tests/cover/unit/cover.txt"
	IntegrationTestCoverDir     = "./target/tests/cover/int/"
	IntegrationTestCoverProfile = "./target/tests/cover/int/cover.txt"
	CombinedCoverProfile        = "./target/tests/cover/combined/cover.txt"
)

Default values used by mageutil/golang/target.Go targets.

View Source
const BinDir = "./target/bin/"

BinDir is base directory for build outputs.

Variables

View Source
var DefaultBuildMatrix = BuildMatrix{
	{OS: "linux", Arch: "amd64"},
	{OS: "linux", Arch: "arm64"},
	{OS: "darwin", Arch: "amd64"},
	{OS: "darwin", Arch: "arm64"},
}

DefaultBuildMatrix defines subset of cross-compile targets supported by Go.

Functions

func CreateCoverProfile

func CreateCoverProfile(ctx context.Context, output string, inputDirs ...string) error

CreateCoverProfile creates combined coverage profile in text format.

func Go

func Go(ctx context.Context, args ...string) error

Go is shorthand for go executable provided by system.

func GoWith

func GoWith(ctx context.Context, env map[string]string, args ...string) error

GoWith is shorthand for go executable provided by system.

func IntegrationTest

func IntegrationTest(ctx context.Context, name, testPkg, coverDir string, runArgs ...string) error

IntegrationTest executes integration tests in 4 phases:

  1. Build application binary with coverage collection support.
  2. Start application binary in background.
  3. Execute integration tests.
  4. Send SIGINT to application and wait for it to exit.

NOTE: RunIntegrationTests model is now preferred and used by target since it gives more control to test code.

func IntegrationTestRunner added in v1.4.1

func IntegrationTestRunner(ctx context.Context, name, coverDir string, testFn func(ctx context.Context) error, runArgs ...string) error

IntegrationTestRunner executes integration tests in 4 phases:

  1. Build application binary with coverage collection support.
  2. Start application binary in background.
  3. Execute testFn.
  4. Send SIGINT to application and wait for it to exit.

For example usage see golang.IntegrationTest function.

NOTE: RunIntegrationTests model is now preferred and used by target since it gives more control to test code.

func ListPackages

func ListPackages(ctx context.Context, target string) ([]string, error)

ListPackages lists all packages in given target.

func RunIntegrationTests

func RunIntegrationTests(ctx context.Context, integrationTestPkg string) error

RunIntegrationTests runs tests inside given package with integration tag. To prevent caching -count=1 argument is also provided.

func SHA256Sum

func SHA256Sum(input, output string) error

SHA256Sum calculates sum for input file and stores it in output file. Output should be compatible with sha256sum program.

func StartAppForIntegrationTests

func StartAppForIntegrationTests(ctx context.Context, bin, coverDir string, args ...string) (stop func() error, err error)

StartAppForIntegrationTests starts application for integration testing in background.

func Tidy

func Tidy(ctx context.Context) error

Tidy runs go mod tidy.

func TidyAndVerify

func TidyAndVerify(ctx context.Context) error

TidyAndVerify runs go mod tidy and verifies that there are no changes to go.mod or go.sum. This is useful in CI/CD pipelines to validate that dependencies match go.mod.

func UnitTest

func UnitTest(ctx context.Context, coverDir string) error

UnitTest runs all tests and collects coverage in coverDir.

Types

type BuildInfo

type BuildInfo struct {
	BinPath string
	GOOS    string
	GOARCH  string
}

BuildInfo contains relevant information about produced binary.

func Build

func Build(ctx context.Context, target string, buildArgs ...string) (BuildInfo, error)

Build builds binary which is created under ./target/bin/{GOOS}/{GOARCH}/.

func BuildForPlatform

func BuildForPlatform(ctx context.Context, goos, goarch, target string, buildArgs ...string) (BuildInfo, error)

BuildForPlatform builds binary for wanted architecture and os.

func BuildForPlatformWith

func BuildForPlatformWith(ctx context.Context, env map[string]string, goos, goarch, target string, buildArgs ...string) (BuildInfo, error)

BuildForPlatform injects env, builds binary for wanted architecture and os.

func BuildForTesting

func BuildForTesting(ctx context.Context, target string, raceDetection bool, binDir string) (BuildInfo, error)

BuildForTesting builds binary that is instrumented for coverage collection and race detection.

func BuildFromMatrixWithSHA

func BuildFromMatrixWithSHA(ctx context.Context, env map[string]string, matrix BuildMatrix, target string, buildArgs ...string) ([]BuildInfo, error)

BuildFromMatrixWithSHA is a higher level build utility function doing cross compilation with sha calculation.

func BuildWith

func BuildWith(ctx context.Context, env map[string]string, target string, buildArgs ...string) (BuildInfo, error)

BuildWith injects given env and builds binary which is created under ./target/bin/{GOOS}/{GOARCH}/.

func WithSHA

func WithSHA(info BuildInfo, err error) (BuildInfo, error)

WithSHA is a wrapper for build functions that adds SHA256Sum calculation.

type BuildMatrix

type BuildMatrix []BuildPlatform

type BuildPlatform

type BuildPlatform struct{ OS, Arch string }

Directories

Path Synopsis
Package target exposes Go targets that can be imported in magefile using [import syntax].
Package target exposes Go targets that can be imported in magefile using [import syntax].

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL