Skip to content

Commit 9889825

Browse files
committed
feat(config): update error handling in GetConfigPath and add test for non-existing config path
1 parent f5a9afc commit 9889825

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package config
1818
import (
1919
// we need this for the config ini in this package
2020
_ "embed"
21+
"fmt"
2122
"os"
2223

2324
"github.com/arduino/go-paths-helper"
@@ -153,7 +154,7 @@ func GetConfigPath() *paths.Path {
153154
if envConfig := os.Getenv("ARDUINO_CREATE_AGENT_CONFIG"); envConfig != "" {
154155
configPath = paths.New(envConfig)
155156
if configPath.NotExist() {
156-
log.Panicf("config from env var %s does not exists", envConfig)
157+
panic(fmt.Sprintf("config from env var %s does not exists", envConfig))
157158
}
158159
log.Infof("using config from env variable: %s", configPath)
159160
} else if defaultConfigPath := configDir.Join("config.ini"); defaultConfigPath.Exist() {

config/config_linux_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"fmt"
45
"os"
56
"runtime"
67
"testing"
@@ -82,7 +83,30 @@ func TestIfHomeDoesNotContainConfigTheDefaultConfigAreCopied(t *testing.T) {
8283
}
8384

8485
assert.Equal(t, string(configContent), string(givenContent))
86+
}
87+
88+
// TestGetConfigPathPanicIfPathDoesNotExist tests that it panics if the ARDUINO_CREATE_AGENT_CONFIG env variable point to an non-existing path
89+
func TestGetConfigPathPanicIfPathDoesNotExist(t *testing.T) {
90+
if runtime.GOOS != "linux" {
91+
t.Skip("Skipping test on non-linux OS")
92+
}
93+
os.Setenv("HOME", "./testdata/dummyhome")
94+
defer os.Unsetenv("HOME")
8595

96+
os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/a-not-existing-path/config.ini")
97+
98+
defer func() {
99+
if r := recover(); r != nil {
100+
assert.Equal(t, fmt.Sprintf("config from env var %s does not exists", "./testdata/a-not-existing-path/config.ini"), r)
101+
} else {
102+
t.Fatal("Expected panic but did not occur")
103+
}
104+
}()
105+
106+
configPath := GetConfigPath()
107+
108+
assert.Equal(t, "testdata/fromxdghome/ArduinoCreateAgent/config.ini", configPath.String())
109+
checkIniName(t, configPath, "this-is-a-config-file-from-xdghome-dir")
86110
}
87111

88112
func checkIniName(t *testing.T, confipath *paths.Path, expected string) {

0 commit comments

Comments
 (0)