2020-10-14 16:56:25 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-28 04:41:07 -08:00
|
|
|
"io/ioutil"
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/environment"
|
|
|
|
"oh-my-posh/mock"
|
2022-01-26 04:53:35 -08:00
|
|
|
"oh-my-posh/properties"
|
2021-12-28 04:41:07 -08:00
|
|
|
"path/filepath"
|
2020-10-14 16:56:25 -07:00
|
|
|
"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-04-22 11:18:58 -07:00
|
|
|
},
|
|
|
|
{
|
2021-12-28 04:41:07 -08:00
|
|
|
Case: "Az CLI Profile",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
ExpectedString: "AzureCliCloud",
|
|
|
|
Template: "{{ .EnvironmentName }}",
|
|
|
|
HasCLI: true,
|
2021-04-22 11:18:58 -07:00
|
|
|
},
|
|
|
|
{
|
2021-12-28 04:41:07 -08:00
|
|
|
Case: "Az Pwsh Profile",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
ExpectedString: "AzurePoshCloud",
|
|
|
|
Template: "{{ .EnvironmentName }}",
|
|
|
|
HasPowerShell: true,
|
2021-04-22 11:18:58 -07:00
|
|
|
},
|
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
|
|
|
},
|
2022-01-13 11:29:03 -08:00
|
|
|
{
|
|
|
|
Case: "PWSH",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
ExpectedString: "PWSH",
|
|
|
|
Template: "{{ .Origin }}",
|
|
|
|
HasPowerShell: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "CLI",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
ExpectedString: "CLI",
|
|
|
|
Template: "{{ .Origin }}",
|
|
|
|
HasCLI: true,
|
|
|
|
},
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
|
2021-03-27 06:52:27 -07:00
|
|
|
for _, tc := range cases {
|
2022-01-26 01:23:18 -08:00
|
|
|
env := new(mock.MockedEnvironment)
|
2021-12-28 04:41:07 -08:00
|
|
|
home := "/Users/posh"
|
2022-01-23 12:37:51 -08:00
|
|
|
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)
|
|
|
|
}
|
2022-01-26 01:23:18 -08:00
|
|
|
env.On("GOOS").Return(environment.LinuxPlatform)
|
2022-01-23 12:37:51 -08:00
|
|
|
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)
|
2022-01-26 05:10:18 -08:00
|
|
|
az := &Az{
|
2021-03-27 06:52:27 -07:00
|
|
|
env: env,
|
2022-01-26 04:53:35 -08:00
|
|
|
props: properties.Map{},
|
2021-03-27 06:52:27 -07:00
|
|
|
}
|
2022-01-26 05:26:56 -08:00
|
|
|
assert.Equal(t, tc.ExpectedEnabled, az.Enabled(), tc.Case)
|
2022-01-23 12:37:51 -08:00
|
|
|
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, az), tc.Case)
|
2020-10-14 16:56:25 -07:00
|
|
|
}
|
|
|
|
}
|