2022-01-26 06:54:36 -08:00
|
|
|
package segments
|
2019-03-13 04:14:30 -07:00
|
|
|
|
|
|
|
import (
|
2023-11-19 10:46:00 -08:00
|
|
|
"fmt"
|
2019-03-13 04:14:30 -07:00
|
|
|
"testing"
|
|
|
|
|
2024-07-03 00:04:11 -07:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/cache"
|
2023-01-05 12:57:38 -08:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
2024-07-02 03:02:57 -07:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
|
2024-07-03 00:04:11 -07:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
|
2024-11-11 23:42:35 -08:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
Template string
|
2023-11-19 10:46:00 -08:00
|
|
|
WhoAmI string
|
|
|
|
Platform string
|
2024-08-05 05:59:34 -07:00
|
|
|
SSHSession bool
|
|
|
|
Root bool
|
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
|
|
|
},
|
2023-11-19 10:46:00 -08:00
|
|
|
{
|
|
|
|
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,
|
2023-11-19 10:46:00 -08:00
|
|
|
ComputerName: "remote",
|
|
|
|
Template: "{{.UserName}}{{if .SSHSession}} on {{.HostName}}{{end}}",
|
|
|
|
},
|
2021-02-15 13:01:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2024-07-03 00:04:11 -07:00
|
|
|
env := new(mock.Environment)
|
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)
|
2024-08-07 08:19:24 -07:00
|
|
|
|
2021-02-15 13:01:09 -08:00
|
|
|
var SSHSession string
|
|
|
|
if tc.SSHSession {
|
|
|
|
SSHSession = "zezzion"
|
|
|
|
}
|
2024-08-07 08:19:24 -07:00
|
|
|
|
2022-01-23 12:37:51 -08:00
|
|
|
env.On("Getenv", "SSH_CONNECTION").Return(SSHSession)
|
|
|
|
env.On("Getenv", "SSH_CLIENT").Return(SSHSession)
|
2024-08-07 08:19:24 -07:00
|
|
|
env.On("Getenv", "POSH_SESSION_DEFAULT_USER").Return(tc.DefaultUserName)
|
|
|
|
|
2023-11-19 10:46:00 -08:00
|
|
|
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)
|
|
|
|
|
2024-10-22 03:22:40 -07:00
|
|
|
session := &Session{}
|
|
|
|
session.Init(properties.Map{}, env)
|
2024-08-07 08:19:24 -07:00
|
|
|
|
2024-11-11 23:42:35 -08:00
|
|
|
template.Cache = &cache.Template{
|
|
|
|
UserName: tc.UserName,
|
|
|
|
HostName: tc.ComputerName,
|
|
|
|
Root: tc.Root,
|
|
|
|
}
|
|
|
|
|
2022-01-26 05:26: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
|
|
|
}
|
|
|
|
}
|