Skip to content

Commit 4f3ac5a

Browse files
committed
test(config): add tests for retrieving config paths from XDG_CONFIG_HOME and HOME directories
1 parent 279de2a commit 4f3ac5a

File tree

3 files changed

+99
-95
lines changed

3 files changed

+99
-95
lines changed

config/config_test.go

+81-95
Original file line numberDiff line numberDiff line change
@@ -6,103 +6,89 @@ import (
66
"time"
77

88
"github.com/arduino/go-paths-helper"
9-
"github.com/sirupsen/logrus"
109
"github.com/stretchr/testify/assert"
1110
)
1211

13-
func TestGetConfigPath(t *testing.T) {
14-
t.Run("read config.ini from ARDUINO_CREATE_AGENT_CONFIG", func(t *testing.T) {
15-
os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/fromenv/config.ini")
16-
defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
17-
configPath := GetConfigPath()
18-
assert.Equal(t, "./testdata/fromenv/config.ini", configPath.String())
19-
})
20-
21-
t.Run("panic if config.ini does not exist", func(t *testing.T) {
22-
os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/nonexistent_config.ini")
23-
defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
24-
25-
defer func() {
26-
if r := recover(); r != nil {
27-
entry, ok := r.(*logrus.Entry)
28-
if !ok {
29-
t.Errorf("Expected panic of type *logrus.Entry but got %T", r)
30-
} else {
31-
assert.Equal(t, "config from env var ./testdata/nonexistent_config.ini does not exists", entry.Message)
32-
}
33-
} else {
34-
t.Errorf("Expected panic but did not get one")
35-
}
36-
}()
37-
38-
GetConfigPath()
39-
})
40-
41-
t.Run("read config.ini from $HOME/.config/ArduinoCreateAgent folder", func(t *testing.T) {
42-
os.Setenv("HOME", "./testdata/home")
43-
defer os.Unsetenv("HOME")
44-
configPath := GetConfigPath()
45-
assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
46-
})
47-
48-
t.Run("legacy config are copied to new location", func(t *testing.T) {
49-
50-
createLegacyConfig := func() string {
51-
// Create a "legacy" config.ini in the same directory as the binary executable
52-
src, err := os.Executable()
53-
if err != nil {
54-
t.Fatal(err)
55-
}
56-
legacyConfigPath, err := paths.New(src).Parent().Join("config.ini").Create()
57-
if err != nil {
58-
t.Fatal(err)
59-
}
60-
// adding a timestamp to the content to make it unique
61-
c := "hostname = legacy-config-file-" + time.Now().String()
62-
n, err := legacyConfigPath.WriteString(c)
63-
if err != nil || n <= 0 {
64-
t.Fatalf("Failed to write legacy config file: %v", err)
65-
}
66-
return c
67-
}
68-
69-
wantContent := createLegacyConfig()
70-
71-
// Expectation: it copies the "legacy" config.ini into the location pointed by $HOME
72-
os.Setenv("HOME", "./testdata/fromlegacy")
73-
defer os.Unsetenv("HOME")
74-
75-
// remove any existing config.ini in the into the location pointed by $HOME
76-
err := os.Remove("./testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini")
77-
if err != nil && !os.IsNotExist(err) {
78-
t.Fatal(err)
79-
}
80-
81-
configPath := GetConfigPath()
82-
assert.Equal(t, "testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini", configPath.String())
83-
84-
given, err := paths.New(configPath.String()).ReadFile()
85-
assert.Nil(t, err)
86-
assert.Equal(t, wantContent, string(given))
87-
})
88-
89-
t.Run("write the default config.ini file", func(t *testing.T) {
90-
os.Setenv("HOME", "./testdata/fromdefault")
91-
os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
92-
93-
// ensure the config.ini does not exist in the target directory
94-
os.Remove("./testdata/fromdefault/.config/ArduinoCreateAgent/config.ini")
95-
96-
configPath := GetConfigPath()
97-
98-
assert.Equal(t, "testdata/fromdefault/.config/ArduinoCreateAgent/config.ini", configPath.String())
99-
100-
givenContent, err := paths.New(configPath.String()).ReadFile()
101-
if err != nil {
102-
t.Fatal(err)
103-
}
104-
105-
assert.Equal(t, configContent, givenContent)
106-
})
12+
func TestGetConfigPathFromXDG_CONFIG_HOME(t *testing.T) {
13+
// read config from $XDG_CONFIG_HOME/ArduinoCreateAgent/config.ini
14+
os.Setenv("XDG_CONFIG_HOME", "./testdata/fromxdghome")
15+
defer os.Unsetenv("XDG_CONFIG_HOME")
16+
configPath := GetConfigPath()
17+
assert.Equal(t, "testdata/fromxdghome/ArduinoCreateAgent/config.ini", configPath.String())
18+
}
19+
20+
func TestGetConfigPathFromHOME(t *testing.T) {
21+
// Test case 2: read config from $HOME/.config/ArduinoCreateAgent/config.ini "
22+
os.Setenv("HOME", "./testdata/fromhome")
23+
defer os.Unsetenv("HOME")
24+
configPath := GetConfigPath()
25+
assert.Equal(t, "testdata/fromhome/.config/ArduinoCreateAgent/config.ini", configPath.String())
26+
27+
}
28+
29+
func TestGetConfigPathFromARDUINO_CREATE_AGENT_CONFIG(t *testing.T) {
30+
// read config from ARDUINO_CREATE_AGENT_CONFIG/config.ini"
31+
os.Setenv("HOME", "./fromhome")
32+
os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/fromenv/config.ini")
33+
defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
34+
35+
configPath := GetConfigPath()
36+
assert.Equal(t, "./testdata/fromenv/config.ini", configPath.String())
37+
}
10738

39+
func TestGetConfigPathFromLegacyConfig(t *testing.T) {
40+
// If no config is found, copy the legacy config to the new location
41+
src, err := os.Executable()
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
legacyConfigPath, err := paths.New(src).Parent().Join("config.ini").Create()
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
// adding a timestamp to the content to make it unique
50+
legacyContent := "hostname = legacy-config-file-" + time.Now().String()
51+
n, err := legacyConfigPath.WriteString(legacyContent)
52+
if err != nil || n <= 0 {
53+
t.Fatalf("Failed to write legacy config file: %v", err)
54+
}
55+
56+
// remove any existing config.ini in the into the location pointed by $HOME
57+
err = os.Remove("./testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini")
58+
if err != nil && !os.IsNotExist(err) {
59+
t.Fatal(err)
60+
}
61+
62+
// Expectation: it copies the "legacy" config.ini into the location pointed by $HOME
63+
os.Setenv("HOME", "./testdata/fromlegacy")
64+
defer os.Unsetenv("HOME")
65+
66+
configPath := GetConfigPath()
67+
assert.Equal(t, "testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini", configPath.String())
68+
69+
given, err := paths.New(configPath.String()).ReadFile()
70+
assert.Nil(t, err)
71+
assert.Equal(t, legacyContent, string(given))
10872
}
73+
74+
// func TestGetConfigPathCreateDefaultConfig(t *testing.T) {
75+
// os.Setenv("HOME", "./testdata/noconfig")
76+
// os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG")
77+
78+
// // ensure the config.ini does not exist in HOME directory
79+
// os.Remove("./testdata/noconfig/.config/ArduinoCreateAgent/config.ini")
80+
// // ensure the config.ini does not exist in target directory
81+
// os.Remove("./testdata/fromdefault/.config/ArduinoCreateAgent/config.ini")
82+
83+
// configPath := GetConfigPath()
84+
85+
// assert.Equal(t, "testdata/fromdefault/.config/ArduinoCreateAgent/config.ini", configPath.String())
86+
87+
// givenContent, err := paths.New(configPath.String()).ReadFile()
88+
// if err != nil {
89+
// t.Fatal(err)
90+
// }
91+
92+
// assert.Equal(t, string(configContent), string(givenContent))
93+
94+
// }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
gc = std
2+
hostname = this-is-a-config-file-from-home-dir
3+
regex = usb|acm|com
4+
v = true
5+
appName = config-from-home-dir
6+
updateUrl = https://downloads.arduino.cc/
7+
origins = https://local.arduino.cc:8000, https://local.arduino.cc:8001, https://*.iot-cloud-arduino-cc.pages.dev
8+
crashreport = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
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)
2+
hostname = unknown-hostname # Override the hostname we get from the OS
3+
regex = usb|acm|com # Regular expression to filter serial port list
4+
v = true # show debug logging
5+
appName = CreateAgent/Stable
6+
updateUrl = https://downloads.arduino.cc/
7+
origins = https://local.arduino.cc:8000
8+
#httpProxy = http://your.proxy:port # Proxy server for HTTP requests
9+
crashreport = false # enable crashreport logging
10+
autostartMacOS = true # the Arduino Create Agent is able to start automatically after login on macOS (launchd agent)

0 commit comments

Comments
 (0)