Skip to content

Commit

Permalink
feat(config): update error handling in GetConfigPath and add test for…
Browse files Browse the repository at this point in the history
… non-existing config path
  • Loading branch information
dido18 committed Jan 29, 2025
1 parent f5a9afc commit 9889825
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config
import (
// we need this for the config ini in this package
_ "embed"
"fmt"
"os"

"github.com/arduino/go-paths-helper"
Expand Down Expand Up @@ -153,7 +154,7 @@ func GetConfigPath() *paths.Path {
if envConfig := os.Getenv("ARDUINO_CREATE_AGENT_CONFIG"); envConfig != "" {
configPath = paths.New(envConfig)
if configPath.NotExist() {
log.Panicf("config from env var %s does not exists", envConfig)
panic(fmt.Sprintf("config from env var %s does not exists", envConfig))
}
log.Infof("using config from env variable: %s", configPath)
} else if defaultConfigPath := configDir.Join("config.ini"); defaultConfigPath.Exist() {
Expand Down
24 changes: 24 additions & 0 deletions config/config_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"runtime"
"testing"
Expand Down Expand Up @@ -82,7 +83,30 @@ func TestIfHomeDoesNotContainConfigTheDefaultConfigAreCopied(t *testing.T) {
}

assert.Equal(t, string(configContent), string(givenContent))
}

// TestGetConfigPathPanicIfPathDoesNotExist tests that it panics if the ARDUINO_CREATE_AGENT_CONFIG env variable point to an non-existing path
func TestGetConfigPathPanicIfPathDoesNotExist(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("Skipping test on non-linux OS")
}
os.Setenv("HOME", "./testdata/dummyhome")
defer os.Unsetenv("HOME")

os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/a-not-existing-path/config.ini")

defer func() {
if r := recover(); r != nil {
assert.Equal(t, fmt.Sprintf("config from env var %s does not exists", "./testdata/a-not-existing-path/config.ini"), r)
} else {
t.Fatal("Expected panic but did not occur")
}
}()

configPath := GetConfigPath()

assert.Equal(t, "testdata/fromxdghome/ArduinoCreateAgent/config.ini", configPath.String())
checkIniName(t, configPath, "this-is-a-config-file-from-xdghome-dir")
}

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

0 comments on commit 9889825

Please sign in to comment.