Documentation
¶
Overview ¶
Package factorytest builds a *factory.Factory for tests with sane, minimal defaults. It replaces the old factory.NewTestFactory god-helper with a thin layer: New(t) gives you a Factory and a buffers handle; individual fields you care about, you override directly on the returned Factory. This mirrors how cli/cli's tests build factories — a literal with a few field assignments — instead of routing every test through a 13-field options struct.
Index ¶
- func New(t *testing.T, opts Opts) (*factory.Factory, *bytes.Buffer, *bytes.Buffer)
- func StubBackend(client backend.Client) func(string) (backend.Client, error)
- func StubHTTPClient(hc factory.HTTPClient) func(string) (factory.HTTPClient, error)
- func UseBackend(f *factory.Factory, client backend.Client)
- func UseProfiles(f *factory.Factory, store *profiles.Store)
- type Opts
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New returns a Factory wired with capture buffers, fake git/keyring/ browser/editor, a 404 HTTP stub, and a config loader pointing at a temp dir. The returned (out, errOut) are the *bytes.Buffer instances underlying f.IOStreams.Out and f.IOStreams.ErrOut.
f.Backend / f.BackendWithOptions read f.HTTPClient at call time, so tests that swap the HTTP stub *after* New() automatically get a backend client wired to the new stub.
func StubBackend ¶
StubBackend returns a Backend func suitable for f.Backend assignment when a test wants every host to dial the same stub client. NOTE: most tests should call UseBackend(f, client) instead, which also wires f.BackendWithOptions — auth flows go through the latter and silently fail otherwise.
func StubHTTPClient ¶
func StubHTTPClient(hc factory.HTTPClient) func(string) (factory.HTTPClient, error)
StubHTTPClient returns an HTTPClient func suitable for f.HTTPClient assignment when a test wants every host to dial the same stub.
func UseBackend ¶
UseBackend wires both f.Backend and f.BackendWithOptions to return the same stub client. This matches the old NewTestFactory's BackendOverride behaviour, which silently routed both call paths to the override. Auth login/logout/refresh tests need this — they dial through BackendWithOptions for the initial probe.
Types ¶
type Opts ¶
type Opts struct {
ConfigDir string // optional; defaults to t.TempDir()
InitialConfig string // optional; written to <ConfigDir>/hosts.yml
// BackendType forces cloud-vs-server dispatch in the default
// Backend wiring. Useful when the hostname doesn't make routing
// obvious (e.g. testing cloud behaviour against git.example.com).
// Empty string → infer from hostname / hosts.yml as production does.
BackendType string
}
Opts narrows the options surface to the two pieces of setup that are inconvenient to do *after* New() returns: the config dir (because Config/UserConfig/Aliases all close over it) and the hosts.yml content (because it has to land on disk before the first f.Config() call). Everything else — IOStreams, GitRunner, Backend, HTTPClient, BaseURL, Now — is overridden on the returned Factory directly. The 11 invisible defaults of the old TestFactoryOpts are gone: tests that care about a field declare it.