Documentation ¶
Overview ¶
package golang provides util functions for managing Go project with Go.
Index ¶
- Constants
- Variables
- func CreateCoverProfile(ctx context.Context, output string, inputDirs ...string) error
- func Go(ctx context.Context, args ...string) error
- func GoWith(ctx context.Context, env map[string]string, args ...string) error
- func IntegrationTest(ctx context.Context, name, testPkg, coverDir string, runArgs ...string) error
- func IntegrationTestRunner(ctx context.Context, name, coverDir string, ...) error
- func ListPackages(ctx context.Context, target string) ([]string, error)
- func RunIntegrationTests(ctx context.Context, integrationTestPkg string) error
- func SHA256Sum(input, output string) error
- func StartAppForIntegrationTests(ctx context.Context, bin, coverDir string, args ...string) (stop func() error, err error)
- func Tidy(ctx context.Context) error
- func TidyAndVerify(ctx context.Context) error
- func UnitTest(ctx context.Context, coverDir string) error
- type BuildInfo
- func Build(ctx context.Context, target string, buildArgs ...string) (BuildInfo, error)
- func BuildForPlatform(ctx context.Context, goos, goarch, target string, buildArgs ...string) (BuildInfo, error)
- func BuildForPlatformWith(ctx context.Context, env map[string]string, goos, goarch, target string, ...) (BuildInfo, error)
- func BuildForTesting(ctx context.Context, target string, raceDetection bool, binDir string) (BuildInfo, error)
- func BuildFromMatrixWithSHA(ctx context.Context, env map[string]string, matrix BuildMatrix, target string, ...) ([]BuildInfo, error)
- func BuildWith(ctx context.Context, env map[string]string, target string, buildArgs ...string) (BuildInfo, error)
- func WithSHA(info BuildInfo, err error) (BuildInfo, error)
- type BuildMatrix
- type BuildPlatform
Constants ¶
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.
const BinDir = "./target/bin/"
BinDir is base directory for build outputs.
Variables ¶
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 ¶
CreateCoverProfile creates combined coverage profile in text format.
func IntegrationTest ¶
IntegrationTest executes integration tests in 4 phases:
- Build application binary with coverage collection support.
- Start application binary in background.
- Execute integration tests.
- 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:
- Build application binary with coverage collection support.
- Start application binary in background.
- Execute testFn.
- 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 ¶
ListPackages lists all packages in given target.
func RunIntegrationTests ¶
RunIntegrationTests runs tests inside given package with integration tag. To prevent caching -count=1 argument is also provided.
func SHA256Sum ¶
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 TidyAndVerify ¶
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.
Types ¶
type BuildInfo ¶
BuildInfo contains relevant information about produced binary.
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.
type BuildMatrix ¶
type BuildMatrix []BuildPlatform
type BuildPlatform ¶
type BuildPlatform struct{ OS, Arch string }