2022-01-26 23:38:46 -08:00
|
|
|
package engine
|
2019-03-13 04:14:30 -07:00
|
|
|
|
|
|
|
import (
|
2020-10-02 07:58:25 -07:00
|
|
|
"encoding/json"
|
2019-03-13 04:14:30 -07:00
|
|
|
"testing"
|
|
|
|
|
2022-12-28 08:30:48 -08:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/mock"
|
|
|
|
"github.com/jandedobbeleer/oh-my-posh/platform"
|
|
|
|
"github.com/jandedobbeleer/oh-my-posh/properties"
|
|
|
|
"github.com/jandedobbeleer/oh-my-posh/segments"
|
|
|
|
|
2019-03-13 04:14:30 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-12-06 10:34:42 -08:00
|
|
|
const (
|
2021-02-15 23:36:37 -08:00
|
|
|
cwd = "Projects/oh-my-posh"
|
2020-12-06 10:34:42 -08:00
|
|
|
)
|
|
|
|
|
2019-03-13 04:14:30 -07:00
|
|
|
func TestMapSegmentWriterCanMap(t *testing.T) {
|
|
|
|
sc := &Segment{
|
2022-01-26 05:10:18 -08:00
|
|
|
Type: SESSION,
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
2022-01-26 01:23:18 -08:00
|
|
|
env := new(mock.MockedEnvironment)
|
2020-10-12 23:57:46 -07:00
|
|
|
err := sc.mapSegmentWithWriter(env)
|
2020-09-17 07:51:29 -07:00
|
|
|
assert.NoError(t, err)
|
2019-03-13 04:14:30 -07:00
|
|
|
assert.NotNil(t, sc.writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMapSegmentWriterCannotMap(t *testing.T) {
|
|
|
|
sc := &Segment{
|
|
|
|
Type: "nilwriter",
|
|
|
|
}
|
2022-01-26 01:23:18 -08:00
|
|
|
env := new(mock.MockedEnvironment)
|
2020-10-12 23:57:46 -07:00
|
|
|
err := sc.mapSegmentWithWriter(env)
|
2020-09-17 07:51:29 -07:00
|
|
|
assert.Error(t, err)
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
2020-10-02 07:58:25 -07:00
|
|
|
|
2021-03-20 11:32:15 -07:00
|
|
|
func TestParseTestConfig(t *testing.T) {
|
2020-10-02 07:58:25 -07:00
|
|
|
segmentJSON :=
|
|
|
|
`
|
|
|
|
{
|
|
|
|
"type": "path",
|
|
|
|
"style": "powerline",
|
2020-10-15 23:37:43 -07:00
|
|
|
"powerline_symbol": "\uE0B0",
|
2020-10-02 07:58:25 -07:00
|
|
|
"foreground": "#ffffff",
|
|
|
|
"background": "#61AFEF",
|
|
|
|
"properties": {
|
|
|
|
"style": "folder",
|
2021-02-27 20:05:51 -08:00
|
|
|
"exclude_folders": [
|
2020-12-06 10:34:42 -08:00
|
|
|
"/super/secret/project"
|
2020-10-02 07:58:25 -07:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
segment := &Segment{}
|
|
|
|
err := json.Unmarshal([]byte(segmentJSON), segment)
|
|
|
|
assert.NoError(t, err)
|
2020-12-06 10:34:42 -08:00
|
|
|
}
|
|
|
|
|
2021-02-27 20:05:51 -08:00
|
|
|
func TestShouldIncludeFolder(t *testing.T) {
|
|
|
|
cases := []struct {
|
2022-04-06 00:05:17 -07:00
|
|
|
Case string
|
|
|
|
Included bool
|
|
|
|
Excluded bool
|
|
|
|
Expected bool
|
2021-02-27 20:05:51 -08:00
|
|
|
}{
|
2022-04-06 00:05:17 -07:00
|
|
|
{Case: "Include", Included: true, Excluded: false, Expected: true},
|
|
|
|
{Case: "Exclude", Included: false, Excluded: true, Expected: false},
|
|
|
|
{Case: "Include & Exclude", Included: true, Excluded: true, Expected: false},
|
|
|
|
{Case: "!Include & !Exclude", Included: false, Excluded: false, Expected: false},
|
2021-02-27 20:05:51 -08:00
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
2022-01-26 01:23:18 -08:00
|
|
|
env := new(mock.MockedEnvironment)
|
2022-11-09 11:27:54 -08:00
|
|
|
env.On("GOOS").Return(platform.LINUX)
|
2022-01-23 12:37:51 -08:00
|
|
|
env.On("Home").Return("")
|
|
|
|
env.On("Pwd").Return(cwd)
|
2022-04-06 00:05:17 -07:00
|
|
|
env.On("DirMatchesOneOf", cwd, []string{"Projects/oh-my-posh"}).Return(tc.Included)
|
|
|
|
env.On("DirMatchesOneOf", cwd, []string{"Projects/nope"}).Return(tc.Excluded)
|
2021-02-27 20:05:51 -08:00
|
|
|
segment := &Segment{
|
2022-01-26 04:53:35 -08:00
|
|
|
Properties: properties.Map{
|
2022-04-06 00:05:17 -07:00
|
|
|
properties.IncludeFolders: []string{"Projects/oh-my-posh"},
|
|
|
|
properties.ExcludeFolders: []string{"Projects/nope"},
|
2021-02-27 20:05:51 -08:00
|
|
|
},
|
2021-09-18 10:39:40 -07:00
|
|
|
env: env,
|
2021-02-27 20:05:51 -08:00
|
|
|
}
|
2021-10-20 08:01:19 -07:00
|
|
|
got := segment.shouldIncludeFolder()
|
2021-02-27 20:05:51 -08:00
|
|
|
assert.Equal(t, tc.Expected, got, tc.Case)
|
2020-12-06 10:34:42 -08:00
|
|
|
}
|
2020-10-02 07:58:25 -07:00
|
|
|
}
|
2020-12-06 10:34:42 -08:00
|
|
|
|
2021-02-13 07:27:31 -08:00
|
|
|
func TestGetColors(t *testing.T) {
|
|
|
|
cases := []struct {
|
2021-02-15 12:34:48 -08:00
|
|
|
Case string
|
|
|
|
Background bool
|
|
|
|
ExpectedColor string
|
|
|
|
Templates []string
|
|
|
|
DefaultColor string
|
|
|
|
Region string
|
|
|
|
Profile string
|
2021-02-13 07:27:31 -08:00
|
|
|
}{
|
2021-02-15 12:34:48 -08:00
|
|
|
{Case: "No template - foreground", ExpectedColor: "color", Background: false, DefaultColor: "color"},
|
|
|
|
{Case: "No template - background", ExpectedColor: "color", Background: true, DefaultColor: "color"},
|
|
|
|
{Case: "Nil template", ExpectedColor: "color", DefaultColor: "color", Templates: nil},
|
2021-02-13 07:27:31 -08:00
|
|
|
{
|
2021-02-15 12:34:48 -08:00
|
|
|
Case: "Template - default",
|
|
|
|
ExpectedColor: "color",
|
|
|
|
DefaultColor: "color",
|
2021-02-13 07:27:31 -08:00
|
|
|
Templates: []string{
|
|
|
|
"{{if contains \"john\" .Profile}}color2{{end}}",
|
|
|
|
},
|
|
|
|
Profile: "doe",
|
|
|
|
},
|
|
|
|
{
|
2021-02-15 12:34:48 -08:00
|
|
|
Case: "Template - override",
|
|
|
|
ExpectedColor: "color2",
|
|
|
|
DefaultColor: "color",
|
2021-02-13 07:27:31 -08:00
|
|
|
Templates: []string{
|
|
|
|
"{{if contains \"john\" .Profile}}color2{{end}}",
|
|
|
|
},
|
|
|
|
Profile: "john",
|
|
|
|
},
|
|
|
|
{
|
2021-02-15 12:34:48 -08:00
|
|
|
Case: "Template - override multiple",
|
|
|
|
ExpectedColor: "color3",
|
|
|
|
DefaultColor: "color",
|
2021-02-13 07:27:31 -08:00
|
|
|
Templates: []string{
|
|
|
|
"{{if contains \"doe\" .Profile}}color2{{end}}",
|
|
|
|
"{{if contains \"john\" .Profile}}color3{{end}}",
|
|
|
|
},
|
|
|
|
Profile: "john",
|
|
|
|
},
|
|
|
|
{
|
2021-02-15 12:34:48 -08:00
|
|
|
Case: "Template - override multiple no match",
|
|
|
|
ExpectedColor: "color",
|
|
|
|
DefaultColor: "color",
|
2021-02-13 07:27:31 -08:00
|
|
|
Templates: []string{
|
|
|
|
"{{if contains \"doe\" .Profile}}color2{{end}}",
|
|
|
|
"{{if contains \"philip\" .Profile}}color3{{end}}",
|
|
|
|
},
|
|
|
|
Profile: "john",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
2022-01-26 01:23:18 -08:00
|
|
|
env := new(mock.MockedEnvironment)
|
2022-11-09 11:27:54 -08:00
|
|
|
env.On("TemplateCache").Return(&platform.TemplateCache{
|
2022-01-23 12:37:51 -08:00
|
|
|
Env: make(map[string]string),
|
|
|
|
})
|
2021-02-13 07:27:31 -08:00
|
|
|
segment := &Segment{
|
2022-01-26 06:54:36 -08:00
|
|
|
writer: &segments.Aws{
|
2021-02-13 07:27:31 -08:00
|
|
|
Profile: tc.Profile,
|
|
|
|
Region: tc.Region,
|
|
|
|
},
|
2022-01-18 00:48:47 -08:00
|
|
|
env: env,
|
2021-02-13 07:27:31 -08:00
|
|
|
}
|
|
|
|
if tc.Background {
|
|
|
|
segment.Background = tc.DefaultColor
|
|
|
|
segment.BackgroundTemplates = tc.Templates
|
|
|
|
color := segment.background()
|
2021-02-15 12:34:48 -08:00
|
|
|
assert.Equal(t, tc.ExpectedColor, color, tc.Case)
|
2021-02-13 07:27:31 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
segment.Foreground = tc.DefaultColor
|
|
|
|
segment.ForegroundTemplates = tc.Templates
|
|
|
|
color := segment.foreground()
|
2021-02-15 12:34:48 -08:00
|
|
|
assert.Equal(t, tc.ExpectedColor, color, tc.Case)
|
2021-02-13 07:27:31 -08:00
|
|
|
}
|
|
|
|
}
|