Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: data race on Systray.SetCurrentConfigFile #1012

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
90cce2c
fix
dido18 Jan 20, 2025
c96490b
feat(config): add default configuration file and update config handling
dido18 Jan 21, 2025
7b4859a
fix(.gitignore): add entry to ignore config.ini file
dido18 Jan 21, 2025
279de2a
test(config): add test for writing default config.ini file and update…
dido18 Jan 21, 2025
4f3ac5a
test(config): add tests for retrieving config paths from XDG_CONFIG_H…
dido18 Jan 21, 2025
c41b815
feat(config): pass config path to main loop and update handling
dido18 Jan 22, 2025
716e8fc
refactor(config): revert to use the `config.ini` to default and remo…
dido18 Jan 29, 2025
29a1bf3
refactor(config): remove default config file and update related tests
dido18 Jan 29, 2025
9143b02
feat(config): skip tests if not linux
dido18 Jan 29, 2025
e4e926e
feat(tests): add .gitignore for test data directories
dido18 Jan 29, 2025
3dfa496
fix(tests): handle error when removing config file in test
dido18 Jan 29, 2025
aaa97f9
refactor(main): remove debug print statement for config path
dido18 Jan 29, 2025
f5a9afc
feat(tests): enhance config tests with ini name checks and update tes…
dido18 Jan 29, 2025
9889825
feat(config): update error handling in GetConfigPath and add test for…
dido18 Jan 29, 2025
a8bcfe7
fix(tests): correct ini name in TestGetConfigPathFromHOME test case
dido18 Jan 29, 2025
a0bd3fd
feat(tests): rename tests of config
dido18 Jan 29, 2025
504ce7e
refactor(tests): rename test functions for consistency and clarity
dido18 Jan 29, 2025
870fac5
fix(tests): simplify cleanup in TestIfHomeDoesNotContainConfigTheDefa…
dido18 Jan 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(config): add default configuration file and update config handling
dido18 committed Jan 21, 2025
commit c96490b00cc260f6ee243d679d079a44864d8ad5
10 changes: 10 additions & 0 deletions config/config-default.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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
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)
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -108,7 +108,7 @@
return paths.New(homeDir)
}

//go:embed config.ini
//go:embed config-default.ini
var configContent []byte

// GenerateConfig function will take a directory path as an input
@@ -143,7 +143,7 @@
return nil
}

func GetConfigPath() *paths.Path {

Check failure on line 146 in config/config.go

GitHub Actions / check-style (./)

exported function GetConfigPath should have comment or be unexported

Check failure on line 146 in config/config.go

GitHub Actions / check-style (./)

exported function GetConfigPath should have comment or be unexported
// Let's handle the config
configDir := GetDefaultConfigDir()
var configPath *paths.Path
48 changes: 38 additions & 10 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package config

import (
"fmt"
"os"
"testing"
"time"

"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
@@ -38,24 +38,52 @@ func TestGetConfigPath(t *testing.T) {
GetConfigPath()
})

t.Run("read config.ini from $HOME", func(t *testing.T) {
t.Run("read config.ini from $HOME/.config/ArduinoCreateAgent folder", func(t *testing.T) {
os.Setenv("HOME", "./testdata/home")
defer os.Unsetenv("HOME")
configPath := GetConfigPath()
assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
})

t.Run("fallback old : read config.ini where the binary is launched", func(t *testing.T) {
src, _ := os.Executable()
paths.New(src).Parent().Join("config.ini").Create() // create a config.ini in the same directory as the binary
// The fallback path is the directory where the binary is launched
fmt.Println(src)
os.Setenv("HOME", "./testdata/noconfig") // force to not have a config in the home directory
t.Run("legacy config are copied to new location", func(t *testing.T) {

createLegacyConfig := func() string {
// Create a "legacy" config.ini in the same directory as the binary executable
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
c := "hostname = legacy-config-file-" + time.Now().String()
n, err := legacyConfigPath.WriteString(c)
if err != nil || n <= 0 {
t.Fatalf("Failed to write legacy config file: %v", err)
}
return c
}

wantContent := createLegacyConfig()

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

// expect it creates a config.ini in the same directory as the binary
// 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)
}

configPath := GetConfigPath()
assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
assert.Equal(t, "testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini", configPath.String())

given, err := paths.New(configPath.String()).ReadFile()
assert.Nil(t, err)
assert.Equal(t, wantContent, string(given))
})

}
1 change: 1 addition & 0 deletions config/testdata/fromlegacy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.ini