runtime

package
v0.6.9-alpha.4 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2023 License: Apache-2.0 Imports: 33 Imported by: 4

Documentation

Index

Constants

View Source
const HTTP2_GOAWAY_CHECK = "http2: server sent GOAWAY and closed the connection"

Variables

This section is empty.

Functions

func IgnoreMissedCoverage

func IgnoreMissedCoverage()

IgnoreMissedCoverage causes any missed coverage information (for example when a function passed to RunMain calls os.Exit, for example) to be ignored. This function should be called before calling RunMain.

func Run

func Run(t *testing.T, p Params)

RunDir runs the tests in the given directory. All files in dir with a ".hls" are considered to be test files.

func RunMain

func RunMain(m TestingM, commands map[string]func() int) (exitCode int)

RunMain should be called within a TestMain function to allow subcommands to be run in the testscript context.

The commands map holds the set of command names, each with an associated run function which should return the code to pass to os.Exit. It's OK for a command function to exit itself, but this may result in loss of coverage information.

When Run is called, these commands will be available as testscript commands; note that these commands behave like commands run with the "exec" command: they set stdout and stderr, and can be run in the background by passing "&" as a final argument.

This function returns an exit code to pass to os.Exit, after calling m.Run.

func RunT

func RunT(t T, p Params)

RunT is like Run but uses an interface type instead of the concrete *testing.T type to make it possible to use testscript functionality outside of go test.

Types

type Config

type Config struct {
}

type Env

type Env struct {
	// WorkDir holds the path to the root directory of the
	// extracted files.
	WorkDir string
	// Vars holds the initial set environment variables that will be passed to the
	// testscript commands.
	Vars []string
	// Cd holds the initial current working directory.
	Cd string
	// Values holds a map of arbitrary values for use by custom
	// testscript commands. This enables Setup to pass arbitrary
	// values (not just strings) through to custom commands.
	Values map[interface{}]interface{}
	// contains filtered or unexported fields
}

Env holds the environment to use at the start of a test script invocation.

func (*Env) Defer

func (e *Env) Defer(f func())

Defer arranges for f to be called at the end of the test. If Defer is called multiple times, the defers are executed in reverse order (similar to Go's defer statement)

func (*Env) Getenv

func (e *Env) Getenv(key string) string

Getenv retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present.

func (*Env) Setenv

func (e *Env) Setenv(key, value string)

Setenv sets the value of the environment variable named by the key. It panics if key is invalid.

func (*Env) T

func (e *Env) T() T

T returns the t argument passed to the current test by the T.Run method. Note that if the tests were started by calling Run, the returned value will implement testing.TB. Note that, despite that, the underlying value will not be of type *testing.T because *testing.T does not implement T.

If Cleanup is called on the returned value, the function will run after any functions passed to Env.Defer.

type Params

type Params struct {
	// Mode is the top level operation mode for script
	// It can be "test" or "run" to control how scripts are isolated (or not)
	Mode string

	// Dir holds the name of the directory holding the scripts.
	// All files in the directory with a .hls suffix will be considered
	// as test scripts. By default the current directory is used.
	// Dir is interpreted relative to the current test directory.
	Dir string

	// Glob holds the patter to match, defaults to '*.hls'
	Glob string

	// Setup is called, if not nil, to complete any setup required
	// for a test. The WorkDir and Vars fields will have already
	// been initialized and all the files extracted into WorkDir,
	// and Cd will be the same as WorkDir.
	// The Setup function may modify Vars and Cd as it wishes.
	Setup func(*Env) error

	// Condition is called, if not nil, to determine whether a particular
	// condition is true. It's called only for conditions not in the
	// standard set, and may be nil.
	Condition func(cond string) (bool, error)

	// Cmds holds a map of commands available to the script.
	// It will only be consulted for commands not part of the standard set.
	Cmds map[string]func(ts *Script, neg int, args []string)

	// Funcs holds a map of functions available to the script.
	// These work like exec and use 'call' instead.
	// Use these to facilitate code coverage (exec does not capture this).
	Funcs map[string]func(ts *Script, args []string) error

	// TestWork specifies that working directories should be
	// left intact for later inspection.
	TestWork bool

	// WorkdirRoot specifies the directory within which scripts' work
	// directories will be created. Setting WorkdirRoot implies TestWork=true.
	// If empty, the work directories will be created inside
	// $GOTMPDIR/go-test-script*, where $GOTMPDIR defaults to os.TempDir().
	WorkdirRoot string

	// IgnoreMissedCoverage specifies that if coverage information
	// is being generated (with the -test.coverprofile flag) and a subcommand
	// function passed to RunMain fails to generate coverage information
	// (for example because the function invoked os.Exit), then the
	// error will be ignored.
	IgnoreMissedCoverage bool

	// UpdateScripts specifies that if a `cmp` command fails and
	// its first argument is `stdout` or `stderr` and its second argument
	// refers to a file inside the testscript file, the command will succeed
	// and the testscript file will be updated to reflect the actual output.
	//
	// The content will be quoted with txtar.Quote if needed;
	// a manual change will be needed if it is not unquoted in the
	// script.
	UpdateScripts bool

	// Line prefix which indicates a new phase
	// defaults to "#"
	PhasePrefix string

	// Comment prefix for a line
	// defaults to "~"
	CommentPrefix string
}

Params holds parameters for a call to Run.

type Phase

type Phase struct {
	Lines []string
	Steps []string
}

type Runner

type Runner struct {
	LogLevel string

	Failed bool
}

func (*Runner) FailNow

func (r *Runner) FailNow()

func (*Runner) Fatal

func (r *Runner) Fatal(is ...interface{})

func (*Runner) Log

func (r *Runner) Log(is ...interface{})

func (*Runner) Parallel

func (r *Runner) Parallel()

func (*Runner) Run

func (r *Runner) Run(n string, f func(T))

func (*Runner) Skip

func (r *Runner) Skip(is ...interface{})

func (*Runner) Verbose

func (r *Runner) Verbose() bool

type Script

type Script struct {
	Logger *zap.SugaredLogger
	// contains filtered or unexported fields
}

A Script holds execution state for a single test script.

func (*Script) BackgroundCmds

func (ts *Script) BackgroundCmds() []*exec.Cmd

BackgroundCmds returns a slice containing all the commands that have been started in the background since the most recent wait command, or the start of the script if wait has not been called.

func (*Script) Check

func (ts *Script) Check(err error)

Check calls ts.Fatalf if err != nil.

func (*Script) CmdBilly

func (ts *Script) CmdBilly(neg int, args []string)

func (*Script) CmdCall

func (ts *Script) CmdCall(neg int, args []string)

call runs the given function.

func (*Script) CmdCd

func (ts *Script) CmdCd(neg int, args []string)

cd changes to a different directory.

func (*Script) CmdChmod

func (ts *Script) CmdChmod(neg int, args []string)

func (*Script) CmdCmp

func (ts *Script) CmdCmp(neg int, args []string)

cmp compares two files.

func (*Script) CmdCmpenv

func (ts *Script) CmdCmpenv(neg int, args []string)

cmpenv compares two files with environment variable substitution.

func (*Script) CmdCp

func (ts *Script) CmdCp(neg int, args []string)

cp copies files, maybe eventually directories.

func (*Script) CmdEnv

func (ts *Script) CmdEnv(neg int, args []string)

env displays or adds to the environment.

func (*Script) CmdEnvsub

func (ts *Script) CmdEnvsub(neg int, args []string)

env displays or adds to the environment.

func (*Script) CmdExec

func (ts *Script) CmdExec(neg int, args []string)

exec runs the given command.

func (*Script) CmdExists

func (ts *Script) CmdExists(neg int, args []string)

exists checks that the list of files exists.

func (*Script) CmdGrep

func (ts *Script) CmdGrep(neg int, args []string)

regexp checks that file content matches a regexp. it accepts Go regexp syntax and returns the matches

func (*Script) CmdHttp

func (ts *Script) CmdHttp(neg int, args []string)

http makes an http call.

func (*Script) CmdLog

func (ts *Script) CmdLog(neg int, args []string)

func (*Script) CmdMkdir

func (ts *Script) CmdMkdir(neg int, args []string)

mkdir creates directories.

func (*Script) CmdRegexp

func (ts *Script) CmdRegexp(neg int, args []string)

regexp checks that file content matches a regexp. it accepts Go regexp syntax.

func (*Script) CmdRm

func (ts *Script) CmdRm(neg int, args []string)

rm removes files or directories.

func (*Script) CmdSed

func (ts *Script) CmdSed(neg int, args []string)

sed finds and replaces in text content it accepts Go regexp syntax and returns the replaced content

func (*Script) CmdSkip

func (ts *Script) CmdSkip(neg int, args []string)

skip marks the test skipped.

func (*Script) CmdStatus

func (ts *Script) CmdStatus(neg int, args []string)

status checks the exit or status code from the last exec or http call

func (*Script) CmdStderr

func (ts *Script) CmdStderr(neg int, args []string)

stderr checks that the last go command standard output matches a regexp.

func (*Script) CmdStdin

func (ts *Script) CmdStdin(neg int, args []string)

func (*Script) CmdStdout

func (ts *Script) CmdStdout(neg int, args []string)

stdout checks that the last go command standard output matches a regexp.

func (*Script) CmdStop

func (ts *Script) CmdStop(neg int, args []string)

stop stops execution of the test (marking it passed).

func (ts *Script) CmdSymlink(neg int, args []string)

symlink creates a symbolic link.

func (*Script) CmdUnquote

func (ts *Script) CmdUnquote(neg int, args []string)

unquote unquotes files.

func (*Script) CmdWait

func (ts *Script) CmdWait(neg int, args []string)

Tait waits for background commands to exit, setting stderr and stdout to their result.

func (*Script) Defer

func (ts *Script) Defer(f func())

Defer arranges for f to be called at the end of the test. If Defer is called multiple times, the defers are executed in reverse order (similar to Go's defer statement)

func (*Script) Exec

func (ts *Script) Exec(command string, args ...string) error

Exec runs the given command and saves its stdout and stderr so they can be inspected by subsequent script commands.

func (*Script) Fatalf

func (ts *Script) Fatalf(format string, args ...interface{})

fatalf aborts the test with the given failure message.

func (*Script) Getenv

func (ts *Script) Getenv(key string) string

Getenv gets the value of the environment variable named by the key.

func (*Script) Logf

func (ts *Script) Logf(format string, args ...interface{})

Logf appends the given formatted message to the test log transcript.

func (*Script) MkAbs

func (ts *Script) MkAbs(file string) string

MkAbs interprets file relative to the test script's current directory and returns the corresponding absolute path.

func (*Script) ReadFile

func (ts *Script) ReadFile(file string) string

ReadFile returns the contents of the file with the given name, interpreted relative to the test script's current directory. It interprets "stdout" and "stderr" to mean the standard output or standard error from the most recent exec or wait command respectively.

If the file cannot be read, the script fails.

func (*Script) Setenv

func (ts *Script) Setenv(key, value string)

Setenv sets the value of the environment variable named by the key.

func (*Script) Value

func (ts *Script) Value(key interface{}) interface{}

Value returns a value from Env.Values, or nil if no value was set by Setup.

type T

type T interface {
	Skip(...interface{})
	Fatal(...interface{})
	Parallel()
	Log(...interface{})
	FailNow()
	Run(string, func(T))
	// Verbose is usually implemented by the testing package
	// directly rather than on the *testing.T type.
	Verbose() bool
}

T holds all the methods of the *testing.T type that are used by testscript.

type TestingM

type TestingM interface {
	Run() int
}

TestingM is implemented by *testing.M. It's defined as an interface to allow testscript to co-exist with other testing frameworks that might also wish to call M.Run.

Jump to

Keyboard shortcuts

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