-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsams_test.go
80 lines (72 loc) · 1.76 KB
/
sams_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
package sams
import (
"testing"
"github.com/hexops/autogold/v2"
"github.com/hexops/valast"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type staticEnvGetter map[string]string
func (e staticEnvGetter) Get(name, defaultValue, _ string) string {
v, ok := e[name]
if !ok {
return defaultValue
}
return v
}
func (e staticEnvGetter) GetOptional(name, description string) *string {
v := e.Get(name, "", description)
if v == "" {
return nil
}
return &v
}
func TestNewClientV1ConnectionConfigFromEnv(t *testing.T) {
for _, tc := range []struct {
name string
env staticEnvGetter
want autogold.Value
wantValidateErr autogold.Value
}{
{
name: "no env",
env: staticEnvGetter{},
want: autogold.Expect(ConnConfig{ExternalURL: "https://accounts.sourcegraph.com"}),
},
{
name: "override API URL",
env: staticEnvGetter{
"SAMS_API_URL": "https://my-internal-url.net",
},
want: autogold.Expect(ConnConfig{
ExternalURL: "https://accounts.sourcegraph.com",
APIURL: valast.Addr("https://my-internal-url.net").(*string),
}),
},
{
name: "set all",
env: staticEnvGetter{
"SAMS_URL": "https://my-external-url.net",
"SAMS_API_URL": "https://my-internal-url.net",
},
want: autogold.Expect(ConnConfig{
ExternalURL: "https://my-external-url.net",
APIURL: valast.Addr("https://my-internal-url.net").(*string),
}),
},
} {
t.Run(tc.name, func(t *testing.T) {
got := NewConnConfigFromEnv(tc.env)
tc.want.Equal(t, got)
t.Run("Validate", func(t *testing.T) {
err := got.Validate()
if tc.wantValidateErr != nil {
require.Error(t, err)
tc.wantValidateErr.Equal(t, err.Error())
} else {
assert.NoError(t, err)
}
})
})
}
}