Skip to content

Commit 4bf4954

Browse files
committed
Add platform/darwin aware CLI config resolution
1 parent 002b3ab commit 4bf4954

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

Diff for: main.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
"os"
1111
"os/exec"
1212
"os/signal"
13-
"os/user"
14-
"path"
1513
"strings"
1614

1715
"github.com/arduino/arduino-language-server/ls"
1816
"github.com/arduino/arduino-language-server/streams"
17+
"github.com/arduino/arduino-language-server/utils"
1918
"github.com/arduino/go-paths-helper"
2019
"github.com/mattn/go-isatty"
2120
)
@@ -101,8 +100,8 @@ func main() {
101100
}
102101
} else {
103102
if *cliConfigPath == "" {
104-
if user, _ := user.Current(); user != nil {
105-
candidate := path.Join(user.HomeDir, ".arduino15/arduino-cli.yaml")
103+
candidate := utils.GetDefaultCliConfigPath()
104+
if candidate != "" {
106105
if _, err := os.Stat(candidate); err == nil {
107106
*cliConfigPath = candidate
108107
log.Printf("ArduinoCLI config file found at %s\n", candidate)

Diff for: utils/path.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package utils
2+
3+
import (
4+
"os/user"
5+
"path"
6+
"runtime"
7+
)
8+
9+
// package-level variables for mocking in tests
10+
var (
11+
userCurrent = user.Current
12+
getGOOS = runtime.GOOS
13+
)
14+
15+
func GetDefaultCliConfigPath() string {
16+
if user, _ := userCurrent(); user != nil {
17+
candidate := path.Join(user.HomeDir, func() string {
18+
switch getGOOS {
19+
case "darwin":
20+
return "Library/Arduino15"
21+
default:
22+
return ".arduino15"
23+
}
24+
}(), "arduino-cli.yaml")
25+
return candidate
26+
}
27+
return ""
28+
}

Diff for: utils/path_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package utils
2+
3+
import (
4+
"os/user"
5+
"path"
6+
"testing"
7+
)
8+
9+
func TestGetDefaultCliConfigPath(t *testing.T) {
10+
// Save original GOOS getter and restore after test
11+
originalGetGOOS := getGOOS
12+
defer func() { getGOOS = originalGetGOOS }()
13+
14+
tests := []struct {
15+
name string
16+
goos string
17+
wantPath string
18+
user *user.User
19+
}{
20+
{
21+
name: "darwin path",
22+
goos: "darwin",
23+
user: &user.User{HomeDir: "/Users/test"},
24+
wantPath: path.Join("/Users/test", "Library/Arduino15", "arduino-cli.yaml"),
25+
},
26+
{
27+
name: "linux path",
28+
goos: "linux",
29+
user: &user.User{HomeDir: "/home/test"},
30+
wantPath: path.Join("/home/test", ".arduino15", "arduino-cli.yaml"),
31+
},
32+
{
33+
name: "windows path",
34+
goos: "windows",
35+
user: &user.User{HomeDir: "C:\\Users\\test"},
36+
wantPath: path.Join("C:\\Users\\test", ".arduino15", "arduino-cli.yaml"),
37+
},
38+
{
39+
name: "nil user",
40+
goos: "linux",
41+
user: nil,
42+
wantPath: "",
43+
},
44+
}
45+
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
// mocks
49+
getGOOS = tt.goos
50+
userCurrent = func() (*user.User, error) {
51+
return tt.user, nil
52+
}
53+
54+
got := GetDefaultCliConfigPath()
55+
if got != tt.wantPath {
56+
t.Errorf("GetDefaultCliConfigPath() = %v, want %v", got, tt.wantPath)
57+
}
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)