debian-mirror-gitlab/tooling/quality/test_level.rb

160 lines
3.6 KiB
Ruby
Raw Normal View History

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
],
2021-01-29 00:20:46 +05:30
frontend_fixture: %w[
frontend/fixtures
],
2019-09-04 21:01:54 +05:30
unit: %w[
bin
2020-05-24 23:13:21 +05:30
channels
2019-09-04 21:01:54 +05:30
config
db
dependencies
2021-02-22 17:27:13 +05:30
elastic
elastic_integration
experiments
2022-04-04 11:22:00 +05:30
events
2019-09-04 21:01:54 +05:30
factories
finders
frontend
graphql
2019-10-12 21:52:04 +05:30
haml_lint
2019-09-04 21:01:54 +05:30
helpers
initializers
lib
2022-01-26 12:08:38 +05:30
metrics_server
2019-09-04 21:01:54 +05:30
models
policies
presenters
rack_servers
2020-03-13 15:44:24 +05:30
replicators
2019-09-04 21:01:54 +05:30
routing
rubocop
2021-12-11 22:18:48 +05:30
scripts
2019-09-04 21:01:54 +05:30
serializers
services
sidekiq
2022-01-26 12:08:38 +05:30
sidekiq_cluster
2021-04-17 20:07:23 +05:30
spam
2020-03-13 15:44:24 +05:30
support_specs
2019-09-04 21:01:54 +05:30
tasks
uploaders
validators
views
workers
2020-06-23 00:09:42 +05:30
tooling
2022-07-23 23:45:48 +05:30
components
2019-09-04 21:01:54 +05:30
],
integration: %w[
2021-12-11 22:18:48 +05:30
commands
2019-09-04 21:01:54 +05:30
controllers
mailers
requests
],
system: ['features']
}.freeze
2021-11-18 22:05:49 +05:30
attr_reader :prefixes
2019-09-04 21:01:54 +05:30
2021-11-18 22:05:49 +05:30
def initialize(prefixes = nil)
@prefixes = Array(prefixes)
2019-09-04 21:01:54 +05:30
@patterns = {}
@regexps = {}
end
def pattern(level)
2022-07-23 23:45:48 +05:30
@patterns[level] ||= "#{prefixes_for_pattern}spec/#{folders_pattern(level)}{,/**/}*#{suffix(level)}".freeze # rubocop:disable Style/RedundantFreeze
2019-09-04 21:01:54 +05:30
end
def regexp(level)
2021-11-18 22:05:49 +05:30
@regexps[level] ||= Regexp.new("#{prefixes_for_regex}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
2021-01-29 00:20:46 +05:30
# Detect frontend fixture before matching other unit tests
when regexp(:frontend_fixture)
:frontend_fixture
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
2021-11-18 22:05:49 +05:30
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
2021-01-29 00:20:46 +05:30
def suffix(level)
case level
when :frontend_fixture
".rb"
else
"_spec.rb"
end
end
2020-07-28 23:09:34 +05:30
def migration_and_background_migration_folders
TEST_LEVEL_FOLDERS.fetch(:migration) + TEST_LEVEL_FOLDERS.fetch(:background_migration)
end
2019-12-21 20:55:43 +05:30
def folders_pattern(level)
case level
2020-07-28 23:09:34 +05:30
when :migration
"{#{migration_and_background_migration_folders.join(',')}}"
2022-07-16 23:28:13 +05:30
when :all
2019-12-21 20:55:43 +05:30
'**'
else
"{#{TEST_LEVEL_FOLDERS.fetch(level).join(',')}}"
end
end
def folders_regex(level)
case level
2020-07-28 23:09:34 +05:30
when :migration
2022-07-23 23:45:48 +05:30
"(#{migration_and_background_migration_folders.join('|')})/"
2022-07-16 23:28:13 +05:30
when :all
2019-12-21 20:55:43 +05:30
''
else
2022-07-23 23:45:48 +05:30
"(#{TEST_LEVEL_FOLDERS.fetch(level).join('|')})/"
2019-12-21 20:55:43 +05:30
end
end
2019-09-04 21:01:54 +05:30
end
end