2019-09-04 21:01:54 +05:30
# frozen_string_literal: true
module Quality
class TestLevel
UnknownTestLevelError = Class . new ( StandardError )
TEST_LEVEL_FOLDERS = {
2020-01-01 13:55:28 +05:30
migration : %w[
migrations
2020-04-08 14:13:33 +05:30
] ,
background_migration : %w[
2020-01-01 13:55:28 +05:30
lib / gitlab / background_migration
2020-04-08 14:13:33 +05:30
lib / ee / gitlab / background_migration
2020-01-01 13:55:28 +05:30
] ,
2019-09-04 21:01:54 +05:30
unit : %w[
bin
config
db
dependencies
factories
finders
frontend
graphql
2019-10-12 21:52:04 +05:30
haml_lint
2019-09-04 21:01:54 +05:30
helpers
initializers
javascripts
lib
models
policies
presenters
rack_servers
2020-03-09 13:42:32 +05:30
replicators
2019-09-04 21:01:54 +05:30
routing
rubocop
serializers
services
sidekiq
2020-03-09 13:42:32 +05:30
support_specs
2019-09-04 21:01:54 +05:30
tasks
uploaders
validators
views
workers
elastic_integration
] ,
integration : %w[
controllers
mailers
requests
] ,
system : [ 'features' ]
} . freeze
attr_reader :prefix
def initialize ( prefix = nil )
@prefix = prefix
@patterns = { }
@regexps = { }
end
def pattern ( level )
2019-12-21 20:55:43 +05:30
@patterns [ level ] || = " #{ prefix } spec/ #{ folders_pattern ( level ) } {,/**/}*_spec.rb "
2019-09-04 21:01:54 +05:30
end
def regexp ( level )
2019-12-21 20:55:43 +05:30
@regexps [ level ] || = Regexp . new ( " #{ prefix } spec/ #{ folders_regex ( level ) } " ) . freeze
2019-09-04 21:01:54 +05:30
end
def level_for ( file_path )
case file_path
2019-12-26 22:10:19 +05:30
# 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-04-08 14:13:33 +05:30
when regexp ( :migration ) , regexp ( :background_migration )
2019-12-26 22:10:19 +05:30
:migration
2019-09-04 21:01:54 +05:30
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-12-21 20:55:43 +05:30
2020-04-08 14:13:33 +05:30
def background_migration? ( file_path )
! ! ( file_path =~ regexp ( :background_migration ) )
end
2019-12-21 20:55:43 +05:30
private
def folders_pattern ( level )
case level
# 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
# 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-09-04 21:01:54 +05:30
end
end