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

Unify settings reset spec helpers

Christophe Bliard0caadaf1dbc5
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 9db59465e77..6b5bcd7f436 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -190,7 +190,10 @@ module Settings
private
- # Currently only required for testing
+ # Currently only required for testing.
+ #
+ # Tag your test with :settings_reset to start test with fresh settings
+ # definitions and restore them after test.
def reset
@all = nil
@loaded = false
diff --git a/spec/constants/settings/definition_spec.rb b/spec/constants/settings/definition_spec.rb
index 08b84826a83..28a7a7b0574 100644
--- a/spec/constants/settings/definition_spec.rb
+++ b/spec/constants/settings/definition_spec.rb
@@ -57,9 +57,7 @@ describe Settings::Definition do
.to eq 20
end
- context 'when overriding from ENV' do
- include_context 'with clean setting definitions'
-
+ context 'when overriding from ENV', :settings_reset do
def value_for(name)
all.detect { |d| d.name == name }.value
end
@@ -373,9 +371,7 @@ describe Settings::Definition do
end
end
- context 'when overriding from file' do
- include_context 'with clean setting definitions'
-
+ context 'when overriding from file', :settings_reset do
let(:file_contents) do
<<~YAML
---
@@ -503,9 +499,7 @@ describe Settings::Definition do
end
end
- context 'when adding an additional setting' do
- include_context 'with clean setting definitions'
-
+ context 'when adding an additional setting', :settings_reset do
it 'includes the setting' do
all
@@ -549,8 +543,7 @@ describe Settings::Definition do
end
end
- context 'when adding a setting late' do
- include_context 'with clean setting definitions'
+ context 'when adding a setting late', :settings_reset do
let(:key) { 'bogus' }
before do
@@ -976,9 +969,7 @@ describe Settings::Definition do
end
end
- describe '#on_change' do
- include_context 'with clean setting definitions'
-
+ describe '#on_change', :settings_reset do
context 'for a definition with a callback' do
let(:callback) { -> { 'foobar ' } }
diff --git a/spec/lib/open_project/feature_decisions_spec.rb b/spec/lib/open_project/feature_decisions_spec.rb
index acb835df827..bd193042299 100644
--- a/spec/lib/open_project/feature_decisions_spec.rb
+++ b/spec/lib/open_project/feature_decisions_spec.rb
@@ -27,7 +27,7 @@
# ++
require 'spec_helper'
-describe OpenProject::FeatureDecisions do
+describe OpenProject::FeatureDecisions, :settings_reset do
let(:flag_name) { :example_flag }
include_context 'with clean feature decisions'
@@ -46,8 +46,6 @@ describe OpenProject::FeatureDecisions do
end
end
- include_context 'with clean setting definitions'
-
describe '`flag_name`_active?' do
context 'without an ENV variable' do
include_context 'when adding without env variable'
diff --git a/spec/requests/api/v3/configuration_resource_spec.rb b/spec/requests/api/v3/configuration_resource_spec.rb
index 0291c08fcb5..faacac657d1 100644
--- a/spec/requests/api/v3/configuration_resource_spec.rb
+++ b/spec/requests/api/v3/configuration_resource_spec.rb
@@ -70,9 +70,7 @@ describe 'API v3 Configuration resource', type: :request do
.not_to have_json_path('_embedded/user_preferences')
end
- context 'with feature flags' do
- include_context 'with clean setting definitions'
-
+ context 'with feature flags', :settings_reset do
before do
stub_const('ENV',
'OPENPROJECT_FEATURE_AN_EXAMPLE_ACTIVE' => 'true',
diff --git a/spec/support/settings/clean_definitions.rb b/spec/support/settings/clean_definitions.rb
deleted file mode 100644
index 9aff3a58868..00000000000
--- a/spec/support/settings/clean_definitions.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-# --copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2022 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-# ++
-
-RSpec.shared_context 'with clean setting definitions' do
- let!(:definitions_before) { Settings::Definition.all.dup }
-
- before do
- Settings::Definition.send(:reset)
- end
-
- after do
- Settings::Definition.send(:reset)
- Settings::Definition.instance_variable_set(:@all, definitions_before)
- end
-end
diff --git a/spec/support/shared/with_settings.rb b/spec/support/shared/with_settings.rb
index b1c14e7633a..726007f0f5e 100644
--- a/spec/support/shared/with_settings.rb
+++ b/spec/support/shared/with_settings.rb
@@ -39,11 +39,13 @@ def aggregate_mocked_settings(example, settings)
end
RSpec.shared_context 'with settings reset' do
- around do |example|
- definitions_before = Settings::Definition.all.dup
+ let!(:definitions_before) { Settings::Definition.all.dup }
+
+ before do
Settings::Definition.send(:reset)
- example.run
- ensure
+ end
+
+ after do
Settings::Definition.send(:reset)
Settings::Definition.instance_variable_set(:@all, definitions_before)
end