This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenvironment_source_test.go
79 lines (61 loc) · 2.25 KB
/
environment_source_test.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
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
package configo
import (
"os"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestEnvironmentSourceFixture(t *testing.T) {
gunit.Run(new(EnvironmentSourceFixture), t)
}
type EnvironmentSourceFixture struct {
*gunit.Fixture
source *EnvironmentSource
}
func (this *EnvironmentSourceFixture) Setup() {
this.source = FromEnvironmentCustomSeparator("configo_", ",")
}
func (this *EnvironmentSourceFixture) TestNonExistentValue() {
values, err := this.source.Strings("notfound")
this.So(values, should.BeEmpty)
this.So(err, should.Equal, ErrKeyNotFound)
}
func (this *EnvironmentSourceFixture) TestKnownValue() {
setEnvironment("configo_Found_Single", "hello")
values, err := this.source.Strings("Found_Single")
this.So(values, should.Resemble, []string{"hello"})
this.So(err, should.BeNil)
}
func (this *EnvironmentSourceFixture) TestKnownValueArray() {
setEnvironment("configo_Found_Multiple", "a,b,c")
values, err := this.source.Strings("Found_Multiple")
this.So(values, should.Resemble, []string{"a", "b", "c"})
this.So(err, should.BeNil)
}
func (this *EnvironmentSourceFixture) TestChecksUpperCaseKey() {
setEnvironment("CONFIGO_UPPERCASE", "value")
values, err := this.source.Strings("uppercase")
this.So(values, should.Resemble, []string{"value"})
this.So(err, should.BeNil)
}
func (this *EnvironmentSourceFixture) TestChecksLowerCaseKey() {
setEnvironment("configo_lowercase", "value")
values, err := this.source.Strings("LOWERCASE")
this.So(values, should.Resemble, []string{"value"})
this.So(err, should.BeNil)
}
func (this *EnvironmentSourceFixture) TestInvalidCharactersReplacedWithUnderscore() {
setEnvironment("configo_0_0_0_0_0_0_0_0_0_0_0_0_0_0_0", "value")
values, err := this.source.Strings("0-0.0~0!0@0#0%0^0&0*0(0)0/0\\0")
this.So(values, should.Resemble, []string{"value"})
this.So(err, should.BeNil)
}
func (this *EnvironmentSourceFixture) TestEnvPrefixIsAlwaysStrippedBeforeTheEnvironmentLookup() {
setEnvironment("configo_my_awesome_variable", "my_awesome_value")
values, err := this.source.Strings("env:my_awesome_variable")
this.So(values, should.Resemble, []string{"my_awesome_value"})
this.So(err, should.BeNil)
}
func setEnvironment(key, value string) {
os.Setenv(key, value)
}