testutil

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2021 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package testutil provides helpers for unit tests.

Index

Constants

This section is empty.

Variables

View Source
var Date = time.Date(2021, time.June, 15, 23, 0, 0, 0, time.UTC)

Date is a consistent date object used by all tests.

View Source
var Err = errors.New("test error")

Err represents a generic error.

Functions

func Args added in v0.32.0

func Args(args string) []string

Args is a simple wrapper function designed to accept a CLI command (including flags) and return it as a slice for consumption by app.Run().

NOTE: One test file (TestBigQueryCreate) passes RSA content inline into the args string which means it has to escape the double quotes (used to infer the content should be considered a single argument) with a backtick. This causes problems when trying to split the args string by a space (as the RSA content has spaces) and so we need to be able to identify when backticks are used and ensure the backtick argument is considered a single argument (i.e. don't incorrectly split by the spaces within the RSA content when converting the arg string into a slice).

The logic checks for backticks, and then replaces the content that is surrounded by backticks with --- and then splits the resulting string by spaces. Afterwards if there was a backtick matched, then we re-insert the backticked content into the slice where --- is found.

func AssertBool

func AssertBool(t *testing.T, want, have bool)

AssertBool fatals a test if the parameters aren't equal.

func AssertEqual added in v0.9.0

func AssertEqual(t *testing.T, want, have interface{})

AssertEqual fatals a test if the parameters aren't equal.

func AssertErrorContains

func AssertErrorContains(t *testing.T, err error, target string)

AssertErrorContains fatals a test if the error's Error string doesn't contain target. As a special case, if target is the empty string, we assume the error should be nil.

func AssertNoError

func AssertNoError(t *testing.T, err error)

AssertNoError fatals a test if the error is not nil.

func AssertPathContentFlag added in v0.36.0

func AssertPathContentFlag(flag string, wantError string, args []string, fixture string, content string, t *testing.T)

AssertPathContentFlag errors a test scenario if the given flag value hasn't been parsed as expected.

Example: Some flags will internally be passed to `cmd.Content` to acquire the value. If passed a file path, then we expect the testdata/<fixture> to have been read, otherwise we expect the given flag value to have been used.

func AssertRemediationErrorContains added in v0.8.0

func AssertRemediationErrorContains(t *testing.T, err error, target string)

AssertRemediationErrorContains fatals a test if the error's RemediationError remediation string doesn't contain target. As a special case, if target is the empty string, we assume the error should be nil.

func AssertString

func AssertString(t *testing.T, want, have string)

AssertString fatals a test if the parameters aren't equal.

func AssertStringContains

func AssertStringContains(t *testing.T, s, substr string)

AssertStringContains fatals a test if the string doesn't contain a substring.

func AssertStringDoesntContain added in v0.37.0

func AssertStringDoesntContain(t *testing.T, s, substr string)

AssertStringDoesntContain fatals a test if the string does contain a substring.

func CloneVersionError added in v0.31.0

func CloneVersionError(i *fastly.CloneVersionInput) (*fastly.Version, error)

CloneVersionError returns a generic error message when attempting to clone a service version.

func CloneVersionResult added in v0.31.0

func CloneVersionResult(version int) func(i *fastly.CloneVersionInput) (*fastly.Version, error)

CloneVersionResult returns a function which returns a specific cloned version.

func CopyFile added in v0.33.0

func CopyFile(t *testing.T, fromFilename, toFilename string)

CopyFile copies a referenced file to a new location.

func ListVersions added in v0.31.0

func ListVersions(i *fastly.ListVersionsInput) ([]*fastly.Version, error)

ListVersions returns a list of service versions in different states.

The first element is active, the second is locked, the third is editable.

NOTE: consult the entire test suite before adding any new entries to the returned type as the tests currently use testutil.CloneVersionResult() as a way of making the test output and expectations as accurate as possible.

func ListVersionsError added in v0.31.0

func ListVersionsError(i *fastly.ListVersionsInput) ([]*fastly.Version, error)

ListVersionsError returns a generic error message when attempting to list service versions.

func MakeTempFile added in v0.21.0

func MakeTempFile(t *testing.T, contents string) string

MakeTempFile creates a tempfile with the given contents and returns its path

func MustParseTimeRFC3339

func MustParseTimeRFC3339(s string) *time.Time

MustParseTimeRFC3339 is a small helper to initialize time constants.

func NewEnv added in v0.33.0

func NewEnv(opts EnvOpts) (rootdir string)

NewEnv creates a new test environment and returns the root directory.

func NewRunOpts added in v0.33.0

func NewRunOpts(args []string, stdout io.Writer) app.RunOpts

NewRunOpts returns a struct that can be used to populate a call to app.Run() while the majority of fields will be pre-populated and only those fields commonly changed for testing purposes will need to be provided.

Types

type EnvOpts added in v0.33.0

type EnvOpts struct {
	T     *testing.T
	Copy  []FileIO // .Src expected to be file path
	Write []FileIO // .Src expected to be file content
	Exec  []string // e.g. []string{"npm", "install"}
}

EnvOpts represents configuration when creating a new environment.

type FileIO added in v0.33.0

type FileIO struct {
	Src string // path to a file inside ./testdata/ OR file content
	Dst string // path to a file relative to test environment's root directory
}

FileIO represents a source file and a destination.

type LogWriter added in v0.34.0

type LogWriter struct{ T *testing.T }

LogWriter is used to debug issues with our tests.

func (LogWriter) Write added in v0.34.0

func (w LogWriter) Write(p []byte) (int, error)

type TestScenario added in v0.32.0

type TestScenario struct {
	API         mock.API
	Args        []string
	Name        string
	WantError   string
	WantOutput  string
	WantOutputs []string
}

TestScenario represents a standard test case to be validated.

Jump to

Keyboard shortcuts

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