-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add platform/darwin aware CLI config resolution
- Loading branch information
Showing
3 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |