Skip to content

ilijamt/envwrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Environment Wrapper

Go Report Card Build Status License

A small and useful utility for tests so you can run your tests with multiple environments from inside the test functions.

The environment is set for the whole application while you use the wrapper, so running tests in parallel may have unexpected problems.

Requirements

Usage

NewStorage

package main

import (
	"fmt"
	"os"

	"github.com/ilijamt/envwrap"
)

func main() {
	env := envwrap.NewStorage()
	defer env.ReleaseAll()
	oldVariable := os.Getenv("A_VARIABLE")
	env.Store("A_VARIABLE", "test")
	fmt.Println("ORIGINAL_VALUE=", oldVariable)
	fmt.Println("A_VARIABLE=", os.Getenv("A_VARIABLE"))
	env.Release("A_VARIABLE")
	fmt.Println("A_VARIABLE=", os.Getenv("A_VARIABLE"))
}

Should print

ORIGINAL_VALUE=
A_VARIABLE= test
A_VARIABLE=

NewCleanStorage

Removes all environment variables

package main

import (
	"fmt"
	"os"

	"github.com/ilijamt/envwrap"
)

func main() {
	fmt.Printf("Total items before new clean storage in environment: %d\n", len(os.Environ()))
	env := envwrap.NewCleanStorage()
	fmt.Printf("Total items in environment: %d\n", len(os.Environ()))
	_ = env.ReleaseAll()
	fmt.Printf("Total items in environment after release: %d\n", len(os.Environ()))
}

Should print

Total items before new clean storage in environment: 55
Total items in environment: 0
Total items in environment after release: 55