factorytest

package
v1.135.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 21 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(t *testing.T, opts Opts) (*factory.Factory, *bytes.Buffer, *bytes.Buffer)

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

func StubBackend(client backend.Client) func(string) (backend.Client, error)

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

func UseBackend(f *factory.Factory, client backend.Client)

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.

func UseProfiles added in v1.32.0

func UseProfiles(f *factory.Factory, store *profiles.Store)

UseProfiles wires f.Profiles to return the given store. This lets tests inject a pre-populated store without touching the real filesystem.

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.

Jump to

Keyboard shortcuts

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