File: osenv_test.go

package info (click to toggle)
golang-github-juju-testing 0.0~git20170608.2fe0e88-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 464 kB
  • sloc: makefile: 6
file content (96 lines) | stat: -rw-r--r-- 2,715 bytes parent folder | download
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package testing_test

import (
	"os"
	"runtime"

	gc "gopkg.in/check.v1"

	"github.com/juju/testing"
)

type osEnvSuite struct {
	osEnvSuite testing.OsEnvSuite
}

var _ = gc.Suite(&osEnvSuite{})

func (s *osEnvSuite) SetUpSuite(c *gc.C) {
	s.osEnvSuite = testing.OsEnvSuite{}
}

func (s *osEnvSuite) TestOriginalEnvironment(c *gc.C) {
	// The original environment is properly cleaned and restored.
	err := os.Setenv("TESTING_OSENV_ORIGINAL", "original-value")
	c.Assert(err, gc.IsNil)
	s.osEnvSuite.SetUpSuite(c)
	c.Assert(os.Getenv("TESTING_OSENV_ORIGINAL"), gc.Equals, "")
	s.osEnvSuite.TearDownSuite(c)
	// The environment has been restored.
	c.Assert(os.Getenv("TESTING_OSENV_ORIGINAL"), gc.Equals, "original-value")
}

func (s *osEnvSuite) TestTestingEnvironment(c *gc.C) {
	// Environment variables set up by tests are properly removed.
	s.osEnvSuite.SetUpSuite(c)
	s.osEnvSuite.SetUpTest(c)
	err := os.Setenv("TESTING_OSENV_NEW", "new-value")
	c.Assert(err, gc.IsNil)
	s.osEnvSuite.TearDownTest(c)
	s.osEnvSuite.TearDownSuite(c)
	c.Assert(os.Getenv("TESTING_OSENV_NEW"), gc.Equals, "")
}

func (s *osEnvSuite) TestPreservesTestingVariables(c *gc.C) {
	err := os.Setenv("JUJU_MONGOD", "preserved-value")
	c.Assert(err, gc.IsNil)
	s.osEnvSuite.SetUpSuite(c)
	s.osEnvSuite.SetUpTest(c)
	c.Assert(os.Getenv("JUJU_MONGOD"), gc.Equals, "preserved-value")
	c.Assert(err, gc.IsNil)
	s.osEnvSuite.TearDownTest(c)
	s.osEnvSuite.TearDownSuite(c)
	c.Assert(os.Getenv("JUJU_MONGOD"), gc.Equals, "preserved-value")
}

func (s *osEnvSuite) TestRestoresTestingVariables(c *gc.C) {
	os.Clearenv()
	s.osEnvSuite.SetUpSuite(c)
	s.osEnvSuite.SetUpTest(c)
	err := os.Setenv("JUJU_MONGOD", "test-value")
	c.Assert(err, gc.IsNil)
	s.osEnvSuite.TearDownTest(c)
	s.osEnvSuite.TearDownSuite(c)
	c.Assert(os.Getenv("JUJU_MONGOD"), gc.Equals, "")
}

func (s *osEnvSuite) TestWindowsPreservesPath(c *gc.C) {
	if runtime.GOOS != "windows" {
		c.Skip("Windows-specific test case")
	}
	err := os.Setenv("PATH", "/new/path")
	c.Assert(err, gc.IsNil)
	s.osEnvSuite.SetUpSuite(c)
	s.osEnvSuite.SetUpTest(c)
	c.Assert(os.Getenv("PATH"), gc.Equals, "/new/path")
	s.osEnvSuite.TearDownTest(c)
	s.osEnvSuite.TearDownSuite(c)
	c.Assert(os.Getenv("PATH"), gc.Equals, "/new/path")
}

func (s *osEnvSuite) TestWindowsRestoresPath(c *gc.C) {
	if runtime.GOOS != "windows" {
		c.Skip("Windows-specific test case")
	}
	os.Clearenv()
	s.osEnvSuite.SetUpSuite(c)
	s.osEnvSuite.SetUpTest(c)
	err := os.Setenv("PATH", "/test/path")
	c.Assert(err, gc.IsNil)
	s.osEnvSuite.TearDownTest(c)
	s.osEnvSuite.TearDownSuite(c)
	c.Assert(os.Getenv("PATH"), gc.Equals, "")
}