envrtr

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

README

Environment Retractor

gopherbadger-tag-do-not-edit Go Report Card Go Reference

envrtr is a pure package for applying and rolling back the state of environment variables.

For what?

This package is really useful when you need to test a piece of code whose behavior depends on the environment variables. The package will help bring the variables to the desired state, and at the end of the test, return them to their original state.

Installation

go get github.com/will-evil/envrtr

Examples

Using only envrtr

When need set environment variables

import (
	"testing"

	"github.com/will-evil/envrtr"
)

func TestSome(t *testing) {
	// map of environment variable names and new values for this variables
	envValues := map[string]string{
		"TEST_ENV":  "value",
		"TEST_ENV2": "value2",
		"TEST_ENV3": "value3",
	}
	// create new retractor instance and set new values for provided environment variables
	r := envrtr.NewEnvRetractor(envValues).PullOut()
	// rolling back environment variables to their original state
	defer r.Retract()

	res := Some()
	// check result of function
}

When need unset environment variables

import (
	"testing"

	"github.com/will-evil/envrtr"
)

func TestSome(t *testing) {
	// slice of environment variable names for unset
	envValues := []string{"TEST_ENV", "TEST_ENV2", "TEST_ENV3"}
	// create new retractor instance and unset environment variables with provided names
	r := envrtr.NewEnvUnsetRetractor(envValues).PullOut()
	// rolling back environment variables to their original state
	defer r.Retract()

	res := Some()
	// check result of function
}
Using with testify's suite
import (
	"testing"

	"github.com/will-evil/envrtr"
	"github.com/stretchr/testify/suite"
)

type ExampleSuite struct {
	suite.Suite
	retractor *envrtr.EnvRetractor
}

func (suite *ExampleSuite) SetupSuite() {
	envValues := map[string]string{
		"TEST_ENV":  "value",
		"TEST_ENV2": "value2",
		"TEST_ENV3": "value3",
	}
	suite.retractor = envrtr.NewEnvRetractor(envValues)
}

func (suite *ExampleSuite) BeforeTest(_, _ string) {
	suite.retractor.PullOut()
}

func (suite *ExampleSuite) AfterTest(_, _ string) {
	suite.retractor.Retract()
}

func (suite *ExampleSuite) TestSomething() {
	suite.Equal(true, true)
}

func TestRunSuite(t *testing.T) {
	suite.Run(t, new(ExampleSuite))
}

Documentation

Overview

Package envrtr provides functional to pull out (apply) and retract (rollback) changes for environment variables.

Package envrtr provides functional to pull out (apply) and retract (rollback) changes for environment variables.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EnvRetractor

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

EnvRetractor provides functional for pull out and retracts environment variables.

func (*EnvRetractor) PullOut

func (r *EnvRetractor) PullOut() Retractor

PullOut sets provided values for environment variables.

func (*EnvRetractor) Retract

func (r *EnvRetractor) Retract()

Retract rolls back the values of an environment variables to its original state.

type EnvUnsetRetractor added in v1.1.0

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

EnvUnsetRetractor provides functional for unseting environment variables and return their original values.

func (*EnvUnsetRetractor) PullOut added in v1.1.0

func (r *EnvUnsetRetractor) PullOut() Retractor

PullOut sets provided values for environment variables.

func (*EnvUnsetRetractor) Retract added in v1.1.0

func (r *EnvUnsetRetractor) Retract()

Retract rolls back the values of an environment variables to its original state.

type Retractor added in v1.2.0

type Retractor interface {
	PullOut() Retractor
	Retract()
}

Retractor common interface for retractors.

func NewEnvRetractor

func NewEnvRetractor(envValues map[string]string) Retractor

NewEnvRetractor constructor for EnvRetractor.

func NewEnvUnsetRetractor added in v1.1.0

func NewEnvUnsetRetractor(envVars []string) Retractor

NewEnvUnsetRetractor constructor for EnvUnsetRetractor.

Jump to

Keyboard shortcuts

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