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

151 lines
3.8 KiB
Go
Raw Normal View History

2022-01-26 06:54:36 -08:00
package segments
import (
2022-06-01 00:50:20 -07:00
"os"
2021-12-28 04:41:07 -08:00
"path/filepath"
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/mock"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
2022-12-28 08:30:48 -08:00
"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,
ExpectedString: "<.Data.Burp>: can't evaluate field Burp in type template.Data",
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-06-01 00:50:20 -07:00
content, _ := os.ReadFile("../test/azureProfile.json")
2021-12-28 04:41:07 -08:00
azureProfile = string(content)
}
if tc.HasPowerShell {
2022-06-01 00:50:20 -07:00
content, _ := os.ReadFile("../test/AzureRmContext.json")
2021-12-28 04:41:07 -08:00
azureRmContext = string(content)
}
2022-11-09 11:27:54 -08:00
env.On("GOOS").Return(platform.LINUX)
env.On("FileContent", filepath.Join(home, ".azure", "azureProfile.json")).Return(azureProfile)
env.On("Getenv", "POSH_AZURE_SUBSCRIPTION").Return(azureRmContext)
env.On("Getenv", "AZURE_CONFIG_DIR").Return("")
if tc.HasCLI {
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "azureProfile.json").Return(true)
} else {
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "azureProfile.json").Return(false)
env.On("HasFilesInDir", filepath.Clean("/Users/posh/.Azure"), "azureProfile.json").Return(false)
}
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)
}
}