fix(text): disable when text resolves to empty

This commit is contained in:
Jan De Dobbeleer 2021-10-06 21:50:52 +02:00 committed by Jan De Dobbeleer
parent 356dda306c
commit d0816773a5
2 changed files with 17 additions and 11 deletions

View file

@ -1,8 +1,9 @@
package main
type text struct {
props *properties
env environmentInfo
props *properties
env environmentInfo
content string
}
const (
@ -11,18 +12,18 @@ const (
)
func (t *text) enabled() bool {
return true
}
func (t *text) string() string {
textProperty := t.props.getString(TextProperty, "!!text property not defined!!")
template := &textTemplate{
Template: textProperty,
Context: t,
Env: t.env,
}
textOutput := template.renderPlainContextTemplate(nil)
return textOutput
t.content = template.renderPlainContextTemplate(nil)
return len(t.content) > 0
}
func (t *text) string() string {
return t.content
}
func (t *text) init(props *properties, env environmentInfo) {

View file

@ -8,15 +8,18 @@ import (
func TestTextSegment(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Text string
Case string
ExpectedString string
Text string
ExpectedDisabled bool
}{
{Case: "standard text", ExpectedString: "hello", Text: "hello"},
{Case: "template text with env var", ExpectedString: "hello world", Text: "{{ .Env.HELLO }} world"},
{Case: "template text with shell name", ExpectedString: "hello world from terminal", Text: "{{ .Env.HELLO }} world from {{ .Shell }}"},
{Case: "template text with folder", ExpectedString: "hello world in posh", Text: "{{ .Env.HELLO }} world in {{ .Folder }}"},
{Case: "template text with user", ExpectedString: "hello Posh", Text: "{{ .Env.HELLO }} {{ .User }}"},
{Case: "empty text", Text: "", ExpectedDisabled: true},
{Case: "empty template result", Text: "{{ .Env.WORLD }}", ExpectedDisabled: true},
}
for _, tc := range cases {
@ -27,6 +30,7 @@ func TestTextSegment(t *testing.T) {
env.On("isRunningAsRoot", nil).Return(true)
env.On("getShellName", nil).Return("terminal")
env.On("getenv", "HELLO").Return("hello")
env.On("getenv", "WORLD").Return("")
env.On("getCurrentUser", nil).Return("Posh")
env.On("getHostName", nil).Return("MyHost", nil)
props := &properties{
@ -38,6 +42,7 @@ func TestTextSegment(t *testing.T) {
env: env,
props: props,
}
assert.Equal(t, tc.ExpectedDisabled, !txt.enabled(), tc.Case)
assert.Equal(t, tc.ExpectedString, txt.string(), tc.Case)
}
}