Skip to content

Commit 2cd44d0

Browse files
committed
fixup main tests
1 parent 5c98127 commit 2cd44d0

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

cmd/src/main.go

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

140+
// configFromFile holds the config as read from the config file,
141+
// which is validated and parsed into the config struct
142+
type configFromFile struct {
143+
Endpoint string `json:"endpoint"`
144+
AccessToken string `json:"accessToken"`
145+
AdditionalHeaders map[string]string `json:"additionalHeaders"`
146+
Proxy string `json:"proxy"`
147+
}
148+
140149
// apiClient returns an api.Client built from the configuration.
141150
func (c *config) apiClient(flags *api.Flags, out io.Writer) api.Client {
142151
return api.NewClient(api.ClientOpts{
@@ -168,24 +177,20 @@ func readConfig() (*config, error) {
168177
if err != nil && (!os.IsNotExist(err) || userSpecified) {
169178
return nil, err
170179
}
171-
var configFromFile struct {
172-
Endpoint string `json:"endpoint"`
173-
AccessToken string `json:"accessToken"`
174-
AdditionalHeaders map[string]string `json:"additionalHeaders"`
175-
Proxy string `json:"proxy"`
176-
}
180+
181+
var cfgFromFile configFromFile
177182
var cfg config
178183
var endpointStr string
179184
var proxyStr string
180185
if err == nil {
181186
cfg.configFilePath = cfgPath
182-
if err := json.Unmarshal(data, &configFromFile); err != nil {
187+
if err := json.Unmarshal(data, &cfgFromFile); err != nil {
183188
return nil, err
184189
}
185-
endpointStr = configFromFile.Endpoint
186-
cfg.accessToken = configFromFile.AccessToken
187-
cfg.additionalHeaders = configFromFile.AdditionalHeaders
188-
proxyStr = configFromFile.Proxy
190+
endpointStr = cfgFromFile.Endpoint
191+
cfg.accessToken = cfgFromFile.AccessToken
192+
cfg.additionalHeaders = cfgFromFile.AdditionalHeaders
193+
proxyStr = cfgFromFile.Proxy
189194
}
190195

191196
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)