Skip to content

Commit

Permalink
refactor(config): remove default config file and update related tests
Browse files Browse the repository at this point in the history
dido18 committed Jan 29, 2025
1 parent 716e8fc commit 29a1bf3
Showing 8 changed files with 29 additions and 93 deletions.
10 changes: 0 additions & 10 deletions config/config-default.ini

This file was deleted.

76 changes: 22 additions & 54 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"os"
"testing"

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

@@ -25,68 +26,35 @@ func TestGetConfigPathFromHOME(t *testing.T) {
}

func TestGetConfigPathFromARDUINO_CREATE_AGENT_CONFIG(t *testing.T) {
// read config from ARDUINO_CREATE_AGENT_CONFIG/config.ini"
os.Setenv("HOME", "./fromhome")
os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/fromenv/config.ini")
// $HOME must be always set, otherwise panic
os.Setenv("HOME", "./testdata/dummyhome")

os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/from-arduino-create-agent-config-env/config.ini")
defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")

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

// func TestGetConfigPathFromLegacyConfig(t *testing.T) {
// // If no config is found, copy the legacy config to the new location
// src, err := os.Executable()
// if err != nil {
// t.Fatal(err)
// }
// legacyConfigPath, err := paths.New(src).Parent().Join("config.ini").Create()
// if err != nil {
// t.Fatal(err)
// }
// // adding a timestamp to the content to make it unique
// legacyContent := "hostname = legacy-config-file-" + time.Now().String()
// n, err := legacyConfigPath.WriteString(legacyContent)
// if err != nil || n <= 0 {
// t.Fatalf("Failed to write legacy config file: %v", err)
// }

// // remove any existing config.ini in the into the location pointed by $HOME
// err = os.Remove("./testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini")
// if err != nil && !os.IsNotExist(err) {
// t.Fatal(err)
// }

// // Expectation: it copies the "legacy" config.ini into the location pointed by $HOME
// os.Setenv("HOME", "./testdata/fromlegacy")
// defer os.Unsetenv("HOME")

// configPath := GetConfigPath()
// assert.Equal(t, "testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini", configPath.String())
// If the ARDUINO_CREATE_AGENT_CONFIG is NOT set and the config.ini does not exist in HOME directory
// then it copies the default config (the config.ini) into the HOME directory
func TestIfHomeDoesNotContainConfigTheDefaultConfigAreCopied(t *testing.T) {
// $HOME must be always set, otherwise panic
os.Setenv("HOME", "./testdata/home-without-config")

// given, err := paths.New(configPath.String()).ReadFile()
// assert.Nil(t, err)
// assert.Equal(t, legacyContent, string(given))
// }
os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
// we want to test the case when the config does not exist in the home directory
os.Remove("./testdata/home-without-config/.config/ArduinoCreateAgent/config.ini")

// func TestGetConfigPathCreateDefaultConfig(t *testing.T) {
// os.Setenv("HOME", "./testdata/noconfig")
// os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")

// // ensure the config.ini does not exist in HOME directory
// os.Remove("./testdata/noconfig/.config/ArduinoCreateAgent/config.ini")
// // ensure the config.ini does not exist in target directory
// os.Remove("./testdata/fromdefault/.config/ArduinoCreateAgent/config.ini")

// configPath := GetConfigPath()
configPath := GetConfigPath()

// assert.Equal(t, "testdata/fromdefault/.config/ArduinoCreateAgent/config.ini", configPath.String())
assert.Equal(t, "testdata/home-without-config/.config/ArduinoCreateAgent/config.ini", configPath.String())

// givenContent, err := paths.New(configPath.String()).ReadFile()
// if err != nil {
// t.Fatal(err)
// }
givenContent, err := paths.New(configPath.String()).ReadFile()
if err != nil {
t.Fatal(err)
}

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

// }
}
Original file line number Diff line number Diff line change
@@ -4,5 +4,5 @@ regex = usb|acm|com
v = true
appName = CreateAgent/Stable
updateUrl = https://downloads.arduino.cc/
origins = https://local.arduino.cc:8000, https://local.arduino.cc:8001, https://create-dev.arduino.cc, https://*.sparklyunicorn.cc, https://*.iot-cloud-arduino-cc.pages.dev, https://cloud.oniudra.cc, https://app.oniudra.cc,https://*.iot-cloud-arduino-cc.pages.dev
origins = https://local.arduino.cc:8000, https://local.arduino.cc:8001
crashreport = false
1 change: 0 additions & 1 deletion config/testdata/fromdefault/.gitignore

This file was deleted.

2 changes: 0 additions & 2 deletions config/testdata/fromlegacy/.gitignore

This file was deleted.

13 changes: 6 additions & 7 deletions config/testdata/fromxdghome/ArduinoCreateAgent/config.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
gc = std # Type of garbage collection. std = Normal garbage collection allowing system to decide (this has been known to cause a stop the world in the middle of a CNC job which can cause lost responses from the CNC controller and thus stalled jobs. use max instead to solve.), off = let memory grow unbounded (you have to send in the gc command manually to garbage collect or you will run out of RAM eventually), max = Force garbage collection on each recv or send on a serial port (this minimizes stop the world events and thus lost serial responses, but increases CPU usage)
hostname = unknown-hostname # Override the hostname we get from the OS
regex = usb|acm|com # Regular expression to filter serial port list
v = true # show debug logging
gc = std
hostname = this-is-a-config-file-from-xdghome-dir
regex = usb|acm|com
v = true
appName = CreateAgent/Stable
updateUrl = https://downloads.arduino.cc/
origins = https://local.arduino.cc:8000
#httpProxy = http://your.proxy:port # Proxy server for HTTP requests
crashreport = false # enable crashreport logging
autostartMacOS = true # the Arduino Create Agent is able to start automatically after login on macOS (launchd agent)
crashreport = false
autostartMacOS = true
8 changes: 0 additions & 8 deletions config/testdata/home/.config/ArduinoCreateAgent/config.ini

This file was deleted.

10 changes: 0 additions & 10 deletions config/testdata/noconfig/.config/ArduinoCreateAgent/config.ini

This file was deleted.

0 comments on commit 29a1bf3

Please sign in to comment.