Skip to content

Commit

Permalink
feat(tests): enhance config tests with ini name checks and update tes…
Browse files Browse the repository at this point in the history
…t data
  • Loading branch information
dido18 committed Jan 29, 2025
1 parent aaa97f9 commit f5a9afc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
21 changes: 19 additions & 2 deletions config/config_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/arduino/go-paths-helper"
"github.com/go-ini/ini"
"github.com/stretchr/testify/assert"
)

Expand All @@ -18,7 +19,9 @@ func TestGetConfigPathFromXDG_CONFIG_HOME(t *testing.T) {
os.Setenv("XDG_CONFIG_HOME", "./testdata/fromxdghome")
defer os.Unsetenv("XDG_CONFIG_HOME")
configPath := GetConfigPath()

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

// TestGetConfigPathFromHOME tests the case when the config.ini is read from $HOME/.config/ArduinoCreateAgent/config.ini
Expand All @@ -29,8 +32,9 @@ func TestGetConfigPathFromHOME(t *testing.T) {
os.Setenv("HOME", "./testdata/fromhome")
defer os.Unsetenv("HOME")
configPath := GetConfigPath()
assert.Equal(t, "testdata/fromhome/.config/ArduinoCreateAgent/config.ini", configPath.String())

assert.Equal(t, "testdata/fromhome/.config/ArduinoCreateAgent/config.ini", configPath.String())
checkIniName(t, configPath, "this-is-a-config-file-from-home-di")
}

// TestGetConfigPathFromARDUINO_CREATE_AGENT_CONFIG tests the case when the config.ini is read from ARDUINO_CREATE_AGENT_CONFIG env variable
Expand All @@ -46,7 +50,7 @@ func TestGetConfigPathFromARDUINO_CREATE_AGENT_CONFIG(t *testing.T) {

configPath := GetConfigPath()
assert.Equal(t, "./testdata/from-arduino-create-agent-config-env/config.ini", configPath.String())

checkIniName(t, configPath, "this-is-a-config-file-from-home-dir-from-ARDUINO_CREATE_AGENT_CONFIG-env")
}

// TestIfHomeDoesNotContainConfigTheDefaultConfigAreCopied tests the case when the default config.ini is copied into $HOME/.config/ArduinoCreateAgent/config.ini
Expand All @@ -70,6 +74,7 @@ func TestIfHomeDoesNotContainConfigTheDefaultConfigAreCopied(t *testing.T) {
configPath := GetConfigPath()

assert.Equal(t, "testdata/home-without-config/.config/ArduinoCreateAgent/config.ini", configPath.String())
checkIniName(t, configPath, "") // the name of the default config is missing (an empty string)

givenContent, err := paths.New(configPath.String()).ReadFile()
if err != nil {
Expand All @@ -79,3 +84,15 @@ func TestIfHomeDoesNotContainConfigTheDefaultConfigAreCopied(t *testing.T) {
assert.Equal(t, string(configContent), string(givenContent))

}

func checkIniName(t *testing.T, confipath *paths.Path, expected string) {
cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true, AllowPythonMultilineValues: true}, confipath.String())
if err != nil {
t.Fatal(err)
}
defaultSection, err := cfg.GetSection("")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, expected, defaultSection.Key("name").String())
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name = this-is-a-config-file-from-home-dir-from-ARDUINO_CREATE_AGENT_CONFIG-env
gc = std
hostname = this-is-a-config-file-from-home-dir-from-ARDUINO_CREATE_AGENT_CONFIG-env
hostname = an-hostname
regex = usb|acm|com
v = true
appName = CreateAgent/Stable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name = this-is-a-config-file-from-home-dir
gc = std
hostname = this-is-a-config-file-from-home-dir
hostname = an-hostname
regex = usb|acm|com
v = true
appName = config-from-home-dir
appName = an-app-n
updateUrl = https://downloads.arduino.cc/
origins = https://local.arduino.cc:8000, https://local.arduino.cc:8001, https://*.iot-cloud-arduino-cc.pages.dev
crashreport = false
3 changes: 2 additions & 1 deletion config/testdata/fromxdghome/ArduinoCreateAgent/config.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name = this-is-a-config-file-from-xdghome-dir
gc = std
hostname = this-is-a-config-file-from-xdghome-dir
hostname = an-hostname
regex = usb|acm|com
v = true
appName = CreateAgent/Stable
Expand Down

0 comments on commit f5a9afc

Please sign in to comment.