oh-my-posh/src/console_title_test.go

131 lines
3.7 KiB
Go
Raw Normal View History

2020-12-26 10:21:26 -08:00
package main
import (
"fmt"
2020-12-26 10:21:26 -08:00
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetConsoleTitle(t *testing.T) {
cases := []struct {
Style ConsoleTitleStyle
2020-12-27 02:56:33 -08:00
Template string
Root bool
User string
2020-12-26 10:21:26 -08:00
Cwd string
PathSeperator string
ShellName string
Expected string
}{
2021-01-13 18:30:14 -08:00
{Style: FolderName, Cwd: "/usr/home", PathSeperator: "/", ShellName: "default", Expected: "\x1b]0;~\a"},
{Style: FullPath, Cwd: "/usr/home/jan", PathSeperator: "/", ShellName: "default", Expected: "\x1b]0;~/jan\a"},
2020-12-27 02:56:33 -08:00
{
Style: Template,
2021-01-13 17:11:47 -08:00
Template: "{{.Env.USERDOMAIN}} :: {{.Path}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
2020-12-27 02:56:33 -08:00
Cwd: "C:\\vagrant",
PathSeperator: "\\",
ShellName: "PowerShell",
Root: true,
2021-01-13 17:11:47 -08:00
Expected: "\x1b]0;MyCompany :: C:\\vagrant :: Admin :: PowerShell\a",
2020-12-27 02:56:33 -08:00
},
{
Style: Template,
Template: "{{.Folder}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
Cwd: "C:\\vagrant",
PathSeperator: "\\",
ShellName: "PowerShell",
Expected: "\x1b]0;vagrant :: PowerShell\a",
},
{
Style: Template,
Template: "{{.User}}@{{.Host}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
Root: true,
User: "MyUser",
PathSeperator: "\\",
ShellName: "PowerShell",
Expected: "\x1b]0;MyUser@MyHost :: Admin :: PowerShell\a",
},
}
for _, tc := range cases {
2021-03-20 11:32:15 -07:00
config := &Config{
ConsoleTitleStyle: tc.Style,
ConsoleTitleTemplate: tc.Template,
}
env := new(MockedEnvironment)
env.On("getcwd", nil).Return(tc.Cwd)
env.On("homeDir", nil).Return("/usr/home")
env.On("getPathSeperator", nil).Return(tc.PathSeperator)
env.On("isRunningAsRoot", nil).Return(tc.Root)
env.On("getShellName", nil).Return(tc.ShellName)
env.On("getenv", "USERDOMAIN").Return("MyCompany")
env.On("getCurrentUser", nil).Return("MyUser")
env.On("getHostName", nil).Return("MyHost", nil)
2021-04-20 12:30:46 -07:00
ansi := &ansiUtils{}
ansi.init(tc.ShellName)
ct := &consoleTitle{
2021-04-20 12:30:46 -07:00
env: env,
config: config,
ansi: ansi,
}
got := ct.getConsoleTitle()
assert.Equal(t, tc.Expected, got)
}
}
func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
cases := []struct {
Style ConsoleTitleStyle
Template string
Root bool
User string
Cwd string
PathSeperator string
ShellName string
Expected string
}{
{
Style: Template,
Template: "Not using Host only {{.User}} and {{.Shell}}",
User: "MyUser",
PathSeperator: "\\",
ShellName: "PowerShell",
Expected: "\x1b]0;Not using Host only MyUser and PowerShell\a",
},
{
Style: Template,
Template: "{{.User}}@{{.Host}} :: {{.Shell}}",
User: "MyUser",
PathSeperator: "\\",
ShellName: "PowerShell",
Expected: "\x1b]0;MyUser@ :: PowerShell\a",
},
2020-12-26 10:21:26 -08:00
}
for _, tc := range cases {
2021-03-20 11:32:15 -07:00
config := &Config{
2020-12-27 02:56:33 -08:00
ConsoleTitleStyle: tc.Style,
ConsoleTitleTemplate: tc.Template,
2020-12-26 10:21:26 -08:00
}
env := new(MockedEnvironment)
env.On("getcwd", nil).Return(tc.Cwd)
env.On("homeDir", nil).Return("/usr/home")
env.On("getPathSeperator", nil).Return(tc.PathSeperator)
2020-12-27 02:56:33 -08:00
env.On("isRunningAsRoot", nil).Return(tc.Root)
env.On("getShellName", nil).Return(tc.ShellName)
2021-01-13 17:11:47 -08:00
env.On("getenv", "USERDOMAIN").Return("MyCompany")
env.On("getCurrentUser", nil).Return("MyUser")
env.On("getHostName", nil).Return("", fmt.Errorf("I have a bad feeling about this"))
2021-04-20 12:30:46 -07:00
ansi := &ansiUtils{}
ansi.init(tc.ShellName)
2020-12-26 10:21:26 -08:00
ct := &consoleTitle{
2021-04-20 12:30:46 -07:00
env: env,
config: config,
ansi: ansi,
2020-12-26 10:21:26 -08:00
}
got := ct.getConsoleTitle()
assert.Equal(t, tc.Expected, got)
}
}