2022-01-26 23:38:46 -08:00
|
|
|
package engine
|
2021-05-22 07:50:34 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2023-01-04 11:44:29 -08:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/ansi"
|
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/shell"
|
|
|
|
|
2021-05-22 07:50:34 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCanWriteRPrompt(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
Expected bool
|
|
|
|
TerminalWidth int
|
|
|
|
TerminalWidthError error
|
|
|
|
PromptLength int
|
|
|
|
RPromptLength int
|
|
|
|
}{
|
|
|
|
{Case: "Width Error", Expected: true, TerminalWidthError: errors.New("burp")},
|
|
|
|
{Case: "Terminal > Prompt enabled", Expected: true, TerminalWidth: 200, PromptLength: 100, RPromptLength: 10},
|
|
|
|
{Case: "Terminal > Prompt enabled edge", Expected: true, TerminalWidth: 200, PromptLength: 100, RPromptLength: 70},
|
|
|
|
{Case: "Prompt > Terminal enabled", Expected: true, TerminalWidth: 200, PromptLength: 300, RPromptLength: 70},
|
2022-02-03 08:36:37 -08:00
|
|
|
{Case: "Terminal > Prompt disabled no breathing", Expected: false, TerminalWidth: 200, PromptLength: 100, RPromptLength: 71},
|
2021-09-16 23:15:06 -07:00
|
|
|
{Case: "Prompt > Terminal disabled no breathing", Expected: false, TerminalWidth: 200, PromptLength: 300, RPromptLength: 80},
|
2021-05-22 07:50:34 -07:00
|
|
|
{Case: "Prompt > Terminal disabled no room", Expected: true, TerminalWidth: 200, PromptLength: 400, RPromptLength: 80},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2022-01-26 01:23:18 -08:00
|
|
|
env := new(mock.MockedEnvironment)
|
2022-01-23 12:37:51 -08:00
|
|
|
env.On("TerminalWidth").Return(tc.TerminalWidth, tc.TerminalWidthError)
|
2022-01-26 23:38:46 -08:00
|
|
|
engine := &Engine{
|
2022-06-12 10:55:57 -07:00
|
|
|
Env: env,
|
|
|
|
rpromptLength: tc.RPromptLength,
|
|
|
|
currentLineLength: tc.PromptLength,
|
|
|
|
rprompt: "hello",
|
2021-05-22 07:50:34 -07:00
|
|
|
}
|
2023-01-02 07:33:25 -08:00
|
|
|
got := engine.canWriteRightBlock(true)
|
2021-09-16 23:15:06 -07:00
|
|
|
assert.Equal(t, tc.Expected, got, tc.Case)
|
2021-05-22 07:50:34 -07:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 06:25:56 -08:00
|
|
|
|
2022-07-13 04:53:55 -07:00
|
|
|
func TestPrintPWD(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
Expected string
|
|
|
|
PWD string
|
|
|
|
OSC99 bool
|
|
|
|
}{
|
|
|
|
{Case: "Empty PWD"},
|
2023-01-04 11:44:29 -08:00
|
|
|
{Case: "OSC99", PWD: ansi.OSC99, Expected: "\x1b]9;9;\"pwd\"\x1b\\"},
|
|
|
|
{Case: "OSC7", PWD: ansi.OSC7, Expected: "\x1b]7;\"file://host/pwd\"\x1b\\"},
|
|
|
|
{Case: "OSC51", PWD: ansi.OSC51, Expected: "\x1b]51;Auser@host:pwd\x1b\\"},
|
2022-07-13 04:53:55 -07:00
|
|
|
{Case: "Deprecated OSC99", OSC99: true, Expected: "\x1b]9;9;\"pwd\"\x1b\\"},
|
|
|
|
{Case: "Template (empty)", PWD: "{{ if eq .Shell \"pwsh\" }}osc7{{ end }}"},
|
2022-07-29 00:28:28 -07:00
|
|
|
{Case: "Template (non empty)", PWD: "{{ if eq .Shell \"shell\" }}osc7{{ end }}", Expected: "\x1b]7;\"file://host/pwd\"\x1b\\"},
|
2022-07-13 04:53:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
env := new(mock.MockedEnvironment)
|
|
|
|
env.On("Pwd").Return("pwd")
|
|
|
|
env.On("Shell").Return("shell")
|
2022-11-24 14:45:44 -08:00
|
|
|
env.On("User").Return("user")
|
2022-07-25 23:28:39 -07:00
|
|
|
env.On("Host").Return("host", nil)
|
2022-11-09 11:27:54 -08:00
|
|
|
env.On("TemplateCache").Return(&platform.TemplateCache{
|
2022-07-13 04:53:55 -07:00
|
|
|
Env: make(map[string]string),
|
|
|
|
Shell: "shell",
|
|
|
|
})
|
2023-01-03 03:21:27 -08:00
|
|
|
|
2023-01-04 11:44:29 -08:00
|
|
|
writer := &ansi.Writer{}
|
2023-01-03 03:21:27 -08:00
|
|
|
writer.Init(shell.GENERIC)
|
2022-07-13 04:53:55 -07:00
|
|
|
engine := &Engine{
|
|
|
|
Env: env,
|
|
|
|
Config: &Config{
|
|
|
|
PWD: tc.PWD,
|
|
|
|
OSC99: tc.OSC99,
|
|
|
|
},
|
2023-01-03 03:21:27 -08:00
|
|
|
Writer: writer,
|
2022-07-13 04:53:55 -07:00
|
|
|
}
|
|
|
|
engine.printPWD()
|
|
|
|
got := engine.print()
|
|
|
|
assert.Equal(t, tc.Expected, got, tc.Case)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 06:25:56 -08:00
|
|
|
func BenchmarkEngineRender(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-03-12 13:04:08 -08:00
|
|
|
engineRender()
|
2021-11-22 06:25:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-12 13:04:08 -08:00
|
|
|
func engineRender() {
|
2022-11-09 11:27:54 -08:00
|
|
|
env := &platform.Shell{}
|
2022-05-07 01:12:22 -07:00
|
|
|
env.Init()
|
2022-01-23 12:37:51 -08:00
|
|
|
defer env.Close()
|
2021-11-22 06:25:56 -08:00
|
|
|
|
2022-01-31 04:33:36 -08:00
|
|
|
cfg := LoadConfig(env)
|
2021-11-22 06:25:56 -08:00
|
|
|
defer testClearDefaultConfig()
|
|
|
|
|
2022-10-13 11:01:51 -07:00
|
|
|
writerColors := cfg.MakeColors()
|
2023-01-04 11:44:29 -08:00
|
|
|
writer := &ansi.Writer{
|
2022-03-21 23:41:36 -07:00
|
|
|
TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground),
|
2022-01-26 04:09:21 -08:00
|
|
|
AnsiColors: writerColors,
|
2021-11-22 06:25:56 -08:00
|
|
|
}
|
2023-01-03 03:21:27 -08:00
|
|
|
writer.Init(shell.GENERIC)
|
2022-01-26 23:38:46 -08:00
|
|
|
engine := &Engine{
|
2023-01-03 03:21:27 -08:00
|
|
|
Config: cfg,
|
|
|
|
Env: env,
|
|
|
|
Writer: writer,
|
2021-11-22 06:25:56 -08:00
|
|
|
}
|
|
|
|
|
2022-03-12 13:04:08 -08:00
|
|
|
engine.PrintPrimary()
|
2021-11-22 06:25:56 -08:00
|
|
|
}
|
2022-01-26 04:09:21 -08:00
|
|
|
|
|
|
|
func BenchmarkEngineRenderPalette(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-03-12 13:04:08 -08:00
|
|
|
engineRender()
|
2022-01-26 04:09:21 -08:00
|
|
|
}
|
|
|
|
}
|
2023-01-03 03:21:27 -08:00
|
|
|
|
|
|
|
func TestGetTitle(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Template string
|
|
|
|
Root bool
|
|
|
|
User string
|
|
|
|
Cwd string
|
|
|
|
PathSeparator string
|
|
|
|
ShellName string
|
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Template: "{{.Env.USERDOMAIN}} :: {{.PWD}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
|
|
|
|
Cwd: "C:\\vagrant",
|
|
|
|
PathSeparator: "\\",
|
|
|
|
ShellName: "PowerShell",
|
|
|
|
Root: true,
|
|
|
|
Expected: "\x1b]0;MyCompany :: C:\\vagrant :: Admin :: PowerShell\a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Template: "{{.Folder}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
|
|
|
|
Cwd: "C:\\vagrant",
|
|
|
|
PathSeparator: "\\",
|
|
|
|
ShellName: "PowerShell",
|
|
|
|
Expected: "\x1b]0;vagrant :: PowerShell\a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Template: "{{.UserName}}@{{.HostName}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
|
|
|
|
Root: true,
|
|
|
|
User: "MyUser",
|
|
|
|
PathSeparator: "\\",
|
|
|
|
ShellName: "PowerShell",
|
|
|
|
Expected: "\x1b]0;MyUser@MyHost :: Admin :: PowerShell\a",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
env := new(mock.MockedEnvironment)
|
|
|
|
env.On("Pwd").Return(tc.Cwd)
|
|
|
|
env.On("Home").Return("/usr/home")
|
|
|
|
env.On("PathSeparator").Return(tc.PathSeparator)
|
|
|
|
env.On("TemplateCache").Return(&platform.TemplateCache{
|
|
|
|
Env: map[string]string{
|
|
|
|
"USERDOMAIN": "MyCompany",
|
|
|
|
},
|
|
|
|
Shell: tc.ShellName,
|
|
|
|
UserName: "MyUser",
|
|
|
|
Root: tc.Root,
|
|
|
|
HostName: "MyHost",
|
|
|
|
PWD: tc.Cwd,
|
|
|
|
Folder: "vagrant",
|
|
|
|
})
|
2023-01-04 11:44:29 -08:00
|
|
|
writer := &ansi.Writer{}
|
|
|
|
writer.Init(shell.GENERIC)
|
2023-01-03 03:21:27 -08:00
|
|
|
engine := &Engine{
|
|
|
|
Config: &Config{
|
|
|
|
ConsoleTitleTemplate: tc.Template,
|
|
|
|
},
|
2023-01-04 11:44:29 -08:00
|
|
|
Writer: writer,
|
2023-01-03 03:21:27 -08:00
|
|
|
Env: env,
|
|
|
|
}
|
|
|
|
title := engine.getTitleTemplateText()
|
2023-01-04 11:44:29 -08:00
|
|
|
got := writer.FormatTitle(title)
|
2023-01-03 03:21:27 -08:00
|
|
|
assert.Equal(t, tc.Expected, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Template string
|
|
|
|
Root bool
|
|
|
|
User string
|
|
|
|
Cwd string
|
|
|
|
PathSeparator string
|
|
|
|
ShellName string
|
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Template: "Not using Host only {{.UserName}} and {{.Shell}}",
|
|
|
|
User: "MyUser",
|
|
|
|
PathSeparator: "\\",
|
|
|
|
ShellName: "PowerShell",
|
|
|
|
Expected: "\x1b]0;Not using Host only MyUser and PowerShell\a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Template: "{{.UserName}}@{{.HostName}} :: {{.Shell}}",
|
|
|
|
User: "MyUser",
|
|
|
|
PathSeparator: "\\",
|
|
|
|
ShellName: "PowerShell",
|
|
|
|
Expected: "\x1b]0;MyUser@ :: PowerShell\a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Template: "\x1b[93m[\x1b[39m\x1b[96mconsole-title\x1b[39m\x1b[96m ≡\x1b[39m\x1b[31m +0\x1b[39m\x1b[31m ~1\x1b[39m\x1b[31m -0\x1b[39m\x1b[31m !\x1b[39m\x1b[93m]\x1b[39m",
|
|
|
|
Expected: "\x1b]0;[console-title ≡ +0 ~1 -0 !]\a",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
env := new(mock.MockedEnvironment)
|
|
|
|
env.On("Pwd").Return(tc.Cwd)
|
|
|
|
env.On("Home").Return("/usr/home")
|
|
|
|
env.On("TemplateCache").Return(&platform.TemplateCache{
|
|
|
|
Env: map[string]string{
|
|
|
|
"USERDOMAIN": "MyCompany",
|
|
|
|
},
|
|
|
|
Shell: tc.ShellName,
|
|
|
|
UserName: "MyUser",
|
|
|
|
Root: tc.Root,
|
|
|
|
HostName: "",
|
|
|
|
})
|
2023-01-04 11:44:29 -08:00
|
|
|
writer := &ansi.Writer{}
|
|
|
|
writer.Init(shell.GENERIC)
|
2023-01-03 03:21:27 -08:00
|
|
|
engine := &Engine{
|
|
|
|
Config: &Config{
|
|
|
|
ConsoleTitleTemplate: tc.Template,
|
|
|
|
},
|
2023-01-04 11:44:29 -08:00
|
|
|
Writer: writer,
|
2023-01-03 03:21:27 -08:00
|
|
|
Env: env,
|
|
|
|
}
|
|
|
|
title := engine.getTitleTemplateText()
|
2023-01-04 11:44:29 -08:00
|
|
|
got := writer.FormatTitle(title)
|
2023-01-03 03:21:27 -08:00
|
|
|
assert.Equal(t, tc.Expected, got)
|
|
|
|
}
|
|
|
|
}
|