|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/arduino/go-paths-helper" |
| 10 | + "github.com/go-ini/ini" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +// TestGetConfigPathFromXDG_CONFIG_HOME tests the case when the config.ini is read from XDG_CONFIG_HOME/ArduinoCreateAgent/config.ini |
| 15 | +func TestGetConfigPathFromXDG_CONFIG_HOME(t *testing.T) { |
| 16 | + if runtime.GOOS != "linux" { |
| 17 | + t.Skip("Skipping test on non-linux OS") |
| 18 | + } |
| 19 | + // read config from $XDG_CONFIG_HOME/ArduinoCreateAgent/config.ini |
| 20 | + os.Setenv("XDG_CONFIG_HOME", "./testdata/fromxdghome") |
| 21 | + defer os.Unsetenv("XDG_CONFIG_HOME") |
| 22 | + configPath := GetConfigPath() |
| 23 | + |
| 24 | + assert.Equal(t, "testdata/fromxdghome/ArduinoCreateAgent/config.ini", configPath.String()) |
| 25 | + checkIniName(t, configPath, "this-is-a-config-file-from-xdghome-dir") |
| 26 | +} |
| 27 | + |
| 28 | +// TestGetConfigPathFromHOME tests the case when the config.ini is read from $HOME/.config/ArduinoCreateAgent/config.ini |
| 29 | +func TestGetConfigPathFromHOME(t *testing.T) { |
| 30 | + if runtime.GOOS != "linux" { |
| 31 | + t.Skip("Skipping test on non-linux OS") |
| 32 | + } |
| 33 | + os.Setenv("HOME", "./testdata/fromhome") |
| 34 | + defer os.Unsetenv("HOME") |
| 35 | + configPath := GetConfigPath() |
| 36 | + |
| 37 | + assert.Equal(t, "testdata/fromhome/.config/ArduinoCreateAgent/config.ini", configPath.String()) |
| 38 | + checkIniName(t, configPath, "this-is-a-config-file-from-home-dir") |
| 39 | +} |
| 40 | + |
| 41 | +// TestGetConfigPathFromARDUINO_CREATE_AGENT_CONFIG tests the case when the config.ini is read from ARDUINO_CREATE_AGENT_CONFIG env variable |
| 42 | +func TestGetConfigPathFromARDUINO_CREATE_AGENT_CONFIG(t *testing.T) { |
| 43 | + if runtime.GOOS != "linux" { |
| 44 | + t.Skip("Skipping test on non-linux OS") |
| 45 | + } |
| 46 | + // $HOME must be always set, otherwise panic |
| 47 | + os.Setenv("HOME", "./testdata/dummyhome") |
| 48 | + |
| 49 | + os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/from-arduino-create-agent-config-env/config.ini") |
| 50 | + defer os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG") |
| 51 | + |
| 52 | + configPath := GetConfigPath() |
| 53 | + assert.Equal(t, "./testdata/from-arduino-create-agent-config-env/config.ini", configPath.String()) |
| 54 | + checkIniName(t, configPath, "this-is-a-config-file-from-home-dir-from-ARDUINO_CREATE_AGENT_CONFIG-env") |
| 55 | +} |
| 56 | + |
| 57 | +// TestIfHomeDoesNotContainConfigTheDefaultConfigAreCopied tests the case when the default config.ini is copied into $HOME/.config/ArduinoCreateAgent/config.ini |
| 58 | +// from the default config.ini |
| 59 | +// If the ARDUINO_CREATE_AGENT_CONFIG is NOT set and the config.ini does not exist in HOME directory |
| 60 | +// then it copies the default config (the config.ini) into the HOME directory |
| 61 | +func TestIfHomeDoesNotContainConfigTheDefaultConfigAreCopied(t *testing.T) { |
| 62 | + if runtime.GOOS != "linux" { |
| 63 | + t.Skip("Skipping test on non-linux OS") |
| 64 | + } |
| 65 | + // $HOME must be always set, otherwise panic |
| 66 | + os.Setenv("HOME", "./testdata/home-without-config") |
| 67 | + |
| 68 | + os.Unsetenv("ARDUINO_CREATE_AGENT_CONFIG") |
| 69 | + // we want to test the case when the config does not exist in the home directory |
| 70 | + err := os.Remove("./testdata/home-without-config/.config/ArduinoCreateAgent/config.ini") |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + configPath := GetConfigPath() |
| 76 | + |
| 77 | + assert.Equal(t, "testdata/home-without-config/.config/ArduinoCreateAgent/config.ini", configPath.String()) |
| 78 | + checkIniName(t, configPath, "") // the name of the default config is missing (an empty string) |
| 79 | + |
| 80 | + givenContent, err := paths.New(configPath.String()).ReadFile() |
| 81 | + if err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + |
| 85 | + assert.Equal(t, string(configContent), string(givenContent)) |
| 86 | +} |
| 87 | + |
| 88 | +// TestGetConfigPathPanicIfPathDoesNotExist tests that it panics if the ARDUINO_CREATE_AGENT_CONFIG env variable point to an non-existing path |
| 89 | +func TestGetConfigPathPanicIfPathDoesNotExist(t *testing.T) { |
| 90 | + if runtime.GOOS != "linux" { |
| 91 | + t.Skip("Skipping test on non-linux OS") |
| 92 | + } |
| 93 | + os.Setenv("HOME", "./testdata/dummyhome") |
| 94 | + defer os.Unsetenv("HOME") |
| 95 | + |
| 96 | + os.Setenv("ARDUINO_CREATE_AGENT_CONFIG", "./testdata/a-not-existing-path/config.ini") |
| 97 | + |
| 98 | + defer func() { |
| 99 | + if r := recover(); r != nil { |
| 100 | + assert.Equal(t, fmt.Sprintf("config from env var %s does not exists", "./testdata/a-not-existing-path/config.ini"), r) |
| 101 | + } else { |
| 102 | + t.Fatal("Expected panic but did not occur") |
| 103 | + } |
| 104 | + }() |
| 105 | + |
| 106 | + configPath := GetConfigPath() |
| 107 | + |
| 108 | + assert.Equal(t, "testdata/fromxdghome/ArduinoCreateAgent/config.ini", configPath.String()) |
| 109 | + checkIniName(t, configPath, "this-is-a-config-file-from-xdghome-dir") |
| 110 | +} |
| 111 | + |
| 112 | +func checkIniName(t *testing.T, confipath *paths.Path, expected string) { |
| 113 | + cfg, err := ini.LoadSources(ini.LoadOptions{IgnoreInlineComment: true, AllowPythonMultilineValues: true}, confipath.String()) |
| 114 | + if err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | + defaultSection, err := cfg.GetSection("") |
| 118 | + if err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + assert.Equal(t, expected, defaultSection.Key("name").String()) |
| 122 | +} |
0 commit comments