diff --git a/Gemfile.lock b/Gemfile.lock index 556dcbe..0218d4b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,13 +9,13 @@ PATH GEM remote: https://rubygems.org/ specs: - actionview (6.1.4.4) - activesupport (= 6.1.4.4) + actionview (6.1.6) + activesupport (= 6.1.6) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) - activesupport (6.1.4.4) + activesupport (6.1.6) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) @@ -23,56 +23,57 @@ GEM zeitwerk (~> 2.3) ast (2.4.2) builder (3.2.4) - concurrent-ruby (1.1.9) + concurrent-ruby (1.1.10) crass (1.0.6) erubi (1.10.0) - i18n (1.8.11) + i18n (1.10.0) concurrent-ruby (~> 1.0) - loofah (2.13.0) + loofah (2.18.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) - minitest (5.15.0) - nokogiri (1.13.1-x86_64-darwin) + minitest (5.16.1) + nokogiri (1.13.6-x86_64-darwin) racc (~> 1.4) - parallel (1.21.0) - parser (3.1.0.0) + parallel (1.22.1) + parser (3.1.2.0) ast (~> 2.4.1) racc (1.6.0) - rack (2.2.3) + rack (2.2.3.1) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) - rails-html-sanitizer (1.4.2) + rails-html-sanitizer (1.4.3) loofah (~> 2.3) rainbow (3.1.1) rake (13.0.6) - regexp_parser (2.2.0) + regexp_parser (2.5.0) rexml (3.2.5) - rubocop (1.25.1) + rubocop (1.31.0) parallel (~> 1.10) parser (>= 3.1.0.0) rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) - rexml - rubocop-ast (>= 1.15.1, < 2.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.18.0, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.15.1) - parser (>= 3.0.1.1) - rubocop-performance (1.13.2) + rubocop-ast (1.18.0) + parser (>= 3.1.1.0) + rubocop-performance (1.14.2) rubocop (>= 1.7.0, < 2.0) rubocop-ast (>= 0.4.0) - rubocop-rails (2.13.2) + rubocop-rails (2.15.1) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.7.0, < 2.0) ruby-progressbar (1.11.0) tzinfo (2.0.4) concurrent-ruby (~> 1.0) - unicode-display_width (2.1.0) - zeitwerk (2.5.3) + unicode-display_width (2.2.0) + zeitwerk (2.6.0) PLATFORMS + x86_64-darwin-19 x86_64-darwin-20 DEPENDENCIES diff --git a/lib/rubocop/cop/github/insecure_hash_algorithm.rb b/lib/rubocop/cop/github/insecure_hash_algorithm.rb index d452730..de1b321 100644 --- a/lib/rubocop/cop/github/insecure_hash_algorithm.rb +++ b/lib/rubocop/cop/github/insecure_hash_algorithm.rb @@ -5,7 +5,7 @@ module RuboCop module Cop module GitHub - class InsecureHashAlgorithm < Cop + class InsecureHashAlgorithm < Base MSG = "This hash function is not allowed" UUID_V3_MSG = "uuid_v3 uses MD5, which is not allowed" UUID_V5_MSG = "uuid_v5 uses SHA1, which is not allowed" diff --git a/lib/rubocop/cop/github/rails_application_record.rb b/lib/rubocop/cop/github/rails_application_record.rb index 1862bc5..1ec30cf 100644 --- a/lib/rubocop/cop/github/rails_application_record.rb +++ b/lib/rubocop/cop/github/rails_application_record.rb @@ -5,7 +5,7 @@ module RuboCop module Cop module GitHub - class RailsApplicationRecord < Cop + class RailsApplicationRecord < Base MSG = "Models should subclass from ApplicationRecord" def_node_matcher :active_record_base_const?, <<-PATTERN @@ -20,7 +20,7 @@ def on_class(node) klass, superclass, _ = *node if active_record_base_const?(superclass) && !(application_record_const?(klass)) - add_offense(superclass, location: :expression) + add_offense(superclass) end end end diff --git a/lib/rubocop/cop/github/rails_controller_render_action_symbol.rb b/lib/rubocop/cop/github/rails_controller_render_action_symbol.rb index 83d7d9b..e3d6eca 100644 --- a/lib/rubocop/cop/github/rails_controller_render_action_symbol.rb +++ b/lib/rubocop/cop/github/rails_controller_render_action_symbol.rb @@ -5,7 +5,9 @@ module RuboCop module Cop module GitHub - class RailsControllerRenderActionSymbol < Cop + class RailsControllerRenderActionSymbol < Base + extend AutoCorrector + MSG = "Prefer `render` with string instead of symbol" def_node_matcher :render_sym?, <<-PATTERN @@ -22,18 +24,22 @@ class RailsControllerRenderActionSymbol < Cop def on_send(node) if sym_node = render_sym?(node) - add_offense(sym_node, location: :expression) + add_offense(sym_node) do |corrector| + register_offense(sym_node, node) + end elsif option_pairs = render_with_options?(node) option_pairs.each do |pair| if sym_node = action_key?(pair) - add_offense(sym_node, location: :expression) + register_offense(sym_node, node) end end end end - def autocorrect(node) - lambda do |corrector| + private + + def register_offense(sym_node, node) + add_offense(sym_node) do |corrector| corrector.replace(node.source_range, "\"#{node.children[0]}\"") end end diff --git a/lib/rubocop/cop/github/rails_controller_render_literal.rb b/lib/rubocop/cop/github/rails_controller_render_literal.rb index 7102637..0f2efd3 100644 --- a/lib/rubocop/cop/github/rails_controller_render_literal.rb +++ b/lib/rubocop/cop/github/rails_controller_render_literal.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module GitHub - class RailsControllerRenderLiteral < Cop + class RailsControllerRenderLiteral < Base include RenderLiteralHelpers MSG = "render must be used with a string literal or an instance of a Class" @@ -66,29 +66,29 @@ def on_send(node) if template_node = option_pairs.map { |pair| template_key?(pair) }.compact.first if !literal?(template_node) - add_offense(node, location: :expression) + add_offense(node) return end else - add_offense(node, location: :expression) + add_offense(node) return end if layout_node = option_pairs.map { |pair| layout_key?(pair) }.compact.first if !literal?(layout_node) - add_offense(node, location: :expression) + add_offense(node) return end end else - add_offense(node, location: :expression) + add_offense(node) return end if render_literal?(node) option_hash = node.arguments[1] if option_hash && !option_hash.hash_type? - add_offense(node, location: :expression) + add_offense(node) return end option_pairs = option_hash && option_hash.pairs @@ -99,7 +99,7 @@ def on_send(node) if option_pairs locals = option_pairs.map { |pair| locals_key?(pair) }.compact.first if locals && (!locals.hash_type? || !hash_with_literal_keys?(locals)) - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb b/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb index 9562d7c..126738a 100644 --- a/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb +++ b/lib/rubocop/cop/github/rails_controller_render_paths_exist.rb @@ -5,7 +5,7 @@ module RuboCop module Cop module GitHub - class RailsControllerRenderPathsExist < Cop + class RailsControllerRenderPathsExist < Base def_node_matcher :render?, <<-PATTERN (send nil? {:render :render_to_string} $...) PATTERN @@ -28,7 +28,7 @@ def on_send(node) if args = render_str?(node) node, path = args unless resolve_template(path.to_s) - add_offense(node, location: :expression, message: "Template could not be found") + add_offense(node, message: "Template could not be found") end elsif pairs = render_options?(node) if pair = pairs.detect { |p| render_key?(p) } @@ -37,11 +37,11 @@ def on_send(node) case key when :action, :template unless resolve_template(path.to_s) - add_offense(node, location: :expression, message: "Template could not be found") + add_offense(node, message: "Template could not be found") end when :partial unless resolve_partial(path.to_s) - add_offense(node, location: :expression, message: "Partial template could not be found") + add_offense(node, message: "Partial template could not be found") end end end diff --git a/lib/rubocop/cop/github/rails_controller_render_shorthand.rb b/lib/rubocop/cop/github/rails_controller_render_shorthand.rb index b59894d..58069e1 100644 --- a/lib/rubocop/cop/github/rails_controller_render_shorthand.rb +++ b/lib/rubocop/cop/github/rails_controller_render_shorthand.rb @@ -5,7 +5,9 @@ module RuboCop module Cop module GitHub - class RailsControllerRenderShorthand < Cop + class RailsControllerRenderShorthand < Base + extend AutoCorrector + MSG = "Prefer `render` template shorthand" def_node_matcher :render_with_options?, <<-PATTERN @@ -20,14 +22,6 @@ class RailsControllerRenderShorthand < Cop ({str sym} $_) PATTERN - def investigate(*) - @autocorrect = {} - end - - def autocorrect(node) - @autocorrect[node] - end - def on_send(node) if option_pairs = render_with_options?(node) option_pairs.each do |pair| @@ -37,10 +31,9 @@ def on_send(node) .sub(/#{pair.source}(,\s*)?/, "") .sub("render ", "render \"#{str(value_node)}\"#{comma}") - @autocorrect[node] = lambda do |corrector| + add_offense(node, message: "Use `#{corrected_source}` instead") do |corrector| corrector.replace(node.source_range, corrected_source) end - add_offense(node, location: :expression, message: "Use `#{corrected_source}` instead") end end end diff --git a/lib/rubocop/cop/github/rails_render_inline.rb b/lib/rubocop/cop/github/rails_render_inline.rb index f84e2ae..8f76fb2 100644 --- a/lib/rubocop/cop/github/rails_render_inline.rb +++ b/lib/rubocop/cop/github/rails_render_inline.rb @@ -5,7 +5,7 @@ module RuboCop module Cop module GitHub - class RailsRenderInline < Cop + class RailsRenderInline < Base MSG = "Avoid `render inline:`" def_node_matcher :render_with_options?, <<-PATTERN @@ -19,7 +19,7 @@ class RailsRenderInline < Cop def on_send(node) if option_pairs = render_with_options?(node) if option_pairs.detect { |pair| inline_key?(pair) } - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/lib/rubocop/cop/github/rails_render_object_collection.rb b/lib/rubocop/cop/github/rails_render_object_collection.rb index c6b10c6..38a27fa 100644 --- a/lib/rubocop/cop/github/rails_render_object_collection.rb +++ b/lib/rubocop/cop/github/rails_render_object_collection.rb @@ -5,7 +5,7 @@ module RuboCop module Cop module GitHub - class RailsRenderObjectCollection < Cop + class RailsRenderObjectCollection < Base MSG = "Avoid `render object:`" def_node_matcher :render_with_options?, <<-PATTERN @@ -34,9 +34,9 @@ def on_send(node) if partial_name.children[0].is_a?(String) suggestion = ", instead `render partial: #{partial_name.source}, locals: { #{File.basename(partial_name.children[0], '.html.erb')}: #{object_node.source} }`" end - add_offense(node, location: :expression, message: "Avoid `render object:`#{suggestion}") + add_offense(node, message: "Avoid `render object:`#{suggestion}") when :collection, :spacer_template - add_offense(node, location: :expression, message: "Avoid `render collection:`") + add_offense(node, message: "Avoid `render collection:`") end end end diff --git a/lib/rubocop/cop/github/rails_view_render_literal.rb b/lib/rubocop/cop/github/rails_view_render_literal.rb index 0596079..c856386 100644 --- a/lib/rubocop/cop/github/rails_view_render_literal.rb +++ b/lib/rubocop/cop/github/rails_view_render_literal.rb @@ -6,7 +6,7 @@ module RuboCop module Cop module GitHub - class RailsViewRenderLiteral < Cop + class RailsViewRenderLiteral < Base include RenderLiteralHelpers MSG = "render must be used with a literal template and use literals for locals keys" @@ -40,15 +40,15 @@ def on_send(node) if partial_node = option_pairs.map { |pair| partial_key?(pair) }.compact.first if !literal?(partial_node) - add_offense(node, location: :expression) + add_offense(node) return end else - add_offense(node, location: :expression) + add_offense(node) return end else - add_offense(node, location: :expression) + add_offense(node) return end @@ -61,10 +61,10 @@ def on_send(node) if locals if locals.hash_type? if !hash_with_literal_keys?(locals) - add_offense(node, location: :expression) + add_offense(node) end else - add_offense(node, location: :expression) + add_offense(node) end end end diff --git a/lib/rubocop/cop/github/rails_view_render_paths_exist.rb b/lib/rubocop/cop/github/rails_view_render_paths_exist.rb index 0684255..49a40f2 100644 --- a/lib/rubocop/cop/github/rails_view_render_paths_exist.rb +++ b/lib/rubocop/cop/github/rails_view_render_paths_exist.rb @@ -5,7 +5,7 @@ module RuboCop module Cop module GitHub - class RailsViewRenderPathsExist < Cop + class RailsViewRenderPathsExist < Base def_node_matcher :render?, <<-PATTERN (send nil? {:render :render_to_string} $...) PATTERN @@ -28,14 +28,14 @@ def on_send(node) if args = render_str?(node) node, path = args unless resolve_partial(path.to_s) - add_offense(node, location: :expression, message: "Partial template could not be found") + add_offense(node, message: "Partial template could not be found") end elsif pairs = render_options?(node) if pair = pairs.detect { |p| partial_key?(p) } node, path = partial_key?(pair) unless resolve_partial(path.to_s) - add_offense(node, location: :expression, message: "Partial template could not be found") + add_offense(node, message: "Partial template could not be found") end end end diff --git a/lib/rubocop/cop/github/rails_view_render_shorthand.rb b/lib/rubocop/cop/github/rails_view_render_shorthand.rb index b8fcc5e..e465d8d 100644 --- a/lib/rubocop/cop/github/rails_view_render_shorthand.rb +++ b/lib/rubocop/cop/github/rails_view_render_shorthand.rb @@ -5,7 +5,7 @@ module RuboCop module Cop module GitHub - class RailsViewRenderShorthand < Cop + class RailsViewRenderShorthand < Base MSG = "Prefer `render` partial shorthand" def_node_matcher :render_with_options?, <<-PATTERN @@ -26,9 +26,9 @@ def on_send(node) locals_key = option_pairs.map { |pair| locals_key?(pair) }.compact.first if option_pairs.length == 1 && partial_key - add_offense(node, location: :expression, message: "Use `render #{partial_key.source}` instead") + add_offense(node, message: "Use `render #{partial_key.source}` instead") elsif option_pairs.length == 2 && partial_key && locals_key - add_offense(node, location: :expression, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead") + add_offense(node, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead") end end end diff --git a/test/cop_test.rb b/test/cop_test.rb index dd19bf5..4af051c 100644 --- a/test/cop_test.rb +++ b/test/cop_test.rb @@ -17,9 +17,9 @@ def setup def investigate(cop, src, filename = nil) processed_source = RuboCop::ProcessedSource.new(src, RUBY_VERSION.to_f, filename) - commissioner = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) - commissioner.investigate(processed_source) - commissioner + team = RuboCop::Cop::Team.new([cop], nil, raise_error: true) + report = team.investigate(processed_source) + report.offenses end def erb_investigate(cop, src, filename = nil) diff --git a/test/test_insecure_hash_algorithm.rb b/test/test_insecure_hash_algorithm.rb index ee18db7..597450b 100644 --- a/test/test_insecure_hash_algorithm.rb +++ b/test/test_insecure_hash_algorithm.rb @@ -15,7 +15,7 @@ def make_cop(allowed:) end def test_benign_apis - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def hexencode_the_string_md5 Digest.hexencode('anything') @@ -26,11 +26,11 @@ def bubblebabble_the_string_md5 end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_openssl_hmac_new_sha256_indirect - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something HASH = OpenSSL::Digest::SHA256 @@ -42,11 +42,11 @@ def secret_hmac end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_openssl_hmac_new_sha1 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something attr :secret @@ -56,12 +56,12 @@ def secret_hmac end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_digest_method_md5_str - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def h Digest('md5') @@ -69,12 +69,12 @@ def h end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_digest_method_md5_symbol - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def h Digest(:MD5) @@ -82,12 +82,12 @@ def h end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_digest_method_sha256_str - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def h Digest('sha256') @@ -95,11 +95,11 @@ def h end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_digest_method_sha256_symbol - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def h Digest('sha256') @@ -111,75 +111,75 @@ def h end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_alias_for_digest_md5 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something HASH = Digest::MD5 end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_alias_for_openssl_digest_md5 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something HASH = OpenSSL::Digest::MD5 end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_alias_for_digest_sha1 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something HASH = Digest::SHA1 end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_alias_for_openssl_digest_sha1 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something HASH = OpenSSL::Digest::SHA1 end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_alias_for_digest_sha256 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something HASH = Digest::SHA256 end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_alias_for_openssl_digest_sha256 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something HASH = OpenSSL::Digest::SHA256 end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_hexdigest_on_random_class - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) HASH.hexdigest(str) @@ -187,11 +187,11 @@ def something(str) end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_md5_hexdigest - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) Digest::MD5.hexdigest(str) @@ -199,12 +199,12 @@ def something(str) end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_openssl_md5_hexdigest - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::Digest::MD5.hexdigest(str) @@ -212,12 +212,12 @@ def something(str) end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_openssl_md5_digest_by_name - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::Digest.digest("MD5", str) @@ -225,12 +225,12 @@ def something(str) end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_openssl_sha1_digest_by_name - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::Digest.digest("SHA1", str) @@ -238,12 +238,12 @@ def something(str) end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_openssl_sha1_hexdigest_by_name_mixed_case - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::Digest.hexdigest("Sha1", str) @@ -251,12 +251,12 @@ def something(str) end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_openssl_sha256_digest_by_name - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::Digest.digest("SHA256", str) @@ -264,11 +264,11 @@ def something(str) end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_openssl_md5_hmac_by_name - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::HMAC.hexdigest('md5', str) @@ -276,12 +276,12 @@ def something(str) end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_openssl_sha1_hmac_by_name - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::HMAC.hexdigest('sha1', str) @@ -289,12 +289,12 @@ def something(str) end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_openssl_sha256_hmac_by_name - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::HMAC.hexdigest('sha256', str) @@ -302,11 +302,11 @@ def something(str) end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_openssl_md5_digest_instance_by_name - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::Digest::Digest.new('md5') @@ -314,12 +314,12 @@ def something(str) end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_openssl_sha1_digest_instance_by_name - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::Digest::Digest.new('sha1') @@ -327,12 +327,12 @@ def something(str) end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::MSG, offenses.first.message end def test_openssl_sha256_digest_instance_by_name - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def something(str) OpenSSL::Digest::Digest.new('sha256') @@ -340,11 +340,11 @@ def something(str) end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_uuid_from_hash - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def uuid # I want to demonstrate that uuid_from_hash isn't a trigger, @@ -354,11 +354,11 @@ def uuid end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_uuid_v3 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def uuid Digest::UUID.uuid_v3('anything', 'anything') @@ -366,13 +366,13 @@ def uuid end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::UUID_V3_MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::UUID_V3_MSG, offenses.first.message end def test_uuid_v3_with_md5_allowed cop = make_cop(allowed: %w[MD5]) - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def uuid Digest::UUID.uuid_v3('anything', 'anything') @@ -380,11 +380,11 @@ def uuid end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_uuid_v4 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def uuid Digest::UUID.uuid_v4 @@ -392,11 +392,11 @@ def uuid end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_uuid_v5 - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def uuid Digest::UUID.uuid_v5('anything', 'anything') @@ -404,13 +404,13 @@ def uuid end RUBY - assert_equal 1, cop.offenses.count - assert_equal cop_class::UUID_V5_MSG, cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal cop_class::UUID_V5_MSG, offenses.first.message end def test_uuid_v5_with_sha1_allowed cop = make_cop(allowed: %w[SHA1]) - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something def uuid Digest::UUID.uuid_v5('anything', 'anything') @@ -418,26 +418,26 @@ def uuid end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_allow_sha512_only cop = make_cop(allowed: %w[SHA512]) - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something HASH = Digest::SHA256 end RUBY - assert_equal 1, cop.offenses.count + assert_equal 1, offenses.count end def test_allow_lots_of_hashes cop = make_cop(allowed: %w[SHA1 SHA256 SHA384 SHA512]) - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Something HASH = Digest::SHA1 end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end end diff --git a/test/test_rails_application_record.rb b/test/test_rails_application_record.rb index d7df2b2..7ac7794 100644 --- a/test/test_rails_application_record.rb +++ b/test/test_rails_application_record.rb @@ -10,30 +10,30 @@ def cop_class end def test_good_model - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Repository < ApplicationRecord end RUBY - assert_empty cop.offenses.map(&:message) + assert_empty offenses.map(&:message) end def test_application_record_model - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class ApplicationRecord < ActiveRecord::Base end RUBY - assert_empty cop.offenses.map(&:message) + assert_empty offenses.map(&:message) end def test_bad_model - investigate(cop, <<-RUBY) + offenses = investigate(cop, <<-RUBY) class Repository < ActiveRecord::Base end RUBY - assert_equal 1, cop.offenses.count - assert_equal "Models should subclass from ApplicationRecord", cop.offenses.first.message + assert_equal 1, offenses.count + assert_equal "Models should subclass from ApplicationRecord", offenses.first.message end end diff --git a/test/test_rails_controller_render_action_symbol.rb b/test/test_rails_controller_render_action_symbol.rb index d234fb5..5731d82 100644 --- a/test/test_rails_controller_render_action_symbol.rb +++ b/test/test_rails_controller_render_action_symbol.rb @@ -10,7 +10,7 @@ def cop_class end def test_render_string_no_offense - investigate cop, <<-RUBY, "app/controllers/foo_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/foo_controller.rb" class FooController < ActionController::Base def show render "show" @@ -26,11 +26,11 @@ def update end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_inline_offense - investigate cop, <<-RUBY, "app/controllers/foo_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/foo_controller.rb" class FooController < ActionController::Base def show render :show @@ -46,10 +46,10 @@ def update end RUBY - assert_equal 3, cop.offenses.count + assert_equal 3, offenses.count expected_message = "Prefer `render` with string instead of symbol" - assert_equal expected_message, cop.offenses[0].message - assert_equal expected_message, cop.offenses[1].message - assert_equal expected_message, cop.offenses[2].message + assert_equal expected_message, offenses[0].message + assert_equal expected_message, offenses[1].message + assert_equal expected_message, offenses[2].message end end diff --git a/test/test_rails_controller_render_literal.rb b/test/test_rails_controller_render_literal.rb index 5f7ba08..9da2f16 100644 --- a/test/test_rails_controller_render_literal.rb +++ b/test/test_rails_controller_render_literal.rb @@ -10,7 +10,7 @@ def cop_class end def test_render_string_literal_class_name_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render(MyClass, title: "foo", bar: "baz") @@ -18,11 +18,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_string_literal_class_instance_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render(MyClass.new(title: "foo", bar: "baz")) @@ -30,11 +30,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_string_literal_module_class_name_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render(Module::MyClass, title: "foo", bar: "baz") @@ -42,11 +42,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_string_literal_module_class_instance_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render(Module::MyClass.new(title: "foo", bar: "baz")) @@ -54,11 +54,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_string_literal_module_class_name_block_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render(Module::MyClass.new(title: "foo", bar: "baz")) do @@ -68,11 +68,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_string_literal_action_name_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render "index" @@ -80,11 +80,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_sym_literal_action_name_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render :index @@ -92,11 +92,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_action_literal_action_name_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render action: :index @@ -104,11 +104,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_string_literal_full_template_name_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render "products/index" @@ -116,11 +116,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_template_literal_full_template_name_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render template: "products/index" @@ -128,11 +128,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_template_and_layout_literal_full_template_name_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render template: "products/index", layout: "layouts/products" @@ -140,11 +140,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_partial_literal_full_template_name_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render partial: "products/index" @@ -152,11 +152,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_partial_literal_full_template_name_and_no_layout_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render partial: "products/index", layout: false @@ -164,11 +164,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_dynamic_file_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render file: "\#{Rails.root}/public/404.html" @@ -177,11 +177,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_inline_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render inline: "Hello <%= @name %>" @@ -189,11 +189,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_xml_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render xml: @product @@ -201,11 +201,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_json_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render json: @product @@ -213,11 +213,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_plain_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render plain: "OK" @@ -225,11 +225,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_html_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render html: "Not Found".html_safe @@ -237,11 +237,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_body_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render body: "raw" @@ -249,11 +249,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_nothing_no_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render nothing: true @@ -261,11 +261,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_implicit_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render @@ -273,12 +273,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_implicit_with_layout_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render layout: "layouts/product" @@ -286,12 +286,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_implicit_with_status_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render status: :ok @@ -299,12 +299,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_variable_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render magic_string @@ -312,12 +312,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_to_string_variable_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render_to_string(magic_string) @@ -325,12 +325,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_action_variable_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render action: magic_symbol @@ -338,12 +338,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_template_variable_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render template: magic_string @@ -351,12 +351,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_partial_variable_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render partial: magic_string @@ -364,12 +364,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_template_with_layout_variable_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render template: "products/index", layout: magic_string @@ -377,12 +377,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_template_variable_with_layout_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render template: magic_string, layout: "layouts/product" @@ -390,12 +390,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a string literal or an instance of a Class", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a string literal or an instance of a Class", offenses[0].message end def test_render_shorthand_static_locals_no_offsense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render "products/index", locals: { product: product } @@ -403,11 +403,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_partial_static_locals_no_offsense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render partial: "products/index", locals: { product: product } @@ -415,11 +415,11 @@ def index end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_literal_dynamic_options_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render "products/product", options @@ -427,11 +427,11 @@ def index end RUBY - assert_equal 1, cop.offenses.count + assert_equal 1, offenses.count end def test_render_literal_dynamic_locals_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render "products/product", locals: locals @@ -439,12 +439,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count + assert_equal 1, offenses.count end def test_render_literal_dynamic_local_key_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render "products/product", locals: { product_key => product } @@ -452,6 +452,6 @@ def index end RUBY - assert_equal 1, cop.offenses.count + assert_equal 1, offenses.count end end diff --git a/test/test_rails_controller_render_shorthand.rb b/test/test_rails_controller_render_shorthand.rb index 89a2834..0c16bd5 100644 --- a/test/test_rails_controller_render_shorthand.rb +++ b/test/test_rails_controller_render_shorthand.rb @@ -10,7 +10,7 @@ def cop_class end def test_render_string_no_offense - investigate cop, <<-RUBY, "app/controllers/foo_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/foo_controller.rb" class FooController < ActionController::Base def index render "index" @@ -22,11 +22,11 @@ def show end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_partial_no_offense - investigate cop, <<-RUBY, "app/controllers/books_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/books_controller.rb" class BooksController < ActionController::Base def show render partial: "books/show" @@ -34,11 +34,11 @@ def show end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_action_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def edit render action: :edit @@ -54,14 +54,14 @@ def confirm end RUBY - assert_equal 3, cop.offenses.count - assert_equal "Use `render \"edit\"` instead", cop.offenses[0].message - assert_equal "Use `render \"new\"` instead", cop.offenses[1].message - assert_equal "Use `render \"confirm.html.erb\"` instead", cop.offenses[2].message + assert_equal 3, offenses.count + assert_equal "Use `render \"edit\"` instead", offenses[0].message + assert_equal "Use `render \"new\"` instead", offenses[1].message + assert_equal "Use `render \"confirm.html.erb\"` instead", offenses[2].message end def test_render_template_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def new render template: "books/new" @@ -77,9 +77,9 @@ def edit end RUBY - assert_equal 3, cop.offenses.count - assert_equal "Use `render \"books/new\"` instead", cop.offenses[0].message - assert_equal "Use `render \"books/show\", locals: { book: @book }` instead", cop.offenses[1].message - assert_equal "Use `render \"books/edit.html.erb\", status: :ok, layout: \"application\"` instead", cop.offenses[2].message + assert_equal 3, offenses.count + assert_equal "Use `render \"books/new\"` instead", offenses[0].message + assert_equal "Use `render \"books/show\", locals: { book: @book }` instead", offenses[1].message + assert_equal "Use `render \"books/edit.html.erb\", status: :ok, layout: \"application\"` instead", offenses[2].message end end diff --git a/test/test_rails_render_inline.rb b/test/test_rails_render_inline.rb index e55c3b4..53d5cad 100644 --- a/test/test_rails_render_inline.rb +++ b/test/test_rails_render_inline.rb @@ -10,7 +10,7 @@ def cop_class end def test_render_string_no_offense - investigate cop, <<-RUBY, "app/controllers/foo_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/foo_controller.rb" class FooController < ActionController::Base def index render template: "index" @@ -22,11 +22,11 @@ def show end RUBY - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_inline_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render inline: "<% products.each do |p| %>

<%= p.name %>

<% end %>" @@ -34,12 +34,12 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "Avoid `render inline:`", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "Avoid `render inline:`", offenses[0].message end def test_render_status_with_inline_offense - investigate cop, <<-RUBY, "app/controllers/products_controller.rb" + offenses = investigate cop, <<-RUBY, "app/controllers/products_controller.rb" class ProductsController < ActionController::Base def index render status: 200, inline: "<% products.each do |p| %>

<%= p.name %>

<% end %>" @@ -47,16 +47,16 @@ def index end RUBY - assert_equal 1, cop.offenses.count - assert_equal "Avoid `render inline:`", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "Avoid `render inline:`", offenses[0].message end def test_erb_render_inline_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render inline: template %> ERB - assert_equal 1, cop.offenses.count - assert_equal "Avoid `render inline:`", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "Avoid `render inline:`", offenses[0].message end end diff --git a/test/test_rails_render_object_collection.rb b/test/test_rails_render_object_collection.rb index 79a12cc..7786ded 100644 --- a/test/test_rails_render_object_collection.rb +++ b/test/test_rails_render_object_collection.rb @@ -10,7 +10,7 @@ def cop_class end def test_render_partial_with_locals_no_offense - erb_investigate cop, <<-ERB, "app/views/advertiser/buy.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/advertiser/buy.html.erb" <%= render partial: "account", locals: { account: @buyer } %> <% @advertisements.each do |ad| %> @@ -18,33 +18,33 @@ def test_render_partial_with_locals_no_offense <% end %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_partial_with_object_offense - erb_investigate cop, <<-ERB, "app/views/advertiser/buyer.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/advertiser/buyer.html.erb" <%= render partial: "account", object: @buyer %> ERB - assert_equal 1, cop.offenses.count - assert_equal "Avoid `render object:`, instead `render partial: \"account\", locals: { account: @buyer }`", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "Avoid `render object:`, instead `render partial: \"account\", locals: { account: @buyer }`", offenses[0].message end def test_render_collection_with_object_offense - erb_investigate cop, <<-ERB, "app/views/advertiser/ads.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/advertiser/ads.html.erb" <%= render partial: "ad", collection: @advertisements %> ERB - assert_equal 1, cop.offenses.count - assert_equal "Avoid `render collection:`", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "Avoid `render collection:`", offenses[0].message end def test_render_spacer_template_with_object_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render partial: @products, spacer_template: "product_ruler" %> ERB - assert_equal 1, cop.offenses.count - assert_equal "Avoid `render collection:`", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "Avoid `render collection:`", offenses[0].message end end diff --git a/test/test_rails_view_render_literal.rb b/test/test_rails_view_render_literal.rb index 051b827..b983d5f 100644 --- a/test/test_rails_view_render_literal.rb +++ b/test/test_rails_view_render_literal.rb @@ -10,162 +10,162 @@ def cop_class end def test_render_string_literal_no_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render "products/listing" %> <%= render partial: "products/listing" %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_layout_string_literal_no_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render layout: "products/listing" do %> Hello <% end %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_layout_string_literal_with_block_pass_no_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render layout: "layouts/head", &head %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_variable_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render magic_string %> <%= render partial: magic_string %> ERB - assert_equal 2, cop.offenses.count - assert_equal "render must be used with a literal template and use literals for locals keys", cop.offenses[0].message + assert_equal 2, offenses.count + assert_equal "render must be used with a literal template and use literals for locals keys", offenses[0].message end def test_render_component_no_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render Module::MyClass.new(title: "foo", bar: "baz") %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_component_instance_no_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render MyClass.new(title: "foo", bar: "baz") %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_component_instance_with_content_no_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render MyClass.new(title: "foo", bar: "baz").with_content("foo") %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_component_instance_block_no_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render Module::MyClass.new(title: "foo", bar: "baz") do %>Content<% end %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_component_collection_no_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render MyClass.with_collection(title: "foo", bar: "baz") %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_component_module_collection_no_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render Foo::MyClass.with_collection(title: "foo", bar: "baz") %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_layout_variable_literal_no_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render layout: magic_string do %> Hello <% end %> ERB - assert_equal 1, cop.offenses.count - assert_equal "render must be used with a literal template and use literals for locals keys", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "render must be used with a literal template and use literals for locals keys", offenses[0].message end def test_render_inline_no_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render inline: magic_string %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_template_literal_no_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render template: "products/listing" %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_literal_static_locals_no_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render "products/product", product: product %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_literal_dynamic_locals_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render "products/product", locals %> ERB - assert_equal 1, cop.offenses.count + assert_equal 1, offenses.count end def test_render_literal_dynamic_local_key_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render "products/product", { product_key => product } %> ERB - assert_equal 1, cop.offenses.count + assert_equal 1, offenses.count end def test_render_options_static_locals_no_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render partial: "products/product", locals: { product: product } %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_options_dynamic_locals_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render partial: "products/product", locals: locals %> ERB - assert_equal 1, cop.offenses.count + assert_equal 1, offenses.count end def test_render_options_dynamic_local_key_offense - erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/products/index.html.erb" <%= render partial: "products/product", locals: { product_key => product } %> ERB - assert_equal 1, cop.offenses.count + assert_equal 1, offenses.count end end diff --git a/test/test_rails_view_render_shorthand.rb b/test/test_rails_view_render_shorthand.rb index 485231e..22ff453 100644 --- a/test/test_rails_view_render_shorthand.rb +++ b/test/test_rails_view_render_shorthand.rb @@ -10,46 +10,46 @@ def cop_class end def test_render_string_no_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render "foo/bar" %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_string_with_locals_no_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render "foo/bar", bar: 42 %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end def test_render_partial_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render partial: "foo/bar" %> ERB - assert_equal 1, cop.offenses.count - assert_equal "Use `render \"foo/bar\"` instead", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "Use `render \"foo/bar\"` instead", offenses[0].message end def test_render_partial_with_locals_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render partial: "foo/bar", locals: { bar: 42 } %> ERB - assert_equal 1, cop.offenses.count - assert_equal "Use `render \"foo/bar\", { bar: 42 }` instead", cop.offenses[0].message + assert_equal 1, offenses.count + assert_equal "Use `render \"foo/bar\", { bar: 42 }` instead", offenses[0].message end def test_render_layout_no_offense - erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" + offenses = erb_investigate cop, <<-ERB, "app/views/foo/index.html.erb" <%= render layout: "foo/bar" do %> Hello <% end %> ERB - assert_equal 0, cop.offenses.count + assert_equal 0, offenses.count end end