Repository: JanDeDobbeleer/oh-my-posh
Pull request #1574 (2022-01-13 21:36:21)

fix(az): proper path fix
### Prerequisites

- [x] I have read and understood the `CONTRIBUTING` guide
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added / updated (for bug fixes/features)

### Description

[Description of the change]

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/segment_az.go b/src/segment_az.go
index 33835f8e..a9428d67 100644
--- a/src/segment_az.go
+++ b/src/segment_az.go
@@ -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


Test patch:
diff --git a/src/segment_az_test.go b/src/segment_az_test.go
index ffb676f0..09faf1b1 100644
--- a/src/segment_az_test.go
+++ b/src/segment_az_test.go
@@ -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,