Skip to content

Commit 921b34b

Browse files
committed
fixup main tests
1 parent 2d77f7b commit 921b34b

2 files changed

Lines changed: 39 additions & 34 deletions

File tree

cmd/src/main.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ type config struct {
139139
endpointURL *url.URL
140140
}
141141

142+
// configFromFile holds the config as read from the config file,
143+
// which is validated and parsed into the config struct
144+
type configFromFile struct {
145+
Endpoint string `json:"endpoint"`
146+
AccessToken string `json:"accessToken"`
147+
AdditionalHeaders map[string]string `json:"additionalHeaders"`
148+
Proxy string `json:"proxy"`
149+
}
150+
142151
// apiClient returns an api.Client built from the configuration.
143152
func (c *config) apiClient(flags *api.Flags, out io.Writer) api.Client {
144153
opts := api.ClientOpts{
@@ -179,24 +188,20 @@ func readConfig() (*config, error) {
179188
if err != nil && (!os.IsNotExist(err) || userSpecified) {
180189
return nil, err
181190
}
182-
var configFromFile struct {
183-
Endpoint string `json:"endpoint"`
184-
AccessToken string `json:"accessToken"`
185-
AdditionalHeaders map[string]string `json:"additionalHeaders"`
186-
Proxy string `json:"proxy"`
187-
}
191+
192+
var cfgFromFile configFromFile
188193
var cfg config
189194
var endpointStr string
190195
var proxyStr string
191196
if err == nil {
192197
cfg.configFilePath = cfgPath
193-
if err := json.Unmarshal(data, &configFromFile); err != nil {
198+
if err := json.Unmarshal(data, &cfgFromFile); err != nil {
194199
return nil, err
195200
}
196-
endpointStr = configFromFile.Endpoint
197-
cfg.accessToken = configFromFile.AccessToken
198-
cfg.additionalHeaders = configFromFile.AdditionalHeaders
199-
proxyStr = configFromFile.Proxy
201+
endpointStr = cfgFromFile.Endpoint
202+
cfg.accessToken = cfgFromFile.AccessToken
203+
cfg.additionalHeaders = cfgFromFile.AdditionalHeaders
204+
proxyStr = cfgFromFile.Proxy
200205
}
201206

202207
envToken := os.Getenv("SRC_ACCESS_TOKEN")

cmd/src/main_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestReadConfig(t *testing.T) {
3030

3131
tests := []struct {
3232
name string
33-
fileContents map[string]any
33+
fileContents *configFromFile
3434
envToken string
3535
envFooHeader string
3636
envHeaders string
@@ -52,10 +52,10 @@ func TestReadConfig(t *testing.T) {
5252
},
5353
{
5454
name: "config file, no overrides, trim slash",
55-
fileContents: map[string]any{
56-
"endpoint": "https://example.com/",
57-
"accessToken": "deadbeef",
58-
"proxy": "https://proxy.com:8080",
55+
fileContents: &configFromFile{
56+
Endpoint: "https://example.com/",
57+
AccessToken: "deadbeef",
58+
Proxy: "https://proxy.com:8080",
5959
},
6060
want: &config{
6161
endpointURL: &url.URL{
@@ -73,30 +73,30 @@ func TestReadConfig(t *testing.T) {
7373
},
7474
{
7575
name: "config file, token override only",
76-
fileContents: map[string]any{
77-
"endpoint": "https://example.com/",
78-
"accessToken": "deadbeef",
76+
fileContents: &configFromFile{
77+
Endpoint: "https://example.com/",
78+
AccessToken: "deadbeef",
7979
},
8080
envToken: "abc",
8181
want: nil,
8282
wantErr: errConfigMerge.Error(),
8383
},
8484
{
8585
name: "config file, endpoint override only",
86-
fileContents: map[string]any{
87-
"endpoint": "https://example.com/",
88-
"accessToken": "deadbeef",
86+
fileContents: &configFromFile{
87+
Endpoint: "https://example.com/",
88+
AccessToken: "deadbeef",
8989
},
9090
envEndpoint: "https://exmaple2.com",
9191
want: nil,
9292
wantErr: errConfigMerge.Error(),
9393
},
9494
{
9595
name: "config file, proxy override only (allow)",
96-
fileContents: map[string]any{
97-
"endpoint": "https://example.com/",
98-
"accessToken": "deadbeef",
99-
"proxy": "https://proxy.com:8080",
96+
fileContents: &configFromFile{
97+
Endpoint: "https://example.com/",
98+
AccessToken: "deadbeef",
99+
Proxy: "https://proxy.com:8080",
100100
},
101101
envProxy: "socks5://other.proxy.com:9999",
102102
want: &config{
@@ -115,10 +115,10 @@ func TestReadConfig(t *testing.T) {
115115
},
116116
{
117117
name: "config file, all override",
118-
fileContents: map[string]any{
119-
"endpoint": "https://example.com/",
120-
"accessToken": "deadbeef",
121-
"proxy": "https://proxy.com:8080",
118+
fileContents: &configFromFile{
119+
Endpoint: "https://example.com/",
120+
AccessToken: "deadbeef",
121+
Proxy: "https://proxy.com:8080",
122122
},
123123
envToken: "abc",
124124
envEndpoint: "https://override.com",
@@ -258,10 +258,10 @@ func TestReadConfig(t *testing.T) {
258258
{
259259
name: "endpoint flag should override config",
260260
flagEndpoint: "https://override.com/",
261-
fileContents: map[string]any{
262-
"endpoint": "https://example.com/",
263-
"accessToken": "deadbeef",
264-
"additionalHeaders": map[string]string{},
261+
fileContents: &configFromFile{
262+
Endpoint: "https://example.com/",
263+
AccessToken: "deadbeef",
264+
AdditionalHeaders: map[string]string{},
265265
},
266266
want: &config{
267267
endpointURL: &url.URL{

0 commit comments

Comments
 (0)