Commit a2d39b80 authored by Rémy Coutable's avatar Rémy Coutable Committed by Robert Speicher

Use gitlab-styles

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 299e04e3
...@@ -16,6 +16,7 @@ engines: ...@@ -16,6 +16,7 @@ engines:
enabled: true enabled: true
rubocop: rubocop:
enabled: true enabled: true
channel: "gitlab-rubocop-0-52"
ratings: ratings:
paths: paths:
- Gemfile.lock - Gemfile.lock
......
...@@ -594,7 +594,7 @@ codequality: ...@@ -594,7 +594,7 @@ codequality:
script: script:
- cp .rubocop.yml .rubocop.yml.bak - cp .rubocop.yml .rubocop.yml.bak
- grep -v "rubocop-gitlab-security" .rubocop.yml.bak > .rubocop.yml - grep -v "rubocop-gitlab-security" .rubocop.yml.bak > .rubocop.yml
- docker run --env CODECLIMATE_CODE="$PWD" --volume "$PWD":/code --volume /var/run/docker.sock:/var/run/docker.sock --volume /tmp/cc:/tmp/cc codeclimate/codeclimate analyze -f json > raw_codeclimate.json - docker run --env CODECLIMATE_CODE="$PWD" --volume "$PWD":/code --volume /var/run/docker.sock:/var/run/docker.sock --volume /tmp/cc:/tmp/cc dev.gitlab.org:5005/gitlab/gitlab-build-images:gitlab-codeclimate-v2 analyze -f json > raw_codeclimate.json
- cat raw_codeclimate.json | docker run -i stedolan/jq -c 'map({check_name,fingerprint,location})' > codeclimate.json - cat raw_codeclimate.json | docker run -i stedolan/jq -c 'map({check_name,fingerprint,location})' > codeclimate.json
- mv .rubocop.yml.bak .rubocop.yml - mv .rubocop.yml.bak .rubocop.yml
artifacts: artifacts:
......
This diff is collapsed.
This diff is collapsed.
...@@ -334,9 +334,11 @@ group :development, :test do ...@@ -334,9 +334,11 @@ group :development, :test do
gem 'spring-commands-rspec', '~> 1.0.4' gem 'spring-commands-rspec', '~> 1.0.4'
gem 'spring-commands-spinach', '~> 1.1.0' gem 'spring-commands-spinach', '~> 1.1.0'
gem 'rubocop', '~> 0.49.1', require: false gem 'gitlab-styles', '~> 2.2.0', require: false
gem 'rubocop-rspec', '~> 1.15.1', require: false # Pin these dependencies, otherwise a new rule could break the CI pipelines
gem 'rubocop-gitlab-security', '~> 0.1.0', require: false gem 'rubocop', '~> 0.52.0'
gem 'rubocop-rspec', '~> 1.20.1'
gem 'scss_lint', '~> 0.54.0', require: false gem 'scss_lint', '~> 0.54.0', require: false
gem 'haml_lint', '~> 0.26.0', require: false gem 'haml_lint', '~> 0.26.0', require: false
gem 'simplecov', '~> 0.14.0', require: false gem 'simplecov', '~> 0.14.0', require: false
......
...@@ -303,6 +303,10 @@ GEM ...@@ -303,6 +303,10 @@ GEM
mime-types (>= 1.16) mime-types (>= 1.16)
posix-spawn (~> 0.3) posix-spawn (~> 0.3)
gitlab-markup (1.6.3) gitlab-markup (1.6.3)
gitlab-styles (2.2.0)
rubocop (~> 0.51)
rubocop-gitlab-security (~> 0.1.0)
rubocop-rspec (~> 1.19)
gitlab_omniauth-ldap (2.0.4) gitlab_omniauth-ldap (2.0.4)
net-ldap (~> 0.16) net-ldap (~> 0.16)
omniauth (~> 1.3) omniauth (~> 1.3)
...@@ -777,21 +781,21 @@ GEM ...@@ -777,21 +781,21 @@ GEM
pg pg
rails rails
sqlite3 sqlite3
rubocop (0.49.1) rubocop (0.52.0)
parallel (~> 1.10) parallel (~> 1.10)
parser (>= 2.3.3.1, < 3.0) parser (>= 2.4.0.2, < 3.0)
powerpack (~> 0.1) powerpack (~> 0.1)
rainbow (>= 1.99.1, < 3.0) rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7) ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1) unicode-display_width (~> 1.0, >= 1.0.1)
rubocop-gitlab-security (0.1.0) rubocop-gitlab-security (0.1.1)
rubocop (>= 0.47.1) rubocop (>= 0.51)
rubocop-rspec (1.15.1) rubocop-rspec (1.20.1)
rubocop (>= 0.42.0) rubocop (>= 0.51.0)
ruby-fogbugz (0.2.1) ruby-fogbugz (0.2.1)
crack (~> 0.4) crack (~> 0.4)
ruby-prof (0.16.2) ruby-prof (0.16.2)
ruby-progressbar (1.8.1) ruby-progressbar (1.9.0)
ruby-saml (1.4.1) ruby-saml (1.4.1)
nokogiri (>= 1.5.10) nokogiri (>= 1.5.10)
ruby_parser (3.9.0) ruby_parser (3.9.0)
...@@ -1046,6 +1050,7 @@ DEPENDENCIES ...@@ -1046,6 +1050,7 @@ DEPENDENCIES
github-linguist (~> 4.7.0) github-linguist (~> 4.7.0)
gitlab-flowdock-git-hook (~> 1.0.1) gitlab-flowdock-git-hook (~> 1.0.1)
gitlab-markup (~> 1.6.2) gitlab-markup (~> 1.6.2)
gitlab-styles (~> 2.2.0)
gitlab_omniauth-ldap (~> 2.0.4) gitlab_omniauth-ldap (~> 2.0.4)
gollum-lib (~> 4.2) gollum-lib (~> 4.2)
gollum-rugged_adapter (~> 0.4.4) gollum-rugged_adapter (~> 0.4.4)
...@@ -1148,9 +1153,8 @@ DEPENDENCIES ...@@ -1148,9 +1153,8 @@ DEPENDENCIES
rspec-retry (~> 0.4.5) rspec-retry (~> 0.4.5)
rspec-set (~> 0.1.3) rspec-set (~> 0.1.3)
rspec_profiling (~> 0.0.5) rspec_profiling (~> 0.0.5)
rubocop (~> 0.49.1) rubocop (~> 0.52.0)
rubocop-gitlab-security (~> 0.1.0) rubocop-rspec (~> 1.20.1)
rubocop-rspec (~> 1.15.1)
ruby-fogbugz (~> 0.2.1) ruby-fogbugz (~> 0.2.1)
ruby-prof (~> 0.16.2) ruby-prof (~> 0.16.2)
ruby_parser (~> 3.8) ruby_parser (~> 3.8)
......
...@@ -18,7 +18,7 @@ Peek.into Peek::Views::Rblineprof ...@@ -18,7 +18,7 @@ Peek.into Peek::Views::Rblineprof
Peek.into Peek::Views::GC Peek.into Peek::Views::GC
Peek.into Peek::Views::Gitaly Peek.into Peek::Views::Gitaly
# rubocop:disable Style/ClassAndModuleCamelCase # rubocop:disable Naming/ClassAndModuleCamelCase
class PEEK_DB_CLIENT class PEEK_DB_CLIENT
class << self class << self
attr_accessor :query_details attr_accessor :query_details
......
...@@ -93,9 +93,7 @@ module Gitlab ...@@ -93,9 +93,7 @@ module Gitlab
private private
def entries attr_reader :entries
@entries
end
end end
end end
end end
......
...@@ -56,11 +56,12 @@ module Gitlab ...@@ -56,11 +56,12 @@ module Gitlab
# Do nothing if hooks already exist # Do nothing if hooks already exist
unless real_local_hooks_path == File.realpath(global_hooks_path) unless real_local_hooks_path == File.realpath(global_hooks_path)
# Move the existing hooks somewhere safe if File.exist?(local_hooks_path)
FileUtils.mv( # Move the existing hooks somewhere safe
local_hooks_path, FileUtils.mv(
"#{local_hooks_path}.old.#{Time.now.to_i}" local_hooks_path,
) if File.exist?(local_hooks_path) "#{local_hooks_path}.old.#{Time.now.to_i}")
end
# Create the hooks symlink # Create the hooks symlink
FileUtils.ln_sf(global_hooks_path, local_hooks_path) FileUtils.ln_sf(global_hooks_path, local_hooks_path)
......
require_relative '../model_helpers'
module RuboCop
module Cop
# Cop that prevents the use of `dependent: ...` in ActiveRecord models.
class ActiveRecordDependent < RuboCop::Cop::Cop
include ModelHelpers
MSG = 'Do not use `dependent: to remove associated data, ' \
'use foreign keys with cascading deletes instead'.freeze
METHOD_NAMES = [:has_many, :has_one, :belongs_to].freeze
def on_send(node)
return unless in_model?(node)
return unless METHOD_NAMES.include?(node.children[1])
node.children.last.each_node(:pair) do |pair|
key_name = pair.children[0].children[0]
add_offense(pair, :expression) if key_name == :dependent
end
end
end
end
end
require_relative '../model_helpers'
module RuboCop
module Cop
# Cop that prevents the use of `serialize` in ActiveRecord models.
class ActiveRecordSerialize < RuboCop::Cop::Cop
include ModelHelpers
MSG = 'Do not store serialized data in the database, use separate columns and/or tables instead'.freeze
def on_send(node)
return unless in_model?(node)
add_offense(node, :selector) if node.children[1] == :serialize
end
end
end
end
module RuboCop
module Cop
# This cop makes sure that custom error classes, when empty, are declared
# with Class.new.
#
# @example
# # bad
# class FooError < StandardError
# end
#
# # okish
# class FooError < StandardError; end
#
# # good
# FooError = Class.new(StandardError)
class CustomErrorClass < RuboCop::Cop::Cop
MSG = 'Use `Class.new(SuperClass)` to define an empty custom error class.'.freeze
def on_class(node)
_klass, parent, body = node.children
return if body
parent_klass = class_name_from_node(parent)
return unless parent_klass && parent_klass.to_s.end_with?('Error')
add_offense(node, :expression)
end
def autocorrect(node)
klass, parent, _body = node.children
replacement = "#{class_name_from_node(klass)} = Class.new(#{class_name_from_node(parent)})"
lambda do |corrector|
corrector.replace(node.source_range, replacement)
end
end
private
# The nested constant `Foo::Bar::Baz` looks like:
#
# s(:const,
# s(:const,
# s(:const, nil, :Foo), :Bar), :Baz)
#
# So recurse through that to get the name as written in the source.
#
def class_name_from_node(node, suffix = nil)
return unless node&.type == :const
name = node.children[1].to_s
name = "#{name}::#{suffix}" if suffix
if node.children[0]
class_name_from_node(node.children[0], name)
else
name
end
end
end
end
end
module RuboCop
module Cop
# This cop prevents usage of the `git` and `github` arguments to `gem` in a
# `Gemfile` in order to avoid additional points of failure beyond
# rubygems.org.
class GemFetcher < RuboCop::Cop::Cop
MSG = 'Do not use gems from git repositories, only use gems from RubyGems.'.freeze
GIT_KEYS = [:git, :github].freeze
def on_send(node)
return unless gemfile?(node)
func_name = node.children[1]
return unless func_name == :gem
node.children.last.each_node(:pair) do |pair|
key_name = pair.children[0].children[0].to_sym
if GIT_KEYS.include?(key_name)
add_offense(node, pair.source_range, MSG)
end
end
end
private
def gemfile?(node)
node
.location
.expression
.source_buffer
.name
.end_with?("Gemfile")
end
end
end
end
...@@ -30,12 +30,12 @@ module RuboCop ...@@ -30,12 +30,12 @@ module RuboCop
if only_ivar_or_assignment?(definition) if only_ivar_or_assignment?(definition)
# We don't allow if any other ivar is used # We don't allow if any other ivar is used
definition.each_descendant(:ivar) do |offense| definition.each_descendant(:ivar) do |offense|
add_offense(offense, :expression) add_offense(offense, location: :expression)
end end
# We allow initialize method and single ivar # We allow initialize method and single ivar
elsif !initialize_method?(definition) && !single_ivar?(definition) elsif !initialize_method?(definition) && !single_ivar?(definition)
definition.each_descendant(:ivar, :ivasgn) do |offense| definition.each_descendant(:ivar, :ivasgn) do |offense|
add_offense(offense, :expression) add_offense(offense, location: :expression)
end end
end end
end end
......
require_relative '../model_helpers'
module RuboCop
module Cop
# Cop that prevents the use of `in_batches`
class InBatches < RuboCop::Cop::Cop
MSG = 'Do not use `in_batches`, use `each_batch` from the EachBatch module instead'.freeze
def on_send(node)
return unless node.children[1] == :in_batches
add_offense(node, :selector)
end
end
end
end
...@@ -9,14 +9,14 @@ module RuboCop ...@@ -9,14 +9,14 @@ module RuboCop
MSG = 'Include `ApplicationWorker`, not `Sidekiq::Worker`.'.freeze MSG = 'Include `ApplicationWorker`, not `Sidekiq::Worker`.'.freeze
def_node_matcher :includes_sidekiq_worker?, <<~PATTERN def_node_matcher :includes_sidekiq_worker?, <<~PATTERN
(send nil :include (const (const nil :Sidekiq) :Worker)) (send nil? :include (const (const nil? :Sidekiq) :Worker))
PATTERN PATTERN
def on_send(node) def on_send(node)
return if in_spec?(node) return if in_spec?(node)
return unless includes_sidekiq_worker?(node) return unless includes_sidekiq_worker?(node)
add_offense(node.arguments.first, :expression) add_offense(node.arguments.first, location: :expression)
end end
def autocorrect(node) def autocorrect(node)
......
# frozen_string_literal: true
module RuboCop
module Cop
# Ensures a line break after guard clauses.
#
# @example
# # bad
# return unless condition
# do_stuff
#
# # good
# return unless condition
#
# do_stuff
#
# # bad
# raise if condition
# do_stuff
#
# # good
# raise if condition
#
# do_stuff
#
# Multiple guard clauses are allowed without
# line break.
#
# # good
# return unless condition_a
# return unless condition_b
#
# do_stuff
#
# Guard clauses in case statement are allowed without
# line break.
#
# # good
# case model
# when condition_a
# return true unless condition_b
# when
# ...
# end
#
# Guard clauses before end are allowed without
# line break.
#
# # good
# if condition_a
# do_something
# else
# do_something_else
# return unless condition
# end
#
# do_something_more
class LineBreakAfterGuardClauses < RuboCop::Cop::Cop
MSG = 'Add a line break after guard clauses'
def_node_matcher :guard_clause_node?, <<-PATTERN
[{(send nil? {:raise :fail :throw} ...) return break next} single_line?]
PATTERN
def on_if(node)
return unless node.single_line?
return unless guard_clause?(node)
return if next_line(node).blank? || clause_last_line?(next_line(node)) || guard_clause?(next_sibling(node))
add_offense(node, :expression, MSG)
end
def autocorrect(node)
lambda do |corrector|
corrector.insert_after(node.loc.expression, "\n")
end
end
private
def guard_clause?(node)
return false unless node.if_type?
guard_clause_node?(node.if_branch)
end
def next_sibling(node)
node.parent.children[node.sibling_index + 1]
end
def next_line(node)
processed_source[node.loc.line]
end
def clause_last_line?(line)
line =~ /^\s*(?:end|elsif|else|when|rescue|ensure)/
end
end
end
end
...@@ -29,7 +29,7 @@ module RuboCop ...@@ -29,7 +29,7 @@ module RuboCop
opts.each_node(:pair) do |pair| opts.each_node(:pair) do |pair|
if hash_key_type(pair) == :sym && hash_key_name(pair) == :default if hash_key_type(pair) == :sym && hash_key_name(pair) == :default
add_offense(node, :selector) add_offense(node, location: :selector)
end end
end end
end end
......
...@@ -15,7 +15,7 @@ module RuboCop ...@@ -15,7 +15,7 @@ module RuboCop
name = node.children[1] name = node.children[1]
add_offense(node, :selector) if name == :add_foreign_key add_offense(node, location: :selector) if name == :add_foreign_key
end end
def method_name(node) def method_name(node)
......
...@@ -21,7 +21,7 @@ module RuboCop ...@@ -21,7 +21,7 @@ module RuboCop
node.each_ancestor(:def) do |def_node| node.each_ancestor(:def) do |def_node|
next unless method_name(def_node) == :change next unless method_name(def_node) == :change
add_offense(def_node, :name) add_offense(def_node, location: :name)
end end
end end
......
...@@ -27,7 +27,7 @@ module RuboCop ...@@ -27,7 +27,7 @@ module RuboCop
# data in these tables yet. # data in these tables yet.
next if new_tables.include?(first_arg) next if new_tables.include?(first_arg)
add_offense(send_node, :selector) add_offense(send_node, location: :selector)
end end
end end
......
...@@ -13,7 +13,7 @@ module RuboCop ...@@ -13,7 +13,7 @@ module RuboCop
def on_send(node) def on_send(node)
return unless in_migration?(node) return unless in_migration?(node)
add_offense(node, :selector) if method_name(node) == :add_timestamps add_offense(node, location: :selector) if method_name(node) == :add_timestamps
end end
def method_name(node) def method_name(node)
......
...@@ -17,7 +17,7 @@ module RuboCop ...@@ -17,7 +17,7 @@ module RuboCop
method_name = node.children[1] method_name = node.children[1]
if method_name == :datetime || method_name == :timestamp if method_name == :datetime || method_name == :timestamp
add_offense(send_node, :selector, format(MSG, method_name)) add_offense(send_node, location: :selector, message: format(MSG, method_name))
end end
end end
end end
...@@ -32,7 +32,7 @@ module RuboCop ...@@ -32,7 +32,7 @@ module RuboCop
last_argument = descendant.children.last last_argument = descendant.children.last
if last_argument == :datetime || last_argument == :timestamp if last_argument == :datetime || last_argument == :timestamp
add_offense(node, :expression, format(MSG, last_argument)) add_offense(node, location: :expression, message: format(MSG, last_argument))
end end
end end
end end
......
...@@ -29,7 +29,7 @@ module RuboCop ...@@ -29,7 +29,7 @@ module RuboCop
hash_key_name(pair) == :using hash_key_name(pair) == :using
if hash_key_value(pair).to_s == 'hash' if hash_key_value(pair).to_s == 'hash'
add_offense(pair, :expression) add_offense(pair, location: :expression)
end end
end end
end end
......
...@@ -20,7 +20,7 @@ module RuboCop ...@@ -20,7 +20,7 @@ module RuboCop
send_method = send_node.children[1] send_method = send_node.children[1]
if send_method == :remove_column if send_method == :remove_column
add_offense(send_node, :selector) add_offense(send_node, location: :selector)
end end
end end
end end
......
...@@ -16,7 +16,7 @@ module RuboCop ...@@ -16,7 +16,7 @@ module RuboCop
return unless node.children[1] == :remove_concurrent_index return unless node.children[1] == :remove_concurrent_index
node.each_ancestor(:def) do |def_node| node.each_ancestor(:def) do |def_node|
add_offense(def_node, :name) if method_name(def_node) == :change add_offense(def_node, location: :name) if method_name(def_node) == :change
end end
end end
......
...@@ -13,7 +13,7 @@ module RuboCop ...@@ -13,7 +13,7 @@ module RuboCop
return unless in_migration?(node) return unless in_migration?(node)
node.each_descendant(:send) do |send_node| node.each_descendant(:send) do |send_node|
add_offense(send_node, :selector) if method_name(send_node) == :remove_index add_offense(send_node, location: :selector) if method_name(send_node) == :remove_index
end end
end end
......
...@@ -9,7 +9,7 @@ module RuboCop ...@@ -9,7 +9,7 @@ module RuboCop
include MigrationHelpers include MigrationHelpers
def_node_matcher :add_column_with_default?, <<~PATTERN def_node_matcher :add_column_with_default?, <<~PATTERN
(send nil :add_column_with_default $...) (send nil? :add_column_with_default $...)
PATTERN PATTERN
def_node_matcher :defines_change?, <<~PATTERN def_node_matcher :defines_change?, <<~PATTERN
...@@ -26,7 +26,7 @@ module RuboCop ...@@ -26,7 +26,7 @@ module RuboCop
node.each_ancestor(:def) do |def_node| node.each_ancestor(:def) do |def_node|
next unless defines_change?(def_node) next unless defines_change?(def_node)
add_offense(def_node, :name) add_offense(def_node, location: :name)
end end
end end
end end
......
...@@ -28,7 +28,7 @@ module RuboCop ...@@ -28,7 +28,7 @@ module RuboCop
].freeze ].freeze
def_node_matcher :add_column?, <<~PATTERN def_node_matcher :add_column?, <<~PATTERN
(send nil :add_column $...) (send nil? :add_column $...)
PATTERN PATTERN
def on_send(node) def on_send(node)
...@@ -54,7 +54,7 @@ module RuboCop ...@@ -54,7 +54,7 @@ module RuboCop
NULL_OFFENSE NULL_OFFENSE
end end
add_offense(node, :expression, format(offense, table)) if offense add_offense(node, location: :expression, message: format(offense, table)) if offense
end end
def no_default?(opts) def no_default?(opts)
......
...@@ -14,7 +14,7 @@ module RuboCop ...@@ -14,7 +14,7 @@ module RuboCop
return unless in_migration?(node) return unless in_migration?(node)
node.each_descendant(:send) do |send_node| node.each_descendant(:send) do |send_node|
add_offense(send_node, :selector) if method_name(send_node) == :timestamps add_offense(send_node, location: :selector) if method_name(send_node) == :timestamps
end end
end end
......
...@@ -18,7 +18,7 @@ module RuboCop ...@@ -18,7 +18,7 @@ module RuboCop
spec_path = spec_filename(node) spec_path = spec_filename(node)
unless File.exist?(File.expand_path(spec_path, rails_root)) unless File.exist?(File.expand_path(spec_path, rails_root))
add_offense(node, :expression, format(MSG, spec_path)) add_offense(node, location: :expression, message: format(MSG, spec_path))
end end
end end
......
...@@ -35,7 +35,7 @@ module RuboCop ...@@ -35,7 +35,7 @@ module RuboCop
].freeze ].freeze
def_node_matcher :batch_update?, <<~PATTERN def_node_matcher :batch_update?, <<~PATTERN
(send nil ${:add_column_with_default :update_column_in_batches} $(sym ...) ...) (send nil? ${:add_column_with_default :update_column_in_batches} $(sym ...) ...)
PATTERN PATTERN
def on_send(node) def on_send(node)
...@@ -49,7 +49,7 @@ module RuboCop ...@@ -49,7 +49,7 @@ module RuboCop
return unless LARGE_TABLES.include?(table) return unless LARGE_TABLES.include?(table)
add_offense(node, :expression, format(MSG, update_method, table)) add_offense(node, location: :expression, message: format(MSG, update_method, table))
end end
end end
end end
......
require_relative '../model_helpers'
module RuboCop
module Cop
# Cop that prevents the use of polymorphic associations
class PolymorphicAssociations < RuboCop::Cop::Cop
include ModelHelpers
MSG = 'Do not use polymorphic associations, use separate tables instead'.freeze
def on_send(node)
return unless in_model?(node)
return unless node.children[1] == :belongs_to
node.children.last.each_node(:pair) do |pair|
key_name = pair.children[0].children[0]
add_offense(pair, :expression) if key_name == :polymorphic
end
end
end
end
end
...@@ -17,7 +17,7 @@ module RuboCop ...@@ -17,7 +17,7 @@ module RuboCop
return unless method_name(namespace_expr) == :namespace return unless method_name(namespace_expr) == :namespace
return unless receiver(namespace_expr) == project_expr return unless receiver(namespace_expr) == project_expr
add_offense(node, :selector) add_offense(node, location: :selector)
end end
def autocorrect(node) def autocorrect(node)
......
module RuboCop
module Cop
# This cop prevents usage of 'redirect_to' in actions 'destroy' without specifying 'status'.
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/31840
class RedirectWithStatus < RuboCop::Cop::Cop
MSG = 'Do not use "redirect_to" without "status" in "destroy" action'.freeze
def on_def(node)
return unless in_controller?(node)
return unless destroy?(node) || destroy_all?(node)
node.each_descendant(:send) do |def_node|
next unless redirect_to?(def_node)
methods = []
def_node.children.last.each_node(:pair) do |pair|
methods << pair.children.first.children.first
end
add_offense(def_node, :selector) unless methods.include?(:status)
end
end
private
def in_controller?(node)
node.location.expression.source_buffer.name.end_with?('_controller.rb')
end
def destroy?(node)
node.children.first == :destroy
end
def destroy_all?(node)
node.children.first == :destroy_all
end
def redirect_to?(node)
node.children[1] == :redirect_to
end
end
end
end
require 'rubocop-rspec'
require_relative '../../spec_helpers' require_relative '../../spec_helpers'
module RuboCop module RuboCop
...@@ -17,7 +16,7 @@ module RuboCop ...@@ -17,7 +16,7 @@ module RuboCop
# before do # before do
# stub_env('FOO', 'bar') # stub_env('FOO', 'bar')
# end # end
class EnvAssignment < Cop class EnvAssignment < RuboCop::Cop::Cop
include SpecHelpers include SpecHelpers
MESSAGE = "Don't assign to ENV, use `stub_env` instead.".freeze MESSAGE = "Don't assign to ENV, use `stub_env` instead.".freeze
...@@ -32,7 +31,7 @@ module RuboCop ...@@ -32,7 +31,7 @@ module RuboCop
return unless in_spec?(node) return unless in_spec?(node)
return unless env_assignment?(node) return unless env_assignment?(node)
add_offense(node, :expression, MESSAGE) add_offense(node, location: :expression, message: MESSAGE)
end end
def autocorrect(node) def autocorrect(node)
......
require 'rubocop-rspec'
module RuboCop
module Cop
module RSpec
# This cop checks for single-line hook blocks
#
# @example
#
# # bad
# before { do_something }
# after(:each) { undo_something }
#
# # good
# before do
# do_something
# end
#
# after(:each) do
# undo_something
# end
class SingleLineHook < Cop
MESSAGE = "Don't use single-line hook blocks.".freeze
def_node_search :rspec_hook?, <<~PATTERN
(send nil {:after :around :before} ...)
PATTERN
def on_block(node)
return unless rspec_hook?(node)
return unless node.single_line?
add_offense(node, :expression, MESSAGE)
end
end
end
end
end
# frozen_string_literal: true
require 'rubocop-rspec'
module RuboCop
module Cop
module RSpec
# Checks for verbose include metadata used in the specs.
#
# @example
# # bad
# describe MyClass, js: true do
# end
#
# # good
# describe MyClass, :js do
# end
class VerboseIncludeMetadata < Cop
MSG = 'Use `%s` instead of `%s`.'
SELECTORS = %i[describe context feature example_group it specify example scenario its].freeze
def_node_matcher :include_metadata, <<-PATTERN
(send {(const nil :RSpec) nil} {#{SELECTORS.map(&:inspect).join(' ')}}
!const
...
(hash $...))
PATTERN
def_node_matcher :invalid_metadata?, <<-PATTERN
(pair
(sym $...)
(true))
PATTERN
def on_send(node)
invalid_metadata_matches(node) do |match|
add_offense(node, :expression, format(MSG, good(match), bad(match)))
end
end
def autocorrect(node)
lambda do |corrector|
invalid_metadata_matches(node) do |match|
corrector.replace(match.loc.expression, good(match))
end
end
end
private
def invalid_metadata_matches(node)
include_metadata(node) do |matches|
matches.select(&method(:invalid_metadata?)).each do |match|
yield match
end
end
end
def bad(match)
"#{metadata_key(match)}: true"
end
def good(match)
":#{metadata_key(match)}"
end
def metadata_key(match)
match.children[0].source
end
end
end
end
end
...@@ -9,7 +9,7 @@ module RuboCop ...@@ -9,7 +9,7 @@ module RuboCop
MSG = 'Do not manually set a queue; `ApplicationWorker` sets one automatically.'.freeze MSG = 'Do not manually set a queue; `ApplicationWorker` sets one automatically.'.freeze
def_node_matcher :sidekiq_options?, <<~PATTERN def_node_matcher :sidekiq_options?, <<~PATTERN
(send nil :sidekiq_options $...) (send nil? :sidekiq_options $...)
PATTERN PATTERN
def on_send(node) def on_send(node)
...@@ -19,7 +19,7 @@ module RuboCop ...@@ -19,7 +19,7 @@ module RuboCop
node.arguments.first.each_node(:pair) do |pair| node.arguments.first.each_node(:pair) do |pair|
key_name = pair.key.children[0] key_name = pair.key.children[0]
add_offense(pair, :expression) if key_name == :queue add_offense(pair, location: :expression) if key_name == :queue
end end
end end
end end
......
module RuboCop
module ModelHelpers
# Returns true if the given node originated from the models directory.
def in_model?(node)
path = node.location.expression.source_buffer.name
models_path = File.join(Dir.pwd, 'app', 'models')
path.start_with?(models_path)
end
end
end
require_relative 'cop/active_record_dependent' require_relative 'cop/active_record_dependent'
require_relative 'cop/active_record_serialize'
require_relative 'cop/custom_error_class'
require_relative 'cop/gem_fetcher'
require_relative 'cop/in_batches'
require_relative 'cop/include_sidekiq_worker'
require_relative 'cop/line_break_after_guard_clauses'
require_relative 'cop/polymorphic_associations'
require_relative 'cop/project_path_helper'
require_relative 'cop/redirect_with_status'
require_relative 'cop/gitlab/module_with_instance_variables' require_relative 'cop/gitlab/module_with_instance_variables'
require_relative 'cop/sidekiq_options_queue' require_relative 'cop/include_sidekiq_worker'
require_relative 'cop/migration/add_column' require_relative 'cop/migration/add_column'
require_relative 'cop/migration/add_concurrent_foreign_key' require_relative 'cop/migration/add_concurrent_foreign_key'
require_relative 'cop/migration/add_concurrent_index' require_relative 'cop/migration/add_concurrent_index'
...@@ -25,6 +16,8 @@ require_relative 'cop/migration/safer_boolean_column' ...@@ -25,6 +16,8 @@ require_relative 'cop/migration/safer_boolean_column'
require_relative 'cop/migration/timestamps' require_relative 'cop/migration/timestamps'
require_relative 'cop/migration/update_column_in_batches' require_relative 'cop/migration/update_column_in_batches'
require_relative 'cop/migration/update_large_table' require_relative 'cop/migration/update_large_table'
require_relative 'cop/project_path_helper'
require_relative 'cop/rspec/env_assignment' require_relative 'cop/rspec/env_assignment'
require_relative 'cop/rspec/single_line_hook' require_relative 'cop/rspec/single_line_hook'
require_relative 'cop/rspec/verbose_include_metadata' require_relative 'cop/rspec/verbose_include_metadata'
require_relative 'cop/sidekiq_options_queue'
...@@ -14,6 +14,7 @@ FactoryBot.define do ...@@ -14,6 +14,7 @@ FactoryBot.define do
trait :closed do trait :closed do
state :closed state :closed
closed_at { Time.now }
end end
factory :closed_issue, traits: [:closed] factory :closed_issue, traits: [:closed]
......
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/active_record_dependent'
describe RuboCop::Cop::ActiveRecordDependent do
include CopHelper
subject(:cop) { described_class.new }
context 'inside the app/models directory' do
it 'registers an offense when dependent: is used' do
allow(cop).to receive(:in_model?).and_return(true)
inspect_source(cop, 'belongs_to :foo, dependent: :destroy')
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
end
end
end
context 'outside the app/models directory' do
it 'does nothing' do
allow(cop).to receive(:in_model?).and_return(false)
inspect_source(cop, 'belongs_to :foo, dependent: :destroy')
expect(cop.offenses).to be_empty
end
end
end
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/active_record_serialize'
describe RuboCop::Cop::ActiveRecordSerialize do
include CopHelper
subject(:cop) { described_class.new }
context 'inside the app/models directory' do
it 'registers an offense when serialize is used' do
allow(cop).to receive(:in_model?).and_return(true)
inspect_source(cop, 'serialize :foo')
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
end
end
end
context 'outside the app/models directory' do
it 'does nothing' do
allow(cop).to receive(:in_model?).and_return(false)
inspect_source(cop, 'serialize :foo')
expect(cop.offenses).to be_empty
end
end
end
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/custom_error_class'
describe RuboCop::Cop::CustomErrorClass do
include CopHelper
subject(:cop) { described_class.new }
context 'when a class has a body' do
it 'does nothing' do
inspect_source(cop, 'class CustomError < StandardError; def foo; end; end')
expect(cop.offenses).to be_empty
end
end
context 'when a class has no explicit superclass' do
it 'does nothing' do
inspect_source(cop, 'class CustomError; end')
expect(cop.offenses).to be_empty
end
end
context 'when a class has a superclass that does not end in Error' do
it 'does nothing' do
inspect_source(cop, 'class CustomError < BasicObject; end')
expect(cop.offenses).to be_empty
end
end
context 'when a class is empty and inherits from a class ending in Error' do
context 'when the class is on a single line' do
let(:source) do
<<-SOURCE
module Foo
class CustomError < Bar::Baz::BaseError; end
end
SOURCE
end
let(:expected) do
<<-EXPECTED
module Foo
CustomError = Class.new(Bar::Baz::BaseError)
end
EXPECTED
end
it 'registers an offense' do
expected_highlights = source.split("\n")[1].strip
inspect_source(cop, source)
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([2])
expect(cop.highlights).to contain_exactly(expected_highlights)
end
end
it 'autocorrects to the right version' do
autocorrected = autocorrect_source(cop, source, 'foo/custom_error.rb')
expect(autocorrected).to eq(expected)
end
end
context 'when the class is on multiple lines' do
let(:source) do
<<-SOURCE
module Foo
class CustomError < Bar::Baz::BaseError
end
end
SOURCE
end
let(:expected) do
<<-EXPECTED
module Foo
CustomError = Class.new(Bar::Baz::BaseError)
end
EXPECTED
end
it 'registers an offense' do
expected_highlights = source.split("\n")[1..2].join("\n").strip
inspect_source(cop, source)
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([2])
expect(cop.highlights).to contain_exactly(expected_highlights)
end
end
it 'autocorrects to the right version' do
autocorrected = autocorrect_source(cop, source, 'foo/custom_error.rb')
expect(autocorrected).to eq(expected)
end
end
end
end
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/gem_fetcher'
describe RuboCop::Cop::GemFetcher do
include CopHelper
subject(:cop) { described_class.new }
context 'in Gemfile' do
before do
allow(cop).to receive(:gemfile?).and_return(true)
end
it 'registers an offense when a gem uses `git`' do
inspect_source(cop, 'gem "foo", git: "https://gitlab.com/foo/bar.git"')
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
expect(cop.highlights).to eq(['git: "https://gitlab.com/foo/bar.git"'])
end
end
it 'registers an offense when a gem uses `github`' do
inspect_source(cop, 'gem "foo", github: "foo/bar.git"')
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
expect(cop.highlights).to eq(['github: "foo/bar.git"'])
end
end
end
context 'outside of Gemfile' do
it 'registers no offense' do
inspect_source(cop, 'gem "foo", git: "https://gitlab.com/foo/bar.git"')
expect(cop.offenses.size).to eq(0)
end
end
end
...@@ -12,7 +12,7 @@ describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do ...@@ -12,7 +12,7 @@ describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do
let(:offending_lines) { options[:offending_lines] } let(:offending_lines) { options[:offending_lines] }
it 'registers an offense when instance variable is used in a module' do it 'registers an offense when instance variable is used in a module' do
inspect_source(cop, source) inspect_source(source)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(offending_lines.size) expect(cop.offenses.size).to eq(offending_lines.size)
...@@ -23,7 +23,7 @@ describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do ...@@ -23,7 +23,7 @@ describe RuboCop::Cop::Gitlab::ModuleWithInstanceVariables do
shared_examples('not registering offense') do shared_examples('not registering offense') do
it 'does not register offenses' do it 'does not register offenses' do
inspect_source(cop, source) inspect_source(source)
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
......
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/in_batches'
describe RuboCop::Cop::InBatches do
include CopHelper
subject(:cop) { described_class.new }
it 'registers an offense when in_batches is used' do
inspect_source(cop, 'foo.in_batches do; end')
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
end
end
end
require 'spec_helper' require 'spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support' require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/include_sidekiq_worker' require_relative '../../../rubocop/cop/include_sidekiq_worker'
describe RuboCop::Cop::IncludeSidekiqWorker do describe RuboCop::Cop::IncludeSidekiqWorker do
...@@ -13,7 +15,7 @@ describe RuboCop::Cop::IncludeSidekiqWorker do ...@@ -13,7 +15,7 @@ describe RuboCop::Cop::IncludeSidekiqWorker do
let(:correct_source) { 'include ApplicationWorker' } let(:correct_source) { 'include ApplicationWorker' }
it 'registers an offense ' do it 'registers an offense ' do
inspect_source(cop, source) inspect_source(source)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -23,7 +25,7 @@ describe RuboCop::Cop::IncludeSidekiqWorker do ...@@ -23,7 +25,7 @@ describe RuboCop::Cop::IncludeSidekiqWorker do
end end
it 'autocorrects to the right version' do it 'autocorrects to the right version' do
autocorrected = autocorrect_source(cop, source) autocorrected = autocorrect_source(source)
expect(autocorrected).to eq(correct_source) expect(autocorrected).to eq(correct_source)
end end
......
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/line_break_after_guard_clauses'
describe RuboCop::Cop::LineBreakAfterGuardClauses do
include CopHelper
subject(:cop) { described_class.new }
shared_examples 'examples with guard clause' do |title|
%w[if unless].each do |conditional|
it "flags violation for #{title} #{conditional} without line breaks" do
source = <<~RUBY
#{title} #{conditional} condition
do_stuff
RUBY
inspect_source(cop, source)
expect(cop.offenses.size).to eq(1)
offense = cop.offenses.first
expect(offense.line).to eq(1)
expect(cop.highlights).to eq(["#{title} #{conditional} condition"])
expect(offense.message).to eq('Add a line break after guard clauses')
end
it "doesn't flag violation for #{title} #{conditional} with line break" do
source = <<~RUBY
#{title} #{conditional} condition
do_stuff
RUBY
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
it "doesn't flag violation for #{title} #{conditional} on multiple lines without line break" do
source = <<~RUBY
#{conditional} condition
#{title}
end
do_stuff
RUBY
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by end keyword" do
source = <<~RUBY
def test
#{title} #{conditional} condition
end
RUBY
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by elsif keyword" do
source = <<~RUBY
if model
#{title} #{conditional} condition
elsif
do_something
end
RUBY
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by else keyword" do
source = <<~RUBY
if model
#{title} #{conditional} condition
else
do_something
end
RUBY
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by when keyword" do
source = <<~RUBY
case model
when condition_a
#{title} #{conditional} condition
when condition_b
do_something
end
RUBY
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by rescue keyword" do
source = <<~RUBY
begin
#{title} #{conditional} condition
rescue StandardError
do_something
end
RUBY
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by ensure keyword" do
source = <<~RUBY
def foo
#{title} #{conditional} condition
ensure
do_something
end
RUBY
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
it "doesn't flag violation for #{title} #{conditional} without line breaks when followed by another guard clause" do
source = <<~RUBY
#{title} #{conditional} condition
#{title} #{conditional} condition
do_stuff
RUBY
inspect_source(cop, source)
expect(cop.offenses).to be_empty
end
it "autocorrects #{title} #{conditional} guard clauses without line break" do
source = <<~RUBY
#{title} #{conditional} condition
do_stuff
RUBY
autocorrected = autocorrect_source(cop, source)
expected_source = <<~RUBY
#{title} #{conditional} condition
do_stuff
RUBY
expect(autocorrected).to eql(expected_source)
end
end
end
%w[return fail raise next break throw].each do |example|
it_behaves_like 'examples with guard clause', example
end
end
require 'spec_helper' require 'spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support' require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/migration/add_concurrent_foreign_key' require_relative '../../../../rubocop/cop/migration/add_concurrent_foreign_key'
describe RuboCop::Cop::Migration::AddConcurrentForeignKey do describe RuboCop::Cop::Migration::AddConcurrentForeignKey do
...@@ -10,7 +12,7 @@ describe RuboCop::Cop::Migration::AddConcurrentForeignKey do ...@@ -10,7 +12,7 @@ describe RuboCop::Cop::Migration::AddConcurrentForeignKey do
context 'outside of a migration' do context 'outside of a migration' do
it 'does not register any offenses' do it 'does not register any offenses' do
inspect_source(cop, 'def up; add_foreign_key(:projects, :users, column: :user_id); end') inspect_source('def up; add_foreign_key(:projects, :users, column: :user_id); end')
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
...@@ -22,7 +24,7 @@ describe RuboCop::Cop::Migration::AddConcurrentForeignKey do ...@@ -22,7 +24,7 @@ describe RuboCop::Cop::Migration::AddConcurrentForeignKey do
end end
it 'registers an offense when using add_foreign_key' do it 'registers an offense when using add_foreign_key' do
inspect_source(cop, 'def up; add_foreign_key(:projects, :users, column: :user_id); end') inspect_source('def up; add_foreign_key(:projects, :users, column: :user_id); end')
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
......
...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::AddConcurrentIndex do ...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::AddConcurrentIndex do
end end
it 'registers an offense when add_concurrent_index is used inside a change method' do it 'registers an offense when add_concurrent_index is used inside a change method' do
inspect_source(cop, 'def change; add_concurrent_index :table, :column; end') inspect_source('def change; add_concurrent_index :table, :column; end')
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::AddConcurrentIndex do ...@@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::AddConcurrentIndex do
end end
it 'registers no offense when add_concurrent_index is used inside an up method' do it 'registers no offense when add_concurrent_index is used inside an up method' do
inspect_source(cop, 'def up; add_concurrent_index :table, :column; end') inspect_source('def up; add_concurrent_index :table, :column; end')
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
...@@ -33,7 +33,7 @@ describe RuboCop::Cop::Migration::AddConcurrentIndex do ...@@ -33,7 +33,7 @@ describe RuboCop::Cop::Migration::AddConcurrentIndex do
context 'outside of migration' do context 'outside of migration' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, 'def change; add_concurrent_index :table, :column; end') inspect_source('def change; add_concurrent_index :table, :column; end')
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
...@@ -53,7 +53,7 @@ describe RuboCop::Cop::Migration::AddTimestamps do ...@@ -53,7 +53,7 @@ describe RuboCop::Cop::Migration::AddTimestamps do
end end
it 'registers an offense when the "add_timestamps" method is used' do it 'registers an offense when the "add_timestamps" method is used' do
inspect_source(cop, migration_with_add_timestamps) inspect_source(migration_with_add_timestamps)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -62,7 +62,7 @@ describe RuboCop::Cop::Migration::AddTimestamps do ...@@ -62,7 +62,7 @@ describe RuboCop::Cop::Migration::AddTimestamps do
end end
it 'does not register an offense when the "add_timestamps" method is not used' do it 'does not register an offense when the "add_timestamps" method is not used' do
inspect_source(cop, migration_without_add_timestamps) inspect_source(migration_without_add_timestamps)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
...@@ -70,7 +70,7 @@ describe RuboCop::Cop::Migration::AddTimestamps do ...@@ -70,7 +70,7 @@ describe RuboCop::Cop::Migration::AddTimestamps do
end end
it 'does not register an offense when the "add_timestamps_with_timezone" method is used' do it 'does not register an offense when the "add_timestamps_with_timezone" method is used' do
inspect_source(cop, migration_with_add_timestamps_with_timezone) inspect_source(migration_with_add_timestamps_with_timezone)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
...@@ -80,9 +80,9 @@ describe RuboCop::Cop::Migration::AddTimestamps do ...@@ -80,9 +80,9 @@ describe RuboCop::Cop::Migration::AddTimestamps do
context 'outside of migration' do context 'outside of migration' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, migration_with_add_timestamps) inspect_source(migration_with_add_timestamps)
inspect_source(cop, migration_without_add_timestamps) inspect_source(migration_without_add_timestamps)
inspect_source(cop, migration_with_add_timestamps_with_timezone) inspect_source(migration_with_add_timestamps_with_timezone)
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
...@@ -67,7 +67,7 @@ describe RuboCop::Cop::Migration::Datetime do ...@@ -67,7 +67,7 @@ describe RuboCop::Cop::Migration::Datetime do
end end
it 'registers an offense when the ":datetime" data type is used' do it 'registers an offense when the ":datetime" data type is used' do
inspect_source(cop, migration_with_datetime) inspect_source(migration_with_datetime)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -77,7 +77,7 @@ describe RuboCop::Cop::Migration::Datetime do ...@@ -77,7 +77,7 @@ describe RuboCop::Cop::Migration::Datetime do
end end
it 'registers an offense when the ":timestamp" data type is used' do it 'registers an offense when the ":timestamp" data type is used' do
inspect_source(cop, migration_with_timestamp) inspect_source(migration_with_timestamp)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -87,7 +87,7 @@ describe RuboCop::Cop::Migration::Datetime do ...@@ -87,7 +87,7 @@ describe RuboCop::Cop::Migration::Datetime do
end end
it 'does not register an offense when the ":datetime" data type is not used' do it 'does not register an offense when the ":datetime" data type is not used' do
inspect_source(cop, migration_without_datetime) inspect_source(migration_without_datetime)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
...@@ -95,7 +95,7 @@ describe RuboCop::Cop::Migration::Datetime do ...@@ -95,7 +95,7 @@ describe RuboCop::Cop::Migration::Datetime do
end end
it 'does not register an offense when the ":datetime_with_timezone" data type is used' do it 'does not register an offense when the ":datetime_with_timezone" data type is used' do
inspect_source(cop, migration_with_datetime_with_timezone) inspect_source(migration_with_datetime_with_timezone)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
...@@ -105,10 +105,10 @@ describe RuboCop::Cop::Migration::Datetime do ...@@ -105,10 +105,10 @@ describe RuboCop::Cop::Migration::Datetime do
context 'outside of migration' do context 'outside of migration' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, migration_with_datetime) inspect_source(migration_with_datetime)
inspect_source(cop, migration_with_timestamp) inspect_source(migration_with_timestamp)
inspect_source(cop, migration_without_datetime) inspect_source(migration_without_datetime)
inspect_source(cop, migration_with_datetime_with_timezone) inspect_source(migration_with_datetime_with_timezone)
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::HashIndex do ...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::HashIndex do
end end
it 'registers an offense when creating a hash index' do it 'registers an offense when creating a hash index' do
inspect_source(cop, 'def change; add_index :table, :column, using: :hash; end') inspect_source('def change; add_index :table, :column, using: :hash; end')
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::HashIndex do ...@@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::HashIndex do
end end
it 'registers an offense when creating a concurrent hash index' do it 'registers an offense when creating a concurrent hash index' do
inspect_source(cop, 'def change; add_concurrent_index :table, :column, using: :hash; end') inspect_source('def change; add_concurrent_index :table, :column, using: :hash; end')
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -34,7 +34,7 @@ describe RuboCop::Cop::Migration::HashIndex do ...@@ -34,7 +34,7 @@ describe RuboCop::Cop::Migration::HashIndex do
end end
it 'registers an offense when creating a hash index using t.index' do it 'registers an offense when creating a hash index using t.index' do
inspect_source(cop, 'def change; t.index :table, :column, using: :hash; end') inspect_source('def change; t.index :table, :column, using: :hash; end')
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -45,7 +45,7 @@ describe RuboCop::Cop::Migration::HashIndex do ...@@ -45,7 +45,7 @@ describe RuboCop::Cop::Migration::HashIndex do
context 'outside of migration' do context 'outside of migration' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, 'def change; index :table, :column, using: :hash; end') inspect_source('def change; index :table, :column, using: :hash; end')
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
...@@ -21,7 +21,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do ...@@ -21,7 +21,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do
end end
it 'registers an offense when remove_column is used in the change method' do it 'registers an offense when remove_column is used in the change method' do
inspect_source(cop, source('change')) inspect_source(source('change'))
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -30,7 +30,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do ...@@ -30,7 +30,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do
end end
it 'registers an offense when remove_column is used in the up method' do it 'registers an offense when remove_column is used in the up method' do
inspect_source(cop, source('up')) inspect_source(source('up'))
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -39,7 +39,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do ...@@ -39,7 +39,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do
end end
it 'registers no offense when remove_column is used in the down method' do it 'registers no offense when remove_column is used in the down method' do
inspect_source(cop, source('down')) inspect_source(source('down'))
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
...@@ -52,7 +52,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do ...@@ -52,7 +52,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do
end end
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, source) inspect_source(source)
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
...@@ -60,7 +60,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do ...@@ -60,7 +60,7 @@ describe RuboCop::Cop::Migration::RemoveColumn do
context 'outside of a migration' do context 'outside of a migration' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, source) inspect_source(source)
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::RemoveConcurrentIndex do ...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::RemoveConcurrentIndex do
end end
it 'registers an offense when remove_concurrent_index is used inside a change method' do it 'registers an offense when remove_concurrent_index is used inside a change method' do
inspect_source(cop, 'def change; remove_concurrent_index :table, :column; end') inspect_source('def change; remove_concurrent_index :table, :column; end')
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::RemoveConcurrentIndex do ...@@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::RemoveConcurrentIndex do
end end
it 'registers no offense when remove_concurrent_index is used inside an up method' do it 'registers no offense when remove_concurrent_index is used inside an up method' do
inspect_source(cop, 'def up; remove_concurrent_index :table, :column; end') inspect_source('def up; remove_concurrent_index :table, :column; end')
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
...@@ -33,7 +33,7 @@ describe RuboCop::Cop::Migration::RemoveConcurrentIndex do ...@@ -33,7 +33,7 @@ describe RuboCop::Cop::Migration::RemoveConcurrentIndex do
context 'outside of migration' do context 'outside of migration' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, 'def change; remove_concurrent_index :table, :column; end') inspect_source('def change; remove_concurrent_index :table, :column; end')
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::RemoveIndex do ...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::RemoveIndex do
end end
it 'registers an offense when remove_index is used' do it 'registers an offense when remove_index is used' do
inspect_source(cop, 'def change; remove_index :table, :column; end') inspect_source('def change; remove_index :table, :column; end')
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -27,7 +27,7 @@ describe RuboCop::Cop::Migration::RemoveIndex do ...@@ -27,7 +27,7 @@ describe RuboCop::Cop::Migration::RemoveIndex do
context 'outside of migration' do context 'outside of migration' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, 'def change; remove_index :table, :column; end') inspect_source('def change; remove_index :table, :column; end')
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::ReversibleAddColumnWithDefault do ...@@ -16,7 +16,7 @@ describe RuboCop::Cop::Migration::ReversibleAddColumnWithDefault do
end end
it 'registers an offense when add_column_with_default is used inside a change method' do it 'registers an offense when add_column_with_default is used inside a change method' do
inspect_source(cop, 'def change; add_column_with_default :table, :column, default: false; end') inspect_source('def change; add_column_with_default :table, :column, default: false; end')
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::ReversibleAddColumnWithDefault do ...@@ -25,7 +25,7 @@ describe RuboCop::Cop::Migration::ReversibleAddColumnWithDefault do
end end
it 'registers no offense when add_column_with_default is used inside an up method' do it 'registers no offense when add_column_with_default is used inside an up method' do
inspect_source(cop, 'def up; add_column_with_default :table, :column, default: false; end') inspect_source('def up; add_column_with_default :table, :column, default: false; end')
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
...@@ -33,7 +33,7 @@ describe RuboCop::Cop::Migration::ReversibleAddColumnWithDefault do ...@@ -33,7 +33,7 @@ describe RuboCop::Cop::Migration::ReversibleAddColumnWithDefault do
context 'outside of migration' do context 'outside of migration' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, 'def change; add_column_with_default :table, :column, default: false; end') inspect_source('def change; add_column_with_default :table, :column, default: false; end')
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
...@@ -32,7 +32,7 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do ...@@ -32,7 +32,7 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do
sources_and_offense.each do |source, offense| sources_and_offense.each do |source, offense|
context "given the source \"#{source}\"" do context "given the source \"#{source}\"" do
it "registers the offense matching \"#{offense}\"" do it "registers the offense matching \"#{offense}\"" do
inspect_source(cop, source) inspect_source(source)
aggregate_failures do aggregate_failures do
expect(cop.offenses.first.message).to match(offense) expect(cop.offenses.first.message).to match(offense)
...@@ -49,7 +49,7 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do ...@@ -49,7 +49,7 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do
inoffensive_sources.each do |source| inoffensive_sources.each do |source|
context "given the source \"#{source}\"" do context "given the source \"#{source}\"" do
it "registers no offense" do it "registers no offense" do
inspect_source(cop, source) inspect_source(source)
aggregate_failures do aggregate_failures do
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
...@@ -61,14 +61,14 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do ...@@ -61,14 +61,14 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do
end end
it 'registers no offense for tables not listed in SMALL_TABLES' do it 'registers no offense for tables not listed in SMALL_TABLES' do
inspect_source(cop, "add_column :large_table, :column, :boolean") inspect_source("add_column :large_table, :column, :boolean")
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
it 'registers no offense for non-boolean columns' do it 'registers no offense for non-boolean columns' do
table = described_class::SMALL_TABLES.sample table = described_class::SMALL_TABLES.sample
inspect_source(cop, "add_column :#{table}, :column, :string") inspect_source("add_column :#{table}, :column, :string")
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
...@@ -77,7 +77,7 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do ...@@ -77,7 +77,7 @@ describe RuboCop::Cop::Migration::SaferBooleanColumn do
context 'outside of migration' do context 'outside of migration' do
it 'registers no offense' do it 'registers no offense' do
table = described_class::SMALL_TABLES.sample table = described_class::SMALL_TABLES.sample
inspect_source(cop, "add_column :#{table}, :column, :boolean") inspect_source("add_column :#{table}, :column, :boolean")
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
......
...@@ -62,7 +62,7 @@ describe RuboCop::Cop::Migration::Timestamps do ...@@ -62,7 +62,7 @@ describe RuboCop::Cop::Migration::Timestamps do
end end
it 'registers an offense when the "timestamps" method is used' do it 'registers an offense when the "timestamps" method is used' do
inspect_source(cop, migration_with_timestamps) inspect_source(migration_with_timestamps)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -71,7 +71,7 @@ describe RuboCop::Cop::Migration::Timestamps do ...@@ -71,7 +71,7 @@ describe RuboCop::Cop::Migration::Timestamps do
end end
it 'does not register an offense when the "timestamps" method is not used' do it 'does not register an offense when the "timestamps" method is not used' do
inspect_source(cop, migration_without_timestamps) inspect_source(migration_without_timestamps)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
...@@ -79,7 +79,7 @@ describe RuboCop::Cop::Migration::Timestamps do ...@@ -79,7 +79,7 @@ describe RuboCop::Cop::Migration::Timestamps do
end end
it 'does not register an offense when the "timestamps_with_timezone" method is used' do it 'does not register an offense when the "timestamps_with_timezone" method is used' do
inspect_source(cop, migration_with_timestamps_with_timezone) inspect_source(migration_with_timestamps_with_timezone)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
...@@ -89,9 +89,9 @@ describe RuboCop::Cop::Migration::Timestamps do ...@@ -89,9 +89,9 @@ describe RuboCop::Cop::Migration::Timestamps do
context 'outside of migration' do context 'outside of migration' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, migration_with_timestamps) inspect_source(migration_with_timestamps)
inspect_source(cop, migration_without_timestamps) inspect_source(migration_without_timestamps)
inspect_source(cop, migration_with_timestamps_with_timezone) inspect_source(migration_with_timestamps_with_timezone)
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
require 'spec_helper' require 'spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support' require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/migration/update_column_in_batches' require_relative '../../../../rubocop/cop/migration/update_column_in_batches'
describe RuboCop::Cop::Migration::UpdateColumnInBatches do describe RuboCop::Cop::Migration::UpdateColumnInBatches do
...@@ -25,7 +27,7 @@ describe RuboCop::Cop::Migration::UpdateColumnInBatches do ...@@ -25,7 +27,7 @@ describe RuboCop::Cop::Migration::UpdateColumnInBatches do
context 'outside of a migration' do context 'outside of a migration' do
it 'does not register any offenses' do it 'does not register any offenses' do
inspect_source(cop, migration_code) inspect_source(migration_code)
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
...@@ -49,7 +51,7 @@ describe RuboCop::Cop::Migration::UpdateColumnInBatches do ...@@ -49,7 +51,7 @@ describe RuboCop::Cop::Migration::UpdateColumnInBatches do
let(:relative_spec_filepath) { Pathname.new(spec_filepath).relative_path_from(tmp_rails_root) } let(:relative_spec_filepath) { Pathname.new(spec_filepath).relative_path_from(tmp_rails_root) }
it 'registers an offense when using update_column_in_batches' do it 'registers an offense when using update_column_in_batches' do
inspect_source(cop, migration_code, @migration_file) inspect_source(migration_code, @migration_file)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -72,7 +74,7 @@ describe RuboCop::Cop::Migration::UpdateColumnInBatches do ...@@ -72,7 +74,7 @@ describe RuboCop::Cop::Migration::UpdateColumnInBatches do
end end
it 'does not register any offenses' do it 'does not register any offenses' do
inspect_source(cop, migration_code, @migration_file) inspect_source(migration_code, @migration_file)
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
......
...@@ -18,7 +18,7 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do ...@@ -18,7 +18,7 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do
shared_examples 'large tables' do |update_method| shared_examples 'large tables' do |update_method|
described_class::LARGE_TABLES.each do |table| described_class::LARGE_TABLES.each do |table|
it "registers an offense for the #{table} table" do it "registers an offense for the #{table} table" do
inspect_source(cop, "#{update_method} :#{table}, :column, default: true") inspect_source("#{update_method} :#{table}, :column, default: true")
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -37,7 +37,7 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do ...@@ -37,7 +37,7 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do
end end
it 'registers no offense for non-blacklisted tables' do it 'registers no offense for non-blacklisted tables' do
inspect_source(cop, "add_column_with_default :table, :column, default: true") inspect_source("add_column_with_default :table, :column, default: true")
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
...@@ -45,7 +45,7 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do ...@@ -45,7 +45,7 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do
it 'registers no offense for non-blacklisted methods' do it 'registers no offense for non-blacklisted methods' do
table = described_class::LARGE_TABLES.sample table = described_class::LARGE_TABLES.sample
inspect_source(cop, "some_other_method :#{table}, :column, default: true") inspect_source("some_other_method :#{table}, :column, default: true")
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
...@@ -55,13 +55,13 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do ...@@ -55,13 +55,13 @@ describe RuboCop::Cop::Migration::UpdateLargeTable do
let(:table) { described_class::LARGE_TABLES.sample } let(:table) { described_class::LARGE_TABLES.sample }
it 'registers no offense for add_column_with_default' do it 'registers no offense for add_column_with_default' do
inspect_source(cop, "add_column_with_default :#{table}, :column, default: true") inspect_source("add_column_with_default :#{table}, :column, default: true")
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
it 'registers no offense for update_column_in_batches' do it 'registers no offense for update_column_in_batches' do
inspect_source(cop, "add_column_with_default :#{table}, :column, default: true") inspect_source("add_column_with_default :#{table}, :column, default: true")
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
......
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/polymorphic_associations'
describe RuboCop::Cop::PolymorphicAssociations do
include CopHelper
subject(:cop) { described_class.new }
context 'inside the app/models directory' do
it 'registers an offense when polymorphic: true is used' do
allow(cop).to receive(:in_model?).and_return(true)
inspect_source(cop, 'belongs_to :foo, polymorphic: true')
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
end
end
end
context 'outside the app/models directory' do
it 'does nothing' do
allow(cop).to receive(:in_model?).and_return(false)
inspect_source(cop, 'belongs_to :foo, polymorphic: true')
expect(cop.offenses).to be_empty
end
end
end
...@@ -15,7 +15,7 @@ describe RuboCop::Cop::ProjectPathHelper do ...@@ -15,7 +15,7 @@ describe RuboCop::Cop::ProjectPathHelper do
let(:correct_source) { 'edit_project_issue_path(@issue.project, @issue)' } let(:correct_source) { 'edit_project_issue_path(@issue.project, @issue)' }
it 'registers an offense' do it 'registers an offense' do
inspect_source(cop, source) inspect_source(source)
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -25,7 +25,7 @@ describe RuboCop::Cop::ProjectPathHelper do ...@@ -25,7 +25,7 @@ describe RuboCop::Cop::ProjectPathHelper do
end end
it 'autocorrects to the right version' do it 'autocorrects to the right version' do
autocorrected = autocorrect_source(cop, source) autocorrected = autocorrect_source(source)
expect(autocorrected).to eq(correct_source) expect(autocorrected).to eq(correct_source)
end end
...@@ -33,7 +33,7 @@ describe RuboCop::Cop::ProjectPathHelper do ...@@ -33,7 +33,7 @@ describe RuboCop::Cop::ProjectPathHelper do
context 'when using namespace_project with a different namespace' do context 'when using namespace_project with a different namespace' do
it 'registers no offense' do it 'registers no offense' do
inspect_source(cop, 'edit_namespace_project_issue_path(namespace, project)') inspect_source('edit_namespace_project_issue_path(namespace, project)')
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/redirect_with_status'
describe RuboCop::Cop::RedirectWithStatus do
include CopHelper
subject(:cop) { described_class.new }
let(:controller_fixture_without_status) do
%q(
class UserController < ApplicationController
def show
user = User.find(params[:id])
redirect_to user_path if user.name == 'John Wick'
end
def destroy
user = User.find(params[:id])
if user.destroy
redirect_to root_path
else
render :show
end
end
end
)
end
let(:controller_fixture_with_status) do
%q(
class UserController < ApplicationController
def show
user = User.find(params[:id])
redirect_to user_path if user.name == 'John Wick'
end
def destroy
user = User.find(params[:id])
if user.destroy
redirect_to root_path, status: 302
else
render :show
end
end
end
)
end
context 'in controller' do
before do
allow(cop).to receive(:in_controller?).and_return(true)
end
it 'registers an offense when a "destroy" action uses "redirect_to" without "status"' do
inspect_source(cop, controller_fixture_without_status)
aggregate_failures do
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([12]) # 'redirect_to' is located on 12th line in controller_fixture.
expect(cop.highlights).to eq(['redirect_to'])
end
end
it 'does not register an offense when a "destroy" action uses "redirect_to" with "status"' do
inspect_source(cop, controller_fixture_with_status)
aggregate_failures do
expect(cop.offenses.size).to eq(0)
end
end
end
context 'outside of controller' do
it 'registers no offense' do
inspect_source(cop, controller_fixture_without_status)
inspect_source(cop, controller_fixture_with_status)
expect(cop.offenses.size).to eq(0)
end
end
end
...@@ -17,7 +17,7 @@ describe RuboCop::Cop::RSpec::EnvAssignment do ...@@ -17,7 +17,7 @@ describe RuboCop::Cop::RSpec::EnvAssignment do
shared_examples 'an offensive ENV#[]= call' do |content| shared_examples 'an offensive ENV#[]= call' do |content|
it "registers an offense for `#{content}`" do it "registers an offense for `#{content}`" do
inspect_source(cop, content, source_file) inspect_source(content, source_file)
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1]) expect(cop.offenses.map(&:line)).to eq([1])
...@@ -27,7 +27,7 @@ describe RuboCop::Cop::RSpec::EnvAssignment do ...@@ -27,7 +27,7 @@ describe RuboCop::Cop::RSpec::EnvAssignment do
shared_examples 'an autocorrected ENV#[]= call' do |content, autocorrected_content| shared_examples 'an autocorrected ENV#[]= call' do |content, autocorrected_content|
it "registers an offense for `#{content}` and autocorrects it to `#{autocorrected_content}`" do it "registers an offense for `#{content}` and autocorrects it to `#{autocorrected_content}`" do
autocorrected = autocorrect_source(cop, content, source_file) autocorrected = autocorrect_source(content, source_file)
expect(autocorrected).to eql(autocorrected_content) expect(autocorrected).to eql(autocorrected_content)
end end
...@@ -51,7 +51,7 @@ describe RuboCop::Cop::RSpec::EnvAssignment do ...@@ -51,7 +51,7 @@ describe RuboCop::Cop::RSpec::EnvAssignment do
context 'outside of a spec file' do context 'outside of a spec file' do
it "does not register an offense for `#{OFFENSE_CALL_SINGLE_QUOTES_KEY}` in a non-spec file" do it "does not register an offense for `#{OFFENSE_CALL_SINGLE_QUOTES_KEY}` in a non-spec file" do
inspect_source(cop, OFFENSE_CALL_SINGLE_QUOTES_KEY) inspect_source(OFFENSE_CALL_SINGLE_QUOTES_KEY)
expect(cop.offenses.size).to eq(0) expect(cop.offenses.size).to eq(0)
end end
......
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/rspec/single_line_hook'
describe RuboCop::Cop::RSpec::SingleLineHook do
include CopHelper
subject(:cop) { described_class.new }
# Override `CopHelper#inspect_source` to always appear to be in a spec file,
# so that our RSpec-only cop actually runs
def inspect_source(*args)
super(*args, 'foo_spec.rb')
end
it 'registers an offense for a single-line `before` block' do
inspect_source(cop, 'before { do_something }')
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
expect(cop.highlights).to eq(['before { do_something }'])
end
it 'registers an offense for a single-line `after` block' do
inspect_source(cop, 'after(:each) { undo_something }')
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
expect(cop.highlights).to eq(['after(:each) { undo_something }'])
end
it 'registers an offense for a single-line `around` block' do
inspect_source(cop, 'around { |ex| do_something_else }')
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.map(&:line)).to eq([1])
expect(cop.highlights).to eq(['around { |ex| do_something_else }'])
end
it 'ignores a multi-line `before` block' do
inspect_source(cop, ['before do',
' do_something',
'end'])
expect(cop.offenses.size).to eq(0)
end
it 'ignores a multi-line `after` block' do
inspect_source(cop, ['after(:each) do',
' undo_something',
'end'])
expect(cop.offenses.size).to eq(0)
end
it 'ignores a multi-line `around` block' do
inspect_source(cop, ['around do |ex|',
' do_something_else',
'end'])
expect(cop.offenses.size).to eq(0)
end
end
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/rspec/verbose_include_metadata'
describe RuboCop::Cop::RSpec::VerboseIncludeMetadata do
include CopHelper
subject(:cop) { described_class.new }
let(:source_file) { 'foo_spec.rb' }
# Override `CopHelper#inspect_source` to always appear to be in a spec file,
# so that our RSpec-only cop actually runs
def inspect_source(*args)
super(*args, source_file)
end
shared_examples 'examples with include syntax' do |title|
it "flags violation for #{title} examples that uses verbose include syntax" do
inspect_source(cop, "#{title} 'Test', js: true do; end")
expect(cop.offenses.size).to eq(1)
offense = cop.offenses.first
expect(offense.line).to eq(1)
expect(cop.highlights).to eq(["#{title} 'Test', js: true"])
expect(offense.message).to eq('Use `:js` instead of `js: true`.')
end
it "doesn't flag violation for #{title} examples that uses compact include syntax", :aggregate_failures do
inspect_source(cop, "#{title} 'Test', :js do; end")
expect(cop.offenses).to be_empty
end
it "doesn't flag violation for #{title} examples that uses flag: symbol" do
inspect_source(cop, "#{title} 'Test', flag: :symbol do; end")
expect(cop.offenses).to be_empty
end
it "autocorrects #{title} examples that uses verbose syntax into compact syntax" do
autocorrected = autocorrect_source(cop, "#{title} 'Test', js: true do; end", source_file)
expect(autocorrected).to eql("#{title} 'Test', :js do; end")
end
end
%w(describe context feature example_group it specify example scenario its).each do |example|
it_behaves_like 'examples with include syntax', example
end
end
require 'spec_helper' require 'spec_helper'
require 'rubocop' require 'rubocop'
require 'rubocop/rspec/support' require 'rubocop/rspec/support'
require_relative '../../../rubocop/cop/sidekiq_options_queue' require_relative '../../../rubocop/cop/sidekiq_options_queue'
describe RuboCop::Cop::SidekiqOptionsQueue do describe RuboCop::Cop::SidekiqOptionsQueue do
...@@ -9,7 +11,7 @@ describe RuboCop::Cop::SidekiqOptionsQueue do ...@@ -9,7 +11,7 @@ describe RuboCop::Cop::SidekiqOptionsQueue do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
it 'registers an offense when `sidekiq_options` is used with the `queue` option' do it 'registers an offense when `sidekiq_options` is used with the `queue` option' do
inspect_source(cop, 'sidekiq_options queue: "some_queue"') inspect_source('sidekiq_options queue: "some_queue"')
aggregate_failures do aggregate_failures do
expect(cop.offenses.size).to eq(1) expect(cop.offenses.size).to eq(1)
...@@ -19,7 +21,7 @@ describe RuboCop::Cop::SidekiqOptionsQueue do ...@@ -19,7 +21,7 @@ describe RuboCop::Cop::SidekiqOptionsQueue do
end end
it 'does not register an offense when `sidekiq_options` is used with another option' do it 'does not register an offense when `sidekiq_options` is used with another option' do
inspect_source(cop, 'sidekiq_options retry: false') inspect_source('sidekiq_options retry: false')
expect(cop.offenses).to be_empty expect(cop.offenses).to be_empty
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment