From 311b0269b4eb9839fa63f80c8d7a58f32b8138a0 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Thu, 18 Nov 2021 13:16:36 +0000 Subject: Add latest changes from gitlab-org/gitlab@14-5-stable-ee --- rubocop/cop/gitlab/bulk_insert.rb | 6 +-- rubocop/cop/gitlab/change_timezone.rb | 20 ++++++++ rubocop/cop/gitlab/change_timzone.rb | 20 -------- rubocop/cop/gitlab/keys-first-and-values-first.rb | 57 ----------------------- rubocop/cop/gitlab/keys_first_and_values_first.rb | 57 +++++++++++++++++++++++ rubocop/cop/gitlab/mark_used_feature_flags.rb | 9 +++- rubocop/cop/qa/duplicate_testcase_link.rb | 46 ++++++++++++++++++ rubocop/rubocop.rb | 3 ++ 8 files changed, 137 insertions(+), 81 deletions(-) create mode 100644 rubocop/cop/gitlab/change_timezone.rb delete mode 100644 rubocop/cop/gitlab/change_timzone.rb delete mode 100644 rubocop/cop/gitlab/keys-first-and-values-first.rb create mode 100644 rubocop/cop/gitlab/keys_first_and_values_first.rb create mode 100644 rubocop/cop/qa/duplicate_testcase_link.rb (limited to 'rubocop') diff --git a/rubocop/cop/gitlab/bulk_insert.rb b/rubocop/cop/gitlab/bulk_insert.rb index 4c8c232043f..baaefc2533c 100644 --- a/rubocop/cop/gitlab/bulk_insert.rb +++ b/rubocop/cop/gitlab/bulk_insert.rb @@ -3,13 +3,13 @@ module RuboCop module Cop module Gitlab - # Cop that disallows the use of `Gitlab::Database.main.bulk_insert`, in favour of using + # Cop that disallows the use of `legacy_bulk_insert`, in favour of using # the `BulkInsertSafe` module. class BulkInsert < RuboCop::Cop::Cop - MSG = 'Use the `BulkInsertSafe` concern, instead of using `Gitlab::Database.main.bulk_insert`. See https://docs.gitlab.com/ee/development/insert_into_tables_in_batches.html' + MSG = 'Use the `BulkInsertSafe` concern, instead of using `LegacyBulkInsert.bulk_insert`. See https://docs.gitlab.com/ee/development/insert_into_tables_in_batches.html' def_node_matcher :raw_union?, <<~PATTERN - (send (send (const (const _ :Gitlab) :Database) :main) :bulk_insert ...) + (send _ :legacy_bulk_insert ...) PATTERN def on_send(node) diff --git a/rubocop/cop/gitlab/change_timezone.rb b/rubocop/cop/gitlab/change_timezone.rb new file mode 100644 index 00000000000..c30a057d51c --- /dev/null +++ b/rubocop/cop/gitlab/change_timezone.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Gitlab + class ChangeTimezone < RuboCop::Cop::Cop + MSG = "Do not change timezone in the runtime (application or rspec), " \ + "it could result in silently modifying other behavior." + + def_node_matcher :changing_timezone?, <<~PATTERN + (send (const nil? :Time) :zone= ...) + PATTERN + + def on_send(node) + changing_timezone?(node) { add_offense(node) } + end + end + end + end +end diff --git a/rubocop/cop/gitlab/change_timzone.rb b/rubocop/cop/gitlab/change_timzone.rb deleted file mode 100644 index c30a057d51c..00000000000 --- a/rubocop/cop/gitlab/change_timzone.rb +++ /dev/null @@ -1,20 +0,0 @@ -# frozen_string_literal: true - -module RuboCop - module Cop - module Gitlab - class ChangeTimezone < RuboCop::Cop::Cop - MSG = "Do not change timezone in the runtime (application or rspec), " \ - "it could result in silently modifying other behavior." - - def_node_matcher :changing_timezone?, <<~PATTERN - (send (const nil? :Time) :zone= ...) - PATTERN - - def on_send(node) - changing_timezone?(node) { add_offense(node) } - end - end - end - end -end diff --git a/rubocop/cop/gitlab/keys-first-and-values-first.rb b/rubocop/cop/gitlab/keys-first-and-values-first.rb deleted file mode 100644 index e9bf266cdd7..00000000000 --- a/rubocop/cop/gitlab/keys-first-and-values-first.rb +++ /dev/null @@ -1,57 +0,0 @@ -# frozen_string_literal: true - -module RuboCop - module Cop - module Gitlab - class KeysFirstAndValuesFirst < RuboCop::Cop::Cop - FIRST_PATTERN = /\Afirst\z/.freeze - - def message(used_method) - <<~MSG - Don't use `.keys.first` and `.values.first`. - Instead use `.each_key.first` and `.each_value.first` (or `.first.first` and `first.second`) - - This will reduce memory usage and execution time. - MSG - end - - def on_send(node) - if find_on_keys_or_values?(node) - add_offense(node, location: :selector, message: message(node.method_name)) - end - end - - def autocorrect(node) - lambda do |corrector| - replace_with = if node.descendants.first.method_name == :values - '.each_value' - elsif node.descendants.first.method_name == :keys - '.each_key' - else - throw("Expect '.values.first' or '.keys.first', but get #{node.descendants.first.method_name}.first") # rubocop:disable Cop/BanCatchThrow - end - - upto_including_keys_or_values = node.descendants.first.source_range - before_keys_or_values = node.descendants[1].source_range - range_to_replace = node.source_range - .with(begin_pos: before_keys_or_values.end_pos, - end_pos: upto_including_keys_or_values.end_pos) - corrector.replace(range_to_replace, replace_with) - end - end - - def find_on_keys_or_values?(node) - chained_on_node = node.descendants.first - node.method_name.to_s =~ FIRST_PATTERN && - chained_on_node.is_a?(RuboCop::AST::SendNode) && - [:keys, :values].include?(chained_on_node.method_name) && - node.descendants[1] - end - - def method_name_for_node(node) - children[1].to_s - end - end - end - end -end diff --git a/rubocop/cop/gitlab/keys_first_and_values_first.rb b/rubocop/cop/gitlab/keys_first_and_values_first.rb new file mode 100644 index 00000000000..e9bf266cdd7 --- /dev/null +++ b/rubocop/cop/gitlab/keys_first_and_values_first.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Gitlab + class KeysFirstAndValuesFirst < RuboCop::Cop::Cop + FIRST_PATTERN = /\Afirst\z/.freeze + + def message(used_method) + <<~MSG + Don't use `.keys.first` and `.values.first`. + Instead use `.each_key.first` and `.each_value.first` (or `.first.first` and `first.second`) + + This will reduce memory usage and execution time. + MSG + end + + def on_send(node) + if find_on_keys_or_values?(node) + add_offense(node, location: :selector, message: message(node.method_name)) + end + end + + def autocorrect(node) + lambda do |corrector| + replace_with = if node.descendants.first.method_name == :values + '.each_value' + elsif node.descendants.first.method_name == :keys + '.each_key' + else + throw("Expect '.values.first' or '.keys.first', but get #{node.descendants.first.method_name}.first") # rubocop:disable Cop/BanCatchThrow + end + + upto_including_keys_or_values = node.descendants.first.source_range + before_keys_or_values = node.descendants[1].source_range + range_to_replace = node.source_range + .with(begin_pos: before_keys_or_values.end_pos, + end_pos: upto_including_keys_or_values.end_pos) + corrector.replace(range_to_replace, replace_with) + end + end + + def find_on_keys_or_values?(node) + chained_on_node = node.descendants.first + node.method_name.to_s =~ FIRST_PATTERN && + chained_on_node.is_a?(RuboCop::AST::SendNode) && + [:keys, :values].include?(chained_on_node.method_name) && + node.descendants[1] + end + + def method_name_for_node(node) + children[1].to_s + end + end + end + end +end diff --git a/rubocop/cop/gitlab/mark_used_feature_flags.rb b/rubocop/cop/gitlab/mark_used_feature_flags.rb index 03ee4805f4e..d3c5cfb827e 100644 --- a/rubocop/cop/gitlab/mark_used_feature_flags.rb +++ b/rubocop/cop/gitlab/mark_used_feature_flags.rb @@ -255,10 +255,17 @@ module RuboCop ] # For EE additionally process `ee/` feature flags - if File.exist?(File.expand_path('../../../ee/app/models/license.rb', __dir__)) && !%w[true 1].include?(ENV['FOSS_ONLY'].to_s) + is_ee = File.exist?(File.expand_path('../../../ee/app/models/license.rb', __dir__)) && !%w[true 1].include?(ENV['FOSS_ONLY'].to_s) + if is_ee flags_paths << 'ee/config/feature_flags/**/*.yml' end + # For JH additionally process `jh/` feature flags + is_jh = is_ee && Dir.exist?(File.expand_path('../../../jh', __dir__)) && !%w[true 1].include?(ENV['EE_ONLY'].to_s) + if is_jh + flags_paths << 'jh/config/feature_flags/**/*.yml' + end + flags_paths.each_with_object([]) do |flags_path, memo| flags_path = File.expand_path("../../../#{flags_path}", __dir__) Dir.glob(flags_path).each do |path| diff --git a/rubocop/cop/qa/duplicate_testcase_link.rb b/rubocop/cop/qa/duplicate_testcase_link.rb new file mode 100644 index 00000000000..82549707a83 --- /dev/null +++ b/rubocop/cop/qa/duplicate_testcase_link.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module QA + # This cop checks for duplicate testcase links across e2e specs + # + # @example + # + # # bad + # it 'some test', testcase: '(...)/quality/test_cases/1892' + # it 'another test, testcase: '(...)/quality/test_cases/1892' + # + # # good + # it 'some test', testcase: '(...)/quality/test_cases/1892' + # it 'another test, testcase: '(...)/quality/test_cases/1894' + class DuplicateTestcaseLink < RuboCop::Cop::Cop + MESSAGE = "Don't reuse the same testcase link in different tests. Replace one of `%s`." + + @testcase_set = Set.new + + def_node_matcher :duplicate_testcase_link, <<~PATTERN + (block + (send nil? ... + ... + (hash + (pair + (sym :testcase) + (str $_))...)...)...) + PATTERN + + def on_block(node) + duplicate_testcase_link(node) do |link| + break unless self.class.duplicate?(link) + + add_offense(node, message: MESSAGE % link) + end + end + + def self.duplicate?(link) + !@testcase_set.add?(link) + end + end + end + end +end diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index c8a573410d8..5a5e76a87e2 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -1,4 +1,7 @@ +# rubocop:disable Naming/FileName # frozen_string_literal: true # Auto-require all cops under `rubocop/cop/**/*.rb` Dir[File.join(__dir__, 'cop', '**', '*.rb')].sort.each(&method(:require)) + +# rubocop:enable Naming/FileName -- cgit v1.2.3