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
javascripts
lib
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-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
2019-05-22 08:16:49 -04:00
] ,
integration : %w[
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 )
2021-10-04 14:12:46 -04:00
@patterns [ level ] || = " #{ prefixes_for_pattern } spec/ #{ folders_pattern ( level ) } {,/**/}* #{ suffix ( level ) } "
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 ( ',' ) } } "
2019-09-27 11:06:16 -04:00
# Geo specs aren't in a specific folder, but they all have the :geo tag, so we must search for them globally
when :all , :geo
'**'
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
" ( #{ migration_and_background_migration_folders . join ( '|' ) } ) "
2019-09-27 11:06:16 -04:00
# Geo specs aren't in a specific folder, but they all have the :geo tag, so we must search for them globally
when :all , :geo
''
else
" ( #{ TEST_LEVEL_FOLDERS . fetch ( level ) . join ( '|' ) } ) "
end
end
2019-05-22 08:16:49 -04:00
end
end