rubocop-rspec 1.23.0 → 1.24.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b88289ae95383f02044f1bf61e01950e5637c1b7d5b454a58e025a31f7562c7
4
- data.tar.gz: 066a150e7e974240b6678f29c6104e157a634b753602c9f41b746fa2a3f0733b
3
+ metadata.gz: fa8c9ef29194d2c818e26ab7304672b410755e182a6ff7476f58f933be307102
4
+ data.tar.gz: 6bd4ae51e5e90ea1dcb4217a4f741fa67b4a503dfd350205a6950b0cd2305397
5
5
  SHA512:
6
- metadata.gz: 2cf65077bc9fa485cf1b734918a0279f7925185ecc21196488d24740e5f0d9151672a87521abde59429a5306c7148cd489309c40f6999dedfff0e26972f5e724
7
- data.tar.gz: 1173b7e16b85c33f08df3998b70f8accfd39da5f40b61c4bdf8c988c5a6169a26761395035de4919808078a3c26976e0a9a8d67b1585cde081ee0c35c8634bf2
6
+ metadata.gz: 70867c1789297dc3408df72fd6a0e831afec0238de3250c5446ce3385100f52ee3cc5962b2d3d4263ce4057bda07eddfc3f365921ce1049926030d0c3b21490c
7
+ data.tar.gz: 9d91b68565b10ba04221e4a516359e1bbc942e6e955bc4076937ae0c45fb317780b90c9d502e4e5eff5898b47d3e3fd9324cc00beb7613a33ffcb3f5f64c733e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ## Master (Unreleased)
4
4
 
5
+ ## 1.24.0 (2018-03-06)
6
+
7
+ * Compatibility with RuboCop v0.53.0. ([@bquorning][])
8
+ * The `Rails/HttpStatus` cop is unavailable if the `rack` gem cannot be loaded. ([@bquorning][])
9
+ * Fix `Rails/HttpStatus` not working with custom HTTP status codes. ([@bquorning][])
10
+ * Fix `FactoryBot/StaticAttributeDefinedDynamically` to handle empty block. ([@abrom][])
11
+ * Fix false positive in `FactoryBot/DynamicAttributeDefinedStatically` when a before/after callback has a symbol proc argument. ([@abrom][])
12
+
5
13
  ## 1.23.0 (2018-02-23)
6
14
 
7
15
  * Add `RSpec/Rails/HttpStatus` cop to enforce consistent usage of the status format (numeric or symbolic). ([@anthony-robin][], [@jojos003][])
@@ -24,7 +24,7 @@ module RuboCop
24
24
  [Layout::ExtraSpacing]
25
25
  end
26
26
 
27
- def investigate(_)
27
+ def investigate(_processed_source)
28
28
  token_aligner.offending_tokens.each do |let|
29
29
  add_offense(let, location: :begin)
30
30
  end
@@ -24,7 +24,7 @@ module RuboCop
24
24
  [Layout::ExtraSpacing]
25
25
  end
26
26
 
27
- def investigate(_)
27
+ def investigate(_processed_source)
28
28
  token_aligner.offending_tokens.each do |let|
29
29
  add_offense(let, location: :end)
30
30
  end
@@ -31,9 +31,14 @@ module RuboCop
31
31
  (block (send nil? {:factory :trait} ...) _ { (begin $...) $(send ...) } )
32
32
  PATTERN
33
33
 
34
+ def_node_matcher :callback_with_symbol_proc?, <<-PATTERN
35
+ (send nil? {:before :after} sym (block_pass sym))
36
+ PATTERN
37
+
34
38
  def on_block(node)
35
39
  factory_attributes(node).to_a.flatten.each do |attribute|
36
- next if value_matcher(attribute).to_a.all? { |v| static?(v) }
40
+ next if callback_with_symbol_proc?(attribute) ||
41
+ static?(attribute)
37
42
  add_offense(attribute, location: :expression)
38
43
  end
39
44
  end
@@ -50,8 +55,10 @@ module RuboCop
50
55
 
51
56
  private
52
57
 
53
- def static?(node)
54
- node.recursive_literal? || node.const_type?
58
+ def static?(attribute)
59
+ value_matcher(attribute).to_a.all? do |node|
60
+ node.recursive_literal? || node.const_type?
61
+ end
55
62
  end
56
63
 
57
64
  def value_hash_without_braces?(node)
@@ -58,14 +58,20 @@ module RuboCop
58
58
  private
59
59
 
60
60
  def static?(node)
61
- node.recursive_literal? || node.const_type?
61
+ node.nil? || node.recursive_literal? || node.const_type?
62
62
  end
63
63
 
64
64
  def autocorrected_source(node)
65
- if node.body.hash_type?
66
- "#{node.send_node.source}(#{node.body.source})"
65
+ "#{node.send_node.source}#{autocorrected_attribute(node.body)}"
66
+ end
67
+
68
+ def autocorrected_attribute(body)
69
+ if body.nil?
70
+ ' nil'
71
+ elsif body.hash_type?
72
+ "(#{body.source})"
67
73
  else
68
- "#{node.send_node.source} #{node.body.source}"
74
+ ' ' + body.source
69
75
  end
70
76
  end
71
77
  end
@@ -107,7 +107,7 @@ module RuboCop
107
107
  File.fnmatch?("*#{glob}", processed_source.buffer.name)
108
108
  end
109
109
 
110
- def relevant_rubocop_rspec_file?(_)
110
+ def relevant_rubocop_rspec_file?(_file)
111
111
  true
112
112
  end
113
113
  end
@@ -59,6 +59,7 @@ module RuboCop
59
59
  # end
60
60
  class HookArgument < Cop
61
61
  include ConfigurableEnforcedStyle
62
+ include RangeHelp
62
63
 
63
64
  IMPLICIT_MSG = 'Omit the default `%<scope>p` ' \
64
65
  'argument for RSpec hooks.'.freeze
@@ -27,6 +27,8 @@ module RuboCop
27
27
  # end
28
28
  # end
29
29
  class LeadingSubject < Cop
30
+ include RangeHelp
31
+
30
32
  MSG = 'Declare `subject` above any other `let` declarations.'.freeze
31
33
 
32
34
  def_node_matcher :subject?, '(block $(send nil? :subject ...) args ...)'
@@ -31,6 +31,8 @@ module RuboCop
31
31
  # expect(some).to be
32
32
  # end
33
33
  class LetBeforeExamples < Cop
34
+ include RangeHelp
35
+
34
36
  MSG = 'Move `let` before the examples in the group.'.freeze
35
37
 
36
38
  def_node_matcher :let?, '(block (send nil? {:let :let!} ...) ...)'
@@ -98,7 +98,7 @@ module RuboCop
98
98
 
99
99
  def_node_search :find_contexts, ExampleGroups::ALL.block_pattern
100
100
 
101
- def on_top_level_describe(node, _)
101
+ def on_top_level_describe(node, _args)
102
102
  find_nested_contexts(node.parent) do |context, nesting|
103
103
  add_offense(
104
104
  context.children.first,
@@ -105,7 +105,7 @@ module RuboCop
105
105
  to_predicate_matcher(name) + args + block)
106
106
  end
107
107
 
108
- def true?(to, matcher)
108
+ def true?(to_symbol, matcher)
109
109
  _recv, name, arg = *matcher
110
110
  result = case name
111
111
  when :be, :eq
@@ -115,7 +115,7 @@ module RuboCop
115
115
  when :be_falsey, :be_falsy, :a_falsey_value, :a_falsy_value
116
116
  false
117
117
  end
118
- to == :to ? result : !result
118
+ to_symbol == :to ? result : !result
119
119
  end
120
120
  end
121
121
 
@@ -199,17 +199,17 @@ module RuboCop
199
199
 
200
200
  def autocorrect_explicit_block(node)
201
201
  predicate_matcher_block?(node) do |actual, matcher|
202
- to, = *node
203
- corrector_explicit(to, actual, matcher, to)
202
+ to_node, = *node
203
+ corrector_explicit(to_node, actual, matcher, to_node)
204
204
  end
205
205
  end
206
206
 
207
- def corrector_explicit(to, actual, matcher, block_child)
207
+ def corrector_explicit(to_node, actual, matcher, block_child)
208
208
  lambda do |corrector|
209
- replacement_matcher = replacement_matcher(to)
209
+ replacement_matcher = replacement_matcher(to_node)
210
210
  corrector.replace(matcher.loc.expression, replacement_matcher)
211
211
  move_predicate(corrector, actual, matcher, block_child)
212
- corrector.replace(to.loc.selector, 'to')
212
+ corrector.replace(to_node.loc.selector, 'to')
213
213
  end
214
214
  end
215
215
 
@@ -297,6 +297,7 @@ module RuboCop
297
297
  include ConfigurableEnforcedStyle
298
298
  include InflectedHelper
299
299
  include ExplicitHelper
300
+ include RangeHelp
300
301
 
301
302
  def on_send(node)
302
303
  case style
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rack/utils'
4
+
3
5
  module RuboCop
4
6
  module Cop
5
7
  module RSpec
@@ -29,13 +31,6 @@ module RuboCop
29
31
  # it { is_expected.to have_http_status :error }
30
32
  #
31
33
  class HttpStatus < Cop
32
- begin
33
- require 'rack/utils'
34
- RACK_LOADED = true
35
- rescue LoadError
36
- RACK_LOADED = false
37
- end
38
-
39
34
  include ConfigurableEnforcedStyle
40
35
 
41
36
  def_node_matcher :http_status, <<-PATTERN
@@ -50,10 +45,6 @@ module RuboCop
50
45
  end
51
46
  end
52
47
 
53
- def support_autocorrect?
54
- RACK_LOADED
55
- end
56
-
57
48
  def autocorrect(node)
58
49
  lambda do |corrector|
59
50
  checker = checker_class.new(node)
@@ -76,8 +67,6 @@ module RuboCop
76
67
  class SymbolicStyleChecker
77
68
  MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
78
69
  'to describe HTTP status code.'.freeze
79
- DEFAULT_MSG = 'Prefer `symbolic` over `numeric` ' \
80
- 'to describe HTTP status code.'.freeze
81
70
 
82
71
  attr_reader :node
83
72
  def initialize(node)
@@ -85,15 +74,11 @@ module RuboCop
85
74
  end
86
75
 
87
76
  def offensive?
88
- !node.sym_type?
77
+ !node.sym_type? && !custom_http_status_code?
89
78
  end
90
79
 
91
80
  def message
92
- if RACK_LOADED
93
- format(MSG, prefer: preferred_style, current: number.to_s)
94
- else
95
- DEFAULT_MSG
96
- end
81
+ format(MSG, prefer: preferred_style, current: number.to_s)
97
82
  end
98
83
 
99
84
  def preferred_style
@@ -109,14 +94,17 @@ module RuboCop
109
94
  def number
110
95
  node.source.to_i
111
96
  end
97
+
98
+ def custom_http_status_code?
99
+ node.int_type? &&
100
+ !::Rack::Utils::SYMBOL_TO_STATUS_CODE.value?(node.source.to_i)
101
+ end
112
102
  end
113
103
 
114
104
  # :nodoc:
115
105
  class NumericStyleChecker
116
106
  MSG = 'Prefer `%<prefer>s` over `%<current>s` ' \
117
107
  'to describe HTTP status code.'.freeze
118
- DEFAULT_MSG = 'Prefer `numeric` over `symbolic` ' \
119
- 'to describe HTTP status code.'.freeze
120
108
 
121
109
  WHITELIST_STATUS = %i[error success missing redirect].freeze
122
110
 
@@ -130,11 +118,7 @@ module RuboCop
130
118
  end
131
119
 
132
120
  def message
133
- if RACK_LOADED
134
- format(MSG, prefer: preferred_style, current: symbol.inspect)
135
- else
136
- DEFAULT_MSG
137
- end
121
+ format(MSG, prefer: preferred_style, current: symbol.inspect)
138
122
  end
139
123
 
140
124
  def preferred_style
@@ -4,7 +4,11 @@ require_relative 'rspec/capybara/feature_methods'
4
4
  require_relative 'rspec/factory_bot/dynamic_attribute_defined_statically'
5
5
  require_relative 'rspec/factory_bot/static_attribute_defined_dynamically'
6
6
 
7
- require_relative 'rspec/rails/http_status'
7
+ begin
8
+ require_relative 'rspec/rails/http_status'
9
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
10
+ # Rails/HttpStatus cannot be loaded if rack/utils is unavailable.
11
+ end
8
12
 
9
13
  require_relative 'rspec/align_left_let_brace'
10
14
  require_relative 'rspec/align_right_let_brace'
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module RSpec
5
5
  # Version information for the RSpec RuboCop plugin.
6
6
  module Version
7
- STRING = '1.23.0'.freeze
7
+ STRING = '1.24.0'.freeze
8
8
  end
9
9
  end
10
10
  end
@@ -1,4 +1,4 @@
1
- $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
1
+ $LOAD_PATH.unshift File.expand_path('lib', __dir__)
2
2
  require 'rubocop/rspec/version'
3
3
 
4
4
  Gem::Specification.new do |spec|
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.test_files = spec.files.grep(%r{^spec/})
33
33
  spec.extra_rdoc_files = ['MIT-LICENSE.md', 'README.md']
34
34
 
35
- spec.add_runtime_dependency 'rubocop', '>= 0.52.1'
35
+ spec.add_runtime_dependency 'rubocop', '>= 0.53.0'
36
36
 
37
37
  spec.add_development_dependency 'rack'
38
38
  spec.add_development_dependency 'rake'
@@ -59,6 +59,9 @@ RSpec.describe RuboCop::Cop::RSpec::FactoryBot::DynamicAttributeDefinedStaticall
59
59
  recent_statuses [:published, :draft]
60
60
  meta_tags(like_count: 2)
61
61
  other_tags({ foo: nil })
62
+
63
+ before(:create, &:initialize_something)
64
+ after(:create, &:rebuild_cache)
62
65
  end
63
66
  end
64
67
  RUBY
@@ -26,6 +26,8 @@ RSpec.describe RuboCop::Cop::RSpec::FactoryBot::StaticAttributeDefinedDynamicall
26
26
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use a block to set a static value to an attribute.
27
27
  meta_tags { { foo: 1 } }
28
28
  ^^^^^^^^^^^^^^^^^^^^^^^^ Do not use a block to set a static value to an attribute.
29
+ title {}
30
+ ^^^^^^^^ Do not use a block to set a static value to an attribute.
29
31
  end
30
32
  end
31
33
  RUBY
@@ -58,6 +60,7 @@ RSpec.describe RuboCop::Cop::RSpec::FactoryBot::StaticAttributeDefinedDynamicall
58
60
  tag Tag::MAGIC
59
61
  recent_updates { [Time.current] }
60
62
  meta_tags { { first_like: Time.current } }
63
+ before(:create) { 'foo' }
61
64
  end
62
65
  end
63
66
  RUBY
@@ -78,6 +81,7 @@ RSpec.describe RuboCop::Cop::RSpec::FactoryBot::StaticAttributeDefinedDynamicall
78
81
  comments_count { 0 }
79
82
  type { User::MAGIC }
80
83
  description { nil }
84
+ title {}
81
85
  recent_statuses { [:published, :draft] }
82
86
  meta_tags { { foo: 1 } }
83
87
  end
@@ -90,6 +94,7 @@ RSpec.describe RuboCop::Cop::RSpec::FactoryBot::StaticAttributeDefinedDynamicall
90
94
  comments_count 0
91
95
  type User::MAGIC
92
96
  description nil
97
+ title nil
93
98
  recent_statuses [:published, :draft]
94
99
  meta_tags({ foo: 1 })
95
100
  end
@@ -19,6 +19,12 @@ RSpec.describe RuboCop::Cop::RSpec::Rails::HttpStatus, :config do
19
19
  RUBY
20
20
  end
21
21
 
22
+ it 'does not register an offense when using custom HTTP code' do
23
+ expect_no_offenses(<<-RUBY)
24
+ it { is_expected.to have_http_status 550 }
25
+ RUBY
26
+ end
27
+
22
28
  include_examples 'autocorrect',
23
29
  'it { is_expected.to have_http_status 200 }',
24
30
  'it { is_expected.to have_http_status :ok }'
@@ -36,17 +42,6 @@ RSpec.describe RuboCop::Cop::RSpec::Rails::HttpStatus, :config do
36
42
  'it { is_expected.to have_http_status(404) }',
37
43
  'it { is_expected.to have_http_status(:not_found) }'
38
44
  end
39
-
40
- context 'when rack is not loaded' do
41
- before { stub_const("#{described_class}::RACK_LOADED", false) }
42
-
43
- it 'registers an offense when using numeric value' do
44
- expect_offense(<<-RUBY)
45
- it { is_expected.to have_http_status 200 }
46
- ^^^ Prefer `symbolic` over `numeric` to describe HTTP status code.
47
- RUBY
48
- end
49
- end
50
45
  end
51
46
 
52
47
  context 'when EnforcedStyle is `numeric`' do
@@ -91,16 +86,5 @@ RSpec.describe RuboCop::Cop::RSpec::Rails::HttpStatus, :config do
91
86
  'it { is_expected.to have_http_status(:not_found) }',
92
87
  'it { is_expected.to have_http_status(404) }'
93
88
  end
94
-
95
- context 'when rack is not loaded' do
96
- before { stub_const("#{described_class}::RACK_LOADED", false) }
97
-
98
- it 'registers an offense when using numeric value' do
99
- expect_offense(<<-RUBY)
100
- it { is_expected.to have_http_status :ok }
101
- ^^^ Prefer `numeric` over `symbolic` to describe HTTP status code.
102
- RUBY
103
- end
104
- end
105
89
  end
106
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.23.0
4
+ version: 1.24.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Backus
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-02-23 00:00:00.000000000 Z
13
+ date: 2018-03-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: 0.52.1
21
+ version: 0.53.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: 0.52.1
28
+ version: 0.53.0
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: rack
31
31
  requirement: !ruby/object:Gem::Requirement