diff --git a/CHANGELOG.md b/CHANGELOG.md index 7336a1c7..d8a0a7e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changes * Get rid of activesupport dependency ([#230](https://github.com/railsconfig/config/pull/230)) +* Ignore .local files in test environment ([#135](https://github.com/railsconfig/config/issues/135), [#233](https://github.com/railsconfig/config/pull/233)) ## 2.0.0 diff --git a/README.md b/README.md index 0a792ed5..5c8831bb 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,10 @@ Rails.root.join("config", "settings", "#{Rails.env}.local.yml").to_s, Rails.root.join("config", "environments", "#{Rails.env}.local.yml").to_s ``` +**NOTE:** The file `settings.local.yml` will not be loaded in tests to prevent local +configuration from causing flaky or non-deterministic tests. Environment-specific files +(e.g. `settings/test.local.yml`) will still be loaded to allow test-specific credentials. + ### Adding sources at runtime You can add new YAML config files at runtime. Just use: diff --git a/lib/config.rb b/lib/config.rb index 59b0590c..8fc0148e 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -58,11 +58,16 @@ def self.setting_files(config_root, env) File.join(config_root, 'settings.yml').to_s, File.join(config_root, 'settings', "#{env}.yml").to_s, File.join(config_root, 'environments', "#{env}.yml").to_s, + *local_setting_files(config_root, env) + ].freeze + end - File.join(config_root, 'settings.local.yml').to_s, + def self.local_setting_files(config_root, env) + [ + (File.join(config_root, 'settings.local.yml').to_s if env != 'test'), File.join(config_root, 'settings', "#{env}.local.yml").to_s, File.join(config_root, 'environments', "#{env}.local.yml").to_s - ].freeze + ].compact end def self.reload! diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 1908fd55..bcabae11 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -3,12 +3,23 @@ describe Config do it "should get setting files" do + config = Config.setting_files("root/config", "staging") + expect(config).to eq([ + 'root/config/settings.yml', + 'root/config/settings/staging.yml', + 'root/config/environments/staging.yml', + 'root/config/settings.local.yml', + 'root/config/settings/staging.local.yml', + 'root/config/environments/staging.local.yml' + ]) + end + + it "should ignore local config in test environment" do config = Config.setting_files("root/config", "test") expect(config).to eq([ 'root/config/settings.yml', 'root/config/settings/test.yml', 'root/config/environments/test.yml', - 'root/config/settings.local.yml', 'root/config/settings/test.local.yml', 'root/config/environments/test.local.yml' ])