From c1f99f8e6ba5abee19f48b7a281de68bfc49c6a0 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 24 Jul 2024 13:17:25 +0200 Subject: [PATCH] fix: disable hyperlink functionality for unsupported shells --- src/config/config_test.go | 4 ++++ src/config/segment_test.go | 1 + src/prompt/engine_test.go | 3 +++ src/segments/argocd_test.go | 2 ++ src/segments/aws_test.go | 5 +++++ src/segments/az_test.go | 2 ++ src/segments/azd_test.go | 1 + src/segments/brewfather_test.go | 2 ++ src/segments/carbon_intensity_test.go | 4 ++++ src/segments/language_test.go | 3 +++ src/segments/path_test.go | 12 ++++++++++++ src/segments/status_test.go | 6 ++++++ src/segments/wakatime_test.go | 3 +++ src/template/files_test.go | 5 +++++ src/template/link.go | 11 +++++++++++ src/template/link_test.go | 6 ++++++ src/template/numbers_test.go | 5 +++++ src/template/round_test.go | 5 +++++ src/template/strings_test.go | 5 +++++ src/template/text.go | 7 +++++++ src/template/text_test.go | 12 ++++++++++++ 21 files changed, 104 insertions(+) diff --git a/src/config/config_test.go b/src/config/config_test.go index b8647576..f10f27fd 100644 --- a/src/config/config_test.go +++ b/src/config/config_test.go @@ -5,6 +5,7 @@ import ( "github.com/jandedobbeleer/oh-my-posh/src/cache" "github.com/jandedobbeleer/oh-my-posh/src/color" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -76,11 +77,14 @@ func TestGetPalette(t *testing.T) { Shell: "bash", }) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + cfg := &Config{ env: env, Palette: tc.Palette, Palettes: tc.Palettes, } + got := cfg.getPalette() assert.Equal(t, tc.ExpectedPalette, got, tc.Case) } diff --git a/src/config/segment_test.go b/src/config/segment_test.go index a978d4db..5aa9cedc 100644 --- a/src/config/segment_test.go +++ b/src/config/segment_test.go @@ -149,6 +149,7 @@ func TestGetColors(t *testing.T) { env.On("TemplateCache").Return(&cache.Template{ Env: make(map[string]string), }) + env.On("Flags").Return(&runtime.Flags{}) segment := &Segment{ writer: &segments.Aws{ diff --git a/src/prompt/engine_test.go b/src/prompt/engine_test.go index 60671174..d8c960b7 100644 --- a/src/prompt/engine_test.go +++ b/src/prompt/engine_test.go @@ -87,6 +87,7 @@ func TestPrintPWD(t *testing.T) { Env: make(map[string]string), Shell: "shell", }) + env.On("Flags").Return(&runtime.Flags{}) terminal.Init(shell.GENERIC) @@ -177,6 +178,7 @@ func TestGetTitle(t *testing.T) { env.On("Home").Return("/usr/home") env.On("PathSeparator").Return(tc.PathSeparator) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) env.On("TemplateCache").Return(&cache.Template{ Env: map[string]string{ "USERDOMAIN": "MyCompany", @@ -240,6 +242,7 @@ func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) { env.On("Pwd").Return(tc.Cwd) env.On("Home").Return("/usr/home") env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) env.On("TemplateCache").Return(&cache.Template{ Env: map[string]string{ "USERDOMAIN": "MyCompany", diff --git a/src/segments/argocd_test.go b/src/segments/argocd_test.go index 6dc8f24f..c5cd36ff 100644 --- a/src/segments/argocd_test.go +++ b/src/segments/argocd_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/jandedobbeleer/oh-my-posh/src/properties" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" testify_ "github.com/stretchr/testify/mock" @@ -251,6 +252,7 @@ servers: env.On("Getenv", argocdOptsEnv).Return(tc.Opts) env.On("FileContent", configFile).Return(tc.Config) env.On("Error", testify_.Anything).Return() + env.On("Flags").Return(&runtime.Flags{}) argocd := &Argocd{ env: env, diff --git a/src/segments/aws_test.go b/src/segments/aws_test.go index f31278e9..8f357c88 100644 --- a/src/segments/aws_test.go +++ b/src/segments/aws_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/jandedobbeleer/oh-my-posh/src/properties" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -63,13 +64,17 @@ func TestAWSSegment(t *testing.T) { props := properties.Map{ properties.DisplayDefault: tc.DisplayDefault, } + env.On("Flags").Return(&runtime.Flags{}) + aws := &Aws{ env: env, props: props, } + if tc.Template == "" { tc.Template = aws.Template() } + assert.Equal(t, tc.ExpectedEnabled, aws.Enabled(), tc.Case) assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, aws), tc.Case) } diff --git a/src/segments/az_test.go b/src/segments/az_test.go index f205534b..feb168f1 100644 --- a/src/segments/az_test.go +++ b/src/segments/az_test.go @@ -110,6 +110,8 @@ func TestAzSegment(t *testing.T) { for _, tc := range cases { env := new(mock.Environment) env.On("Home").Return(poshHome) + env.On("Flags").Return(&runtime.Flags{}) + var azureProfile, azureRmContext string if tc.HasCLI { diff --git a/src/segments/azd_test.go b/src/segments/azd_test.go index 901dabc6..699f6f27 100644 --- a/src/segments/azd_test.go +++ b/src/segments/azd_test.go @@ -37,6 +37,7 @@ func TestAzdSegment(t *testing.T) { for _, tc := range cases { env := new(mock.Environment) env.On("Debug", testify_.Anything) + env.On("Flags").Return(&runtime.Flags{}) if tc.IsInited { fileInfo := &runtime.FileInfo{ diff --git a/src/segments/brewfather_test.go b/src/segments/brewfather_test.go index d26adb97..9e8a9cbb 100644 --- a/src/segments/brewfather_test.go +++ b/src/segments/brewfather_test.go @@ -7,6 +7,7 @@ import ( cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock" "github.com/jandedobbeleer/oh-my-posh/src/properties" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -160,6 +161,7 @@ func TestBrewfatherSegment(t *testing.T) { env.On("HTTPRequest", BFBatchURL).Return([]byte(tc.BatchJSONResponse), tc.Error) env.On("HTTPRequest", BFBatchReadingsURL).Return([]byte(tc.BatchReadingsJSONResponse), tc.Error) env.On("Cache").Return(cache) + env.On("Flags").Return(&runtime.Flags{}) ns := &Brewfather{ props: props, diff --git a/src/segments/carbon_intensity_test.go b/src/segments/carbon_intensity_test.go index 188aadbc..125b387b 100644 --- a/src/segments/carbon_intensity_test.go +++ b/src/segments/carbon_intensity_test.go @@ -7,6 +7,7 @@ import ( cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock" "github.com/jandedobbeleer/oh-my-posh/src/properties" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -228,6 +229,7 @@ func TestCarbonIntensitySegmentSingle(t *testing.T) { env.On("HTTPRequest", CARBONINTENSITYURL).Return([]byte(jsonResponse), responseError) env.On("Error", testify_.Anything) + env.On("Flags").Return(&runtime.Flags{}) d := &CarbonIntensity{ props: props, @@ -259,6 +261,8 @@ func TestCarbonIntensitySegmentFromCache(t *testing.T) { env: env, } + env.On("Flags").Return(&runtime.Flags{}) + cache.On("Get", CARBONINTENSITYURL).Return(response, true) cache.On("Set").Return() env.On("Cache").Return(cache) diff --git a/src/segments/language_test.go b/src/segments/language_test.go index 4489aa45..5935739b 100644 --- a/src/segments/language_test.go +++ b/src/segments/language_test.go @@ -63,6 +63,8 @@ func bootStrapLanguageTest(args *languageArgs) *language { env.On("Pwd").Return(cwd) env.On("Home").Return(home) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + env.On("TemplateCache").Return(&cache.Template{ Env: make(map[string]string), }) @@ -583,6 +585,7 @@ func getMockedLanguageEnv(params *mockedLanguageParams) (*mock.Environment, prop Env: make(map[string]string), }) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) props := properties.Map{ properties.FetchVersion: true, } diff --git a/src/segments/path_test.go b/src/segments/path_test.go index e118c3eb..3fa39853 100644 --- a/src/segments/path_test.go +++ b/src/segments/path_test.go @@ -41,6 +41,8 @@ func renderTemplateNoTrimSpace(env *mock.Environment, segmentTemplate string, co env.On("Error", testify_.Anything) env.On("Debug", testify_.Anything) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + tmpl := &template.Text{ Template: segmentTemplate, Context: context, @@ -1013,6 +1015,8 @@ func TestPowerlevelMappedLocations(t *testing.T) { env.On("PathSeparator").Return("/") env.On("Shell").Return(shell.GENERIC) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + path := &Path{ env: env, props: properties.Map{ @@ -1020,8 +1024,10 @@ func TestPowerlevelMappedLocations(t *testing.T) { MappedLocations: tc.MappedLocations, }, } + path.setPaths() path.setStyle() + got := renderTemplateNoTrimSpace(env, "{{ .Path }}", path) assert.Equal(t, tc.Expected, got) } @@ -1462,6 +1468,8 @@ func TestGetFolderSeparator(t *testing.T) { env.On("Error", testify_.Anything) env.On("Debug", testify_.Anything) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + path := &Path{ env: env, pathSeparator: "/", @@ -1540,6 +1548,8 @@ func TestReplaceMappedLocations(t *testing.T) { env.On("GOOS").Return(runtime.DARWIN) env.On("Home").Return("/a/b/k") env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + path := &Path{ env: env, props: properties.Map{ @@ -1552,6 +1562,7 @@ func TestReplaceMappedLocations(t *testing.T) { }, }, } + path.setPaths() assert.Equal(t, tc.Expected, path.pwd) } @@ -1659,6 +1670,7 @@ func TestGetMaxWidth(t *testing.T) { env := new(mock.Environment) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) env.On("Error", testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) env.On("TemplateCache").Return(&cache.Template{ Env: map[string]string{ "MAX_WIDTH": "120", diff --git a/src/segments/status_test.go b/src/segments/status_test.go index 49b251bc..b18f9027 100644 --- a/src/segments/status_test.go +++ b/src/segments/status_test.go @@ -5,6 +5,7 @@ import ( "github.com/jandedobbeleer/oh-my-posh/src/cache" "github.com/jandedobbeleer/oh-my-posh/src/properties" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -31,6 +32,7 @@ func TestStatusWriterEnabled(t *testing.T) { }) env.On("Error", testify_.Anything).Return(nil) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) props := properties.Map{} if len(tc.Template) > 0 { @@ -97,12 +99,16 @@ func TestFormatStatus(t *testing.T) { }) env.On("Error", testify_.Anything).Return(nil) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + props := properties.Map{ StatusTemplate: tc.Template, StatusSeparator: tc.Separator, } + s := &Status{} s.Init(props, env) + assert.Equal(t, tc.Expected, s.formatStatus(tc.Status, tc.PipeStatus), tc.Case) } } diff --git a/src/segments/wakatime_test.go b/src/segments/wakatime_test.go index 79e17a16..e3cd053c 100644 --- a/src/segments/wakatime_test.go +++ b/src/segments/wakatime_test.go @@ -8,6 +8,7 @@ import ( "github.com/jandedobbeleer/oh-my-posh/src/cache" cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock" "github.com/jandedobbeleer/oh-my-posh/src/properties" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -83,6 +84,7 @@ func TestWTTrackedTime(t *testing.T) { mockedCache.On("Set", FAKEAPIURL, response, tc.CacheTimeout).Return() env.On("Cache").Return(mockedCache) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) env.On("TemplateCache").Return(&cache.Template{ Env: map[string]string{"HELLO": "hello"}, @@ -133,6 +135,7 @@ func TestWTGetUrl(t *testing.T) { env.On("TemplateCache").Return(&cache.Template{ Env: map[string]string{"HELLO": "hello"}, }) + env.On("Flags").Return(&runtime.Flags{}) w := &Wakatime{ props: properties.Map{ diff --git a/src/template/files_test.go b/src/template/files_test.go index f2161bdc..8e6c93c6 100644 --- a/src/template/files_test.go +++ b/src/template/files_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/jandedobbeleer/oh-my-posh/src/cache" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -27,17 +28,21 @@ func TestGlob(t *testing.T) { env.On("TemplateCache").Return(&cache.Template{ Env: make(map[string]string), }) + env.On("Flags").Return(&runtime.Flags{}) + for _, tc := range cases { tmpl := &Text{ Template: tc.Template, Context: nil, Env: env, } + text, err := tmpl.Render() if tc.ShouldError { assert.Error(t, err) continue } + assert.Equal(t, tc.Expected, text, tc.Case) } } diff --git a/src/template/link.go b/src/template/link.go index 1fef434e..3b180e54 100644 --- a/src/template/link.go +++ b/src/template/link.go @@ -3,10 +3,16 @@ package template import ( "fmt" link "net/url" + "slices" ) // url builds an hyperlink if url is not empty, otherwise returns the text only func url(text, url string) (string, error) { + unsupported := []string{elvish, xonsh} + if slices.Contains(unsupported, shell) { + return text, nil + } + if url == "" { return text, nil } @@ -18,5 +24,10 @@ func url(text, url string) (string, error) { } func path(text, path string) (string, error) { + unsupported := []string{elvish, xonsh} + if slices.Contains(unsupported, shell) { + return text, nil + } + return fmt.Sprintf("file:%s%s", path, text), nil } diff --git a/src/template/link_test.go b/src/template/link_test.go index ee9cd954..adc25363 100644 --- a/src/template/link_test.go +++ b/src/template/link_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/jandedobbeleer/oh-my-posh/src/cache" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -28,6 +29,8 @@ func TestUrl(t *testing.T) { env.On("Error", testify_.Anything) env.On("Debug", testify_.Anything) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + for _, tc := range cases { tmpl := &Text{ Template: tc.Template, @@ -57,12 +60,15 @@ func TestPath(t *testing.T) { env.On("TemplateCache").Return(&cache.Template{ Env: make(map[string]string), }) + env.On("Flags").Return(&runtime.Flags{}) + for _, tc := range cases { tmpl := &Text{ Template: tc.Template, Context: nil, Env: env, } + text, _ := tmpl.Render() assert.Equal(t, tc.Expected, text, tc.Case) } diff --git a/src/template/numbers_test.go b/src/template/numbers_test.go index 932c5d03..3a4feda8 100644 --- a/src/template/numbers_test.go +++ b/src/template/numbers_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/jandedobbeleer/oh-my-posh/src/cache" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -28,17 +29,21 @@ func TestHResult(t *testing.T) { env.On("Error", testify_.Anything) env.On("Debug", testify_.Anything) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + for _, tc := range cases { tmpl := &Text{ Template: tc.Template, Context: nil, Env: env, } + text, err := tmpl.Render() if tc.ShouldError { assert.Error(t, err) continue } + assert.Equal(t, tc.Expected, text, tc.Case) } } diff --git a/src/template/round_test.go b/src/template/round_test.go index f8840c3a..06d1ab30 100644 --- a/src/template/round_test.go +++ b/src/template/round_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/jandedobbeleer/oh-my-posh/src/cache" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -35,17 +36,21 @@ func TestRoundSeconds(t *testing.T) { env.On("Error", testify_.Anything) env.On("Debug", testify_.Anything) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + for _, tc := range cases { tmpl := &Text{ Template: tc.Template, Context: nil, Env: env, } + text, err := tmpl.Render() if tc.ShouldError { assert.Error(t, err) continue } + assert.Equal(t, tc.Expected, text, tc.Case) } } diff --git a/src/template/strings_test.go b/src/template/strings_test.go index 9eded460..dfc0c6be 100644 --- a/src/template/strings_test.go +++ b/src/template/strings_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/jandedobbeleer/oh-my-posh/src/cache" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -32,17 +33,21 @@ func TestTrunc(t *testing.T) { env.On("Error", testify_.Anything) env.On("Debug", testify_.Anything) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + for _, tc := range cases { tmpl := &Text{ Template: tc.Template, Context: nil, Env: env, } + text, err := tmpl.Render() if tc.ShouldError { assert.Error(t, err) continue } + assert.Equal(t, tc.Expected, text, tc.Case) } } diff --git a/src/template/text.go b/src/template/text.go index 2671c758..51e6db3c 100644 --- a/src/template/text.go +++ b/src/template/text.go @@ -18,6 +18,9 @@ const ( IncorrectTemplate = "unable to create text based on template" globalRef = ".$" + + elvish = "elvish" + xonsh = "xonsh" ) var ( @@ -41,6 +44,8 @@ var ( "Var", "Data", } + + shell string ) type Text struct { @@ -72,6 +77,8 @@ func (c *Context) init(t *Text) { func (t *Text) Render() (string, error) { t.Env.DebugF("Rendering template: %s", t.Template) + shell = t.Env.Flags().Shell + if !strings.Contains(t.Template, "{{") || !strings.Contains(t.Template, "}}") { return t.Template, nil } diff --git a/src/template/text_test.go b/src/template/text_test.go index 0b0cd304..b1e8fefa 100644 --- a/src/template/text_test.go +++ b/src/template/text_test.go @@ -5,6 +5,7 @@ import ( "github.com/jandedobbeleer/oh-my-posh/src/cache" "github.com/jandedobbeleer/oh-my-posh/src/maps" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" "github.com/stretchr/testify/assert" @@ -162,17 +163,21 @@ func TestRenderTemplate(t *testing.T) { }) env.On("Error", testify_.Anything) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + for _, tc := range cases { tmpl := &Text{ Template: tc.Template, Context: tc.Context, Env: env, } + text, err := tmpl.Render() if tc.ShouldError { assert.Error(t, err) continue } + assert.NoError(t, err) assert.Equal(t, tc.Expected, text, tc.Case) } @@ -248,16 +253,20 @@ func TestRenderTemplateEnvVar(t *testing.T) { }) env.On("Error", testify_.Anything) env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil) + env.On("Flags").Return(&runtime.Flags{}) + tmpl := &Text{ Template: tc.Template, Context: tc.Context, Env: env, } + text, err := tmpl.Render() if tc.ShouldError { assert.Error(t, err) continue } + assert.Equal(t, tc.Expected, text, tc.Case) } } @@ -362,12 +371,15 @@ func TestSegmentContains(t *testing.T) { Env: make(map[string]string), Segments: segments, }) + env.On("Flags").Return(&runtime.Flags{}) + for _, tc := range cases { tmpl := &Text{ Template: tc.Template, Context: nil, Env: env, } + text, _ := tmpl.Render() assert.Equal(t, tc.Expected, text, tc.Case) }