golang

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package golang provides utilities for the Go programming language.

Index

Constants

View Source
const RunQuarantinedTestsEnvVar = "RUN_QUARANTINED_TESTS"

RunQuarantinedTestsEnvVar is the environment variable that controls whether quarantined tests are run.

Variables

View Source
var (
	// ErrTestNotFound is returned when a test is not found in the go code.
	ErrTestNotFound = errors.New("test not found")
	// ErrPackageNotFound is returned when a package is not found in the go code.
	ErrPackageNotFound = errors.New("package not found")
)

Absolute path to root directory -> PackageInfo

Functions

func WriteQuarantineResultsToFiles

func WriteQuarantineResultsToFiles(l zerolog.Logger, results QuarantineResults) error

WriteQuarantineResultsToFiles writes successfully quarantined tests to the file system.

Types

type PackageInfo

type PackageInfo struct {
	ImportPath   string   // Package import path (e.g., "github.com/user/repo/pkg")
	Name         string   // Package name
	Dir          string   // Directory containing the package
	GoFiles      []string // .go source files
	TestGoFiles  []string // _test.go files
	XTestGoFiles []string // _test.go files with different package names
	Module       string   // Module path
	IsCommand    bool     // True if this is a main package
}

PackageInfo contains comprehensive information about a Go package

func (*PackageInfo) String

func (p *PackageInfo) String() string

type PackagesInfo

type PackagesInfo struct {
	Packages map[string]PackageInfo
}

PackagesInfo contains all the packages found in a Go project.

func Packages

func Packages(l zerolog.Logger, rootDir string, buildFlags []string) (*PackagesInfo, error)

Packages finds all Go packages in the given Go project directory and subdirectories. This includes packages of nested Go projects by discovering all go.mod files and loading packages from each module root. buildFlags are passed to the go command when loading packages, e.g. []string{"-tags", "build_tag"}

func (*PackagesInfo) Get

func (p *PackagesInfo) Get(importPath string) (PackageInfo, error)

Get returns the PackageInfo for the given import path. Note that the import path is the full import path, not just the package name.

func (*PackagesInfo) String

func (p *PackagesInfo) String() string

type QuarantineOption

type QuarantineOption func(*quarantineOptions)

QuarantineOption is a function that can be used to configure the quarantine process.

func WithBuildFlags

func WithBuildFlags(buildFlags []string) QuarantineOption

WithBuildFlags sets the build flags to use when loading packages.

type QuarantinePackageResults

type QuarantinePackageResults struct {
	Package   string            // Import path of the Go package (redundant, but kept for handy access)
	Successes []QuarantinedFile // Every file where we found and quarantined tests
	Failures  []string          // Names of the test functions that were not able to be quarantined
}

QuarantinePackageResults describes the result of quarantining a list of tests in a package.

func (QuarantinePackageResults) SuccessfulTestsCount

func (q QuarantinePackageResults) SuccessfulTestsCount() int

SuccessfulTestsCount returns the number of tests that were successfully quarantined.

type QuarantineResults

type QuarantineResults map[string]QuarantinePackageResults

QuarantineResults describes the result of quarantining multiple packages. The key is the import path of the package, and the value is the result of quarantining that package.

func QuarantineTests

func QuarantineTests(
	l zerolog.Logger,
	repoPath string,
	quarantineTargets []QuarantineTarget,
	options ...QuarantineOption,
) (QuarantineResults, error)

QuarantineTests looks through a Go project to find and quarantine any tests that match the given targets. It returns a list of results for each target, including whether it was able to be quarantined, and the modified source code to quarantine the test. The modified source code is returned so that it can be committed to the repository. You must do something with it, as the code is not edited or committed by this function.

Tests quarantined by this process will use t.Skip() to skip the test, unless the environment variable RUN_QUARANTINED_TESTS is set to "true".

func (QuarantineResults) Markdown

func (q QuarantineResults) Markdown() string

Markdown returns a Markdown representation of the quarantine results. Good for a PR description.

func (QuarantineResults) String

func (q QuarantineResults) String() string

String returns a string representation of the quarantine results. Good for debugging and logging.

type QuarantineTarget

type QuarantineTarget struct {
	Package string   // Import path of the Go package
	Tests   []string // Names of the test functions in the package to quarantine
}

QuarantineTarget describes a package and a list of test functions to quarantine.

type QuarantinedFile

type QuarantinedFile struct {
	Package            string            // Import path of the Go package (redundant, but kept for handy access)
	File               string            // Relative path to the file where the tests were found and quarantined (if any)
	FileAbs            string            // Absolute path to the file where the tests were found and quarantined on the local filesystem (if any)
	Tests              []QuarantinedTest // All the test functions successfully quarantined in this file
	ModifiedSourceCode string            // Modified source code to quarantine the tests (if any)
}

QuarantinedFile describes the outputs of successfully quarantining a list of tests in a package in a single file.

func (QuarantinedFile) TestNames

func (q QuarantinedFile) TestNames() []string

TestNames returns the names of the test functions that were quarantined in this file.

type QuarantinedTest

type QuarantinedTest struct {
	Name         string // Name of the test function that was quarantined
	OriginalLine int    // Line number of the test function that was quarantined
	ModifiedLine int    // Line number of the test function that was quarantined after modification of the file
}

QuarantinedTest describes a test function that was quarantined.

Jump to

Keyboard shortcuts

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