Commit 52c9045b authored by Bogdan Denkovych's avatar Bogdan Denkovych

Force eager loading of the application code for specs require it

In https://gitlab.com/gitlab-org/gitlab/-/merge_requests/72964
we disabled eager loading for the test environment.
As it was mentioned there are specs that depend on all the application that is being
loaded. See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/72964#note_713060754

This MR introduces `:eager_load` rspec tag and adds this tag to specs
that require it. There are still might be some specs that require this
tag. This MR do not address all the cases from https://gitlab.com/gitlab-org/gitlab/-/issues/343878.
Those two still need  further investigation:
 - https://gitlab.com/gitlab-org/gitlab/blob/8ba2a598bab0475bd6f2ab9ebcef4a1852b8d512/spec/support/before_all_adapter.rb#L5
 - https://gitlab.com/gitlab-org/gitlab/blob/452d554d81b0e310ef0e86eb8ea3a1c7bf6fbf90/spec/support/helpers/migrations_helpers.rb#L70
So I am not sure whether we can close the issue after merging this MR.

Adding this tag to the spec would make `Rails.application.eager_load!`
be called before its execution to load the application code.
See https://api.rubyonrails.org/classes/Rails/Application.html#method-i-eager_load-21
parent 7d567806
......@@ -64,6 +64,9 @@ use the `GITLAB_TEST_EAGER_LOAD` environment variable:
GITLAB_TEST_EAGER_LOAD=1 bin/rspec spec/models/project_spec.rb
```
If your test depends on all the application code that is being loaded, add the `:eager_load` tag.
This ensures that the application code is eagerly loaded before the test execution.
### Ruby warnings
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/47767) in GitLab 13.7.
......
......@@ -192,7 +192,7 @@ RSpec.describe Note, :elastic, :clean_gitlab_redis_shared_state do
note.update!(note: 'some other text here')
end
it 'uses same index for Note subclasses' do
it 'uses same index for Note subclasses', :eager_load do
Note.subclasses.each do |note_class|
expect(note_class.index_name).to eq(Note.index_name)
expect(note_class.document_type).to eq(Note.document_type)
......
......@@ -63,7 +63,7 @@ RSpec.describe Snippet, :elastic do
expect(snippet.__elasticsearch__.as_indexed_json).to eq(expected_hash)
end
it 'uses same index for Snippet subclasses' do
it 'uses same index for Snippet subclasses', :eager_load do
Snippet.subclasses.each do |snippet_class|
expect(snippet_class.index_name).to eq(Snippet.index_name)
expect(snippet_class.document_type).to eq(Snippet.document_type)
......
......@@ -210,7 +210,7 @@ RSpec.describe 'Database schema' do
# We are skipping GEO models for now as it adds up complexity
describe 'for jsonb columns' do
it 'uses json schema validator' do
it 'uses json schema validator', :eager_load do
columns_name_with_jsonb.each do |hash|
next if models_by_table_name[hash["table_name"]].nil?
......
......@@ -32,7 +32,7 @@ RSpec.describe Banzai::Pipeline::PlainMarkdownPipeline do
expect(result[:escaped_literals]).to be_truthy
end
it 'ensure we handle all the GitLab reference characters' do
it 'ensure we handle all the GitLab reference characters', :eager_load do
reference_chars = ObjectSpace.each_object(Class).map do |klass|
next unless klass.included_modules.include?(Referable)
next unless klass.respond_to?(:reference_prefix)
......
......@@ -375,7 +375,7 @@ RSpec.describe ReactiveCaching, :use_clean_rails_memory_store_caching do
end
describe 'classes including this concern' do
it 'sets reactive_cache_work_type' do
it 'sets reactive_cache_work_type', :eager_load do
classes = ObjectSpace.each_object(Class).select do |klass|
klass < described_class && klass.name
end
......
......@@ -8,7 +8,7 @@ RSpec.describe MergeRequests::Mergeability::RunChecksService do
let_it_be(:merge_request) { create(:merge_request) }
describe '#CHECKS' do
it 'contains every subclass of the base checks service' do
it 'contains every subclass of the base checks service', :eager_load do
expect(described_class::CHECKS).to contain_exactly(*MergeRequests::Mergeability::CheckBaseService.subclasses)
end
end
......@@ -19,7 +19,7 @@ RSpec.describe MergeRequests::Mergeability::RunChecksService do
let(:params) { {} }
let(:success_result) { Gitlab::MergeRequests::Mergeability::CheckResult.success }
context 'when every check is skipped' do
context 'when every check is skipped', :eager_load do
before do
MergeRequests::Mergeability::CheckBaseService.subclasses.each do |subclass|
expect_next_instance_of(subclass) do |service|
......
......@@ -431,6 +431,10 @@ RSpec.configure do |config|
Gitlab::Metrics.reset_registry!
end
config.before(:example, :eager_load) do
Rails.application.eager_load!
end
# This makes sure the `ApplicationController#can?` method is stubbed with the
# original implementation for all view specs.
config.before(:each, type: :view) do
......
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