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

140 lines
3.4 KiB
Go
Raw Normal View History

2022-01-26 06:54:36 -08:00
package segments
import (
2021-12-28 04:41:07 -08:00
"io/ioutil"
"oh-my-posh/environment"
"oh-my-posh/mock"
"oh-my-posh/properties"
2022-01-26 06:54:36 -08:00
"oh-my-posh/template"
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 {
Case string
ExpectedEnabled bool
ExpectedString string
HasCLI bool
HasPowerShell bool
Template string
Source 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 }}",
HasPowerShell: true,
2022-01-13 13:35:58 -08:00
},
2021-05-21 10:56:31 -07:00
{
2021-12-28 04:41:07 -08:00
Case: "Faulty template",
ExpectedEnabled: true,
2022-01-26 06:54:36 -08:00
ExpectedString: template.IncorrectTemplate,
2021-12-28 04:41:07 -08:00
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,
},
{
Case: "Az CLI Profile only",
ExpectedEnabled: true,
ExpectedString: "AzureCliCloud",
Template: "{{ .EnvironmentName }}",
HasCLI: true,
Source: cli,
},
{
Case: "Az CLI Profile only - disabled",
ExpectedEnabled: false,
Template: "{{ .EnvironmentName }}",
HasCLI: false,
Source: cli,
},
{
Case: "PowerShell Profile only",
ExpectedEnabled: true,
ExpectedString: "AzurePoshCloud",
Template: "{{ .EnvironmentName }}",
HasPowerShell: true,
Source: pwsh,
},
{
Case: "Az CLI Profile only - disabled",
ExpectedEnabled: false,
Template: "{{ .EnvironmentName }}",
Source: pwsh,
},
2022-03-21 08:05:23 -07:00
{
Case: "Az CLI account type",
ExpectedEnabled: true,
ExpectedString: "user",
Template: "{{ .User.Type }}",
HasCLI: true,
Source: cli,
},
}
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)
var azureProfile, azureRmContext string
2021-12-28 04:41:07 -08:00
if tc.HasCLI {
2022-01-26 06:54:36 -08:00
content, _ := ioutil.ReadFile("../test/azureProfile.json")
2021-12-28 04:41:07 -08:00
azureProfile = string(content)
}
if tc.HasPowerShell {
2022-01-26 06:54:36 -08:00
content, _ := ioutil.ReadFile("../test/AzureRmContext.json")
2021-12-28 04:41:07 -08:00
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("Getenv", "AZURE_CONFIG_DIR").Return("")
if tc.Source == "" {
tc.Source = firstMatch
}
2022-01-26 05:10:18 -08:00
az := &Az{
env: env,
props: properties.Map{
Source: tc.Source,
},
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)
}
}