Skip to content
Gasv0.10.0

gas/config

config/configtest

import "github.com/gasmod/gas/config/configtest"

Package configtest holds MockConfig, a config.ConfigProvider for tests of code that reads configuration, and StaticProvider, a config.Provider that loads fixed values.

A MockConfig records each call, and calls the function in the field named after the method, when it is set:

m := &configtest.MockConfig{
GetFn: func(key string) any { return "db" },
}
svc := NewService(m)
// m.CallCount("Get") is how many times svc called Get

NewMockConfigWithValues sets every field to the methods of a real config.Config holding the given values, so Get, Find, Bind and the rest behave as they do in an app:

m, err := configtest.NewMockConfigWithValues(map[string]any{
"database": map[string]any{"host": "db"},
})

Each method appends a Call with its name and arguments to Calls, then calls its field. With the field nil, it returns zero values: nil, false, or a nil error. LoadProviderContext records the provider but not the context.

The methods may be called from several goroutines at once, as Calls is appended to under a lock. MockConfig.CallCount and MockConfig.Reset take the lock too, but reading Calls directly does not, so read it once the calls are done. Setting a field while a method runs is a data race.

NewMockConfigWithValues sets the values with config.Config.SetDefaults, so their keys are trimmed and lowercased, and two keys that become the same are an error. Nil values make an empty configuration. No provider loads, so the environment does not reach it. Any field may be replaced afterwards, and LoadProvider loads a provider into the configuration.

A StaticProvider gives a real config.Config fixed values, for a test of code that builds its own Config from providers, such as an app’s function that loads its configuration:

c := config.New(config.WithProvider(configtest.NewStaticProvider(map[string]any{
"database": map[string]any{"host": "db"},
})))
if err := c.Load(); err != nil {
return err
}

Each load returns the values, and the Config treats them as any provider’s: their keys are trimmed and lowercased, two keys that become the same fail the load, and a later provider overrides them. Errors name the provider static. Nil values load nothing.

The provider keeps the map it is given rather than copying it, so do not change the map while a load may run.

type Call struct {
Method string
Args []any
}

Call is one call of a MockConfig method.

type MockConfig struct {
SetDefaultFn func(key string, value any) error
SetDefaultsFn func(values any) error
SetFn func(key string, value any) error
BindFn func(dest any) error
GetFn func(key string) any
FindFn func(key string) (any, bool)
ValuesFn func() map[string]any
LoadProviderFn func(p config.Provider) error
LoadProviderContextFn func(ctx context.Context, p config.Provider) error
Calls []Call
// contains filtered or unexported fields
}

MockConfig is a config.ConfigProvider that records its calls, as described in the package documentation. Each method calls the field named after it, when it is set.

func NewMockConfigWithValues(values map[string]any) (*MockConfig, error)

NewMockConfigWithValues returns a MockConfig whose fields are the methods of a config.Config holding values, as described in the package documentation.

func (m *MockConfig) Bind(dest any) error

Bind records the call and delegates to BindFn if set.

func (m *MockConfig) CallCount(method string) int

CallCount returns the number of times the given method was called.

func (m *MockConfig) Find(key string) (value any, exist bool)

Find records the call and delegates to FindFn if set.

func (m *MockConfig) Get(key string) any

Get records the call and delegates to GetFn if set.

func (m *MockConfig) LoadProvider(p config.Provider) error

LoadProvider records the call and delegates to LoadProviderFn if set.

func (m *MockConfig) LoadProviderContext(ctx context.Context, p config.Provider) error

LoadProviderContext records the call, without ctx, and delegates to LoadProviderContextFn if set.

func (m *MockConfig) Reset()

Reset clears all recorded calls.

func (m *MockConfig) Set(key string, value any) error

Set records the call and delegates to SetFn if set.

func (m *MockConfig) SetDefault(key string, value any) error

SetDefault records the call and delegates to SetDefaultFn if set.

func (m *MockConfig) SetDefaults(values any) error

SetDefaults records the call and delegates to SetDefaultsFn if set.

func (m *MockConfig) Values() map[string]any

Values records the call and delegates to ValuesFn if set.

type StaticProvider struct {
// contains filtered or unexported fields
}

StaticProvider is a config.Provider that loads fixed values, as described in the package documentation.

func NewStaticProvider(values map[string]any) *StaticProvider

NewStaticProvider returns a StaticProvider that loads values. It keeps the map rather than copying it.

func (p *StaticProvider) Load() (map[string]any, error)

Load returns the values the provider was given.

func (p *StaticProvider) Name() string

Name returns “static”.