oh-my-posh/src/segment_az_test.go

101 lines
2.6 KiB
Go
Raw Normal View History

package main
import (
2021-12-28 04:41:07 -08:00
"io/ioutil"
"oh-my-posh/environment"
"oh-my-posh/mock"
"oh-my-posh/properties"
2021-12-28 04:41:07 -08:00
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
2021-03-27 06:52:27 -07:00
func TestAzSegment(t *testing.T) {
cases := []struct {
2022-01-13 13:35:58 -08:00
Case string
ExpectedEnabled bool
ExpectedString string
HasCLI bool
HasPowerShell bool
HasPowerShellUnix bool
Template string
2021-03-27 06:52:27 -07:00
}{
2021-05-21 10:56:31 -07:00
{
2021-12-28 04:41:07 -08:00
Case: "no config files found",
2021-03-27 06:52:27 -07:00
ExpectedEnabled: false,
},
{
2021-12-28 04:41:07 -08:00
Case: "Az CLI Profile",
ExpectedEnabled: true,
ExpectedString: "AzureCliCloud",
Template: "{{ .EnvironmentName }}",
HasCLI: true,
},
{
2021-12-28 04:41:07 -08:00
Case: "Az Pwsh Profile",
ExpectedEnabled: true,
ExpectedString: "AzurePoshCloud",
Template: "{{ .EnvironmentName }}",
HasPowerShell: true,
},
2022-01-13 13:35:58 -08:00
{
Case: "Az Pwsh Profile",
ExpectedEnabled: true,
ExpectedString: "AzurePoshCloud",
Template: "{{ .EnvironmentName }}",
HasPowerShellUnix: true,
},
2021-05-21 10:56:31 -07:00
{
2021-12-28 04:41:07 -08:00
Case: "Faulty template",
ExpectedEnabled: true,
ExpectedString: incorrectTemplate,
Template: "{{ .Burp }}",
HasPowerShell: true,
2021-05-21 10:56:31 -07:00
},
{
Case: "PWSH",
ExpectedEnabled: true,
ExpectedString: "PWSH",
Template: "{{ .Origin }}",
HasPowerShell: true,
},
{
Case: "CLI",
ExpectedEnabled: true,
ExpectedString: "CLI",
Template: "{{ .Origin }}",
HasCLI: true,
},
}
2021-03-27 06:52:27 -07:00
for _, tc := range cases {
env := new(mock.MockedEnvironment)
2021-12-28 04:41:07 -08:00
home := "/Users/posh"
env.On("Home").Return(home)
2022-01-13 13:35:58 -08:00
var azureProfile, azureRmContext, azureRMContext string
2021-12-28 04:41:07 -08:00
if tc.HasCLI {
content, _ := ioutil.ReadFile("./test/azureProfile.json")
azureProfile = string(content)
}
if tc.HasPowerShell {
content, _ := ioutil.ReadFile("./test/AzureRmContext.json")
azureRmContext = string(content)
}
2022-01-13 13:35:58 -08:00
if tc.HasPowerShellUnix {
content, _ := ioutil.ReadFile("./test/AzureRmContext.json")
azureRMContext = string(content)
}
env.On("GOOS").Return(environment.LinuxPlatform)
env.On("FileContent", filepath.Join(home, ".azure", "azureProfile.json")).Return(azureProfile)
env.On("FileContent", filepath.Join(home, ".Azure", "AzureRmContext.json")).Return(azureRmContext)
env.On("FileContent", filepath.Join(home, ".azure", "AzureRmContext.json")).Return(azureRMContext)
2021-03-27 06:52:27 -07:00
az := &az{
env: env,
props: properties.Map{},
2021-03-27 06:52:27 -07:00
}
assert.Equal(t, tc.ExpectedEnabled, az.enabled(), tc.Case)
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, az), tc.Case)
}
}