File: envy_test.go

package info (click to toggle)
golang-github-gobuffalo-envy 1.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, buster-backports, sid
  • size: 108 kB
  • sloc: makefile: 3
file content (191 lines) | stat: -rw-r--r-- 3,753 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package envy

import (
	"os"
	"runtime"
	"testing"

	"github.com/stretchr/testify/require"
)

func Test_Get(t *testing.T) {
	r := require.New(t)
	r.NotZero(os.Getenv("GOPATH"))
	r.Equal(os.Getenv("GOPATH"), Get("GOPATH", "foo"))
	r.Equal("bar", Get("IDONTEXIST", "bar"))
}

func Test_MustGet(t *testing.T) {
	r := require.New(t)
	r.NotZero(os.Getenv("GOPATH"))
	v, err := MustGet("GOPATH")
	r.NoError(err)
	r.Equal(os.Getenv("GOPATH"), v)

	_, err = MustGet("IDONTEXIST")
	r.Error(err)
}

func Test_Set(t *testing.T) {
	r := require.New(t)
	_, err := MustGet("FOO")
	r.Error(err)

	Set("FOO", "foo")
	r.Equal("foo", Get("FOO", "bar"))
}

func Test_MustSet(t *testing.T) {
	r := require.New(t)

	r.Zero(os.Getenv("FOO"))

	err := MustSet("FOO", "BAR")
	r.NoError(err)

	r.Equal("BAR", os.Getenv("FOO"))
}

func Test_Temp(t *testing.T) {
	r := require.New(t)

	_, err := MustGet("BAR")
	r.Error(err)

	Temp(func() {
		Set("BAR", "foo")
		r.Equal("foo", Get("BAR", "bar"))
		_, err = MustGet("BAR")
		r.NoError(err)
	})

	_, err = MustGet("BAR")
	r.Error(err)
}

func Test_GoPath(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		Set("GOPATH", "/foo")
		r.Equal("/foo", GoPath())
	})
}

func Test_GoPaths(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		if runtime.GOOS == "windows" {
			Set("GOPATH", "/foo;/bar")
		} else {
			Set("GOPATH", "/foo:/bar")
		}
		r.Equal([]string{"/foo", "/bar"}, GoPaths())
	})
}

func Test_CurrentPackage(t *testing.T) {
	r := require.New(t)
	r.Equal("github.com/gobuffalo/envy", CurrentPackage())
}

// Env files loading
func Test_LoadEnvLoadsEnvFile(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		r.Equal("root", Get("DIR", ""))
		r.Equal("none", Get("FLAVOUR", ""))
		r.Equal("false", Get("INSIDE_FOLDER", ""))
	})
}

func Test_LoadDefaultEnvWhenNoArgsPassed(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		err := Load()
		r.NoError(err)

		r.Equal("root", Get("DIR", ""))
		r.Equal("none", Get("FLAVOUR", ""))
		r.Equal("false", Get("INSIDE_FOLDER", ""))
	})
}

func Test_DoNotLoadDefaultEnvWhenArgsPassed(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		err := Load("test_env/.env")
		r.NoError(err)

		r.Equal("test_env", Get("DIR", ""))
		r.Equal("none", Get("FLAVOUR", ""))
		r.Equal("true", Get("INSIDE_FOLDER", ""))
	})
}

func Test_OverloadParams(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		err := Load("test_env/.env.test", "test_env/.env.prod")
		r.NoError(err)

		r.Equal("production", Get("FLAVOUR", ""))
	})
}

func Test_ErrorWhenSingleFileLoadDoesNotExist(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		delete(env, "FLAVOUR")
		err := Load(".env.fake")

		r.Error(err)
		r.Equal("FAILED", Get("FLAVOUR", "FAILED"))
	})
}

func Test_KeepEnvWhenFileInListFails(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		err := Load(".env", ".env.FAKE")
		r.Error(err)
		r.Equal("none", Get("FLAVOUR", "FAILED"))
		r.Equal("root", Get("DIR", "FAILED"))
	})
}

func Test_KeepEnvWhenSecondLoadFails(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		err := Load(".env")
		r.NoError(err)
		r.Equal("none", Get("FLAVOUR", "FAILED"))
		r.Equal("root", Get("DIR", "FAILED"))

		err = Load(".env.FAKE")

		r.Equal("none", Get("FLAVOUR", "FAILED"))
		r.Equal("root", Get("DIR", "FAILED"))
	})
}

func Test_StopLoadingWhenFileInListFails(t *testing.T) {
	r := require.New(t)
	Temp(func() {
		err := Load(".env", ".env.FAKE", "test_env/.env.prod")
		r.Error(err)
		r.Equal("none", Get("FLAVOUR", "FAILED"))
		r.Equal("root", Get("DIR", "FAILED"))
	})
}

func Test_GOPATH_Not_Set(t *testing.T) {
	r := require.New(t)

	Temp(func() {
		MustSet("GOPATH", "/go")
		loadEnv()
		r.Equal("/go", Get("GOPATH", "notset"))
	})

	r.Equal("github.com/gobuffalo/envy", CurrentPackage())
}