1package testy
2
3import (
4	"os"
5	"testing"
6
7	"github.com/flimzy/diff"
8)
9
10func TestRestoreEnv(t *testing.T) {
11	os.Clearenv()
12	os.Setenv("foo", "bar")
13	os.Setenv("bar", "baz")
14
15	func() {
16		defer RestoreEnv()()
17		os.Setenv("baz", "qux")
18		os.Unsetenv("bar")
19		env := os.Environ()
20		expected := []string{"foo=bar", "baz=qux"}
21		if d := diff.Interface(expected, env); d != nil {
22			t.Fatal(d)
23		}
24	}()
25
26	env := os.Environ()
27	expected := []string{"foo=bar", "bar=baz"}
28	if d := diff.Interface(expected, env); d != nil {
29		t.Fatal(d)
30	}
31}
32
33func TestEnviron(t *testing.T) {
34	os.Clearenv()
35	os.Setenv("foo", "bar")
36	os.Setenv("bar", "baz")
37	expected := map[string]string{
38		"foo": "bar",
39		"bar": "baz",
40	}
41	env := Environ()
42	if d := diff.Interface(expected, env); d != nil {
43		t.Fatal(d)
44	}
45}
46
47func TestSetEnv(t *testing.T) {
48	os.Clearenv()
49	os.Setenv("foo", "bar")
50
51	SetEnv(map[string]string{
52		"bar": "baz",
53		"baz": "qux",
54	})
55
56	expected := map[string]string{
57		"foo": "bar",
58		"bar": "baz",
59		"baz": "qux",
60	}
61	env := Environ()
62	if d := diff.Interface(expected, env); d != nil {
63		t.Fatal(d)
64	}
65
66}
67