testrun

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package testrun contains helper functionality for executing CLI tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TestRun

type TestRun struct {
	T       testing.TB
	Context context.Context

	Run *run.Run

	// Out contains the output written to stdout after TestRun.Exec is invoked.
	Out *bytes.Buffer

	// ErrOut contains the output written to stderr after TestRun.Exec is invoked.
	ErrOut *bytes.Buffer
	// contains filtered or unexported fields
}

TestRun is a helper for testing sq commands.

func New

func New(ctx context.Context, tb testing.TB, from *TestRun) *TestRun

New returns a new run instance for testing sq commands. If from is non-nil, its config is used. This allows sequential commands to use the same config.

You can also use TestRun.Reset to reuse a TestRun instance.

func (*TestRun) Add

func (tr *TestRun) Add(srcs ...source.Source) *TestRun

Add adds srcs to tr.Run.Config.Collection. If the collection does not already have an active source, the first element of srcs is used as the active source.

REVISIT: Why not use *source.Source instead of the value?

func (*TestRun) Bind

func (tr *TestRun) Bind(v any) *TestRun

Bind marshals tr.Out to v (as JSON), failing the test on any error.

func (*TestRun) BindCSV added in v0.41.0

func (tr *TestRun) BindCSV() [][]string

BindCSV reads CSV from tr.Out and returns all records, failing the testing on any problem. Obviously the Exec call should have specified "--csv".

func (*TestRun) BindMap

func (tr *TestRun) BindMap() map[string]any

BindMap is a convenience method for binding tr.Out to a map (assuming tr.Out is JSON).

func (*TestRun) BindSliceMap added in v0.41.0

func (tr *TestRun) BindSliceMap() []map[string]any

BindSliceMap is a convenience method for binding tr.Out to a slice of map (assuming tr.Out is JSON).

func (*TestRun) BindYAML added in v0.44.0

func (tr *TestRun) BindYAML(v any) *TestRun

BindYAML marshals tr.Out to v (as YAML), failing the test on any error.

func (*TestRun) Exec

func (tr *TestRun) Exec(args ...string) error

Exec executes the sq command specified by args. If the first element of args is not "sq", that value is prepended to the args for execution. This method may only be invoked once on this TestRun instance, unless TestRun.Reset is called. The backing Run will also be closed. If an error occurs on the client side during execution, that error is returned. Either tr.Out or tr.ErrOut will be filled, according to what the CLI outputs.

func (*TestRun) Hush

func (tr *TestRun) Hush() *TestRun

Hush suppresses the printing of output collected in out and errOut to t.Log. Set to true for tests that output excessive content, binary files, etc.

func (*TestRun) OutString added in v0.43.0

func (tr *TestRun) OutString() string

OutString returns the contents of tr.Out as a string, with the final trailing newline removed.

func (*TestRun) Reset added in v0.43.0

func (tr *TestRun) Reset() *TestRun

Reset resets tr to a clean slate. Note that a new TestRun instance is created behind the scenes, so any references to the previous TestRun's fields are now invalid.

See also: testrun.New.

Jump to

Keyboard shortcuts

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