2019-05-22 08:16:49 -04:00
# frozen_string_literal: true
module Quality
class TestLevel
UnknownTestLevelError = Class . new ( StandardError )
TEST_LEVEL_FOLDERS = {
2019-12-17 13:07:48 -05:00
migration : %w[
migrations
2020-03-17 17:09:16 -04:00
] ,
background_migration : %w[
2019-12-17 13:07:48 -05:00
lib / gitlab / background_migration
2020-02-25 22:09:07 -05:00
lib / ee / gitlab / background_migration
2019-12-17 13:07:48 -05:00
] ,
2020-11-10 10:09:14 -05:00
frontend_fixture : %w[
frontend / fixtures
] ,
2019-05-22 08:16:49 -04:00
unit : %w[
bin
2020-04-30 08:09:45 -04:00
channels
2019-05-22 08:16:49 -04:00
config
db
dependencies
2020-12-04 01:09:34 -05:00
elastic
elastic_integration
2020-12-16 04:10:26 -05:00
experiments
2019-05-22 08:16:49 -04:00
factories
finders
frontend
graphql
2019-07-23 16:39:11 -04:00
haml_lint
2019-05-22 08:16:49 -04:00
helpers
initializers
lib
2021-12-02 22:14:42 -05:00
metrics_server
2019-05-22 08:16:49 -04:00
models
policies
presenters
rack_servers
2020-02-05 22:08:47 -05:00
replicators
2019-05-22 08:16:49 -04:00
routing
rubocop
2021-10-18 20:10:29 -04:00
scripts
2019-05-22 08:16:49 -04:00
serializers
services
sidekiq
2021-12-08 10:13:43 -05:00
sidekiq_cluster
2021-02-25 04:10:45 -05:00
spam
2020-02-12 13:09:21 -05:00
support_specs
2019-05-22 08:16:49 -04:00
tasks
uploaders
validators
views
workers
2020-06-03 05:08:47 -04:00
tooling
2022-06-13 08:08:29 -04:00
components
2019-05-22 08:16:49 -04:00
] ,
integration : %w[
2021-11-08 13:09:52 -05:00
commands
2019-05-22 08:16:49 -04:00
controllers
mailers
requests
] ,
system : [ 'features' ]
} . freeze
2021-10-04 14:12:46 -04:00
attr_reader :prefixes
2019-05-22 08:16:49 -04:00
2021-10-04 14:12:46 -04:00
def initialize ( prefixes = nil )
@prefixes = Array ( prefixes )
2019-05-22 08:16:49 -04:00
@patterns = { }
@regexps = { }
end
def pattern ( level )
2022-06-15 11:09:20 -04:00
@patterns [ level ] || = " #{ prefixes_for_pattern } spec/ #{ folders_pattern ( level ) } {,/**/}* #{ suffix ( level ) } " . freeze # rubocop:disable Style/RedundantFreeze
2019-05-22 08:16:49 -04:00
end
def regexp ( level )
2021-10-04 14:12:46 -04:00
@regexps [ level ] || = Regexp . new ( " #{ prefixes_for_regex } spec/ #{ folders_regex ( level ) } " ) . freeze
2019-05-22 08:16:49 -04:00
end
def level_for ( file_path )
case file_path
2019-12-02 16:06:51 -05:00
# Detect migration first since some background migration tests are under
# spec/lib/gitlab/background_migration and tests under spec/lib are unit by default
2020-03-17 17:09:16 -04:00
when regexp ( :migration ) , regexp ( :background_migration )
2019-12-02 16:06:51 -05:00
:migration
2020-11-10 10:09:14 -05:00
# Detect frontend fixture before matching other unit tests
when regexp ( :frontend_fixture )
:frontend_fixture
2019-05-22 08:16:49 -04:00
when regexp ( :unit )
:unit
when regexp ( :integration )
:integration
when regexp ( :system )
:system
else
raise UnknownTestLevelError , " Test level for #{ file_path } couldn't be set. Please rename the file properly or change the test level detection regexes in #{ __FILE__ } . "
end
end
2019-09-27 11:06:16 -04:00
2020-03-17 17:09:16 -04:00
def background_migration? ( file_path )
! ! ( file_path =~ regexp ( :background_migration ) )
end
2019-09-27 11:06:16 -04:00
private
2021-10-04 14:12:46 -04:00
def prefixes_for_pattern
return '' if prefixes . empty?
" { #{ prefixes . join ( ',' ) } } "
end
def prefixes_for_regex
return '' if prefixes . empty?
regex_prefix = prefixes . map ( & Regexp . method ( :escape ) ) . join ( '|' )
" ( #{ regex_prefix } ) "
end
2020-11-10 10:09:14 -05:00
def suffix ( level )
case level
when :frontend_fixture
" .rb "
else
" _spec.rb "
end
end
2020-07-07 11:08:49 -04:00
def migration_and_background_migration_folders
TEST_LEVEL_FOLDERS . fetch ( :migration ) + TEST_LEVEL_FOLDERS . fetch ( :background_migration )
end
2019-09-27 11:06:16 -04:00
def folders_pattern ( level )
case level
2020-07-07 11:08:49 -04:00
when :migration
" { #{ migration_and_background_migration_folders . join ( ',' ) } } "
2022-05-05 08:08:03 -04:00
when :all
2019-09-27 11:06:16 -04:00
'**'
else
" { #{ TEST_LEVEL_FOLDERS . fetch ( level ) . join ( ',' ) } } "
end
end
def folders_regex ( level )
case level
2020-07-07 11:08:49 -04:00
when :migration
2022-06-13 08:08:29 -04:00
" ( #{ migration_and_background_migration_folders . join ( '|' ) } )/ "
2022-05-05 08:08:03 -04:00
when :all
2019-09-27 11:06:16 -04:00
''
else
2022-06-13 08:08:29 -04:00
" ( #{ TEST_LEVEL_FOLDERS . fetch ( level ) . join ( '|' ) } )/ "
2019-09-27 11:06:16 -04:00
end
end
2019-05-22 08:16:49 -04:00
end
end