Skip to content

Commit

Permalink
Add platform/darwin aware CLI config resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
artis101 committed Oct 30, 2024
1 parent 002b3ab commit b2c8536
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
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
28 changes: 28 additions & 0 deletions utils/path.go
Original file line number Diff line number Diff line change
@@ -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 ""
}
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", ".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)
}
})
}
}

0 comments on commit b2c8536

Please sign in to comment.