diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-20 14:43:17 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-20 14:43:17 +0300 |
commit | dfc94207fec2d84314b1a5410cface22e8b369bd (patch) | |
tree | c54022f61ced104305889a64de080998a0dc773b /rubocop | |
parent | b874efeff674f6bf0355d5d242ecf81c6f7155df (diff) |
Add latest changes from gitlab-org/gitlab@15-11-stable-eev15.11.0-rc42
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/rspec/misspelled_aggregate_failures.rb | 35 | ||||
-rw-r--r-- | rubocop/cop/rspec/shared_groups_metadata.rb | 52 | ||||
-rw-r--r-- | rubocop/cop/search/namespaced_class.rb | 74 | ||||
-rw-r--r-- | rubocop/cop/sidekiq_load_balancing/worker_data_consistency.rb | 29 | ||||
-rw-r--r-- | rubocop/migration_helpers.rb | 2 |
5 files changed, 173 insertions, 19 deletions
diff --git a/rubocop/cop/rspec/misspelled_aggregate_failures.rb b/rubocop/cop/rspec/misspelled_aggregate_failures.rb new file mode 100644 index 00000000000..04eec515318 --- /dev/null +++ b/rubocop/cop/rspec/misspelled_aggregate_failures.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'rubocop/cop/rspec/base' + +module RuboCop + module Cop + module RSpec + class MisspelledAggregateFailures < RuboCop::Cop::RSpec::Base + extend RuboCop::Cop::AutoCorrector + + CORRECT_SPELLING = :aggregate_failures + MSG = "Use `#{CORRECT_SPELLING.inspect}` to aggregate failures.".freeze + + # @!method aggregate_tag(node) + def_node_matcher :aggregate_tag, <<~PATTERN + (block + (send #rspec? {#ExampleGroups.all #Examples.all} + <$(sym /^aggregate[a-z]*_[a-z]+$/) ...> + ) + ... + ) + PATTERN + + def on_block(node) + tag_node = aggregate_tag(node) + return if tag_node.nil? || tag_node.value == CORRECT_SPELLING + + add_offense(tag_node) do |corrector| + corrector.replace(tag_node, CORRECT_SPELLING.inspect) + end + end + end + end + end +end diff --git a/rubocop/cop/rspec/shared_groups_metadata.rb b/rubocop/cop/rspec/shared_groups_metadata.rb new file mode 100644 index 00000000000..ac2406e54d2 --- /dev/null +++ b/rubocop/cop/rspec/shared_groups_metadata.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require 'rubocop/cop/rspec/base' + +module RuboCop + module Cop + module RSpec + # Ensures that shared examples and shared context don't have any metadata. + # + # @example + # + # # bad + # RSpec.shared_examples 'an external link with rel attribute', feature_category: :team_planning do + # end + # + # RSpec.shared_examples 'an external link with rel attribute', :aggregate_failures do + # end + # + # RSpec.shared_context 'an external link with rel attribute', :aggregate_failures do + # end + # + # # good + # RSpec.shared_examples 'an external link with rel attribute' do + # end + # + # shared_examples 'an external link with rel attribute' do + # end + # + # it 'adds rel="nofollow" to external links', feature_category: :team_planning do + # end + class SharedGroupsMetadata < RuboCop::Cop::RSpec::Base + MSG = 'Avoid using metadata on shared examples and shared context. They might cause flaky tests. See https://gitlab.com/gitlab-org/gitlab/-/issues/404388' + + # @!method metadata_value(node) + def_node_matcher :metadata_value, <<~PATTERN + (block + (send #rspec? {#SharedGroups.all} _description $_ ...) + ... + ) + PATTERN + + def on_block(node) + value_node = metadata_value(node) + + return unless value_node + + add_offense(value_node, message: MSG) + end + end + end + end +end diff --git a/rubocop/cop/search/namespaced_class.rb b/rubocop/cop/search/namespaced_class.rb new file mode 100644 index 00000000000..8824107ae61 --- /dev/null +++ b/rubocop/cop/search/namespaced_class.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Search + # Cop that enforces use of Search namespace for search related code. + # + # @example + # # bad + # class MySearchClass + # end + # + # # good + # module Search + # class MySearchClass + # end + # end + class NamespacedClass < RuboCop::Cop::Base + MSG = 'Search related code must be declared inside Search top level namespace. For more info: https://gitlab.com/gitlab-org/gitlab/-/issues/398207' + + # These namespaces are considered acceptable. + # Note: Nested namespace like Foo::Bar are also supported. + PERMITTED_NAMESPACES = %w[Search EE::Search API::Search EE::API::Search RuboCop::Cop::Search] + .map { |x| x.split('::') }.freeze + + SEARCH_REGEXES = [ + /elastic/i, + /zoekt/i, + /search/i + ].freeze + + def on_module(node) + add_identifiers(node) + + run_search_namespace_cop(node) if node.child_nodes.none? { |n| n.module_type? || n.class_type? } + end + + def on_class(node) + add_identifiers(node) + run_search_namespace_cop(node) + end + + private + + def run_search_namespace_cop(node) + add_offense(node.loc.name) if !namespace_allowed? && namespace_search_related? + end + + def add_identifiers(node) + identifiers.concat(identifiers_for(node)) + end + + def identifiers + @identifiers ||= [] + end + + def identifiers_for(node) + source = node.respond_to?(:identifier) ? node.identifier.source : node.source + source.sub(/^::/, '').split('::') + end + + def namespace_allowed? + PERMITTED_NAMESPACES.any? do |namespaces| + identifiers.first(namespaces.size) == namespaces + end + end + + def namespace_search_related? + SEARCH_REGEXES.any? { |x| x.match?(identifiers.join('::')) } + end + end + end + end +end diff --git a/rubocop/cop/sidekiq_load_balancing/worker_data_consistency.rb b/rubocop/cop/sidekiq_load_balancing/worker_data_consistency.rb index 7fe308d6b40..badd81ff138 100644 --- a/rubocop/cop/sidekiq_load_balancing/worker_data_consistency.rb +++ b/rubocop/cop/sidekiq_load_balancing/worker_data_consistency.rb @@ -28,36 +28,29 @@ module RuboCop HELP_LINK = 'https://docs.gitlab.com/ee/development/sidekiq/worker_attributes.html#job-data-consistency-strategies' - MSG = <<~MSG + MISSING_DATA_CONSISTENCY_MSG = <<~MSG Should define data_consistency expectation. - - It is encouraged for workers to use database replicas as much as possible by declaring - data_consistency to use the :delayed or :sticky modes. Mode :always will result in the - worker always hitting the primary database node for both reads and writes, which limits - scalability. - - Some guidelines: - - 1. If your worker mostly writes or reads its own writes, use mode :always. TRY TO AVOID THIS. - 2. If your worker performs mostly reads and can tolerate small delays, use mode :delayed. - 3. If your worker performs mostly reads but cannot tolerate any delays, use mode :sticky. - See #{HELP_LINK} for a more detailed explanation of these settings. MSG + DISCOURAGE_ALWAYS_MSG = "Refrain from using `:always` if possible." \ + "See #{HELP_LINK} for a more detailed explanation of these settings." + def_node_search :application_worker?, <<~PATTERN `(send nil? :include (const nil? :ApplicationWorker)) PATTERN - def_node_search :data_consistency_defined?, <<~PATTERN - `(send nil? :data_consistency ...) + def_node_matcher :data_consistency_value, <<~PATTERN + `(send nil? :data_consistency $(sym _) ...) PATTERN def on_class(node) - return unless in_worker?(node) && application_worker?(node) - return if data_consistency_defined?(node) + return unless application_worker?(node) + + consistency = data_consistency_value(node) + return add_offense(node, message: MISSING_DATA_CONSISTENCY_MSG) if consistency.nil? - add_offense(node) + add_offense(consistency, message: DISCOURAGE_ALWAYS_MSG) if consistency.value == :always end end end diff --git a/rubocop/migration_helpers.rb b/rubocop/migration_helpers.rb index d14b1bdd6bb..db8fc079774 100644 --- a/rubocop/migration_helpers.rb +++ b/rubocop/migration_helpers.rb @@ -21,7 +21,7 @@ module RuboCop # or through a create/alter table (TABLE_METHODS) ADD_COLUMN_METHODS = %i(add_column change_column_type_concurrently).freeze - TABLE_METHODS = %i(create_table create_table_if_not_exists change_table create_table_with_constraints).freeze + TABLE_METHODS = %i(create_table create_table_if_not_exists change_table).freeze def high_traffic_tables @high_traffic_tables ||= rubocop_migrations_config.dig('Migration/UpdateLargeTable', 'HighTrafficTables') |