forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_title_test.go
130 lines (124 loc) · 3.67 KB
/
console_title_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetConsoleTitle(t *testing.T) {
cases := []struct {
Style ConsoleTitleStyle
Template string
Root bool
User string
Cwd string
PathSeperator string
ShellName string
Expected string
}{
{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"},
{
Style: Template,
Template: "{{.Env.USERDOMAIN}} :: {{.Path}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
Cwd: "C:\\vagrant",
PathSeperator: "\\",
ShellName: "PowerShell",
Root: true,
Expected: "\x1b]0;MyCompany :: C:\\vagrant :: Admin :: PowerShell\a",
},
{
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 {
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)
ansi := &ansiUtils{}
ansi.init(tc.ShellName)
ct := &consoleTitle{
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",
},
}
for _, tc := range cases {
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("", fmt.Errorf("I have a bad feeling about this"))
ansi := &ansiUtils{}
ansi.init(tc.ShellName)
ct := &consoleTitle{
env: env,
config: config,
ansi: ansi,
}
got := ct.getConsoleTitle()
assert.Equal(t, tc.Expected, got)
}
}