fix(az): proper path fix

This commit is contained in:
Jan De Dobbeleer 2022-01-13 22:35:58 +01:00 committed by Jan De Dobbeleer
parent 7aaae67a77
commit 4542e92940
2 changed files with 33 additions and 15 deletions

View file

@ -102,11 +102,6 @@ func (a *az) enabled() bool {
}
func (a *az) getFileContentWithoutBom(file string) string {
azFolder := ".azure"
if a.env.getRuntimeGOOS() == linuxPlatform {
azFolder = ".Azure"
}
file = filepath.Join(a.env.homeDir(), azFolder, file)
config := a.env.getFileContent(file)
const ByteOrderMark = "\ufeff"
return strings.TrimLeft(config, ByteOrderMark)
@ -114,7 +109,8 @@ func (a *az) getFileContentWithoutBom(file string) string {
func (a *az) getAzureProfile() bool {
var content string
if content = a.getFileContentWithoutBom("azureProfile.json"); len(content) == 0 {
profile := filepath.Join(a.env.homeDir(), ".azure", "azureProfile.json")
if content = a.getFileContentWithoutBom(profile); len(content) == 0 {
return false
}
var config AzureConfig
@ -133,7 +129,16 @@ func (a *az) getAzureProfile() bool {
func (a *az) getAzureRmContext() bool {
var content string
if content = a.getFileContentWithoutBom("AzureRmContext.json"); len(content) == 0 {
profiles := []string{
filepath.Join(a.env.homeDir(), ".azure", "AzureRmContext.json"),
filepath.Join(a.env.homeDir(), ".Azure", "AzureRmContext.json"),
}
for _, profile := range profiles {
if content = a.getFileContentWithoutBom(profile); len(content) != 0 {
break
}
}
if len(content) == 0 {
return false
}
var config AzurePowerShellConfig

View file

@ -10,12 +10,13 @@ import (
func TestAzSegment(t *testing.T) {
cases := []struct {
Case string
ExpectedEnabled bool
ExpectedString string
HasCLI bool
HasPowerShell bool
Template string
Case string
ExpectedEnabled bool
ExpectedString string
HasCLI bool
HasPowerShell bool
HasPowerShellUnix bool
Template string
}{
{
Case: "no config files found",
@ -35,6 +36,13 @@ func TestAzSegment(t *testing.T) {
Template: "{{ .EnvironmentName }}",
HasPowerShell: true,
},
{
Case: "Az Pwsh Profile",
ExpectedEnabled: true,
ExpectedString: "AzurePoshCloud",
Template: "{{ .EnvironmentName }}",
HasPowerShellUnix: true,
},
{
Case: "Faulty template",
ExpectedEnabled: true,
@ -62,7 +70,7 @@ func TestAzSegment(t *testing.T) {
env := new(MockedEnvironment)
home := "/Users/posh"
env.On("homeDir", nil).Return(home)
var azureProfile, azureRmContext string
var azureProfile, azureRmContext, azureRMContext string
if tc.HasCLI {
content, _ := ioutil.ReadFile("./test/azureProfile.json")
azureProfile = string(content)
@ -71,9 +79,14 @@ func TestAzSegment(t *testing.T) {
content, _ := ioutil.ReadFile("./test/AzureRmContext.json")
azureRmContext = string(content)
}
if tc.HasPowerShellUnix {
content, _ := ioutil.ReadFile("./test/AzureRmContext.json")
azureRMContext = string(content)
}
env.On("getRuntimeGOOS", nil).Return(linuxPlatform)
env.On("getFileContent", filepath.Join(home, ".Azure", "azureProfile.json")).Return(azureProfile)
env.On("getFileContent", filepath.Join(home, ".azure", "azureProfile.json")).Return(azureProfile)
env.On("getFileContent", filepath.Join(home, ".Azure", "AzureRmContext.json")).Return(azureRmContext)
env.On("getFileContent", filepath.Join(home, ".azure", "AzureRmContext.json")).Return(azureRMContext)
env.onTemplate()
props := properties{
SegmentTemplate: tc.Template,