-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
121 lines (115 loc) · 2.72 KB
/
config_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
package mikku
import (
"errors"
"os"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestConfig_validate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
GitHubAccessToken string
GitHubOwner string
wantErr error
}{
{
name: "all fields are filled",
GitHubAccessToken: "github-access-token",
GitHubOwner: "github-owner",
wantErr: nil,
},
{
name: "empty github access token",
GitHubAccessToken: "",
GitHubOwner: "github-owner",
wantErr: errEmptyGitHubAccessToken,
},
{
name: "empty github owner",
GitHubAccessToken: "github-access-token",
GitHubOwner: "",
wantErr: errEmptyGitHubOwner,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Config{
GitHubAccessToken: tt.GitHubAccessToken,
GitHubOwner: tt.GitHubOwner,
}
err := cfg.validate()
if (tt.wantErr == nil && err != nil) || (tt.wantErr != nil && !errors.As(err, &tt.wantErr)) {
t.Errorf("Config.validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestReadConfig(t *testing.T) {
tests := []struct {
name string
setEnv func() func()
want *Config
wantErr bool
}{
{
name: "no environment variable set",
setEnv: func() func() {
return func() {}
},
want: nil,
wantErr: true,
},
{
name: "GitHub access token doesn't be set",
setEnv: func() func() {
_ = os.Setenv("MIKKU_GITHUB_OWNER", "MIKKU_GITHUB_OWNER")
return func() {
_ = os.Unsetenv("MIKKU_GITHUB_OWNER")
}
},
want: nil,
wantErr: true,
},
{
name: "GitHub owner doesn't be set",
setEnv: func() func() {
_ = os.Setenv("MIKKU_GITHUB_ACCESS_TOKEN", "MIKKU_GITHUB_ACCESS_TOKEN")
return func() {
_ = os.Unsetenv("MIKKU_GITHUB_ACCESS_TOKEN")
}
},
want: nil,
wantErr: true,
},
{
name: "All env config be set",
setEnv: func() func() {
_ = os.Setenv("MIKKU_GITHUB_ACCESS_TOKEN", "MIKKU_GITHUB_ACCESS_TOKEN")
_ = os.Setenv("MIKKU_GITHUB_OWNER", "MIKKU_GITHUB_OWNER")
return func() {
_ = os.Unsetenv("MIKKU_GITHUB_ACCESS_TOKEN")
_ = os.Unsetenv("MIKKU_GITHUB_OWNER")
}
},
want: &Config{
GitHubAccessToken: "MIKKU_GITHUB_ACCESS_TOKEN",
GitHubOwner: "MIKKU_GITHUB_OWNER",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer tt.setEnv()()
got, err := readConfig()
if (err != nil) != tt.wantErr {
t.Errorf("readConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !cmp.Equal(got, tt.want) {
t.Errorf("readConfig() diff=%s", cmp.Diff(got, tt.want))
}
})
}
}