oh-my-posh/src/segments/session_test.go

153 lines
4.1 KiB
Go
Raw Normal View History

2022-01-26 06:54:36 -08:00
package segments
2019-03-13 04:14:30 -07:00
import (
"fmt"
2019-03-13 04:14:30 -07:00
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
2024-07-02 03:02:57 -07:00
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
"github.com/jandedobbeleer/oh-my-posh/src/template"
2022-12-28 08:30:48 -08:00
2019-03-13 04:14:30 -07:00
"github.com/stretchr/testify/assert"
)
func TestSessionSegmentTemplate(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
UserName string
DefaultUserName string
ComputerName string
Template string
WhoAmI string
Platform string
2024-08-05 05:59:34 -07:00
SSHSession bool
Root bool
}{
{
2022-01-22 10:46:56 -08:00
Case: "user and computer",
ExpectedString: "john@company-laptop",
ComputerName: "company-laptop",
UserName: "john",
Template: "{{.UserName}}@{{.HostName}}",
},
{
2022-01-22 10:46:56 -08:00
Case: "user only",
ExpectedString: "john",
UserName: "john",
Template: "{{.UserName}}",
},
{
2022-01-22 10:46:56 -08:00
Case: "user with ssh",
ExpectedString: "john on remote",
UserName: "john",
SSHSession: true,
ComputerName: "remote",
Template: "{{.UserName}}{{if .SSHSession}} on {{.HostName}}{{end}}",
},
{
2022-01-22 10:46:56 -08:00
Case: "user without ssh",
ExpectedString: "john",
UserName: "john",
SSHSession: false,
ComputerName: "remote",
Template: "{{.UserName}}{{if .SSHSession}} on {{.HostName}}{{end}}",
},
{
2022-01-22 10:46:56 -08:00
Case: "user with root and ssh",
ExpectedString: "super john on remote",
UserName: "john",
SSHSession: true,
ComputerName: "remote",
Root: true,
Template: "{{if .Root}}super {{end}}{{.UserName}}{{if .SSHSession}} on {{.HostName}}{{end}}",
},
{
2022-01-22 10:46:56 -08:00
Case: "no template",
ExpectedString: "",
2022-01-22 10:46:56 -08:00
UserName: "john",
SSHSession: true,
ComputerName: "remote",
Root: true,
},
{
Case: "default user not equal",
ExpectedString: "john",
UserName: "john",
DefaultUserName: "jack",
SSHSession: true,
ComputerName: "remote",
Root: true,
2021-12-03 14:44:58 -08:00
Template: "{{if ne .Env.POSH_SESSION_DEFAULT_USER .UserName}}{{.UserName}}{{end}}",
},
{
Case: "default user equal",
ExpectedString: "",
UserName: "john",
DefaultUserName: "john",
SSHSession: true,
ComputerName: "remote",
Root: true,
2021-12-03 14:44:58 -08:00
Template: "{{if ne .Env.POSH_SESSION_DEFAULT_USER .UserName}}{{.UserName}}{{end}}",
},
{
Case: "user with ssh using who am i",
ExpectedString: "john on remote",
UserName: "john",
SSHSession: false,
WhoAmI: "sascha pts/1 2023-11-08 22:56 (89.246.1.1)",
ComputerName: "remote",
Template: "{{.UserName}}{{if .SSHSession}} on {{.HostName}}{{end}}",
},
{
Case: "user with ssh using who am i (windows)",
ExpectedString: "john",
UserName: "john",
SSHSession: false,
WhoAmI: "sascha pts/1 2023-11-08 22:56 (89.246.1.1)",
2024-07-02 03:02:57 -07:00
Platform: runtime.WINDOWS,
ComputerName: "remote",
Template: "{{.UserName}}{{if .SSHSession}} on {{.HostName}}{{end}}",
},
}
for _, tc := range cases {
env := new(mock.Environment)
env.On("User").Return(tc.UserName)
env.On("GOOS").Return("burp")
env.On("Host").Return(tc.ComputerName, nil)
var SSHSession string
if tc.SSHSession {
SSHSession = "zezzion"
}
env.On("Getenv", "SSH_CONNECTION").Return(SSHSession)
env.On("Getenv", "SSH_CLIENT").Return(SSHSession)
env.On("Getenv", "POSH_SESSION_DEFAULT_USER").Return(tc.DefaultUserName)
env.On("Platform").Return(tc.Platform)
var whoAmIErr error
if len(tc.WhoAmI) == 0 {
whoAmIErr = fmt.Errorf("who am i error")
}
env.On("RunCommand", "who", []string{"am", "i"}).Return(tc.WhoAmI, whoAmIErr)
session := &Session{}
session.Init(properties.Map{}, env)
template.Cache = &cache.Template{
UserName: tc.UserName,
HostName: tc.ComputerName,
Root: tc.Root,
}
_ = session.Enabled()
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, session), tc.Case)
}
}