Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add platform aware CLI config resolution #197

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions utils/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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"
case "windows":
return "AppData\\Local\\Arduino15"
default:
return ".arduino15"
artis101 marked this conversation as resolved.
Show resolved Hide resolved
}
}(), "arduino-cli.yaml")
}
return ""
}
60 changes: 60 additions & 0 deletions utils/path_test.go
Original file line number Diff line number Diff line change
@@ -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", "AppData\\Local\\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)
}
})
}
}
Loading