testcontext

package
v0.0.0-...-eb3fb91 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: MIT Imports: 14 Imported by: 23

Documentation

Overview

Package testcontext implements convenience context for testing.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTimeout = 3 * time.Minute

DefaultTimeout is the default timeout used by new context.

Functions

This section is empty.

Types

type Context

type Context struct {
	context.Context
	// contains filtered or unexported fields
}

Context is a context that has utility methods for testing and waiting for asynchronous errors.

func New

func New(test TB) *Context

New creates a new test context with default timeout.

func NewWithContext

func NewWithContext(parentCtx context.Context, test TB) *Context

NewWithContext creates a new test context with a parent context.

func NewWithContextAndTimeout

func NewWithContextAndTimeout(parentCtx context.Context, test TB, timeout time.Duration) *Context

NewWithContextAndTimeout creates a new test context with a given timeout and the parent context.

func NewWithTimeout

func NewWithTimeout(test TB, timeout time.Duration) *Context

NewWithTimeout creates a new test context with a given timeout.

func (*Context) Check

func (ctx *Context) Check(fn func() error)

Check calls fn and checks result.

func (*Context) Cleanup

func (ctx *Context) Cleanup()

Cleanup waits everything to be completed, checks errors and goroutines which haven't ended and tries to cleanup directories.

Since Go 1.14 this method isn't required anymore because the https://pkg.go.dev/testing#T.Cleanup addition.

func (*Context) Compile

func (ctx *Context) Compile(pkg string, preArgs ...string) string

Compile compiles the specified package and returns the executable name.

func (*Context) CompileAt

func (ctx *Context) CompileAt(workDir, pkg string, preArgs ...string) string

CompileAt compiles the specified package and returns the executable name.

func (*Context) CompileWithLDFlagsX

func (ctx *Context) CompileWithLDFlagsX(pkg string, ldFlagsX map[string]string) string

CompileWithLDFlagsX compiles the specified package with the -ldflags flag set to "-s -w [-X <key>=<value>,...]" given the passed map and returns the executable name.

func (*Context) Dir

func (ctx *Context) Dir(elem ...string) string

Dir creates a subdirectory inside temp joining any number of path elements into a single path and return its absolute path.

func (*Context) File

func (ctx *Context) File(elem ...string) string

File returns a filepath inside a temp directory joining any number of path elements into a single path and returns its absolute path.

func (*Context) Go

func (ctx *Context) Go(fn func() error)

Go runs fn in a goroutine. Call Wait to check the result.

func (*Context) StackTrace

func (ctx *Context) StackTrace() string

StackTrace returns stack trace about the goroutines that are related to this Context.

func (*Context) Wait

func (ctx *Context) Wait()

Wait blocks until all of the goroutines launched with Go are done and fails the test if any of them returned an error.

type TB

type TB interface {
	Name() string
	Helper()

	Cleanup(f func())

	Log(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
}

TB is a subset of testing.TB methods.

Jump to

Keyboard shortcuts

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