Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow partial overriding of settings from file in test env

Christophe Bliard51ec0870cbf2
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 0935f598607..7775830faec 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -141,19 +141,19 @@ module Settings
@by_name = nil
definition = new(name,
- format: format,
- default: default,
- writable: writable,
- allowed: allowed,
- env_alias: env_alias)
+ format:,
+ default:,
+ writable:,
+ allowed:,
+ env_alias:)
override_value(definition)
all << definition
end
- def define(&block)
- instance_exec(&block)
+ def define(&)
+ instance_exec(&)
end
def [](name)
@@ -215,18 +215,17 @@ module Settings
# Replace values for which an entry in the config file or as an environment variable exists.
def override_value(definition)
- # The test setup should govern the configuration
- override_value_from_file(definition) unless Rails.env.test?
+ override_value_from_file(definition)
override_value_from_env(definition)
end
def override_value_from_file(definition)
- name = definition.name
+ envs = ['default', Rails.env]
+ envs.delete('default') if Rails.env.test? # The test setup should govern the configuration
+ envs.each do |env|
+ next unless file_config.dig(env, definition.name)
- ['default', Rails.env].each do |env|
- next unless file_config.dig(env, name)
-
- definition.override_value(file_config.dig(env, name))
+ definition.override_value(file_config.dig(env, definition.name))
end
end
diff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rb
index 992420068e4..69751691b2c 100644
--- a/spec/constants/settings/definition_spec.rb
+++ b/spec/constants/settings/definition_spec.rb
@@ -402,7 +402,7 @@ describe Settings::Definition do
.with(Rails.root.join('config/configuration.yml'))
.and_return(file_contents)
- # Loading of the config file is disabled in test env normally.
+ # Loading of the config file is partially disabled in test env
allow(Rails.env)
.to receive(:test?)
.and_return(false)
@@ -448,6 +448,24 @@ describe Settings::Definition do
.to eql DateTime.parse("2222-01-01")
end
+ context 'when Rails environment is test' do
+ before do
+ allow(Rails.env)
+ .to receive(:test?)
+ .and_return(true)
+ end
+
+ it 'does not override from file default' do
+ expect(all.detect { |d| d.name == 'edition' }.value)
+ .not_to eql 'bim'
+ end
+
+ it 'overrides from file current env' do
+ expect(all.detect { |d| d.name == 'smtp_address' }.value)
+ .to eql 'test address'
+ end
+ end
+
context 'when having invalid values in the file' do
let(:file_contents) do
<<~YAML
diff --git a/spec/tasks/setting_spec.rb b/spec/tasks/setting_spec.rb
index 527d330e7b0..3b1b3688653 100644
--- a/spec/tasks/setting_spec.rb
+++ b/spec/tasks/setting_spec.rb
@@ -43,6 +43,8 @@ RSpec.describe Rake::Task, 'setting', :settings_reset do
context 'if setting is overridden from config/configuration.yml file' do
before do
+ # disable test env detection because loading of the config file is partially disabled in test env
+ allow(Rails.env).to receive(:test?).and_return(false)
allow(Settings::Definition).to receive(:file_config)
.and_return('default' => { 'email_delivery_method' => 'initial_file_value' })
Settings::Definition.send(:override_value_from_file, Settings::Definition['email_delivery_method'])
@@ -77,6 +79,8 @@ RSpec.describe Rake::Task, 'setting', :settings_reset do
context 'if setting is overridden from config/configuration.yml file' do
before do
+ # disable test env detection because loading of the config file is partially disabled in test env
+ allow(Rails.env).to receive(:test?).and_return(false)
allow(Settings::Definition).to receive(:file_config)
.and_return('default' => { 'email_delivery_method' => 'initial_file_value' })
Settings::Definition.send(:override_value_from_file, Settings::Definition['email_delivery_method'])