File tree 3 files changed +91
-4
lines changed
3 files changed +91
-4
lines changed Original file line number Diff line number Diff line change @@ -10,12 +10,11 @@ import (
10
10
"os"
11
11
"os/exec"
12
12
"os/signal"
13
- "os/user"
14
- "path"
15
13
"strings"
16
14
17
15
"github.com/arduino/arduino-language-server/ls"
18
16
"github.com/arduino/arduino-language-server/streams"
17
+ "github.com/arduino/arduino-language-server/utils"
19
18
"github.com/arduino/go-paths-helper"
20
19
"github.com/mattn/go-isatty"
21
20
)
@@ -101,8 +100,8 @@ func main() {
101
100
}
102
101
} else {
103
102
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 != "" {
106
105
if _ , err := os .Stat (candidate ); err == nil {
107
106
* cliConfigPath = candidate
108
107
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
+ 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
+ }
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