Skip to content

Commit 1a264de

Browse files
committed
Fix test failure
1 parent 93ac10a commit 1a264de

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

artifactory/commands/ide/jetbrains/jetbrains.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,14 @@ func (jc *JetbrainsCommand) detectJetBrainsIDEs() error {
164164
case "windows":
165165
configBasePath = filepath.Join(os.Getenv("APPDATA"), "JetBrains")
166166
case "linux":
167-
configBasePath = filepath.Join(os.Getenv("HOME"), ".config", "JetBrains")
168-
// Also check legacy location
167+
// Respect XDG_CONFIG_HOME environment variable
168+
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
169+
if xdgConfigHome != "" {
170+
configBasePath = filepath.Join(xdgConfigHome, "JetBrains")
171+
} else {
172+
configBasePath = filepath.Join(os.Getenv("HOME"), ".config", "JetBrains")
173+
}
174+
// Also check legacy location if primary path doesn't exist
169175
if _, err := os.Stat(configBasePath); os.IsNotExist(err) {
170176
legacyPath := filepath.Join(os.Getenv("HOME"), ".JetBrains")
171177
if _, err := os.Stat(legacyPath); err == nil {

artifactory/commands/ide/jetbrains/jetbrains_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,58 @@ func TestJetbrainsCommand_DetectJetBrainsIDEs(t *testing.T) {
113113
}
114114
}
115115

116+
func TestJetbrainsCommand_DetectJetBrainsIDEs_WithXDGConfigHome(t *testing.T) {
117+
if runtime.GOOS != "linux" {
118+
// For testing purposes, we can't change runtime.GOOS, so skip on non-Linux
119+
t.Skip("XDG_CONFIG_HOME test is only fully testable on Linux")
120+
}
121+
122+
// Create temporary directory for XDG_CONFIG_HOME
123+
tempDir := t.TempDir()
124+
xdgConfigHome := filepath.Join(tempDir, "config")
125+
126+
// Create mock JetBrains configuration directory structure
127+
jetbrainsDir := filepath.Join(xdgConfigHome, "JetBrains")
128+
ideaDir := filepath.Join(jetbrainsDir, "IntelliJIdea2023.3")
129+
err := os.MkdirAll(ideaDir, 0755)
130+
require.NoError(t, err)
131+
132+
// Create mock idea.properties file
133+
propertiesPath := filepath.Join(ideaDir, "idea.properties")
134+
err = os.WriteFile(propertiesPath, []byte("# Test properties\n"), 0644)
135+
require.NoError(t, err)
136+
137+
// Set XDG_CONFIG_HOME environment variable
138+
originalXDG := os.Getenv("XDG_CONFIG_HOME")
139+
defer func() {
140+
if originalXDG != "" {
141+
if err := os.Setenv("XDG_CONFIG_HOME", originalXDG); err != nil {
142+
t.Logf("Warning: failed to restore XDG_CONFIG_HOME: %v", err)
143+
}
144+
} else {
145+
if err := os.Unsetenv("XDG_CONFIG_HOME"); err != nil {
146+
t.Logf("Warning: failed to unset XDG_CONFIG_HOME: %v", err)
147+
}
148+
}
149+
}()
150+
err = os.Setenv("XDG_CONFIG_HOME", xdgConfigHome)
151+
require.NoError(t, err)
152+
153+
// Test detection
154+
cmd := NewJetbrainsCommand("", "")
155+
err = cmd.detectJetBrainsIDEs()
156+
assert.NoError(t, err, "Detection should succeed when XDG_CONFIG_HOME is set")
157+
assert.Len(t, cmd.detectedIDEs, 1, "Should detect one IDE")
158+
159+
if len(cmd.detectedIDEs) > 0 {
160+
ide := cmd.detectedIDEs[0]
161+
assert.Equal(t, "IntelliJ IDEA", ide.Name)
162+
assert.Equal(t, "2023.3", ide.Version)
163+
assert.Equal(t, propertiesPath, ide.PropertiesPath)
164+
assert.Equal(t, ideaDir, ide.ConfigDir)
165+
}
166+
}
167+
116168
func TestJetbrainsCommand_CreateBackup(t *testing.T) {
117169
// Create temporary idea.properties file
118170
tempDir := t.TempDir()

0 commit comments

Comments
 (0)