forked from juju/testing
/
osenv.go
40 lines (32 loc) · 856 Bytes
/
osenv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package testing
import (
"os"
"strings"
gc "launchpad.net/gocheck"
)
// OsEnvSuite isolates the tests from the underlaying system environment.
// Environment variables are reset in SetUpTest and restored in TearDownTest.
type OsEnvSuite struct {
oldEnvironment map[string]string
}
func (s *OsEnvSuite) SetUpSuite(c *gc.C) {
s.oldEnvironment = make(map[string]string)
for _, envvar := range os.Environ() {
parts := strings.SplitN(envvar, "=", 2)
s.oldEnvironment[parts[0]] = parts[1]
}
os.Clearenv()
}
func (s *OsEnvSuite) TearDownSuite(c *gc.C) {
os.Clearenv()
for name, value := range s.oldEnvironment {
os.Setenv(name, value)
}
}
func (s *OsEnvSuite) SetUpTest(c *gc.C) {
os.Clearenv()
}
func (s *OsEnvSuite) TearDownTest(c *gc.C) {
}