forked from Shopify/ejson2env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets_test.go
97 lines (84 loc) · 2.38 KB
/
secrets_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
package ejson2env
import (
"bytes"
"os"
"strings"
"testing"
)
func TestReadSecrets(t *testing.T) {
var err error
_, err = ReadAndExtractEnv("testdata/bad.ejson", "./key", TestKeyValue)
if nil == err {
t.Fatal("failed to fail when loading a broken ejson file")
}
if !strings.Contains(err.Error(), "no such file or directory") {
t.Errorf("error should be \"no such file or directory\": %s", err)
}
}
func TestReadAndExportEnv(t *testing.T) {
outputBuffer := new(bytes.Buffer)
output = outputBuffer
// ensure that output returns to os.Stdout
defer func() {
output = os.Stdout
}()
tests := []struct {
name string
exportFunc ExportFunction
ejsonFile string
expectedOutput string
}{
{
name: "ExportEnv",
exportFunc: ExportEnv,
ejsonFile: "testdata/test-expected-usage.ejson",
expectedOutput: "export test_key='test value'\n",
},
{
name: "ExportQuiet",
exportFunc: ExportQuiet,
ejsonFile: "testdata/test-expected-usage.ejson",
expectedOutput: "test_key='test value'\n",
},
{
name: "ExportEnvTrimUnderscore",
exportFunc: TrimLeadingUnderscoreExportWrapper(ExportEnv),
ejsonFile: "testdata/test-leading-underscore-env-key.ejson",
expectedOutput: "export test_key='test value'\n",
},
{
name: "ExportEnvNoTrimUnderscore",
exportFunc: ExportEnv,
ejsonFile: "testdata/test-leading-underscore-env-key.ejson",
expectedOutput: "export _test_key='test value'\n",
},
}
for _, test := range tests {
err := ReadAndExportEnv(test.ejsonFile, "./key", TestKeyValue, test.exportFunc)
if nil != err {
t.Errorf("testing %s failed: %s", test.name, err)
continue
}
actualOutput := outputBuffer.String()
if test.expectedOutput != actualOutput {
t.Error(formatInvalid(actualOutput, test.expectedOutput))
}
outputBuffer.Reset()
}
}
func TestReadAndExportEnvWithBadEjson(t *testing.T) {
var err error
outputBuffer := new(bytes.Buffer)
output = outputBuffer
// ensure that output returns to os.Stdout
defer func() {
output = os.Stdout
}()
err = ReadAndExportEnv("bad.ejson", "./key", TestKeyValue, ExportEnv)
if nil == err {
t.Fatal("failed to fail when loading a broken ejson file")
}
if !strings.Contains(err.Error(), "no such file or directory") {
t.Errorf("error should be \"no such file or directory\": %s", err)
}
}