mock

package
v0.0.0-...-b8d801a Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GenerateOption

type GenerateOption func(*GenerateOptions) error

GenerateOption modifies a GenerateOptions value and be used to configure Generate.

func WithArgs

func WithArgs(args ...any) GenerateOption

WithArgs applies each GenerateOption in the given slice. If any of the GenerateOptions return an error, WithArgs will return the error immediately. The args use the any type to be compatible with the subcommands package.

func WithDir

func WithDir(dir string) GenerateOption

WithDir sets the directory to run the build system's query tool.

func WithEnv

func WithEnv(env []string) GenerateOption

WithEnv sets the environment to use when invoking the build system's query tool.

func WithHeader

func WithHeader(header []byte) GenerateOption

WithHeader sets the header to insert at the start of each generated file.

func WithHeaderFile

func WithHeaderFile(headerFile string) GenerateOption

WithHeaderFile sets the header to insert at the start of each generated file to the contents of the given file.

func WithPrefixFileName

func WithPrefixFileName(prefix string) GenerateOption

WithPrefixFileName sets the prefix of the file name to write the generated output to. The suffix will be "vermock_gen.go" or "vermock_gen_test.go".

func WithTags

func WithTags(tags string) GenerateOption

WithTags sets the build tags to use when generating the mock files.

func WithWDFallback

func WithWDFallback() GenerateOption

WithWDFallback sets the directory to run the build system's query tool to the current working directory if Dir is empty.

type GenerateOptions

type GenerateOptions struct {
	// Header will be inserted at the start of each generated file.
	Header []byte

	// PrefixOutputFile is the prefix of the file name to write the generated
	// output to. The suffix will be "vermock_gen.go" or "vermock_gen_test.go".
	PrefixOutputFile string

	// Tags is a list of additional build tags to add to the generated file.
	Tags string

	// Dir is the directory to run the build system's query tool
	// that provides information about the packages.
	// If Dir is empty, the tool is run in the current directory.
	Dir string

	// Env is the environment to use when invoking the build system's query tool.
	// If Env is nil, the current environment is used.
	// As in os/exec's Cmd, only the last value in the slice for
	// each environment key is used.
	Env []string
}

GenerateOptions holds options for Generate.

type GenerateResult

type GenerateResult struct {
	// PkgPath is the package's PkgPath.
	PkgPath string
	// OutputPath is the path where the generated output should be written.
	// May be empty if there were errors.
	OutputPath string
	// Content is the gofmt'd source code that was generated. May be nil if
	// there were errors during generation.
	Content []byte
	// Errs is a slice of errors identified during generation.
	Errs []error
}

GenerateResult stores the result for a package from a call to Generate.

func Generate

func Generate(ctx context.Context, patterns []string, opts GenerateOptions) ([]GenerateResult, []error)

Generate generates a code file for each package matching the given patterns. The code file will contain mock implementations for each struct type in any file in the package that has the vermockstub build tag. As a consequence, the generated files will not be included in the package's build when using the vermockstub build tag. An implementation for each method of each interface type that the struct type embeds will be generated, unless an implementation already exists elsewhere in the package. The generated files will be named vermock_gen.go, with an optional prefix. The generated files will also include a go:generate comment that can be used to regenerate the file.

func (GenerateResult) Commit

func (gen GenerateResult) Commit() error

Commit writes the generated file to disk.

Jump to

Keyboard shortcuts

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