From 32f1977bdc04d0e8f217646d137ad897851d1441 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer <2492783+JanDeDobbeleer@users.noreply.github.com> Date: Tue, 16 Aug 2022 20:55:08 +0200 Subject: [PATCH] feat(wakatime): parse url as template --- src/segments/wakatime.go | 16 +++++++++- src/segments/wakatime_test.go | 56 ++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/segments/wakatime.go b/src/segments/wakatime.go index 7a1fa095..ac4420cb 100644 --- a/src/segments/wakatime.go +++ b/src/segments/wakatime.go @@ -4,6 +4,7 @@ import ( "encoding/json" "oh-my-posh/environment" "oh-my-posh/properties" + "oh-my-posh/template" ) type Wakatime struct { @@ -34,7 +35,10 @@ func (w *Wakatime) Enabled() bool { } 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) if cacheTimeout > 0 { // check if data stored in cache @@ -64,6 +68,16 @@ func (w *Wakatime) setAPIData() error { 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) { w.props = props w.env = env diff --git a/src/segments/wakatime_test.go b/src/segments/wakatime_test.go index aebd3b65..cd8071d7 100644 --- a/src/segments/wakatime_test.go +++ b/src/segments/wakatime_test.go @@ -3,11 +3,13 @@ package segments import ( "errors" "fmt" + "oh-my-posh/environment" "oh-my-posh/mock" "oh-my-posh/properties" "testing" "github.com/stretchr/testify/assert" + mock2 "github.com/stretchr/testify/mock" ) func TestWTTrackedTime(t *testing.T) { @@ -80,9 +82,12 @@ func TestWTTrackedTime(t *testing.T) { cache.On("Set", FAKEAPIURL, response, tc.CacheTimeout).Return() env.On("Cache").Return(cache) + env.On("TemplateCache").Return(&environment.TemplateCache{ + Env: map[string]string{"HELLO": "hello"}, + }) + w := &Wakatime{ props: properties.Map{ - APIKey: "key", properties.CacheTimeout: tc.CacheTimeout, URL: FAKEAPIURL, }, @@ -93,3 +98,52 @@ func TestWTTrackedTime(t *testing.T) { 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) + } +}