Commit 1a39f59e authored by Peter Leitzen's avatar Peter Leitzen

Clear cache for projects' licensed features in EE specs

This improves the overall experience with `stub_licensed_features`.
parent a98fec6d
......@@ -897,9 +897,11 @@ RSpec.describe Project do
it 'only loads licensed availability once' do
expect(project).to receive(:load_licensed_feature_available)
.once.and_call_original
.once.and_call_original
2.times { project.feature_available?(:push_rules) }
with_license_feature_cache do
2.times { project.feature_available?(:push_rules) }
end
end
context 'when feature symbol is not included on Namespace features code' do
......
......@@ -31,6 +31,13 @@ module EE
end
end
# Do not clear license feature cache in this block.
#
# Useful for specs which rely on caching license features.
def with_license_feature_cache(&block)
ClearLicensedFeatureAvailableCache.without_clear_cache(&block)
end
def enable_namespace_license_check!
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
::Gitlab::CurrentSettings.update!(check_namespace_plan: true)
......@@ -42,6 +49,35 @@ module EE
gl_license = create(:gitlab_license, options)
create(:license, data: gl_license.export)
end
::Project.prepend ClearLicensedFeatureAvailableCache
end
# This patch helps `stub_licensed_features` to work properly
# without the need of clearing caches manually in `before` blocks or
# using `let_it_be_refind` deliberately.
#
# See https://gitlab.com/gitlab-org/gitlab/-/issues/10385
module ClearLicensedFeatureAvailableCache
class << self
attr_accessor :clear_cache
def without_clear_cache
self.clear_cache = false
yield
ensure
self.clear_cache = true
end
end
# Enabled by default but can be disabled via `without_clear_cache`.
self.clear_cache = true
def licensed_feature_available?(*)
clear_memoization(:licensed_feature_available) if ClearLicensedFeatureAvailableCache.clear_cache
super
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment