Skip to content

Commit

Permalink
refactor(config): revert to use the config.ini to default and remov…
Browse files Browse the repository at this point in the history
…e testing that branch
  • Loading branch information
dido18 committed Jan 29, 2025
1 parent c41b815 commit 716e8fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func GetDefaultHomeDir() *paths.Path {
return paths.New(homeDir)
}

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

// GenerateConfig function will take a directory path as an input
Expand Down
66 changes: 32 additions & 34 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package config
import (
"os"
"testing"
"time"

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

Expand Down Expand Up @@ -36,40 +34,40 @@ func TestGetConfigPathFromARDUINO_CREATE_AGENT_CONFIG(t *testing.T) {
assert.Equal(t, "./testdata/fromenv/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")
// 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)
// }

configPath := GetConfigPath()
assert.Equal(t, "testdata/fromlegacy/.config/ArduinoCreateAgent/config.ini", configPath.String())
// // 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)
// }

given, err := paths.New(configPath.String()).ReadFile()
assert.Nil(t, err)
assert.Equal(t, legacyContent, string(given))
}
// // 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())

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

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

0 comments on commit 716e8fc

Please sign in to comment.