forked from amacneil/dbmate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
180 lines (145 loc) · 4.94 KB
/
main_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
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
package main
import (
"flag"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
)
func TestGetDatabaseUrl(t *testing.T) {
// set environment variables
require.NoError(t, os.Setenv("DATABASE_URL", "foo://example.org/one"))
require.NoError(t, os.Setenv("CUSTOM_URL", "foo://example.org/two"))
app := NewApp()
flagset := flag.NewFlagSet(app.Name, flag.ContinueOnError)
for _, f := range app.Flags {
require.NoError(t, f.Apply(flagset))
}
ctx := cli.NewContext(app, flagset, nil)
// no flags defaults to DATABASE_URL
u, err := getDatabaseURL(ctx)
require.NoError(t, err)
require.Equal(t, "foo://example.org/one", u.String())
// --env overwrites DATABASE_URL
require.NoError(t, ctx.Set("env", "CUSTOM_URL"))
u, err = getDatabaseURL(ctx)
require.NoError(t, err)
require.Equal(t, "foo://example.org/two", u.String())
// --url takes precedence over preceding two options
require.NoError(t, ctx.Set("url", "foo://example.org/three"))
u, err = getDatabaseURL(ctx)
require.NoError(t, err)
require.Equal(t, "foo://example.org/three", u.String())
}
func TestRedactLogString(t *testing.T) {
examples := []struct {
in string
expected string
}{
{"normal string",
"normal string"},
// malformed URL example (note forward slash in password)
{"parse \"mysql://username:otS33+tb/e4=@localhost:3306/database\": invalid",
"parse \"mysql://username:********@localhost:3306/database\": invalid"},
// invalid port, but probably not a password since there is no @
{"parse \"mysql://localhost:abc/database\": invalid",
"parse \"mysql://localhost:abc/database\": invalid"},
}
for _, ex := range examples {
require.Equal(t, ex.expected, redactLogString(ex.in))
}
}
func TestLoadEnvFiles(t *testing.T) {
setup := func(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
env := os.Environ()
os.Clearenv()
err = os.Chdir("fixtures/loadEnvFiles")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
err := os.Chdir(cwd)
if err != nil {
t.Fatal(err)
}
os.Clearenv()
for _, e := range env {
pair := strings.SplitN(e, "=", 2)
os.Setenv(pair[0], pair[1])
}
})
}
t.Run("default file is .env", func(t *testing.T) {
setup(t)
err := loadEnvFiles([]string{})
require.NoError(t, err)
require.Equal(t, 1, len(os.Environ()))
require.Equal(t, "default", os.Getenv("TEST_DOTENV"))
})
t.Run("valid file", func(t *testing.T) {
setup(t)
err := loadEnvFiles([]string{"--env-file", "first.txt"})
require.NoError(t, err)
require.Equal(t, 1, len(os.Environ()))
require.Equal(t, "one", os.Getenv("FIRST"))
})
t.Run("two valid files", func(t *testing.T) {
setup(t)
err := loadEnvFiles([]string{"--env-file", "first.txt", "--env-file", "second.txt"})
require.NoError(t, err)
require.Equal(t, 2, len(os.Environ()))
require.Equal(t, "one", os.Getenv("FIRST"))
require.Equal(t, "two", os.Getenv("SECOND"))
})
t.Run("nonexistent file", func(t *testing.T) {
setup(t)
err := loadEnvFiles([]string{"--env-file", "nonexistent.txt"})
require.NoError(t, err)
require.Equal(t, 0, len(os.Environ()))
})
t.Run("no overload", func(t *testing.T) {
setup(t)
// we do not load values over existing values
os.Setenv("FIRST", "not one")
err := loadEnvFiles([]string{"--env-file", "first.txt"})
require.NoError(t, err)
require.Equal(t, 1, len(os.Environ()))
require.Equal(t, "not one", os.Getenv("FIRST"))
})
t.Run("invalid file", func(t *testing.T) {
setup(t)
err := loadEnvFiles([]string{"--env-file", "invalid.txt"})
require.Error(t, err)
require.Contains(t, err.Error(), "unexpected character \"\\n\" in variable name near \"INVALID ENV FILE\\n\"")
require.Equal(t, 0, len(os.Environ()))
})
t.Run("invalid file followed by a valid file", func(t *testing.T) {
setup(t)
err := loadEnvFiles([]string{"--env-file", "invalid.txt", "--env-file", "first.txt"})
require.Error(t, err)
require.Contains(t, err.Error(), "unexpected character \"\\n\" in variable name near \"INVALID ENV FILE\\n\"")
require.Equal(t, 0, len(os.Environ()))
})
t.Run("valid file followed by an invalid file", func(t *testing.T) {
setup(t)
err := loadEnvFiles([]string{"--env-file", "first.txt", "--env-file", "invalid.txt"})
require.Error(t, err)
require.Contains(t, err.Error(), "unexpected character \"\\n\" in variable name near \"INVALID ENV FILE\\n\"")
require.Equal(t, 1, len(os.Environ()))
require.Equal(t, "one", os.Getenv("FIRST"))
})
t.Run("valid file followed by an invalid file followed by a valid file", func(t *testing.T) {
setup(t)
err := loadEnvFiles([]string{"--env-file", "first.txt", "--env-file", "invalid.txt", "--env-file", "second.txt"})
require.Error(t, err)
require.Contains(t, err.Error(), "unexpected character \"\\n\" in variable name near \"INVALID ENV FILE\\n\"")
// files after an invalid file should not get loaded
require.Equal(t, 1, len(os.Environ()))
require.Equal(t, "one", os.Getenv("FIRST"))
})
}