cmdtest

package
v0.50.11-lsm Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package cmdtest contains a framework for testing cobra Commands within Go unit tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RunResult

type RunResult struct {
	Stdout, Stderr bytes.Buffer

	Err error
}

RunResult is the stdout and stderr resulting from a call to a System's Run family of methods, and any error that was returned.

type System

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

System is a system under test.

func NewSystem

func NewSystem() *System

NewSystem returns a new System.

func (*System) AddCommands

func (s *System) AddCommands(cmds ...*cobra.Command)

AddCommands sets commands to be available to the Run family of methods on s.

func (*System) MustRun

func (s *System) MustRun(t TestingT, args ...string) RunResult

MustRun calls s.Run, but also calls t.FailNow if RunResult.Err is not nil.

func (*System) MustRunC

func (s *System) MustRunC(t TestingT, ctx context.Context, args ...string) RunResult

MustRunC calls s.RunWithInput, but also calls t.FailNow if RunResult.Err is not nil.

func (*System) MustRunWithInput

func (s *System) MustRunWithInput(t TestingT, in io.Reader, args ...string) RunResult

MustRunWithInput calls s.RunWithInput, but also calls t.FailNow if RunResult.Err is not nil.

func (*System) MustRunWithInputC

func (s *System) MustRunWithInputC(t TestingT, ctx context.Context, in io.Reader, args ...string) RunResult

MustRunWithInputC calls s.RunWithInputC, but also calls t.FailNow if RunResult.Err is not nil.

func (*System) Run

func (s *System) Run(args ...string) RunResult

Run calls s.RunC with context.Background().

func (*System) RunC

func (s *System) RunC(ctx context.Context, args ...string) RunResult

RunC calls s.RunWithInput with an empty stdin.

func (*System) RunWithInput

func (s *System) RunWithInput(in io.Reader, args ...string) RunResult

RunWithInput calls s.RunWithInputC with context.Background().

func (*System) RunWithInputC

func (s *System) RunWithInputC(ctx context.Context, in io.Reader, args ...string) RunResult

RunWithInputC executes a new root command with subcommands that were set in s.AddCommands(). The command's stdin is set to the in argument. RunWithInputC returns a RunResult wrapping stdout, stderr, and any returned error.

type TestingT

type TestingT interface {
	Helper()

	Logf(format string, args ...any)

	FailNow()
}

TestingT is a subset of testing.TB, containing only what the (*System).Must methods use.

This simplifies using other testing wrappers, such as testify suite, etc.

Jump to

Keyboard shortcuts

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