Repository: JanDeDobbeleer/oh-my-posh
Pull request #2772 (2022-09-12 17:35:31)

fix az
- chore: rename wholespace correctly
- fix(az): find config in all possible folders

### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md]
- [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

<!--TemplateBody-->

<!---

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.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/segments/az.go b/src/segments/az.go
index 93d46ce1..f094709a 100644
--- a/src/segments/az.go
+++ b/src/segments/az.go
@@ -119,13 +119,12 @@ func (a *Az) FileContentWithoutBom(file string) string {
 }
 
 func (a *Az) getCLISubscription() bool {
-	var content string
-	configDir, err := a.ConfigDir(true)
+	cfg, err := a.findConfig("azureProfile.json")
 	if err != nil {
 		return false
 	}
-	profile := filepath.Join(configDir, "azureProfile.json")
-	if content = a.FileContentWithoutBom(profile); len(content) == 0 {
+	content := a.FileContentWithoutBom(cfg)
+	if len(content) == 0 {
 		return false
 	}
 	var config AzureConfig
@@ -143,19 +142,11 @@ func (a *Az) getCLISubscription() bool {
 }
 
 func (a *Az) getModuleSubscription() bool {
-	var content string
-	configDir, err := a.ConfigDir(false)
+	cfg, err := a.findConfig("AzureRmContext.json")
 	if err != nil {
 		return false
 	}
-	profiles := []string{
-		filepath.Join(configDir, "AzureRmContext.json"),
-	}
-	for _, profile := range profiles {
-		if content = a.FileContentWithoutBom(profile); len(content) != 0 {
-			break
-		}
-	}
+	content := a.FileContentWithoutBom(cfg)
 	if len(content) == 0 {
 		return false
 	}
@@ -181,17 +172,15 @@ func (a *Az) getModuleSubscription() bool {
 	return true
 }
 
-func (a *Az) ConfigDir(cli bool) (string, error) {
+func (a *Az) findConfig(fileName string) (string, error) {
 	configDirs := []string{
 		filepath.Join(a.env.Home(), ".azure"),
 		filepath.Join(a.env.Home(), ".Azure"),
-	}
-	if cli {
-		configDirs = append([]string{a.env.Getenv("AZURE_CONFIG_DIR")}, configDirs...)
+		a.env.Getenv("AZURE_CONFIG_DIR"),
 	}
 	for _, dir := range configDirs {
-		if len(dir) != 0 && a.env.HasFolder(dir) {
-			return dir, nil
+		if len(dir) != 0 && a.env.HasFilesInDir(dir, fileName) {
+			return filepath.Join(dir, fileName), nil
 		}
 	}
 	return "", errors.New("azure config dir not found")


Test patch:
diff --git a/src/segments/az_test.go b/src/segments/az_test.go
index abda7cd5..06a0f0a0 100644
--- a/src/segments/az_test.go
+++ b/src/segments/az_test.go
@@ -112,6 +112,7 @@ func TestAzSegment(t *testing.T) {
 		home := "/Users/posh"
 		env.On("Home").Return(home)
 		var azureProfile, azureRmContext string
+
 		if tc.HasCLI {
 			content, _ := os.ReadFile("../test/azureProfile.json")
 			azureProfile = string(content)
@@ -120,14 +121,30 @@ func TestAzSegment(t *testing.T) {
 			content, _ := os.ReadFile("../test/AzureRmContext.json")
 			azureRmContext = string(content)
 		}
+
 		env.On("GOOS").Return(environment.LINUX)
 		env.On("FileContent", filepath.Join(home, ".azure", "azureProfile.json")).Return(azureProfile)
 		env.On("FileContent", filepath.Join(home, ".azure", "AzureRmContext.json")).Return(azureRmContext)
 		env.On("Getenv", "AZURE_CONFIG_DIR").Return("")
-		env.On("HasFolder", filepath.Clean("/Users/posh/.azure")).Return(true)
+
+		if tc.HasCLI {
+			env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "azureProfile.json").Return(true)
+			env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "AzureRmContext.json").Return(false)
+		} else if tc.HasPowerShell {
+			env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "azureProfile.json").Return(false)
+			env.On("HasFilesInDir", filepath.Clean("/Users/posh/.Azure"), "azureProfile.json").Return(false)
+			env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "AzureRmContext.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)
+			env.On("HasFilesInDir", filepath.Clean("/Users/posh/.azure"), "AzureRmContext.json").Return(false)
+			env.On("HasFilesInDir", filepath.Clean("/Users/posh/.Azure"), "AzureRmContext.json").Return(false)
+		}
+
 		if tc.Source == "" {
 			tc.Source = firstMatch
 		}
+
 		az := &Az{
 			env: env,
 			props: properties.Map{