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 (
|
|
|
|
cwd = "Projects/oh-my-posh3"
|
|
|
|
)
|
|
|
|
|
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)
|
|
|
|
assert.NotNil(t, sc.props)
|
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)
|
|
|
|
assert.Nil(t, sc.props)
|
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
|
|
|
|
|
|
|
func TestParseTestSettings(t *testing.T) {
|
|
|
|
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",
|
|
|
|
"ignore_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
|
|
|
cwd := "/super/secret/project"
|
|
|
|
got := segment.shouldIgnoreFolder(cwd)
|
|
|
|
assert.True(t, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldIgnoreFolderRegex(t *testing.T) {
|
|
|
|
segment := &Segment{
|
|
|
|
Properties: map[Property]interface{}{
|
|
|
|
IgnoreFolders: []string{"Projects[\\/].*"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
got := segment.shouldIgnoreFolder(cwd)
|
|
|
|
assert.True(t, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldIgnoreFolderRegexNonEscaped(t *testing.T) {
|
|
|
|
segment := &Segment{
|
|
|
|
Properties: map[Property]interface{}{
|
|
|
|
IgnoreFolders: []string{"Projects/.*"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
got := segment.shouldIgnoreFolder(cwd)
|
2020-10-02 07:58:25 -07:00
|
|
|
assert.True(t, got)
|
|
|
|
}
|
2020-12-06 10:34:42 -08:00
|
|
|
|
|
|
|
func TestShouldIgnoreFolderRegexInverted(t *testing.T) {
|
|
|
|
segment := &Segment{
|
|
|
|
Properties: map[Property]interface{}{
|
|
|
|
IgnoreFolders: []string{"(?!Projects[\\/]).*"},
|
|
|
|
},
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
segment.shouldIgnoreFolder(cwd)
|
2020-12-06 10:34:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldIgnoreFolderRegexInvertedNonEscaped(t *testing.T) {
|
|
|
|
segment := &Segment{
|
|
|
|
Properties: map[Property]interface{}{
|
|
|
|
IgnoreFolders: []string{"(?!Projects/).*"},
|
|
|
|
},
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
segment.shouldIgnoreFolder(cwd)
|
2020-12-06 10:34:42 -08:00
|
|
|
}
|