2019-09-04 21:01:54 +05:30
# frozen_string_literal: true
2022-11-25 23:54:43 +05:30
require 'fast_spec_helper'
2022-07-23 23:45:48 +05:30
2021-04-29 21:17:54 +05:30
require_relative '../../../tooling/quality/test_level'
2019-09-04 21:01:54 +05:30
2023-03-17 16:20:25 +05:30
RSpec . describe Quality :: TestLevel , feature_category : :tooling do
2022-07-23 23:45:48 +05:30
describe 'TEST_LEVEL_FOLDERS constant' do
2023-03-17 16:20:25 +05:30
it 'ensures all directories it refers to exists' , :aggregate_failures do
2022-07-23 23:45:48 +05:30
ee_only_directories = %w[
lib / ee / gitlab / background_migration
elastic
elastic_integration
replicators
]
described_class :: TEST_LEVEL_FOLDERS . values . flatten . each do | dir |
next if ee_only_directories . include? ( dir ) && ! Gitlab . ee?
spec_directory = if ee_only_directories . include? ( dir )
File . join ( 'ee' , 'spec' , dir )
else
File . join ( 'spec' , dir )
end
expect ( File . exist? ( spec_directory ) ) . to eq ( true ) , " #{ spec_directory } does not exist! "
end
end
end
2019-09-04 21:01:54 +05:30
describe '#pattern' do
2019-12-21 20:55:43 +05:30
context 'when level is all' do
it 'returns a pattern' do
expect ( subject . pattern ( :all ) )
. to eq ( " spec/**{,/**/}*_spec.rb " )
end
end
2021-01-29 00:20:46 +05:30
context 'when level is frontend_fixture' do
it 'returns a pattern' do
expect ( subject . pattern ( :frontend_fixture ) )
. to eq ( " spec/{frontend/fixtures}{,/**/}*.rb " )
end
end
2019-09-04 21:01:54 +05:30
context 'when level is unit' do
it 'returns a pattern' do
expect ( subject . pattern ( :unit ) )
2023-06-20 00:43:36 +05:30
. to eq ( " spec/{bin,channels,components,config,contracts,db,dependencies,elastic,elastic_integration,experiments,factories,finders,frontend,graphql,haml_lint,helpers,initializers,lib,metrics_server,models,policies,presenters,rack_servers,replicators,routing,rubocop,scripts,serializers,services,sidekiq,sidekiq_cluster,spam,support_specs,tasks,uploaders,validators,views,workers,tooling}{,/**/}*_spec.rb " )
2019-09-04 21:01:54 +05:30
end
end
2019-12-26 22:10:19 +05:30
context 'when level is migration' do
it 'returns a pattern' do
expect ( subject . pattern ( :migration ) )
2023-03-17 16:20:25 +05:30
. to eq ( " spec/{migrations}{,/**/}*_spec.rb " )
2020-04-08 14:13:33 +05:30
end
end
context 'when level is background_migration' do
it 'returns a pattern' do
expect ( subject . pattern ( :background_migration ) )
. to eq ( " spec/{lib/gitlab/background_migration,lib/ee/gitlab/background_migration}{,/**/}*_spec.rb " )
2019-12-26 22:10:19 +05:30
end
end
2019-09-04 21:01:54 +05:30
context 'when level is integration' do
it 'returns a pattern' do
expect ( subject . pattern ( :integration ) )
2021-12-11 22:18:48 +05:30
. to eq ( " spec/{commands,controllers,mailers,requests}{,/**/}*_spec.rb " )
2019-09-04 21:01:54 +05:30
end
end
context 'when level is system' do
it 'returns a pattern' do
expect ( subject . pattern ( :system ) )
. to eq ( " spec/{features}{,/**/}*_spec.rb " )
end
end
context 'with a prefix' do
it 'returns a pattern' do
expect ( described_class . new ( 'ee/' ) . pattern ( :system ) )
2021-11-18 22:05:49 +05:30
. to eq ( " {ee/}spec/{features}{,/**/}*_spec.rb " )
end
end
context 'with several prefixes' do
it 'returns a pattern' do
expect ( described_class . new ( [ '' , 'ee/' , 'jh/' ] ) . pattern ( :system ) )
. to eq ( " {,ee/,jh/}spec/{features}{,/**/}*_spec.rb " )
2019-09-04 21:01:54 +05:30
end
end
describe 'performance' do
it 'memoizes the pattern for a given level' do
expect ( subject . pattern ( :system ) . object_id ) . to eq ( subject . pattern ( :system ) . object_id )
end
it 'freezes the pattern for a given level' do
expect ( subject . pattern ( :system ) ) . to be_frozen
end
end
end
describe '#regexp' do
2019-12-21 20:55:43 +05:30
context 'when level is all' do
it 'returns a regexp' do
expect ( subject . regexp ( :all ) )
. to eq ( %r{ spec/ } )
end
end
2021-01-29 00:20:46 +05:30
context 'when level is frontend_fixture' do
it 'returns a regexp' do
expect ( subject . regexp ( :frontend_fixture ) )
2022-07-23 23:45:48 +05:30
. to eq ( %r{ spec/(frontend/fixtures)/ } )
2021-01-29 00:20:46 +05:30
end
end
2019-09-04 21:01:54 +05:30
context 'when level is unit' do
it 'returns a regexp' do
expect ( subject . regexp ( :unit ) )
2023-06-20 00:43:36 +05:30
. to eq ( %r{ spec/(bin|channels|components|config|contracts|db|dependencies|elastic|elastic_integration|experiments|factories|finders|frontend|graphql|haml_lint|helpers|initializers|lib|metrics_server|models|policies|presenters|rack_servers|replicators|routing|rubocop|scripts|serializers|services|sidekiq|sidekiq_cluster|spam|support_specs|tasks|uploaders|validators|views|workers|tooling)/ } )
2019-09-04 21:01:54 +05:30
end
end
2019-12-26 22:10:19 +05:30
context 'when level is migration' do
it 'returns a regexp' do
expect ( subject . regexp ( :migration ) )
2023-03-17 16:20:25 +05:30
. to eq ( %r{ spec/(migrations)/ } )
2020-04-08 14:13:33 +05:30
end
end
context 'when level is background_migration' do
it 'returns a regexp' do
expect ( subject . regexp ( :background_migration ) )
2022-07-23 23:45:48 +05:30
. to eq ( %r{ spec/(lib/gitlab/background_migration|lib/ee/gitlab/background_migration)/ } )
2019-12-26 22:10:19 +05:30
end
end
2019-09-04 21:01:54 +05:30
context 'when level is integration' do
it 'returns a regexp' do
expect ( subject . regexp ( :integration ) )
2022-07-23 23:45:48 +05:30
. to eq ( %r{ spec/(commands|controllers|mailers|requests)/ } )
2019-09-04 21:01:54 +05:30
end
end
context 'when level is system' do
it 'returns a regexp' do
expect ( subject . regexp ( :system ) )
2022-07-23 23:45:48 +05:30
. to eq ( %r{ spec/(features)/ } )
2019-09-04 21:01:54 +05:30
end
end
context 'with a prefix' do
it 'returns a regexp' do
expect ( described_class . new ( 'ee/' ) . regexp ( :system ) )
2022-07-23 23:45:48 +05:30
. to eq ( %r{ (ee/)spec/(features)/ } )
2021-11-18 22:05:49 +05:30
end
end
context 'with several prefixes' do
it 'returns a regexp' do
expect ( described_class . new ( [ '' , 'ee/' , 'jh/' ] ) . regexp ( :system ) )
2022-07-23 23:45:48 +05:30
. to eq ( %r{ (|ee/|jh/)spec/(features)/ } )
2019-09-04 21:01:54 +05:30
end
end
2023-06-20 00:43:36 +05:30
context 'when start_with == true' do
it 'returns a regexp' do
expect ( described_class . new ( [ 'ee/' ] ) . regexp ( :system , true ) )
. to eq ( %r{ ^(ee/)spec/(features)/ } )
end
end
2019-09-04 21:01:54 +05:30
describe 'performance' do
it 'memoizes the regexp for a given level' do
expect ( subject . regexp ( :system ) . object_id ) . to eq ( subject . regexp ( :system ) . object_id )
end
it 'freezes the regexp for a given level' do
expect ( subject . regexp ( :system ) ) . to be_frozen
end
end
end
describe '#level_for' do
it 'returns the correct level for a unit test' do
expect ( subject . level_for ( 'spec/models/abuse_report_spec.rb' ) ) . to eq ( :unit )
end
2021-01-29 00:20:46 +05:30
it 'returns the correct level for a frontend fixture test' do
expect ( subject . level_for ( 'spec/frontend/fixtures/pipelines.rb' ) ) . to eq ( :frontend_fixture )
end
2020-06-23 00:09:42 +05:30
it 'returns the correct level for a tooling test' do
expect ( subject . level_for ( 'spec/tooling/lib/tooling/test_file_finder_spec.rb' ) ) . to eq ( :unit )
end
2019-12-26 22:10:19 +05:30
it 'returns the correct level for a migration test' do
expect ( subject . level_for ( 'spec/migrations/add_default_and_free_plans_spec.rb' ) ) . to eq ( :migration )
end
2020-04-08 14:13:33 +05:30
it 'returns the correct level for a background migration test' do
2023-03-17 16:20:25 +05:30
expect ( subject . level_for ( 'spec/lib/gitlab/background_migration/archive_legacy_traces_spec.rb' ) ) . to eq ( :background_migration )
2019-12-26 22:10:19 +05:30
end
2021-02-22 17:27:13 +05:30
it 'returns the correct level for an EE file without passing a prefix' do
expect ( subject . level_for ( 'ee/spec/migrations/geo/migrate_ci_job_artifacts_to_separate_registry_spec.rb' ) ) . to eq ( :migration )
end
2019-12-26 22:10:19 +05:30
it 'returns the correct level for a geo migration test' do
expect ( described_class . new ( 'ee/' ) . level_for ( 'ee/spec/migrations/geo/migrate_ci_job_artifacts_to_separate_registry_spec.rb' ) ) . to eq ( :migration )
end
2020-04-08 14:13:33 +05:30
it 'returns the correct level for a EE-namespaced background migration test' do
2023-03-17 16:20:25 +05:30
expect ( described_class . new ( 'ee/' ) . level_for ( 'ee/spec/lib/ee/gitlab/background_migration/prune_orphaned_geo_events_spec.rb' ) ) . to eq ( :background_migration )
2020-04-08 14:13:33 +05:30
end
2019-09-04 21:01:54 +05:30
it 'returns the correct level for an integration test' do
expect ( subject . level_for ( 'spec/mailers/abuse_report_mailer_spec.rb' ) ) . to eq ( :integration )
end
2021-12-11 22:18:48 +05:30
it 'returns the correct level for an integration test in a subfolder' do
expect ( subject . level_for ( 'spec/commands/sidekiq_cluster/cli.rb' ) ) . to eq ( :integration )
end
2019-09-04 21:01:54 +05:30
it 'returns the correct level for a system test' do
expect ( subject . level_for ( 'spec/features/abuse_report_spec.rb' ) ) . to eq ( :system )
end
it 'raises an error for an unknown level' do
expect { subject . level_for ( 'spec/unknown/foo_spec.rb' ) }
. to raise_error ( described_class :: UnknownTestLevelError ,
2021-04-29 21:17:54 +05:30
%r{ Test level for spec/unknown/foo_spec.rb couldn't be set. Please rename the file properly or change the test level detection regexes in .+/tooling/quality/test_level.rb. } )
2019-09-04 21:01:54 +05:30
end
2020-04-08 14:13:33 +05:30
2023-03-17 16:20:25 +05:30
it 'ensures all spec/ folders are covered by a test level' do
Dir [ '{,ee/}spec/**/*/' ] . each do | path |
next if path =~ %r{ \ A(ee/)?spec/(benchmarks|docs_screenshots|fixtures|frontend_integration|support)/ }
2020-04-08 14:13:33 +05:30
2023-03-17 16:20:25 +05:30
expect { subject . level_for ( path ) } . not_to raise_error
end
2020-04-08 14:13:33 +05:30
end
end
2019-09-04 21:01:54 +05:30
end