2023-05-03 12:43:20 -07:00
|
|
|
package segments
|
|
|
|
|
|
|
|
import (
|
2023-06-07 22:40:05 -07:00
|
|
|
"path"
|
2023-05-03 12:43:20 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
2023-06-07 22:40:05 -07:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
2023-05-03 12:43:20 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSitecoreSegment(t *testing.T) {
|
|
|
|
cases := []struct {
|
2023-06-07 22:40:05 -07:00
|
|
|
Case string
|
|
|
|
ExpectedString string
|
|
|
|
ExpectedEnabled bool
|
|
|
|
SitecoreFileExists bool
|
|
|
|
UserFileExists bool
|
|
|
|
UserFileContent string
|
|
|
|
DisplayDefault bool
|
2023-05-03 12:43:20 -07:00
|
|
|
}{
|
2023-06-07 22:40:05 -07:00
|
|
|
{Case: "Disabled, no sitecore.json file and user.json file", ExpectedString: "", ExpectedEnabled: false, SitecoreFileExists: false, UserFileExists: false},
|
|
|
|
{Case: "Disabled, only sitecore.json file exists", ExpectedString: "", ExpectedEnabled: false, SitecoreFileExists: true, UserFileExists: false},
|
|
|
|
{Case: "Disabled, only user.json file exists", ExpectedString: "", ExpectedEnabled: false, SitecoreFileExists: false, UserFileExists: true},
|
2023-05-03 12:43:20 -07:00
|
|
|
{
|
2023-06-07 22:40:05 -07:00
|
|
|
Case: "Disabled, user.json is empty",
|
|
|
|
ExpectedString: "",
|
|
|
|
ExpectedEnabled: false,
|
|
|
|
SitecoreFileExists: true,
|
|
|
|
UserFileExists: true,
|
|
|
|
UserFileContent: "",
|
2023-05-03 12:43:20 -07:00
|
|
|
},
|
|
|
|
{
|
2023-06-07 22:40:05 -07:00
|
|
|
Case: "Disabled, user.json contains non-json text",
|
|
|
|
ExpectedString: "",
|
|
|
|
ExpectedEnabled: false,
|
|
|
|
SitecoreFileExists: true,
|
|
|
|
UserFileExists: true,
|
|
|
|
UserFileContent: testUserJSONNotJSONFormat,
|
2023-05-03 12:43:20 -07:00
|
|
|
},
|
|
|
|
{
|
2023-06-07 22:40:05 -07:00
|
|
|
Case: "Disabled with default endpoint",
|
|
|
|
ExpectedString: "default",
|
|
|
|
ExpectedEnabled: false,
|
|
|
|
SitecoreFileExists: true,
|
|
|
|
UserFileExists: true,
|
|
|
|
UserFileContent: testUserJSONOnlyDefaultEnv,
|
|
|
|
DisplayDefault: false,
|
2023-05-03 12:43:20 -07:00
|
|
|
},
|
|
|
|
{
|
2023-06-07 22:40:05 -07:00
|
|
|
Case: "Enabled, user.json initial state",
|
|
|
|
ExpectedString: "default",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
SitecoreFileExists: true,
|
|
|
|
UserFileExists: true,
|
|
|
|
UserFileContent: testUserJSONDefaultEmpty,
|
|
|
|
DisplayDefault: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Enabled, user.json with custom default endpoint and without endpoints",
|
|
|
|
ExpectedString: "MySuperEnv",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
SitecoreFileExists: true,
|
|
|
|
UserFileExists: true,
|
|
|
|
UserFileContent: testUserJSONCustomDefaultEnvWithoutEndpoints,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Enabled, user.json with custom default endpoint and configured endpoints",
|
|
|
|
ExpectedString: "myEnv (https://host.com)",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
SitecoreFileExists: true,
|
|
|
|
UserFileExists: true,
|
|
|
|
UserFileContent: testUserJSONCustomDefaultEnv,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "Enabled, user.json with custom default endpoint and empty host",
|
|
|
|
ExpectedString: "envWithEmptyHost",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
SitecoreFileExists: true,
|
|
|
|
UserFileExists: true,
|
|
|
|
UserFileContent: testUserJSONCustomDefaultEnvAndEmptyHost,
|
2023-05-03 12:43:20 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
env := new(mock.MockedEnvironment)
|
2023-06-07 22:40:05 -07:00
|
|
|
env.On("HasFiles", "sitecore.json").Return(tc.SitecoreFileExists)
|
|
|
|
env.On("HasFiles", path.Join(".sitecore", "user.json")).Return(tc.UserFileExists)
|
|
|
|
env.On("FileContent", path.Join(".sitecore", "user.json")).Return(tc.UserFileContent)
|
2023-05-03 12:43:20 -07:00
|
|
|
|
2023-06-07 22:40:05 -07:00
|
|
|
props := properties.Map{
|
|
|
|
properties.DisplayDefault: tc.DisplayDefault,
|
2023-05-03 12:43:20 -07:00
|
|
|
}
|
|
|
|
|
2023-06-07 22:40:05 -07:00
|
|
|
sitecore := &Sitecore{}
|
|
|
|
sitecore.Init(props, env)
|
|
|
|
assert.Equal(t, tc.ExpectedEnabled, sitecore.Enabled(), tc.Case)
|
|
|
|
assert.Equal(t, tc.ExpectedString, renderTemplate(env, sitecore.Template(), sitecore), tc.Case)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testUserJSONDefaultEmpty = `
|
|
|
|
{
|
|
|
|
"endpoints": {}
|
|
|
|
}`
|
2023-05-03 12:43:20 -07:00
|
|
|
|
2023-06-07 22:40:05 -07:00
|
|
|
var testUserJSONCustomDefaultEnvWithoutEndpoints = `
|
|
|
|
{
|
|
|
|
"endpoints": {},
|
|
|
|
"defaultEndpoint": "MySuperEnv"
|
|
|
|
}`
|
2023-05-03 12:43:20 -07:00
|
|
|
|
2023-06-07 22:40:05 -07:00
|
|
|
var testUserJSONCustomDefaultEnv = `
|
|
|
|
{
|
|
|
|
"endpoints": {
|
|
|
|
"myEnv": {
|
|
|
|
"host": "https://host.com"
|
2023-05-03 12:43:20 -07:00
|
|
|
}
|
2023-06-07 22:40:05 -07:00
|
|
|
},
|
|
|
|
"defaultEndpoint": "myEnv"
|
|
|
|
}`
|
2023-05-03 12:43:20 -07:00
|
|
|
|
2023-06-07 22:40:05 -07:00
|
|
|
var testUserJSONCustomDefaultEnvAndEmptyHost = `
|
|
|
|
{
|
|
|
|
"endpoints": {
|
|
|
|
"myEnv": {
|
|
|
|
"host": ""
|
2023-05-03 12:43:20 -07:00
|
|
|
}
|
2023-06-07 22:40:05 -07:00
|
|
|
},
|
|
|
|
"defaultEndpoint": "envWithEmptyHost"
|
|
|
|
}`
|
2023-05-03 12:43:20 -07:00
|
|
|
|
2023-06-07 22:40:05 -07:00
|
|
|
var testUserJSONNotJSONFormat = `
|
|
|
|
---
|
|
|
|
doe: "a deer, a female deer"
|
|
|
|
ray: "a drop of golden sun"
|
|
|
|
pi: 3.14159
|
|
|
|
xmas: true
|
|
|
|
french-hens: 3
|
|
|
|
calling-birds:
|
|
|
|
- huey
|
|
|
|
- dewey
|
|
|
|
- louie
|
|
|
|
- fred
|
|
|
|
xmas-fifth-day:
|
|
|
|
calling-birds: four
|
|
|
|
french-hens: 3
|
|
|
|
golden-rings: 5
|
|
|
|
partridges:
|
|
|
|
count: 1
|
|
|
|
location: "a pear tree"
|
|
|
|
turtle-doves: two`
|
2023-05-03 12:43:20 -07:00
|
|
|
|
2023-06-07 22:40:05 -07:00
|
|
|
var testUserJSONOnlyDefaultEnv = `
|
|
|
|
{
|
|
|
|
"endpoints": {
|
|
|
|
"default": {
|
|
|
|
"host": "https://host.com"
|
2023-05-03 12:43:20 -07:00
|
|
|
}
|
|
|
|
}
|
2023-06-07 22:40:05 -07:00
|
|
|
}`
|