|
16 | 16 | package config_test
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "path/filepath" |
19 | 20 | "testing"
|
20 | 21 |
|
21 | 22 | "github.com/arduino/arduino-cli/internal/integrationtest"
|
| 23 | + "github.com/arduino/go-paths-helper" |
22 | 24 | "github.com/stretchr/testify/require"
|
23 | 25 | "go.bug.st/testifyjson/requirejson"
|
24 | 26 | "gopkg.in/yaml.v3"
|
@@ -815,3 +817,40 @@ func TestDelete(t *testing.T) {
|
815 | 817 | require.NotContains(t, configLines, "additional_urls")
|
816 | 818 | require.NotContains(t, configLines, "board_manager")
|
817 | 819 | }
|
| 820 | + |
| 821 | +func TestInitializationOrderOfConfigThroughFlagAndEnv(t *testing.T) { |
| 822 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 823 | + defer env.CleanUp() |
| 824 | + |
| 825 | + createConfig := func(path *paths.Path, content string) { |
| 826 | + f, err := path.Create() |
| 827 | + require.NoError(t, err) |
| 828 | + _, err = f.WriteString(content) |
| 829 | + require.NoError(t, err) |
| 830 | + } |
| 831 | + tmp := t.TempDir() |
| 832 | + cliConfig, envConfig := paths.New(filepath.Join(tmp, "cli.yaml")), paths.New(filepath.Join(tmp, "env.yaml")) |
| 833 | + createConfig(cliConfig, `cli-test: "test"`) |
| 834 | + createConfig(envConfig, `env-test: "test"`) |
| 835 | + |
| 836 | + // No flag nor env specified. |
| 837 | + stdout, _, err := cli.Run("config", "dump", "--format", "json") |
| 838 | + require.NoError(t, err) |
| 839 | + requirejson.NotEmpty(t, stdout) |
| 840 | + |
| 841 | + // Flag specified |
| 842 | + stdout, _, err = cli.Run("config", "dump", "--config-file", cliConfig.String(), "--format", "json") |
| 843 | + require.NoError(t, err) |
| 844 | + requirejson.Contains(t, stdout, `{"config":{ "cli-test": "test" }}`) |
| 845 | + |
| 846 | + // Env specified |
| 847 | + customEnv := map[string]string{"ARDUINO_CONFIG_FILE": envConfig.String()} |
| 848 | + stdout, _, err = cli.RunWithCustomEnv(customEnv, "config", "dump", "--format", "json") |
| 849 | + require.NoError(t, err) |
| 850 | + requirejson.Contains(t, stdout, `{"config":{ "env-test": "test" }}`) |
| 851 | + |
| 852 | + // Flag and env specified, flag takes precedence |
| 853 | + stdout, _, err = cli.RunWithCustomEnv(customEnv, "config", "dump", "--config-file", cliConfig.String(), "--format", "json") |
| 854 | + require.NoError(t, err) |
| 855 | + requirejson.Contains(t, stdout, `{"config":{ "cli-test": "test" }}`) |
| 856 | +} |
0 commit comments