mockenv

package
v1.20.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package mockenv contains facilities for unit testing commands

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DevPlumbing added in v1.15.0

type DevPlumbing struct {
	// Plumbing is the underlying plumbing used by this DevPlumbing.
	git.Plumbing

	// when set to true, silence standard error output for any operations.
	SilenceStderr bool

	// URLMap is the map that contains the mapping that URLs are mapped via.
	// Keys correspond to URLs passed to this plumbing.
	// Values take the form of url passed to the underlying plumbing.
	URLMap map[string]string
}

DevPlumbing is a version of a git.Plumbing that runs all URLs via a translation mapping and can optionally silence standard error output. It otherwise wraps an underlying plumbing that is used for all operations.

It is intended for use during for testing. It should be used in git operations that would otherwise require a real remote repository.

func (DevPlumbing) Backward added in v1.15.0

func (dp DevPlumbing) Backward(url string) string

Backward maps a URL passed to the underlying plumbing into a URL to this plumbing. When the url does not exist in the mapping, calls panic().

func (DevPlumbing) Clone added in v1.15.0

func (dp DevPlumbing) Clone(stream stream.IOStream, remoteURI, clonePath string, extraargs ...string) error

Clone translates remoteURI and calls Clone on the underlying Plumbing.

func (DevPlumbing) Fetch added in v1.15.0

func (dp DevPlumbing) Fetch(stream stream.IOStream, clonePath string, cache any) (err error)

Fetch called Fetch on the underlying Plumbing

func (DevPlumbing) Forward added in v1.15.0

func (dp DevPlumbing) Forward(url string) string

Forward maps a URL passed to this plumbing into a URL to the underlying plumbing. When the url does not exist in the mapping, calls panic().

func (DevPlumbing) GetCanonicalRemote added in v1.15.0

func (dp DevPlumbing) GetCanonicalRemote(clonePath string, repoObject any) (name string, urls []string, err error)

GetCanonicalRemote calls GetCanonicalRemote() on the underlying Plumbing and translates all returned urls.

func (DevPlumbing) GetRemotes added in v1.15.0

func (dp DevPlumbing) GetRemotes(clonePath string, repoObject any) (remotes map[string][]string, err error)

GetRemotes calls GetRemotes() on the underlying Plumbing and translates the returned URLs.

func (DevPlumbing) Pull added in v1.15.0

func (dp DevPlumbing) Pull(stream stream.IOStream, clonePath string, cache any) (err error)

Fetch calls Pull on the underlying Plumbing

func (DevPlumbing) SetRemoteURLs added in v1.15.0

func (dp DevPlumbing) SetRemoteURLs(clonePath string, repoObject any, name string, urls []string) (err error)

SetRemoteURLs translates urls and calls SetRemoteURLs() on the underlying Plumbing.

type MockEnv

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

MockEnv represents a new environment that can be used for testing ggman commands.

The mocked environment creates a temporary folder which can be used to hold repositories. In order to mock a certain local state, repositories can be installed into this folder using the Register() and Install() commands.

func NewMockEnv

func NewMockEnv(t *testing.T) *MockEnv

NewMockEnv creates a new MockEnv for testing ggman programs.

func (*MockEnv) AssertOutput

func (mock *MockEnv) AssertOutput(t TestingT, prefix, got string, wants ...string)

AssertOutput asserts that the standard error or output returned by Run() is equal to one of wants. If this is not the case, calls TestingT.Errorf() with an error message relating to the last want.

For consistency across runs, strings of the form `${GGROOT a b c}` in want are resolved into an absolute path. Furthermore when `${}` is surrounded by "s, (e.g. "${GGROOT a b c}"), go quotes the string. When text is instead surrounded by “s`s, (e.g. `${GGROOT a b c}`) shell escapes the string.

Context should be additional information to be prefixed for the error message.

func (*MockEnv) Clone added in v1.13.0

func (mock *MockEnv) Clone(remote string, path ...string) (clonePath string)

Clone is like Install, but calls Register(remote) beforehand. Returns the return value of Install.

This function is untested because Register and Install are tested.

func (*MockEnv) Install

func (mock *MockEnv) Install(remote string, path ...string) string

Install installs the provided remote into the provided path. Returns the path the repository has been installed into. Assumes that the remote has been registered.

When the remote has not been registered, consider using Install instead.

If something goes wrong, calls panic().

func (*MockEnv) Register

func (mock *MockEnv) Register(remotes ...string) (repo *git.Repository)

Register registers a new remote repository with the provided urls. All remote urls point to the same path. Returns a reference to the remote repository.

Remotes must not have been registered before, or panic() will be called.

The purpose of registering a remote is that it does not place a requirement for external services to be alive during testing. Calls to clone or fetch the provided repository will instead of talking to the remote talk to this dummy repository instead.

func (MockEnv) Resolve added in v1.14.0

func (mock MockEnv) Resolve(path ...string) string

Resolve resolves a local path within this environment

func (*MockEnv) Run

func (mock *MockEnv) Run(command ggman.Command, workdir string, stdin string, argv ...string) (code uint8, stdout, stderr string)

Run runs the command with the provided arguments. It afterwards resets the concrete value stored in command to it's zero value.

The arguments should include the name of the command. The command is provided the given string as standard input.

Commands are not executed on the real system; instead they are executed within the sandboxed environment. In particular all interactions with remote git repositories are intercepted, see the Register() method for details.

Run returns the exit code of the provided command, along with standard output and standard error.

type TestingT

type TestingT interface {
	Errorf(format string, args ...any)
}

TestingT is an interface around TestingT

Jump to

Keyboard shortcuts

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