package sample

import "golang.org/x/pkgsite/internal/testing/sample"

Package sample provides functionality for generating sample values of the types contained in the internal package.

Index

Variables

var (
	ModulePath                = "github.com/valid/module_name"
	RepositoryURL             = "https://github.com/valid/module_name"
	VersionString             = "v1.0.0"
	CommitTime                = NowTruncated()
	LicenseType               = "MIT"
	LicenseFilePath           = "LICENSE"
	NonRedistributableLicense = &licenses.License{
		Metadata: &licenses.Metadata{
			FilePath: "NONREDIST_LICENSE",
			Types:    []string{"UNKNOWN"},
		},
		Contents: []byte(`unknown`),
	}
	PackageName    = "foo"
	Suffix         = "foo"
	PackagePath    = path.Join(ModulePath, Suffix)
	V1Path         = PackagePath
	ReadmeFilePath = "README.md"
	ReadmeContents = "readme"
	GOOS           = internal.All
	GOARCH         = internal.All
	Doc            = Documentation(GOOS, GOARCH, DocContents)
	API            = []*internal.Symbol{
		Constant,
		Variable,
		Function,
		Type,
	}
	DocContents = `
		// Package p is a package.
		//
		//
		// Links
		//
		// - pkg.go.dev, https://pkg.go.dev
 		package p
		var V int
	`
	Constant = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:     "Constant",
			Synopsis: "const Constant",
			Section:  internal.SymbolSectionConstants,
			Kind:     internal.SymbolKindConstant,
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
	}
	Variable = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:     "Variable",
			Synopsis: "var Variable",
			Section:  internal.SymbolSectionVariables,
			Kind:     internal.SymbolKindVariable,
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
	}
	Function = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:     "Function",
			Synopsis: "func Function() error",
			Section:  internal.SymbolSectionFunctions,
			Kind:     internal.SymbolKindFunction,
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
	}
	FunctionNew = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:       "New",
			Synopsis:   "func New() *Type",
			Section:    internal.SymbolSectionTypes,
			Kind:       internal.SymbolKindFunction,
			ParentName: "Type",
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
	}
	Type = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:     "Type",
			Synopsis: "type Type struct",
			Section:  internal.SymbolSectionTypes,
			Kind:     internal.SymbolKindType,
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
		Children: []*internal.SymbolMeta{
			&FunctionNew.SymbolMeta,
			&Field,
			&Method,
		},
	}
	Field = internal.SymbolMeta{
		Name:       "Type.Field",
		Synopsis:   "field",
		Section:    internal.SymbolSectionTypes,
		Kind:       internal.SymbolKindField,
		ParentName: "Type",
	}
	Method = internal.SymbolMeta{
		Name:       "Type.Method",
		Synopsis:   "method",
		Section:    internal.SymbolSectionTypes,
		Kind:       internal.SymbolKindMethod,
		ParentName: "Type",
	}
)

These sample values can be used to construct test cases.

var LicenseCmpOpts = []cmp.Option{
	cmp.Comparer(coveragePercentEqual),
	cmpopts.IgnoreFields(licensecheck.Match{}, "Start", "End"),
}

LicenseCmpOpts are options to use when comparing licenses with the cmp package.

Functions

func AddLicense

func AddLicense(m *internal.Module, lic *licenses.License)

func AddPackage

func AddPackage(m *internal.Module, pkg *internal.Unit) *internal.Module

func AddUnit

func AddUnit(m *internal.Module, u *internal.Unit)

func DefaultModule

func DefaultModule() *internal.Module

func DefaultVersionMap

func DefaultVersionMap() *internal.VersionMap

func Documentation

func Documentation(goos, goarch, fileContents string) *internal.Documentation

Documentation returns a Documentation value for the given Go source. It panics if there are errors parsing or encoding the source.

func Imports

func Imports() []string

func LicenseMetadata

func LicenseMetadata() []*licenses.Metadata

func Licenses

func Licenses() []*licenses.License

func Module

func Module(modulePath, version string, suffixes ...string) *internal.Module

Module creates a Module with the given path and version. The list of suffixes is used to create Units within the module.

func ModuleInfo

func ModuleInfo(modulePath, versionString string) *internal.ModuleInfo

func NowTruncated

func NowTruncated() time.Time

NowTruncated returns time.Now() truncated to Microsecond precision.

This makes it easier to work with timestamps in PostgreSQL, which have Microsecond precision:

https://www.postgresql.org/docs/9.1/datatype-datetime.html

func PackageMeta

func PackageMeta(fullPath string) *internal.PackageMeta

func ReplaceLicense

func ReplaceLicense(m *internal.Module, lic *licenses.License)

ReplaceLicense replaces all licenses having the same file path as lic with lic.

func UnitEmpty

func UnitEmpty(path, modulePath, version string) *internal.Unit

func UnitForModuleRoot

func UnitForModuleRoot(m *internal.ModuleInfo) *internal.Unit

func UnitForPackage

func UnitForPackage(path, modulePath, version, name string, isRedistributable bool) *internal.Unit

UnitForPackage constructs a unit with the given module path and suffix.

If modulePath is the standard library, the package path is the suffix, which must not be empty. Otherwise, the package path is the concatenation of modulePath and suffix.

The package name is last component of the package path.

func UnitMeta

func UnitMeta(path, modulePath, version, name string, moduleIsRedistributable bool) *internal.UnitMeta

Source Files

sample.go

Version
v0.0.0-20250218150137-224a1368cf02 (latest)
Published
Feb 18, 2025
Platform
linux/amd64
Imports
17 packages
Last checked
2 months ago

Tools for package owners.