2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-02 07:58:25 -07:00
|
|
|
"encoding/json"
|
2019-03-13 04:14:30 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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{
|
|
|
|
Type: Session,
|
|
|
|
}
|
|
|
|
env := new(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",
|
|
|
|
}
|
|
|
|
env := new(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": {
|
2020-10-16 08:12:34 -07:00
|
|
|
"prefix": " \uE5FF ",
|
2020-10-02 07:58:25 -07:00
|
|
|
"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 {
|
|
|
|
Case string
|
|
|
|
IncludeFolders []string
|
|
|
|
ExcludeFolders []string
|
|
|
|
Expected bool
|
|
|
|
}{
|
|
|
|
{Case: "Base Case", IncludeFolders: nil, ExcludeFolders: nil, Expected: true},
|
|
|
|
{Case: "Base Case Empty Arrays", IncludeFolders: []string{}, ExcludeFolders: []string{}, Expected: true},
|
2020-12-06 10:34:42 -08:00
|
|
|
|
2021-02-27 20:05:51 -08:00
|
|
|
{Case: "Include", IncludeFolders: []string{"Projects/oh-my-posh"}, ExcludeFolders: nil, Expected: true},
|
|
|
|
{Case: "Include Regex", IncludeFolders: []string{"Projects.*"}, ExcludeFolders: nil, Expected: true},
|
|
|
|
{Case: "Include Mismatch", IncludeFolders: []string{"Projects/nope"}, ExcludeFolders: nil, Expected: false},
|
|
|
|
{Case: "Include Regex Mismatch", IncludeFolders: []string{"zProjects.*"}, ExcludeFolders: nil, Expected: false},
|
|
|
|
|
|
|
|
{Case: "Exclude", IncludeFolders: nil, ExcludeFolders: []string{"Projects/oh-my-posh"}, Expected: false},
|
|
|
|
{Case: "Exclude Regex", IncludeFolders: nil, ExcludeFolders: []string{"Projects.*"}, Expected: false},
|
|
|
|
{Case: "Exclude Mismatch", IncludeFolders: nil, ExcludeFolders: []string{"Projects/nope"}, Expected: true},
|
|
|
|
{Case: "Exclude Regex Mismatch", IncludeFolders: nil, ExcludeFolders: []string{"zProjects.*"}, Expected: true},
|
|
|
|
|
|
|
|
{Case: "Include Match / Exclude Match", IncludeFolders: []string{"Projects.*"}, ExcludeFolders: []string{"Projects/oh-my-posh"}, Expected: false},
|
|
|
|
{Case: "Include Match / Exclude Mismatch", IncludeFolders: []string{"Projects.*"}, ExcludeFolders: []string{"Projects/nope"}, Expected: true},
|
|
|
|
{Case: "Include Mismatch / Exclude Match", IncludeFolders: []string{"zProjects.*"}, ExcludeFolders: []string{"Projects/oh-my-posh"}, Expected: false},
|
|
|
|
{Case: "Include Mismatch / Exclude Mismatch", IncludeFolders: []string{"zProjects.*"}, ExcludeFolders: []string{"Projects/nope"}, Expected: false},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
2021-09-18 10:39:40 -07:00
|
|
|
env := new(MockedEnvironment)
|
2022-01-15 11:37:46 -08:00
|
|
|
env.On("getRuntimeGOOS").Return(linuxPlatform)
|
|
|
|
env.On("homeDir").Return("")
|
|
|
|
env.On("getcwd").Return(cwd)
|
2021-02-27 20:05:51 -08:00
|
|
|
segment := &Segment{
|
2022-01-01 11:08:08 -08:00
|
|
|
Properties: properties{
|
2021-02-27 20:05:51 -08:00
|
|
|
IncludeFolders: tc.IncludeFolders,
|
|
|
|
ExcludeFolders: tc.ExcludeFolders,
|
|
|
|
},
|
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-27 20:05:51 -08:00
|
|
|
func TestShouldIncludeFolderRegexInverted(t *testing.T) {
|
2021-09-18 10:39:40 -07:00
|
|
|
env := new(MockedEnvironment)
|
2022-01-15 11:37:46 -08:00
|
|
|
env.On("getRuntimeGOOS").Return(linuxPlatform)
|
|
|
|
env.On("homeDir").Return("")
|
|
|
|
env.On("getcwd").Return(cwd)
|
2020-12-06 10:34:42 -08:00
|
|
|
segment := &Segment{
|
2022-01-01 11:08:08 -08:00
|
|
|
Properties: properties{
|
2021-02-27 20:05:51 -08:00
|
|
|
ExcludeFolders: []string{"(?!Projects[\\/]).*"},
|
2020-12-06 10:34:42 -08:00
|
|
|
},
|
2021-09-18 10:39:40 -07:00
|
|
|
env: env,
|
2020-12-06 10:34:42 -08:00
|
|
|
}
|
2020-12-30 13:01:39 -08:00
|
|
|
// detect panic(thrown by MustCompile)
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
// display a message explaining omp failed(with the err)
|
|
|
|
assert.Equal(t, "regexp: Compile(`^(?!Projects[\\/]).*$`): error parsing regexp: invalid or unsupported Perl syntax: `(?!`", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-10-20 08:01:19 -07:00
|
|
|
segment.shouldIncludeFolder()
|
2020-12-06 10:34:42 -08:00
|
|
|
}
|
|
|
|
|
2021-02-27 20:05:51 -08:00
|
|
|
func TestShouldIncludeFolderRegexInvertedNonEscaped(t *testing.T) {
|
2021-09-18 10:39:40 -07:00
|
|
|
env := new(MockedEnvironment)
|
2022-01-15 11:37:46 -08:00
|
|
|
env.On("getRuntimeGOOS").Return(linuxPlatform)
|
|
|
|
env.On("homeDir").Return("")
|
|
|
|
env.On("getcwd").Return(cwd)
|
2020-12-06 10:34:42 -08:00
|
|
|
segment := &Segment{
|
2022-01-01 11:08:08 -08:00
|
|
|
Properties: properties{
|
2021-02-27 20:05:51 -08:00
|
|
|
ExcludeFolders: []string{"(?!Projects/).*"},
|
2020-12-06 10:34:42 -08:00
|
|
|
},
|
2021-09-18 10:39:40 -07:00
|
|
|
env: env,
|
2020-12-06 10:34:42 -08:00
|
|
|
}
|
2020-12-30 13:01:39 -08:00
|
|
|
// detect panic(thrown by MustCompile)
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
// display a message explaining omp failed(with the err)
|
|
|
|
assert.Equal(t, "regexp: Compile(`^(?!Projects/).*$`): error parsing regexp: invalid or unsupported Perl syntax: `(?!`", err)
|
|
|
|
}
|
|
|
|
}()
|
2021-10-20 08:01:19 -07:00
|
|
|
segment.shouldIncludeFolder()
|
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 {
|
|
|
|
segment := &Segment{
|
|
|
|
writer: &aws{
|
|
|
|
Profile: tc.Profile,
|
|
|
|
Region: tc.Region,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|