From b2c8536dbcf3d6cb47e8171ed4377015ea7ef78e Mon Sep 17 00:00:00 2001 From: Artis Avotins Date: Wed, 30 Oct 2024 14:11:25 +0200 Subject: [PATCH 1/2] Add platform/darwin aware CLI config resolution --- main.go | 7 +++--- utils/path.go | 28 ++++++++++++++++++++++ utils/path_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 utils/path.go create mode 100644 utils/path_test.go diff --git a/main.go b/main.go index b1b5883..eaaf96e 100644 --- a/main.go +++ b/main.go @@ -10,12 +10,11 @@ import ( "os" "os/exec" "os/signal" - "os/user" - "path" "strings" "github.com/arduino/arduino-language-server/ls" "github.com/arduino/arduino-language-server/streams" + "github.com/arduino/arduino-language-server/utils" "github.com/arduino/go-paths-helper" "github.com/mattn/go-isatty" ) @@ -101,8 +100,8 @@ func main() { } } else { if *cliConfigPath == "" { - if user, _ := user.Current(); user != nil { - candidate := path.Join(user.HomeDir, ".arduino15/arduino-cli.yaml") + candidate := utils.GetDefaultCliConfigPath() + if candidate != "" { if _, err := os.Stat(candidate); err == nil { *cliConfigPath = candidate log.Printf("ArduinoCLI config file found at %s\n", candidate) diff --git a/utils/path.go b/utils/path.go new file mode 100644 index 0000000..9d57889 --- /dev/null +++ b/utils/path.go @@ -0,0 +1,28 @@ +package utils + +import ( + "os/user" + "path" + "runtime" +) + +// package-level variables for mocking in tests +var ( + userCurrent = user.Current + getGOOS = runtime.GOOS +) + +// GetDefaultCliConfigPath returns the default path for the ArduinoCLI configuration file. +func GetDefaultCliConfigPath() string { + if user, _ := userCurrent(); user != nil { + return path.Join(user.HomeDir, func() string { + switch getGOOS { + case "darwin": + return "Library/Arduino15" + default: + return ".arduino15" + } + }(), "arduino-cli.yaml") + } + return "" +} diff --git a/utils/path_test.go b/utils/path_test.go new file mode 100644 index 0000000..cd8b653 --- /dev/null +++ b/utils/path_test.go @@ -0,0 +1,60 @@ +package utils + +import ( + "os/user" + "path" + "testing" +) + +func TestGetDefaultCliConfigPath(t *testing.T) { + // Save original GOOS getter and restore after test + originalGetGOOS := getGOOS + defer func() { getGOOS = originalGetGOOS }() + + tests := []struct { + name string + goos string + wantPath string + user *user.User + }{ + { + name: "darwin path", + goos: "darwin", + user: &user.User{HomeDir: "/Users/test"}, + wantPath: path.Join("/Users/test", "Library/Arduino15", "arduino-cli.yaml"), + }, + { + name: "linux path", + goos: "linux", + user: &user.User{HomeDir: "/home/test"}, + wantPath: path.Join("/home/test", ".arduino15", "arduino-cli.yaml"), + }, + { + name: "windows path", + goos: "windows", + user: &user.User{HomeDir: "C:\\Users\\test"}, + wantPath: path.Join("C:\\Users\\test", ".arduino15", "arduino-cli.yaml"), + }, + { + name: "nil user", + goos: "linux", + user: nil, + wantPath: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // mocks + getGOOS = tt.goos + userCurrent = func() (*user.User, error) { + return tt.user, nil + } + + got := GetDefaultCliConfigPath() + if got != tt.wantPath { + t.Errorf("GetDefaultCliConfigPath() = %v, want %v", got, tt.wantPath) + } + }) + } +} From ca853c1c4f28d9a34bc309ea6e9aaae9ec2e369f Mon Sep 17 00:00:00 2001 From: Artis Avotins Date: Wed, 30 Oct 2024 15:47:02 +0200 Subject: [PATCH 2/2] Add platform aware windows path handling --- utils/path.go | 2 ++ utils/path_test.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/utils/path.go b/utils/path.go index 9d57889..e8df153 100644 --- a/utils/path.go +++ b/utils/path.go @@ -19,6 +19,8 @@ func GetDefaultCliConfigPath() string { switch getGOOS { case "darwin": return "Library/Arduino15" + case "windows": + return "AppData\\Local\\Arduino15" default: return ".arduino15" } diff --git a/utils/path_test.go b/utils/path_test.go index cd8b653..972de24 100644 --- a/utils/path_test.go +++ b/utils/path_test.go @@ -33,7 +33,7 @@ func TestGetDefaultCliConfigPath(t *testing.T) { name: "windows path", goos: "windows", user: &user.User{HomeDir: "C:\\Users\\test"}, - wantPath: path.Join("C:\\Users\\test", ".arduino15", "arduino-cli.yaml"), + wantPath: path.Join("C:\\Users\\test", "AppData\\Local\\Arduino15", "arduino-cli.yaml"), }, { name: "nil user",