config

package
v3.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: BSD-3-Clause Imports: 28 Imported by: 0

README

Config

The config package provides mockery configuration semantics and behaviors. The RootConfig type defines the schema for the top-level .mockery.yml file.

Documentation

Overview

Package config defines the schemas and functionality of the .mockery.yml config files. This package is NOT meant to be used by external Go libraries. We expose the contents of this package purely for documentation purposes.

Do NOT import this package. We cannot guarantee backwards-compatibility of the methods herein.

Index

Constants

This section is empty.

Variables

View Source
var ErrInfiniteLoop = fmt.Errorf("infinite loop in template variables detected")

Functions

func NewDefaultKoanf

func NewDefaultKoanf(ctx context.Context) (*koanf.Koanf, error)

Types

type Config

type Config struct {
	All                   *bool          `koanf:"all" yaml:"all,omitempty"`
	Anchors               map[string]any `koanf:"_anchors" yaml:"_anchors,omitempty"`
	BuildTags             *string        `koanf:"build-tags" yaml:"build-tags,omitempty"`
	ConfigFile            *string        `koanf:"config" yaml:"config,omitempty"`
	Dir                   *string        `koanf:"dir" yaml:"dir,omitempty"`
	ExcludeSubpkgRegex    []string       `koanf:"exclude-subpkg-regex" yaml:"exclude-subpkg-regex,omitempty"`
	ExcludeInterfaceRegex *string        `koanf:"exclude-interface-regex" yaml:"exclude-interface-regex,omitempty"`
	FileName              *string        `koanf:"filename" yaml:"filename,omitempty"`
	// ForceFileWrite controls whether mockery will overwrite existing files when generating mocks. This is by default set to false.
	ForceFileWrite        *bool             `koanf:"force-file-write" yaml:"force-file-write,omitempty"`
	Formatter             *string           `koanf:"formatter" yaml:"formatter,omitempty"`
	FormatterOptions      *FormatterOptions `koanf:"formatter-options" yaml:"formatter-options,omitempty"`
	Generate              *bool             `koanf:"generate" yaml:"generate,omitempty"`
	IncludeAutoGenerated  *bool             `koanf:"include-auto-generated" yaml:"include-auto-generated,omitempty"`
	IncludeInterfaceRegex *string           `koanf:"include-interface-regex" yaml:"include-interface-regex,omitempty"`
	InPackage             *bool             `koanf:"inpackage" yaml:"inpackage,omitempty"`
	LogLevel              *string           `koanf:"log-level" yaml:"log-level,omitempty"`
	StructName            *string           `koanf:"structname" yaml:"structname,omitempty"`
	PkgName               *string           `koanf:"pkgname" yaml:"pkgname,omitempty"`
	Recursive             *bool             `koanf:"recursive" yaml:"recursive,omitempty"`
	// ReplaceType is a nested map of format map["package path"]["type name"]*ReplaceType
	ReplaceType map[string]map[string]*ReplaceType `koanf:"replace-type" yaml:"replace-type,omitempty"`
	// RequireTemplateSchemaExists sets whether mockery will fail if the specified
	// template did not have an associated JSON schema.
	RequireTemplateSchemaExists *bool          `koanf:"require-template-schema-exists" yaml:"require-template-schema-exists,omitempty"`
	Template                    *string        `koanf:"template" yaml:"template,omitempty"`
	TemplateData                map[string]any `koanf:"template-data" yaml:"template-data,omitempty"`
	// TemplateSchema is the URL of the template's JSON schema.
	TemplateSchema *string `koanf:"template-schema" yaml:"template-schema,omitempty"`
}

func ExtractDirectiveConfig added in v3.6.0

func ExtractDirectiveConfig(ctx context.Context, decl *ast.GenDecl) (*Config, error)

ExtractDirectiveConfig parses interface's documentation from a declaration node and extracts mockery's directive configuration.

Mockery directives are comments that start with "mockery:" and can appear multiple times in the interface's doc comments. All such comments are combined and interpreted as YAML configuration.

func (*Config) FilePath

func (c *Config) FilePath() string

func (*Config) GetReplacement

func (c *Config) GetReplacement(pkgPath string, typeName string) *ReplaceType

func (*Config) ParseTemplates

func (c *Config) ParseTemplates(
	ctx context.Context,

	ifaceFilePath string,
	ifaceName string,
	srcPkg *packages.Package,
) error

ParseTemplates parses various templated strings in the config struct into their fully defined values. This mutates the config object passed. An *Interface object can be supplied to satisfy template variables that need information about the original interface being mocked. If this argument is nil, interface-specific template variables will be set to the empty string. The srcPkg is also needed to satisfy template variables regarding the source package.

func (*Config) ShouldExcludeSubpkg

func (c *Config) ShouldExcludeSubpkg(pkgPath string) bool

type FormatterOptions added in v3.7.0

type FormatterOptions struct {
	GoImports *GoImports `koanf:"goimports" yaml:"goimports,omitempty"`
}

type GoImports added in v3.7.0

type GoImports struct {
	AllErrors   *bool   `koanf:"all-errors" yaml:"all-errors,omitempty"`
	Comments    *bool   `koanf:"comments" yaml:"comments,omitempty"`
	FormatOnly  *bool   `koanf:"format-only" yaml:"format-only,omitempty"`
	Fragment    *bool   `koanf:"fragment" yaml:"fragment,omitempty"`
	LocalPrefix *string `koanf:"local-prefix" yaml:"local-prefix,omitempty"`
	TabIndent   *bool   `koanf:"tab-indent" yaml:"tab-indent,omitempty"`
	TabWidth    *int    `koanf:"tab-width" yaml:"tab-width,omitempty"`
}

func (*GoImports) GetLocalPrefix added in v3.7.0

func (g *GoImports) GetLocalPrefix() string

func (*GoImports) Options added in v3.7.0

func (g *GoImports) Options() *imports.Options

type InterfaceConfig

type InterfaceConfig struct {
	Config  *Config   `koanf:"config" yaml:"config,omitempty"`
	Configs []*Config `koanf:"configs" yaml:"configs,omitempty"`
}

func NewInterfaceConfig

func NewInterfaceConfig() *InterfaceConfig

func (*InterfaceConfig) Initialize

func (c *InterfaceConfig) Initialize(ctx context.Context) error

type PackageConfig

type PackageConfig struct {
	Config     *Config                     `koanf:"config" yaml:"config,omitempty"`
	Interfaces map[string]*InterfaceConfig `koanf:"interfaces" yaml:"interfaces,omitempty"`
}

func NewPackageConfig

func NewPackageConfig() *PackageConfig

func (PackageConfig) GetInterfaceConfig

func (c PackageConfig) GetInterfaceConfig(ctx context.Context, interfaceName string, directiveConfig *Config) (*InterfaceConfig, error)

func (*PackageConfig) Initialize

func (c *PackageConfig) Initialize(ctx context.Context) error

func (PackageConfig) ShouldGenerateInterface

func (c PackageConfig) ShouldGenerateInterface(
	ctx context.Context,
	interfaceName string,
	ifaceConfig Config,
	hasDirectiveComment bool,
) (bool, error)

type ReplaceType

type ReplaceType struct {
	PkgPath  string `koanf:"pkg-path" yaml:"pkg-path,omitempty"`
	TypeName string `koanf:"type-name" yaml:"type-name,omitempty"`
}

type RootConfig

type RootConfig struct {
	Config   `koanf:",squash" yaml:",inline"`
	Packages map[string]*PackageConfig `koanf:"packages" yaml:"packages"`
	// contains filtered or unexported fields
}

func NewRootConfig

func NewRootConfig(
	ctx context.Context,
	flags *pflag.FlagSet,
) (*RootConfig, *koanf.Koanf, error)

func (*RootConfig) ConfigFileUsed

func (c *RootConfig) ConfigFileUsed() string

func (*RootConfig) GetPackageConfig

func (c *RootConfig) GetPackageConfig(ctx context.Context, pkgPath string) (*PackageConfig, error)

func (*RootConfig) GetPackages

func (c *RootConfig) GetPackages(ctx context.Context) ([]string, error)

GetPackages returns a list of the packages that are defined in the `packages` config section.

func (*RootConfig) Initialize

func (c *RootConfig) Initialize(ctx context.Context) error

type TemplateData

type TemplateData struct {
	// ConfigDir is the directory of where the mockery config file is located.
	ConfigDir string
	// InterfaceDir is the directory of the interface being mocked.
	InterfaceDir string
	// InterfaceDirRelative is the same as InterfaceDir, but made relative to mockery's current working directory.
	InterfaceDirRelative string
	// InterfaceFile is the filename of where the interface is defined.
	InterfaceFile string
	// InterfaceName is the name of the interface (duh).
	InterfaceName string
	// Mock is a parameter that takes the value of "Mock" if the interface is exported, and "mock" otherwise.
	Mock string
	// StructName is the configured name of the mock.
	StructName string
	// SrcPackageName is the name of the source package as defined by the `package [name]` in the source package.
	SrcPackageName string
	// SrcPackagePath is the fully qualified package path of the source package. e.g. "github.com/vektra/mockery/v3".
	SrcPackagePath string
	// Template is the value of the `template` parameter.
	Template string
}

TemplateData is the data sent to the template for the config file.

Jump to

Keyboard shortcuts

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