osexit

package
v0.0.0-...-dc64ae7 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2021 License: CC0-1.0 Imports: 1 Imported by: 0

Documentation

Overview

Package osexit mocks the os.Exit function.

To use, first set a package-specific exit function, e.g.

var exit = os.Exit

Then use it instead of a direct call to os.Exit, e.g.

if somethingFatal {
    exit(1)
    return
}

Make sure to return immediately after the call to exit, so that testing code will match real code as closely as possible.

You can now use the utility functions provided by this package to override exit for testing purposes, e.g.

exit = osexit.Set()
invokeCodeCallingExit()
if !osexit.Called() {
    t.Fatalf("os.Exit was not called as expected")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Called

func Called() bool

Called returns whether the mock os.Exit function was called.

func Func

func Func(code int)

Func provides a mock for the os.Exit function. Special care must be taken when testing os.Exit to make sure no code runs after the call to Exit. It's recommended to put a return statement after Exit calls so that the behaviour of the mock matches that of the real function as much as possible.

func Reset

func Reset()

Reset resets the state of the mock function.

func Set

func Set() func(int)

Set returns the mock os.Exit function after calling Reset.

func Status

func Status() int

Status returns the status code that the mock os.Exit function was called with.

Types

This section is empty.

Jump to

Keyboard shortcuts

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