test

package
v0.0.42 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2021 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

Package test includes small test helpers, as is appropriate for this snippets module. It is envisaged that it may well outgrow this module at which point it will move to foxygo.at/test.

Index

Constants

This section is empty.

Variables

View Source
var Env = Environ{}

Env is a global Environ variable for simpler use. Since the OS environment is global, using a global test.Environ is rarely a problem. Use is as simple as:

func TestFoo(t *testing.T) {
    test.Env.Set("FOO", "BAR").Unset("BAZ")
    defer test.Env.Restore()
    ... test, test, test ...
}

Functions

func ReadJSON added in v0.0.37

func ReadJSON(t testing.TB, fname string, v interface{})

ReadJSON decodes JSON from a file into a value. If it fails to open the given filename or decode the contents of the file, ReadJSON will call t.Fatal with the error.

The type of the first parameter as testing.TB allows it to be used from both tests and benchmarks, passing either a *testing.T or *testing.B.

cfg := Config{}
test.ReadJSON(t, "testdata/config.json", &cfg)

Types

type Environ

type Environ map[string]*string

Environ is a frontend to the OS environment that keeps track of the previous value of an environment variable before setting or unsetting it. Later, Restore() can be called to reset all modified variables back to their original values and their presence.

It is intended to be used in test cases where a particular starting environment is needed and to ensure that it is reset back to the starting state at the end.

The operations on Environ are not concurrency-safe as the global OS environment is also not concurrency-safe. You should not use Environ in test functions that are marked t.Parallel().

The methods on Environ return the receiver so environment setup can be chained:

e := test.Environ{}.Set("FOO", "BAR").Unset("BAZ")
defer e.Restore()

func (Environ) Restore

func (e Environ) Restore()

Restore restores all the saved values from Set and Unset back to the original values and presence. When Restore returns, all saved values will be forgotten. This permits the Environ to be reused. If Restore is unable to set or unset any variables back to their original state, it will panic.

func (Environ) Set

func (e Environ) Set(key, value string) Environ

Set sets key to value in the OS environment, saving the previous value and presence. It returns the receiver so calls can easily be chained. If key or value are invalid (as per os.Setenv), this method will panic.

func (Environ) Unset

func (e Environ) Unset(key string) Environ

Unset removes key from the OS environment, saving the previous value and presence. It returns the receive so calls can easily be chained. If os.Unsetenv returns an error, this method will panic. However, it appears that os.Unsetenv does not return an error.

Jump to

Keyboard shortcuts

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