fix(az): fetch path from AZURE_CONFIG_DIR

resolves #1775
This commit is contained in:
Jan De Dobbeleer 2022-02-18 19:42:17 +01:00 committed by Jan De Dobbeleer
parent 6a06bc5b4c
commit 3efb979053
2 changed files with 25 additions and 22 deletions

View file

@ -101,7 +101,7 @@ func (a *Az) FileContentWithoutBom(file string) string {
func (a *Az) getAzureProfile() bool { func (a *Az) getAzureProfile() bool {
var content string var content string
profile := filepath.Join(a.env.Home(), ".azure", "azureProfile.json") profile := filepath.Join(a.ConfigHome(), "azureProfile.json")
if content = a.FileContentWithoutBom(profile); len(content) == 0 { if content = a.FileContentWithoutBom(profile); len(content) == 0 {
return false return false
} }
@ -121,9 +121,9 @@ func (a *Az) getAzureProfile() bool {
func (a *Az) getAzureRmContext() bool { func (a *Az) getAzureRmContext() bool {
var content string var content string
cfgHome := a.ConfigHome()
profiles := []string{ profiles := []string{
filepath.Join(a.env.Home(), ".azure", "AzureRmContext.json"), filepath.Join(cfgHome, "AzureRmContext.json"),
filepath.Join(a.env.Home(), ".Azure", "AzureRmContext.json"),
} }
for _, profile := range profiles { for _, profile := range profiles {
if content = a.FileContentWithoutBom(profile); len(content) != 0 { if content = a.FileContentWithoutBom(profile); len(content) != 0 {
@ -153,3 +153,11 @@ func (a *Az) getAzureRmContext() bool {
a.Origin = "PWSH" a.Origin = "PWSH"
return true return true
} }
func (a *Az) ConfigHome() string {
cfgHome := a.env.Getenv("AZURE_CONFIG_DIR")
if len(cfgHome) != 0 {
return cfgHome
}
return filepath.Join(a.env.Home(), ".azure")
}

View file

@ -14,13 +14,12 @@ import (
func TestAzSegment(t *testing.T) { func TestAzSegment(t *testing.T) {
cases := []struct { cases := []struct {
Case string Case string
ExpectedEnabled bool ExpectedEnabled bool
ExpectedString string ExpectedString string
HasCLI bool HasCLI bool
HasPowerShell bool HasPowerShell bool
HasPowerShellUnix bool Template string
Template string
}{ }{
{ {
Case: "no config files found", Case: "no config files found",
@ -41,11 +40,11 @@ func TestAzSegment(t *testing.T) {
HasPowerShell: true, HasPowerShell: true,
}, },
{ {
Case: "Az Pwsh Profile", Case: "Az Pwsh Profile",
ExpectedEnabled: true, ExpectedEnabled: true,
ExpectedString: "AzurePoshCloud", ExpectedString: "AzurePoshCloud",
Template: "{{ .EnvironmentName }}", Template: "{{ .EnvironmentName }}",
HasPowerShellUnix: true, HasPowerShell: true,
}, },
{ {
Case: "Faulty template", Case: "Faulty template",
@ -74,7 +73,7 @@ func TestAzSegment(t *testing.T) {
env := new(mock.MockedEnvironment) env := new(mock.MockedEnvironment)
home := "/Users/posh" home := "/Users/posh"
env.On("Home").Return(home) env.On("Home").Return(home)
var azureProfile, azureRmContext, azureRMContext string var azureProfile, azureRmContext string
if tc.HasCLI { if tc.HasCLI {
content, _ := ioutil.ReadFile("../test/azureProfile.json") content, _ := ioutil.ReadFile("../test/azureProfile.json")
azureProfile = string(content) azureProfile = string(content)
@ -83,14 +82,10 @@ func TestAzSegment(t *testing.T) {
content, _ := ioutil.ReadFile("../test/AzureRmContext.json") content, _ := ioutil.ReadFile("../test/AzureRmContext.json")
azureRmContext = string(content) azureRmContext = string(content)
} }
if tc.HasPowerShellUnix {
content, _ := ioutil.ReadFile("../test/AzureRmContext.json")
azureRMContext = string(content)
}
env.On("GOOS").Return(environment.LinuxPlatform) env.On("GOOS").Return(environment.LinuxPlatform)
env.On("FileContent", filepath.Join(home, ".azure", "azureProfile.json")).Return(azureProfile) 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)
env.On("FileContent", filepath.Join(home, ".azure", "AzureRmContext.json")).Return(azureRMContext) env.On("Getenv", "AZURE_CONFIG_DIR").Return("")
az := &Az{ az := &Az{
env: env, env: env,
props: properties.Map{}, props: properties.Map{},