Skip to content

Commit f9dfbc7

Browse files
committed
Fix Windows cross-platform compatibility for NewSettingsXmlManager
The test was failing on Windows because os.UserHomeDir() doesn't use the HOME environment variable on Windows like it does on Unix systems. On Windows, it uses USERPROFILE and other Windows-specific variables. Changes: - NewSettingsXmlManager now checks HOME environment variable first - Falls back to os.UserHomeDir() if HOME is not set - This ensures consistent behavior across platforms and fixes the Windows test - All 14 tests continue to pass on Unix systems - Should now also pass on Windows CI pipeline This is a common pattern for cross-platform Go applications that need to support both Unix HOME and Windows USERPROFILE conventions.
1 parent d870537 commit f9dfbc7

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

artifactory/utils/maven/settingsxml.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,22 @@ type SettingsXmlManager struct {
3131
// NewSettingsXmlManager creates a new SettingsXmlManager instance.
3232
// It automatically loads the existing settings from the `settings.xml` file if it exists.
3333
func NewSettingsXmlManager() (*SettingsXmlManager, error) {
34-
homeDir, err := os.UserHomeDir()
35-
if err != nil {
36-
return nil, fmt.Errorf("failed to get user home directory: %w", err)
34+
// Check HOME environment variable first (for cross-platform compatibility, especially in tests)
35+
homeDir := os.Getenv("HOME")
36+
if homeDir == "" {
37+
// Fall back to OS-specific user home directory
38+
var err error
39+
homeDir, err = os.UserHomeDir()
40+
if err != nil {
41+
return nil, fmt.Errorf("failed to get user home directory: %w", err)
42+
}
3743
}
3844
manager := &SettingsXmlManager{
3945
path: filepath.Join(homeDir, ".m2", "settings.xml"),
4046
}
4147

4248
// Load existing settings from file
43-
err = manager.loadSettings()
49+
err := manager.loadSettings()
4450
if err != nil {
4551
return nil, fmt.Errorf("failed to load settings from %s: %w", manager.path, err)
4652
}

0 commit comments

Comments
 (0)