Skip to content

Commit a31a539

Browse files
committed
Refactor FirstNonEmptyString to accept empty values
1 parent 604df3a commit a31a539

File tree

4 files changed

+96
-70
lines changed

4 files changed

+96
-70
lines changed

cmd/logfile/logfile_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func TestLoadParams_LogFile_FromEnvVar(t *testing.T) {
8383
ctx := context.Background()
8484

8585
v := viper.New()
86+
8687
t.Setenv("WAKATIME_HOME", dir)
8788

8889
params, err := logfile.LoadParams(ctx, v)

cmd/params/params_test.go

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,86 +2345,103 @@ func TestLoadAPIParams_APIKeyFromEnv_ConfigTakesPrecedence(t *testing.T) {
23452345
assert.Equal(t, "00000000-0000-4000-8000-000000000000", params.Key)
23462346
}
23472347

2348-
func TestLoadAPIParams_APIUrl(t *testing.T) {
2348+
func TestLoadAPIParams_APIUrl_Sanitize(t *testing.T) {
23492349
ctx := context.Background()
23502350

23512351
tests := map[string]struct {
2352-
ViperAPIUrl string
2353-
ViperAPIUrlConfig string
2354-
ViperAPIUrlOld string
2355-
Expected cmdparams.API
2352+
URL string
2353+
Expected string
23562354
}{
2357-
"api url flag takes precedence": {
2358-
ViperAPIUrl: "http://localhost:8080",
2359-
ViperAPIUrlConfig: "http://localhost:8081",
2360-
ViperAPIUrlOld: "http://localhost:8082",
2361-
Expected: cmdparams.API{
2362-
Key: "00000000-0000-4000-8000-000000000000",
2363-
URL: "http://localhost:8080",
2364-
Hostname: "my-computer",
2365-
},
2366-
},
2367-
"api url deprecated flag takes precedence": {
2368-
ViperAPIUrlConfig: "http://localhost:8081",
2369-
ViperAPIUrlOld: "http://localhost:8082",
2370-
Expected: cmdparams.API{
2371-
Key: "00000000-0000-4000-8000-000000000000",
2372-
URL: "http://localhost:8082",
2373-
Hostname: "my-computer",
2374-
},
2375-
},
2376-
"api url from config": {
2377-
ViperAPIUrlConfig: "http://localhost:8081",
2378-
Expected: cmdparams.API{
2379-
Key: "00000000-0000-4000-8000-000000000000",
2380-
URL: "http://localhost:8081",
2381-
Hostname: "my-computer",
2382-
},
2383-
},
23842355
"api url with legacy heartbeats endpoint": {
2385-
ViperAPIUrl: "http://localhost:8080/api/v1/heartbeats.bulk",
2386-
Expected: cmdparams.API{
2387-
Key: "00000000-0000-4000-8000-000000000000",
2388-
URL: "http://localhost:8080/api/v1",
2389-
Hostname: "my-computer",
2390-
},
2356+
URL: "http://localhost:8080/api/v1/heartbeats.bulk",
2357+
Expected: "http://localhost:8080/api/v1",
2358+
},
2359+
"api url with users heartbeats endpoint": {
2360+
URL: "http://localhost:8080/users/current/heartbeats",
2361+
Expected: "http://localhost:8080",
23912362
},
23922363
"api url with trailing slash": {
2393-
ViperAPIUrl: "http://localhost:8080/api/",
2394-
Expected: cmdparams.API{
2395-
Key: "00000000-0000-4000-8000-000000000000",
2396-
URL: "http://localhost:8080/api",
2397-
Hostname: "my-computer",
2398-
},
2364+
URL: "http://localhost:8080/api/",
2365+
Expected: "http://localhost:8080/api",
23992366
},
24002367
"api url with wakapi style endpoint": {
2401-
ViperAPIUrl: "http://localhost:8080/api/heartbeat",
2402-
Expected: cmdparams.API{
2403-
Key: "00000000-0000-4000-8000-000000000000",
2404-
URL: "http://localhost:8080/api",
2405-
Hostname: "my-computer",
2406-
},
2368+
URL: "http://localhost:8080/api/heartbeat",
2369+
Expected: "http://localhost:8080/api",
24072370
},
24082371
}
24092372

24102373
for name, test := range tests {
24112374
t.Run(name, func(t *testing.T) {
24122375
v := viper.New()
2413-
v.Set("hostname", "my-computer")
2414-
v.Set("timeout", 0)
24152376
v.Set("key", "00000000-0000-4000-8000-000000000000")
2416-
v.Set("api-url", test.ViperAPIUrl)
2417-
v.Set("apiurl", test.ViperAPIUrlOld)
2418-
v.Set("settings.api_url", test.ViperAPIUrlConfig)
2377+
v.Set("api-url", test.URL)
24192378

24202379
params, err := cmdparams.LoadAPIParams(ctx, v)
24212380
require.NoError(t, err)
24222381

2423-
assert.Equal(t, test.Expected, params)
2382+
assert.Equal(t, test.Expected, params.URL)
24242383
})
24252384
}
24262385
}
24272386

2387+
func TestLoadAPIParams_Url(t *testing.T) {
2388+
ctx := context.Background()
2389+
2390+
v := viper.New()
2391+
2392+
v.Set("key", "00000000-0000-4000-8000-000000000000")
2393+
v.Set("api-url", "http://localhost:8080")
2394+
2395+
params, err := cmdparams.LoadAPIParams(ctx, v)
2396+
require.NoError(t, err)
2397+
2398+
assert.Equal(t, "http://localhost:8080", params.URL)
2399+
}
2400+
2401+
func TestLoadAPIParams_Url_FlagTakesPrecedence(t *testing.T) {
2402+
ctx := context.Background()
2403+
2404+
v := viper.New()
2405+
2406+
v.Set("key", "00000000-0000-4000-8000-000000000000")
2407+
v.Set("api-url", "http://localhost:8080")
2408+
v.Set("settings.api_url", "http://localhost:8081")
2409+
2410+
params, err := cmdparams.LoadAPIParams(ctx, v)
2411+
require.NoError(t, err)
2412+
2413+
assert.Equal(t, "http://localhost:8080", params.URL)
2414+
}
2415+
2416+
func TestLoadAPIParams_Url_FlagDeprecatedTakesPrecedence(t *testing.T) {
2417+
ctx := context.Background()
2418+
2419+
v := viper.New()
2420+
2421+
v.Set("key", "00000000-0000-4000-8000-000000000000")
2422+
v.Set("apiurl", "http://localhost:8080")
2423+
v.Set("settings.api_url", "http://localhost:8081")
2424+
2425+
params, err := cmdparams.LoadAPIParams(ctx, v)
2426+
require.NoError(t, err)
2427+
2428+
assert.Equal(t, "http://localhost:8080", params.URL)
2429+
}
2430+
2431+
func TestLoadAPIParams_Url_FromConfig(t *testing.T) {
2432+
ctx := context.Background()
2433+
2434+
v := viper.New()
2435+
2436+
v.Set("key", "00000000-0000-4000-8000-000000000000")
2437+
v.Set("settings.api_url", "http://localhost:8081")
2438+
2439+
params, err := cmdparams.LoadAPIParams(ctx, v)
2440+
require.NoError(t, err)
2441+
2442+
assert.Equal(t, "http://localhost:8081", params.URL)
2443+
}
2444+
24282445
func TestLoadAPIParams_Url_Default(t *testing.T) {
24292446
v := viper.New()
24302447
v.Set("key", "00000000-0000-4000-8000-000000000000")
@@ -2444,7 +2461,7 @@ func TestLoadAPIParams_Url_InvalidFormat(t *testing.T) {
24442461

24452462
var errauth api.ErrAuth
24462463

2447-
assert.ErrorAs(t, err, &errauth)
2464+
require.ErrorAs(t, err, &errauth)
24482465
assert.EqualError(t, errauth, `invalid api url: parse "http://in valid": invalid character " " in host name`)
24492466
}
24502467

pkg/vipertools/vipertools.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,21 @@ func FirstNonEmptyString(v *viper.Viper, keys ...string) string {
6868
}
6969

7070
for _, key := range keys {
71-
// if !v.IsSet(key) {
72-
// continue
73-
// }
74-
75-
// value := v.Get(key)
76-
77-
// parsed, err := cast.ToStringE(value)
78-
// if err != nil {
79-
// continue
80-
// }
71+
if !v.IsSet(key) {
72+
continue
73+
}
8174

82-
// return parsed
75+
value := v.Get(key)
8376

84-
if value := GetString(v, key); value != "" {
85-
return value
77+
parsed, err := cast.ToStringE(value)
78+
if err != nil {
79+
continue
8680
}
81+
82+
return strings.Trim(parsed, `"'`)
83+
// if value := GetString(v, key); value != "" {
84+
// return value
85+
// }
8786
}
8887

8988
return ""

pkg/vipertools/vipertools_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ func TestFirstNonEmptyString(t *testing.T) {
103103
assert.Equal(t, "secret", value)
104104
}
105105

106+
func TestFirstNonEmptyString_Empty(t *testing.T) {
107+
v := viper.New()
108+
v.Set("second", "")
109+
v.Set("third", "secret")
110+
111+
value := vipertools.FirstNonEmptyString(v, "first", "second", "third")
112+
assert.Empty(t, value)
113+
}
114+
106115
func TestFirstNonEmptyString_NilPointer(t *testing.T) {
107116
value := vipertools.FirstNonEmptyString(nil, "first")
108117
assert.Empty(t, value)

0 commit comments

Comments
 (0)