feat(wakatime): parse url as template

This commit is contained in:
Jan De Dobbeleer 2022-08-16 20:55:08 +02:00 committed by GitHub
parent 70008eccab
commit 32f1977bdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 2 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/properties" "oh-my-posh/properties"
"oh-my-posh/template"
) )
type Wakatime struct { type Wakatime struct {
@ -34,7 +35,10 @@ func (w *Wakatime) Enabled() bool {
} }
func (w *Wakatime) setAPIData() error { func (w *Wakatime) setAPIData() error {
url := w.props.GetString(URL, "") url, err := w.getURL()
if err != nil {
return err
}
cacheTimeout := w.props.GetInt(properties.CacheTimeout, properties.DefaultCacheTimeout) cacheTimeout := w.props.GetInt(properties.CacheTimeout, properties.DefaultCacheTimeout)
if cacheTimeout > 0 { if cacheTimeout > 0 {
// check if data stored in cache // check if data stored in cache
@ -64,6 +68,16 @@ func (w *Wakatime) setAPIData() error {
return nil return nil
} }
func (w *Wakatime) getURL() (string, error) {
url := w.props.GetString(URL, "")
tmpl := &template.Text{
Template: url,
Context: w,
Env: w.env,
}
return tmpl.Render()
}
func (w *Wakatime) Init(props properties.Properties, env environment.Environment) { func (w *Wakatime) Init(props properties.Properties, env environment.Environment) {
w.props = props w.props = props
w.env = env w.env = env

View file

@ -3,11 +3,13 @@ package segments
import ( import (
"errors" "errors"
"fmt" "fmt"
"oh-my-posh/environment"
"oh-my-posh/mock" "oh-my-posh/mock"
"oh-my-posh/properties" "oh-my-posh/properties"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
mock2 "github.com/stretchr/testify/mock"
) )
func TestWTTrackedTime(t *testing.T) { func TestWTTrackedTime(t *testing.T) {
@ -80,9 +82,12 @@ func TestWTTrackedTime(t *testing.T) {
cache.On("Set", FAKEAPIURL, response, tc.CacheTimeout).Return() cache.On("Set", FAKEAPIURL, response, tc.CacheTimeout).Return()
env.On("Cache").Return(cache) env.On("Cache").Return(cache)
env.On("TemplateCache").Return(&environment.TemplateCache{
Env: map[string]string{"HELLO": "hello"},
})
w := &Wakatime{ w := &Wakatime{
props: properties.Map{ props: properties.Map{
APIKey: "key",
properties.CacheTimeout: tc.CacheTimeout, properties.CacheTimeout: tc.CacheTimeout,
URL: FAKEAPIURL, URL: FAKEAPIURL,
}, },
@ -93,3 +98,52 @@ func TestWTTrackedTime(t *testing.T) {
assert.Equal(t, tc.Expected, renderTemplate(env, w.Template(), w), tc.Case+" - String") assert.Equal(t, tc.Expected, renderTemplate(env, w.Template(), w), tc.Case+" - String")
} }
} }
func TestWTGetUrl(t *testing.T) {
cases := []struct {
Case string
Expected string
URL string
ShouldError bool
}{
{
Case: "no template",
Expected: "test",
URL: "test",
},
{
Case: "template",
URL: "{{ .Env.HELLO }} world",
Expected: "hello world",
},
{
Case: "error",
URL: "{{ .BURR }}",
ShouldError: true,
},
}
for _, tc := range cases {
env := &mock.MockedEnvironment{}
env.On("Log", mock2.Anything, mock2.Anything, mock2.Anything)
env.On("TemplateCache").Return(&environment.TemplateCache{
Env: map[string]string{"HELLO": "hello"},
})
w := &Wakatime{
props: properties.Map{
URL: tc.URL,
},
env: env,
}
got, err := w.getURL()
if tc.ShouldError {
assert.Error(t, err, tc.Case)
continue
}
assert.Equal(t, tc.Expected, got, tc.Case)
}
}