From 07c79f947496485cc4b2eed6af3f295dd4feebaf Mon Sep 17 00:00:00 2001
From: Thong Kuah <tkuah@gitlab.com>
Date: Thu, 9 Jun 2022 22:51:59 +1200
Subject: [PATCH] Update quality level regex to end with a slash

This makes our regex stricter so that directories like spec/components
does not match %r{spec/component}.
---
 spec/tooling/quality/test_level_spec.rb | 16 ++++++++--------
 tooling/quality/test_level.rb           | 10 ++++++++--
 2 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/spec/tooling/quality/test_level_spec.rb b/spec/tooling/quality/test_level_spec.rb
index 800b5f8531ef..10afcb18a733 100644
--- a/spec/tooling/quality/test_level_spec.rb
+++ b/spec/tooling/quality/test_level_spec.rb
@@ -114,56 +114,56 @@
     context 'when level is frontend_fixture' do
       it 'returns a regexp' do
         expect(subject.regexp(:frontend_fixture))
-          .to eq(%r{spec/(frontend/fixtures)})
+          .to eq(%r{spec/(frontend/fixtures)/})
       end
     end
 
     context 'when level is unit' do
       it 'returns a regexp' do
         expect(subject.regexp(:unit))
-          .to eq(%r{spec/(bin|channels|config|db|dependencies|elastic|elastic_integration|experiments|events|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|components)})
+          .to eq(%r{spec/(bin|channels|config|db|dependencies|elastic|elastic_integration|experiments|events|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|components)/})
       end
     end
 
     context 'when level is migration' do
       it 'returns a regexp' do
         expect(subject.regexp(:migration))
-          .to eq(%r{spec/(migrations|lib/gitlab/background_migration|lib/ee/gitlab/background_migration)})
+          .to eq(%r{spec/(migrations|lib/gitlab/background_migration|lib/ee/gitlab/background_migration)/})
       end
     end
 
     context 'when level is background_migration' do
       it 'returns a regexp' do
         expect(subject.regexp(:background_migration))
-          .to eq(%r{spec/(lib/gitlab/background_migration|lib/ee/gitlab/background_migration)})
+          .to eq(%r{spec/(lib/gitlab/background_migration|lib/ee/gitlab/background_migration)/})
       end
     end
 
     context 'when level is integration' do
       it 'returns a regexp' do
         expect(subject.regexp(:integration))
-          .to eq(%r{spec/(commands|controllers|mailers|requests)})
+          .to eq(%r{spec/(commands|controllers|mailers|requests)/})
       end
     end
 
     context 'when level is system' do
       it 'returns a regexp' do
         expect(subject.regexp(:system))
-          .to eq(%r{spec/(features)})
+          .to eq(%r{spec/(features)/})
       end
     end
 
     context 'with a prefix' do
       it 'returns a regexp' do
         expect(described_class.new('ee/').regexp(:system))
-          .to eq(%r{(ee/)spec/(features)})
+          .to eq(%r{(ee/)spec/(features)/})
       end
     end
 
     context 'with several prefixes' do
       it 'returns a regexp' do
         expect(described_class.new(['', 'ee/', 'jh/']).regexp(:system))
-          .to eq(%r{(|ee/|jh/)spec/(features)})
+          .to eq(%r{(|ee/|jh/)spec/(features)/})
       end
     end
 
diff --git a/tooling/quality/test_level.rb b/tooling/quality/test_level.rb
index 1f8b741ee277..e6945ddb526a 100644
--- a/tooling/quality/test_level.rb
+++ b/tooling/quality/test_level.rb
@@ -81,6 +81,10 @@ def regexp(level)
       @regexps[level] ||= Regexp.new("#{prefixes_for_regex}spec/#{folders_regex(level)}").freeze
     end
 
+    def legacy_factories_regexp
+      @legacy_factories_regexp ||= %r{spec/factories_spec.rb}.freeze
+    end
+
     def level_for(file_path)
       case file_path
       # Detect migration first since some background migration tests are under
@@ -96,6 +100,8 @@ def level_for(file_path)
         :integration
       when regexp(:system)
         :system
+      when legacy_factories_regexp
+        :unit
       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
@@ -148,11 +154,11 @@ def folders_pattern(level)
     def folders_regex(level)
       case level
       when :migration
-        "(#{migration_and_background_migration_folders.join('|')})"
+        "(#{migration_and_background_migration_folders.join('|')})/"
       when :all
         ''
       else
-        "(#{TEST_LEVEL_FOLDERS.fetch(level).join('|')})"
+        "(#{TEST_LEVEL_FOLDERS.fetch(level).join('|')})/"
       end
     end
   end
-- 
GitLab