File tree Expand file tree Collapse file tree 3 files changed +91
-4
lines changed Expand file tree Collapse file tree 3 files changed +91
-4
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 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+ // GetDefaultCliConfigPath returns the default path for the ArduinoCLI configuration file.
16+ func GetDefaultCliConfigPath () string {
17+ if user , _ := userCurrent (); user != nil {
18+ return path .Join (user .HomeDir , func () string {
19+ switch getGOOS {
20+ case "darwin" :
21+ return "Library/Arduino15"
22+ default :
23+ return ".arduino15"
24+ }
25+ }(), "arduino-cli.yaml" )
26+ }
27+ return ""
28+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments