Skip to content

Fix default config file not found #1021

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 12 additions & 8 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ func Init(configPath string) {
viper.SetConfigName("arduino-cli")
}

// Get default data path if none was provided
if configPath == "" {
configPath = getDefaultArduinoDataDir()
}

// Add paths where to search for a config file
viper.AddConfigPath(filepath.Dir(configPath))

// Bind env vars
viper.SetEnvPrefix("ARDUINO")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
Expand All @@ -61,6 +53,18 @@ func Init(configPath string) {
viper.BindEnv("directories.Downloads", "ARDUINO_DOWNLOADS_DIR")
viper.BindEnv("directories.Data", "ARDUINO_DATA_DIR")

if configPath == "" {
// Get default data path if none was provided
if configPath = viper.GetString("directories.Data"); configPath != "" {
viper.AddConfigPath(configPath)
} else {
configPath = getDefaultArduinoDataDir()
viper.AddConfigPath(configPath)
}
} else {
viper.AddConfigPath(filepath.Dir(configPath))
}

// Early access directories.Data and directories.User in case
// those were set through env vars or cli flags
dataDir := viper.GetString("directories.Data")
Expand Down
15 changes: 13 additions & 2 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,30 @@ def test_init_config_file_flag_with_overwrite_flag(run_command, working_dir):
assert str(config_file) in result.stdout


def test_dump(run_command, working_dir):
def test_dump(run_command, data_dir, working_dir):
# Create a config file first
config_file = Path(working_dir) / "config" / "test" / "config.yaml"
assert not config_file.exists()
result = run_command(f'config init --dest-file "{config_file}"')
assert result.ok
assert config_file.exists()

result = run_command("config dump --format json")
result = run_command(f'config dump --config-file "{config_file}" --format json')
assert result.ok
settings_json = json.loads(result.stdout)
assert [] == settings_json["board_manager"]["additional_urls"]

result = run_command('config init --additional-urls "https://example.com"')
assert result.ok
config_file = Path(data_dir) / "arduino-cli.yaml"
assert str(config_file) in result.stdout
assert config_file.exists()

result = run_command("config dump --format json")
assert result.ok
settings_json = json.loads(result.stdout)
assert ["https://example.com"] == settings_json["board_manager"]["additional_urls"]


def test_dump_with_config_file_flag(run_command, working_dir):
# Create a config file first
Expand Down