/usr/share/gocode/src/go.pedge.io/env/populate_test.go is in golang-go.pedge-env-dev 0.0~git20171203.5f5a7de-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 192 193 194 195 196 197 198 | package env
import (
"bytes"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
"testing"
)
type subEnv struct {
SubEnvRequiredString string `env:"SUB_ENV_REQUIRED_STRING,required"`
SubEnvOptionalString string `env:"SUB_ENV_OPTIONAL_STRING"`
SubEnvOptionalUint16Default uint16 `env:"SUB_ENV_OPTIONAL_UINT16_DEFAULT,default=1024"`
}
type testEnv struct {
RequiredString string `env:"REQUIRED_STRING,required"`
OptionalString string `env:"OPTIONAL_STRING"`
OptionalInt int `env:"OPTIONAL_INT"`
OptionalBool bool `env:"OPTIONAL_BOOL"`
SubEnv subEnv
Struct struct {
StructOptionalInt int `env:"STRUCT_OPTIONAL_INT"`
}
OptionalStringDefault string `env:"OPTIONAL_STRING_DEFAULT,default=foo"`
OptionalFloat32 float32 `env:"OPTIONAL_FLOAT32"`
OptionalFloat64 float64 `env:"OPTIONAL_FLOAT64,default=2.2"`
}
// TODO(pedge): if tests are run in parallel, this is affecting global state
func TestBasic(t *testing.T) {
runTest(
t,
func(t *testing.T, testEnv *testEnv) {
checkEqual(t, "foo", testEnv.RequiredString)
checkEqual(t, "", testEnv.OptionalString)
checkEqual(t, 1234, testEnv.OptionalInt)
checkEqual(t, true, testEnv.OptionalBool)
checkEqual(t, "bar", testEnv.SubEnv.SubEnvRequiredString)
checkEqual(t, "baz", testEnv.SubEnv.SubEnvOptionalString)
checkEqual(t, uint16(1024), testEnv.SubEnv.SubEnvOptionalUint16Default)
checkEqual(t, 5678, testEnv.Struct.StructOptionalInt)
checkEqual(t, "foo", testEnv.OptionalStringDefault)
checkEqual(t, float32(1.0), testEnv.OptionalFloat32)
checkEqual(t, 2.2, testEnv.OptionalFloat64)
},
map[string]string{
"REQUIRED_STRING": "foo",
"OPTIONAL_INT": "1234",
"OPTIONAL_BOOL": "T",
"SUB_ENV_REQUIRED_STRING": "bar",
"SUB_ENV_OPTIONAL_STRING": "baz",
"SUB_ENV_OPTIONAL_STRING_DEFAULT": "baz",
"STRUCT_OPTIONAL_INT": "5678",
"OPTIONAL_FLOAT32": "1",
},
)
}
func TestMissing(t *testing.T) {
runErrorTest(t, envKeyNotSetWhenRequiredErr, map[string]string{"REQUIRED_STRING": "foo"})
runErrorTest(t, envKeyNotSetWhenRequiredErr, map[string]string{"SUB_ENV_REQUIRED_STRING": "bar"})
}
func TestCannotParse(t *testing.T) {
runErrorTest(
t,
cannotParseErr,
map[string]string{
"REQUIRED_STRING": "foo",
"SUB_ENV_REQUIRED_STRING": "bar",
"OPTIONAL_INT": "abc",
},
)
}
type keyValueEnv struct {
KeyValue string `env:"KEY_VALUE,default=a=b,c=d"`
}
func TestKeyValueDefault(t *testing.T) {
keyValueEnv := &keyValueEnv{}
if err := Populate(keyValueEnv); err != nil {
t.Error(err)
return
}
checkEqual(t, "a=b,c=d", keyValueEnv.KeyValue)
}
func TestKeyValue(t *testing.T) {
setEnv(
t,
map[string]string{
"KEY_VALUE": "b=a,c=e,e=f,g=h",
},
func(t *testing.T) {
keyValueEnv := &keyValueEnv{}
if err := Populate(keyValueEnv); err != nil {
t.Error(err)
return
}
checkEqual(t, "b=a,c=e,e=f,g=h", keyValueEnv.KeyValue)
},
)
}
func runTest(t *testing.T, f func(*testing.T, *testEnv), env map[string]string, envFiles ...string) {
runTestLong(t, "", f, env, envFiles...)
}
func runErrorTest(t *testing.T, expectedError string, env map[string]string, envFiles ...string) {
runTestLong(t, expectedError, nil, env, envFiles...)
}
func runTestLong(t *testing.T, expectedError string, f func(*testing.T, *testEnv), env map[string]string, envFiles ...string) {
decoders := make([]Decoder, len(envFiles))
for i, envFile := range envFiles {
reader := getTestReader(t, envFile)
if strings.HasSuffix(envFile, ".env") {
decoders[i] = newEnvFileDecoder(reader)
} else if strings.HasSuffix(envFile, ".json") {
decoders[i] = newJSONDecoder(reader)
} else {
t.Fatalf("unknown suffix for file name: %s", envFile)
}
}
setEnv(
t,
env,
func(t *testing.T) {
testEnv := &testEnv{}
err := Populate(
testEnv,
decoders...,
)
if err != nil && expectedError == "" {
t.Error(err)
return
}
if err != nil && expectedError != "" {
if !strings.HasPrefix(err.Error(), expectedError) {
t.Errorf("expected error type %s, got error %s", expectedError, err.Error())
return
}
}
if err == nil && expectedError != "" {
t.Errorf("expected error %s, but no error", expectedError)
return
}
if f != nil {
f(t, testEnv)
}
},
)
}
func setEnv(t *testing.T, env map[string]string, f func(t *testing.T)) {
originalEnv := make(map[string]string)
for key, value := range env {
originalEnv[key] = os.Getenv(key)
_ = os.Setenv(key, value)
}
defer func() {
for key, value := range originalEnv {
_ = os.Setenv(key, value)
}
}()
f(t)
}
func getTestReader(t *testing.T, filePath string) io.Reader {
file, err := os.Open(filePath)
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(file)
if err != nil {
if err := file.Close(); err != nil {
t.Error(err)
}
t.Fatal(err)
}
if err := file.Close(); err != nil {
t.Fatal(err)
}
return bytes.NewBuffer(data)
}
func checkEqual(t *testing.T, expected interface{}, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
// TODO(pedge): fatals out when need to call defer to reset env
t.Fatalf("expected %v, got %v", expected, actual)
}
}
|