|
1 | 1 | package config
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "fmt" |
5 | 4 | "os"
|
6 | 5 | "testing"
|
| 6 | + "time" |
7 | 7 |
|
8 | 8 | "github.com/arduino/go-paths-helper"
|
9 | 9 | "github.com/sirupsen/logrus"
|
@@ -38,24 +38,52 @@ func TestGetConfigPath(t *testing.T) {
|
38 | 38 | GetConfigPath()
|
39 | 39 | })
|
40 | 40 |
|
41 |
| - t.Run("read config.ini from $HOME", func(t *testing.T) { |
| 41 | + t.Run("read config.ini from $HOME/.config/ArduinoCreateAgent folder", func(t *testing.T) { |
42 | 42 | os.Setenv("HOME", "./testdata/home")
|
43 | 43 | defer os.Unsetenv("HOME")
|
44 | 44 | configPath := GetConfigPath()
|
45 | 45 | assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String())
|
46 | 46 | })
|
47 | 47 |
|
48 |
| - t.Run("fallback old : read config.ini where the binary is launched", func(t *testing.T) { |
49 |
| - src, _ := os.Executable() |
50 |
| - paths.New(src).Parent().Join("config.ini").Create() // create a config.ini in the same directory as the binary |
51 |
| - // The fallback path is the directory where the binary is launched |
52 |
| - fmt.Println(src) |
53 |
| - os.Setenv("HOME", "./testdata/noconfig") // force to not have a config in the home directory |
| 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") |
54 | 73 | defer os.Unsetenv("HOME")
|
55 | 74 |
|
56 |
| - // expect it creates a config.ini in the same directory as the binary |
| 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 | + |
57 | 81 | configPath := GetConfigPath()
|
58 |
| - assert.Equal(t, "testdata/home/.config/ArduinoCreateAgent/config.ini", configPath.String()) |
| 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)) |
59 | 87 | })
|
60 | 88 |
|
61 | 89 | }
|
0 commit comments