fix: disable hyperlink functionality for unsupported shells

This commit is contained in:
Jan De Dobbeleer 2024-07-24 13:17:25 +02:00 committed by Jan De Dobbeleer
parent c7e7e7464f
commit c1f99f8e6b
21 changed files with 104 additions and 0 deletions

View file

@ -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)
}

View file

@ -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{

View file

@ -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",

View file

@ -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,

View file

@ -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)
}

View file

@ -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 {

View file

@ -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{

View file

@ -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,

View file

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

View file

@ -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,
}

View file

@ -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",

View file

@ -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)
}
}

View file

@ -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{

View file

@ -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)
}
}

View file

@ -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("<LINK>file:%s<TEXT>%s</TEXT></LINK>", path, text), nil
}

View file

@ -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)
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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
}

View file

@ -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)
}