2021-05-22 07:50:34 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/environment"
|
|
|
|
"oh-my-posh/mock"
|
2021-11-22 06:25:56 -08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-05-22 07:50:34 -07:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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: "Terminal > Prompt disabled no breathing", Expected: false, TerminalWidth: 200, PromptLength: 100, RPromptLength: 71},
|
|
|
|
{Case: "Prompt > Terminal enabled", Expected: true, TerminalWidth: 200, PromptLength: 300, RPromptLength: 70},
|
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)
|
2021-05-22 07:50:34 -07:00
|
|
|
ansi := &ansiUtils{}
|
|
|
|
ansi.init(plain)
|
|
|
|
engine := &engine{
|
|
|
|
env: env,
|
|
|
|
ansi: ansi,
|
|
|
|
}
|
|
|
|
engine.rprompt = strings.Repeat("x", tc.RPromptLength)
|
|
|
|
engine.console.WriteString(strings.Repeat("x", tc.PromptLength))
|
|
|
|
got := engine.canWriteRPrompt()
|
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
|
|
|
|
|
|
|
func BenchmarkEngineRender(b *testing.B) {
|
|
|
|
var err error
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
err = engineRender("jandedobbeleer.omp.json")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func engineRender(configPath string) error {
|
|
|
|
testDir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:51:13 -08:00
|
|
|
configPath = filepath.Join(testDir, "test", configPath)
|
2021-11-22 06:25:56 -08:00
|
|
|
|
|
|
|
var (
|
|
|
|
debug = false
|
|
|
|
eval = false
|
|
|
|
shell = "pwsh"
|
|
|
|
plain = false
|
|
|
|
pwd = ""
|
|
|
|
pswd = ""
|
|
|
|
code = 2
|
|
|
|
execTime = 917.0
|
|
|
|
)
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
args := &environment.Args{
|
2021-11-22 06:25:56 -08:00
|
|
|
Debug: &debug,
|
|
|
|
Config: &configPath,
|
|
|
|
Eval: &eval,
|
|
|
|
Shell: &shell,
|
|
|
|
Plain: &plain,
|
|
|
|
PWD: &pwd,
|
|
|
|
PSWD: &pswd,
|
|
|
|
ErrorCode: &code,
|
|
|
|
ExecutionTime: &execTime,
|
|
|
|
}
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
env := &environment.ShellEnvironment{}
|
|
|
|
env.Init(args)
|
2022-01-23 12:37:51 -08:00
|
|
|
defer env.Close()
|
2021-11-22 06:25:56 -08:00
|
|
|
|
|
|
|
cfg := GetConfig(env)
|
|
|
|
defer testClearDefaultConfig()
|
|
|
|
|
|
|
|
ansi := &ansiUtils{}
|
2022-01-23 12:37:51 -08:00
|
|
|
ansi.init(env.Shell())
|
2021-11-22 06:25:56 -08:00
|
|
|
writerColors := MakeColors(env, cfg)
|
|
|
|
writer := &AnsiWriter{
|
|
|
|
ansi: ansi,
|
|
|
|
terminalBackground: getConsoleBackgroundColor(env, cfg.TerminalBackground),
|
|
|
|
ansiColors: writerColors,
|
|
|
|
}
|
|
|
|
title := &consoleTitle{
|
|
|
|
env: env,
|
|
|
|
config: cfg,
|
|
|
|
ansi: ansi,
|
|
|
|
}
|
|
|
|
engine := &engine{
|
|
|
|
config: cfg,
|
|
|
|
env: env,
|
|
|
|
writer: writer,
|
|
|
|
consoleTitle: title,
|
|
|
|
ansi: ansi,
|
|
|
|
plain: *args.Plain,
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.render()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|