2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/environment"
|
|
|
|
"oh-my-posh/mock"
|
2022-01-26 04:53:35 -08:00
|
|
|
"oh-my-posh/properties"
|
2019-03-13 04:14:30 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-02-15 13:01:09 -08:00
|
|
|
func TestSessionSegmentTemplate(t *testing.T) {
|
|
|
|
cases := []struct {
|
2021-02-24 00:04:04 -08:00
|
|
|
Case string
|
|
|
|
ExpectedString string
|
|
|
|
UserName string
|
|
|
|
DefaultUserName string
|
|
|
|
ComputerName string
|
|
|
|
SSHSession bool
|
|
|
|
Root bool
|
|
|
|
Template string
|
2021-02-15 13:01:09 -08:00
|
|
|
}{
|
|
|
|
{
|
2022-01-22 10:46:56 -08:00
|
|
|
Case: "user and computer",
|
|
|
|
ExpectedString: "john@company-laptop",
|
|
|
|
ComputerName: "company-laptop",
|
|
|
|
UserName: "john",
|
|
|
|
Template: "{{.UserName}}@{{.HostName}}",
|
2021-02-15 13:01:09 -08:00
|
|
|
},
|
|
|
|
{
|
2022-01-22 10:46:56 -08:00
|
|
|
Case: "user only",
|
|
|
|
ExpectedString: "john",
|
|
|
|
UserName: "john",
|
|
|
|
Template: "{{.UserName}}",
|
2021-02-15 13:01:09 -08:00
|
|
|
},
|
|
|
|
{
|
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}}",
|
2021-02-15 13:01:09 -08:00
|
|
|
},
|
|
|
|
{
|
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}}",
|
2021-02-15 13:01:09 -08:00
|
|
|
},
|
|
|
|
{
|
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}}",
|
2021-02-15 13:01:09 -08:00
|
|
|
},
|
|
|
|
{
|
2022-01-22 10:46:56 -08:00
|
|
|
Case: "no template",
|
2022-01-23 11:24:02 -08:00
|
|
|
ExpectedString: "",
|
2022-01-22 10:46:56 -08:00
|
|
|
UserName: "john",
|
|
|
|
SSHSession: true,
|
|
|
|
ComputerName: "remote",
|
|
|
|
Root: true,
|
2021-02-24 00:04:04 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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}}",
|
2021-02-24 00:04:04 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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}}",
|
2021-02-15 13:01:09 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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("User").Return(tc.UserName)
|
|
|
|
env.On("GOOS").Return("burp")
|
|
|
|
env.On("Host").Return(tc.ComputerName, nil)
|
2021-02-15 13:01:09 -08:00
|
|
|
var SSHSession string
|
|
|
|
if tc.SSHSession {
|
|
|
|
SSHSession = "zezzion"
|
|
|
|
}
|
2022-01-23 12:37:51 -08:00
|
|
|
env.On("Getenv", "SSH_CONNECTION").Return(SSHSession)
|
|
|
|
env.On("Getenv", "SSH_CLIENT").Return(SSHSession)
|
2022-01-26 01:23:18 -08:00
|
|
|
env.On("TemplateCache").Return(&environment.TemplateCache{
|
2022-01-18 00:48:47 -08:00
|
|
|
UserName: tc.UserName,
|
|
|
|
HostName: tc.ComputerName,
|
|
|
|
Env: map[string]string{
|
2022-01-23 11:24:02 -08:00
|
|
|
"SSH_CONNECTION": SSHSession,
|
|
|
|
"SSH_CLIENT": SSHSession,
|
|
|
|
"POSH_SESSION_DEFAULT_USER": tc.DefaultUserName,
|
2022-01-18 00:48:47 -08:00
|
|
|
},
|
|
|
|
Root: tc.Root,
|
|
|
|
})
|
2022-01-26 05:10:18 -08:00
|
|
|
session := &Session{
|
2022-01-23 12:37:51 -08:00
|
|
|
env: env,
|
2022-01-26 04:53:35 -08:00
|
|
|
props: properties.Map{},
|
2021-02-15 13:01:09 -08:00
|
|
|
}
|
2022-01-22 10:46:56 -08:00
|
|
|
_ = session.enabled()
|
2022-01-23 12:37:51 -08:00
|
|
|
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, session), tc.Case)
|
2021-02-15 13:01:09 -08:00
|
|
|
}
|
|
|
|
}
|