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

Introduce climate_control to modify ENV in tests

Christophe Bliardee2611e4215e
diff --git a/Gemfile b/Gemfile
index 89aaa2165fe..8eab62defaf 100644
--- a/Gemfile
+++ b/Gemfile
@@ -226,6 +226,9 @@ group :test do
gem 'retriable', '~> 3.1.1'
gem 'rspec-retry', '~> 0.6.1'
+ # Modify ENV
+ gem 'climate_control'
+
# XML comparison tests
gem 'compare-xml', '~> 0.66', require: false
diff --git a/Gemfile.lock b/Gemfile.lock
index ee612d428be..ed4fe04aa23 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -363,6 +363,7 @@ GEM
carrierwave_direct (2.1.0)
carrierwave (>= 1.0.0)
fog-aws
+ climate_control (1.2.0)
closure_tree (7.4.0)
activerecord (>= 4.2.10)
with_advisory_lock (>= 4.0.0)
@@ -1000,6 +1001,7 @@ DEPENDENCIES
capybara-screenshot (~> 1.0.17)
carrierwave (~> 1.3.1)
carrierwave_direct (~> 2.1.0)
+ climate_control
closure_tree (~> 7.4.0)
colored2
commonmarker (~> 0.23.9)
diff --git a/spec/features/admin/languages_settings_spec.rb b/spec/features/admin/languages_settings_spec.rb
index a8e17c96353..32e645dee13 100644
--- a/spec/features/admin/languages_settings_spec.rb
+++ b/spec/features/admin/languages_settings_spec.rb
@@ -55,13 +55,12 @@ RSpec.describe 'Languages settings page', js: true do
end
context 'when available languages setting is not writable (set by env var)', :settings_reset do
- before do
- Setting.default_language = 'fr'
- stub_const('ENV', { 'OPENPROJECT_AVAILABLE__LANGUAGES' => 'de en' })
+ it 'does not change the available languages setting', with_env: {
+ OPENPROJECT_AVAILABLE__LANGUAGES: 'de en',
+ OPENPROJECT_DEFAULT_LANGUAGE: 'fr'
+ } do
reset(:available_languages)
- end
-
- it 'does not change the available languages setting' do
+ reset(:default_language)
expect(Setting.available_languages_writable?).to be false
languages_page.visit!
diff --git a/spec/support/shared/with_env.rb b/spec/support/shared/with_env.rb
new file mode 100644
index 00000000000..8fe5a28122f
--- /dev/null
+++ b/spec/support/shared/with_env.rb
@@ -0,0 +1,61 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+# Aggregates example and parent metadata for a given key.
+def aggregate_metadata(example, metadata_key)
+ hash = example.metadata[metadata_key] || {}
+ example.example_group.module_parents.each do |parent|
+ if parent.respond_to?(:metadata) && parent.metadata[metadata_key]
+ hash.reverse_merge!(parent.metadata[metadata_key])
+ end
+ end
+ hash
+end
+
+module WithEnvMixin
+ module_function
+
+ def with_env(environment_overrides, &)
+ ClimateControl.modify(environment_overrides, &)
+ end
+end
+
+RSpec.configure do |config|
+ config.include WithEnvMixin
+
+ config.around do |example|
+ environment_overrides = aggregate_metadata(example, :with_env)
+ if environment_overrides.present?
+ with_env(environment_overrides) do
+ example.run
+ end
+ else
+ example.run
+ end
+ end
+end