-
Notifications
You must be signed in to change notification settings - Fork 0
/
envcfg_test.go
152 lines (128 loc) · 4.12 KB
/
envcfg_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
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
// Copyright 2012 Axel Magnuson. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Tests for envcfg package
package envcfg_test
import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/axelmagn/envcfg"
"io/ioutil"
"math/rand"
"os"
"testing"
)
var setValue string = "TEST"
var settings map[string]string
func randString() string {
randInt := rand.Int63()
randBytes := make([]byte, 10)
binary.PutVarint(randBytes, randInt)
return hex.EncodeToString(randBytes)
}
func TestSetenvControl(t *testing.T) {
envKey := envcfg.ENV_PREFIX + randString()
err := os.Setenv(envKey, setValue)
if err != nil {
t.Errorf("Error while setting Env Variable %s to %s: %s", envKey, setValue, err.Error())
}
envValue := os.Getenv(envKey)
if envValue != setValue {
t.Errorf("Extracted Env Variable %s had value %s. Expected %s.", envKey, envValue, setValue)
}
}
func TestExtractEnvIfPrefix(t *testing.T) {
// defined env variable
envKey := envcfg.ENV_PREFIX + randString()
err := os.Setenv(envKey[len(envcfg.ENV_PREFIX):], setValue)
if err != nil {
t.Errorf("Error while setting Env Variable %s to %s: %s", envKey, setValue, err.Error())
}
envValue, prefixPresent := envcfg.ExtractEnvIfPrefix(envKey, envcfg.ENV_PREFIX)
if envValue != setValue || prefixPresent != true {
t.Errorf("Extracted Env Variable %s had value %s, %b. Expected %s, %b.", envKey, envValue, prefixPresent, setValue, true)
}
}
func TestExtractEnvIfPrefix_UndefinedEnv(t *testing.T) {
// set envKey as a random alphanumeric string
envKey := envcfg.ENV_PREFIX + randString()
envValue, prefixPresent := envcfg.ExtractEnvIfPrefix(envKey, envcfg.ENV_PREFIX)
if envValue != "" || prefixPresent != true {
t.Errorf("Extracted Env Variable %s had value %s. Expected \"\" for undefined env variable.", envKey, envValue)
}
}
func TestExtractEnvIfPrefix_NoPrefix(t *testing.T) {
// set envKey as a random alphanumeric string
envKey := randString()
envValue, prefixPresent := envcfg.ExtractEnvIfPrefix(envKey, envcfg.ENV_PREFIX)
if envValue != "" || prefixPresent != false {
t.Errorf("Extracted Env Variable %s had value %s. Expected nil for absent prefix.", envKey, envValue)
}
}
// we use this as the initializer for the settings variable
// I don't know if that's good practice or not.
func TestReadSettings(t *testing.T) {
var err error
var cfgFile *os.File
// Create sample settings data
rawSettings := `
# Key flag
KEY_FLAG
# Value Literal
VLKEY VLVALUE
# Env Key
EKKEY ENV:ENVCFG_TEST_ENV_KEY_VALUE
# Env Key Default Where Env Variable is defined
EKDKEY ENV:ENVCFG_TEST_ENV_DEFINED ekd_default
# Env Key Default Where Env Variable is not defined
EKUKEY ENV:ENVCFG_TEST_ENV_UNDEFINED eku_default
`
// write sample to temp file
cfgFile, err = ioutil.TempFile(os.TempDir(), "envcfg_test_config.ecfg")
if err != nil {
t.Errorf("Error while creating temporary settings file: %s", err.Error())
}
_, err = cfgFile.WriteString(rawSettings)
if err != nil {
t.Errorf("Error while writing settings to temp file: %s", err.Error())
}
err = cfgFile.Close()
if err != nil {
t.Errorf("Error while closing temp file: %s", err.Error())
}
cfgFileName := cfgFile.Name()
// open sample file
cfgFile, err = os.Open(cfgFileName)
if err != nil {
t.Errorf("Error while opening temp settings file for reading: %s", err.Error())
}
// configure ENV for different settings
os.Setenv("ENVCFG_TEST_ENV_KEY_VALUE", "ek_value")
os.Setenv("ENVCFG_TEST_ENV_DEFINED", "ekd_value")
// read settings
settings, err = envcfg.ReadSettings(cfgFile)
if err != nil {
t.Errorf("ReadSettings failed with error: %s", err.Error())
}
}
func ExampleReadSettings_KeyFlag() {
fmt.Println(settings["KEY_FLAG"])
// Output: 1
}
func ExampleReadSettings_ValueLiteral() {
fmt.Println(settings["VLKEY"])
// Output: VLVALUE
}
func ExampleReadSettings_EnvKey() {
fmt.Println(settings["EKKEY"])
// Output: ek_value
}
func ExampleReadSettings_EnvKeyDefault() {
fmt.Println(settings["EKDKEY"])
// Output: ekd_value
}
func ExampleReadSettings_EnvKeyDefaultUndefined() {
fmt.Println(settings["EKUKEY"])
// Output: eku_default
}