Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop mistakens plain ruby for rspec DSL #1184

Closed
githubuseracct opened this issue Aug 21, 2021 · 1 comment
Closed

Rubocop mistakens plain ruby for rspec DSL #1184

githubuseracct opened this issue Aug 21, 2021 · 1 comment
Assignees
Labels

Comments

@githubuseracct
Copy link

PDK v2.2.0 using rubocop v1.6.1 (pdk enforces the rubocop version, so sorry if this has been fixed already).

Expected behavior

Rubocop would distinguish between plain ruby and rspec DSL.

Actual behavior

Given the line

hosts.each { |_x| install_puppet } unless ENV['BEAKER_provision'] == 'no'

I get

hosts.each { |_x| install_puppet } unless ENV['BEAKER_provision'] == 'no'
^^^^^^^^^^

Steps to reproduce the problem

$ pdk new module whatever --skip-interview
$ cd whatever
$ echo "hosts.each { |_x| install_puppet } unless ENV['BEAKER_provision'] == 'no'" >spec/spec_helper_local.rb
$ pdk bundle exec rubocop --debug spec/spec_helper_local.rb 
pdk (INFO): Using Ruby 2.7.3
pdk (INFO): Using Puppet 7.9.0
warning: parser/current is loading parser/ruby27, which recognizes
warning: 2.7.4-compliant syntax, but you are running 2.7.3.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
For /home/me/whatever: configuration from /home/me/whatever/.rubocop.yml
configuration from /opt/puppetlabs/pdk/share/cache/ruby/2.7.0/gems/rubocop-performance-1.9.1/config/default.yml
configuration from /opt/puppetlabs/pdk/share/cache/ruby/2.7.0/gems/rubocop-performance-1.9.1/config/default.yml
Default configuration from /opt/puppetlabs/pdk/share/cache/ruby/2.7.0/gems/rubocop-1.6.1/config/default.yml
configuration from /opt/puppetlabs/pdk/share/cache/ruby/2.7.0/gems/rubocop-rspec-2.0.1/config/default.yml
configuration from /opt/puppetlabs/pdk/share/cache/ruby/2.7.0/gems/rubocop-rspec-2.0.1/config/default.yml
Inspecting 1 file
Scanning /home/me/whatever/spec/spec_helper_local.rb
C

Offenses:

spec/spec_helper_local.rb:1:1: C: RSpec/IteratedExpectation: Prefer using the all matcher instead of iterating over an array.
hosts.each { |_x| install_puppet } unless ENV['BEAKER_provision'] == 'no'
^^^^^^^^^^

RuboCop version

$ pdk bundle exec rubocop -V
pdk (INFO): Using Ruby 2.7.3
pdk (INFO): Using Puppet 7.9.0
warning: parser/current is loading parser/ruby27, which recognizes
warning: 2.7.4-compliant syntax, but you are running 2.7.3.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1.6.1 (using Parser 3.0.2.0, rubocop-ast 1.10.0, running on ruby 2.7.3 x86_64-linux)
  - rubocop-performance 1.9.1
  - rubocop-rspec 2.0.1
@koic koic transferred this issue from rubocop/rubocop Aug 21, 2021
@pirj
Copy link
Member

pirj commented Aug 21, 2021

Tracked it down to .all?:

rubocop-rspec/lib/rubocop/cop/rspec/iterated_expectation.rb:39 RuboCop::Cop::RSpec::IteratedExpectation#on_block:

    36: def on_block(node)
    37:   each?(node) do |arg, body|
    38:     require 'pry'; binding.pry
 => 39:     if single_expectation?(body, arg) || only_expectations?(body, arg)
    40:       add_offense(node.send_node)

here,

only_expectations?(body, arg)
=> true

but

body.child_nodes.count # => 0

only_expectations? is defined as:

          def only_expectations?(body, arg)
            body.each_child_node.all? { |child| expectation?(child, arg) }

which is incorrect for the case when there are no children.
As in:

[].all? { |x| x == 100 } # => true

I'd suggest adding a check:

def only_expectations?(body, arg)
+ body.each_child_node.any? &&
  body.each_child_node.all? { |child| expectation?(child, arg) }
end

Would you like to send a PR, @githubuseracct ?
Your example is quite good to be added as a spec as is.

@Darhazer Darhazer added the bug label Aug 24, 2021
@Darhazer Darhazer self-assigned this Aug 24, 2021
Darhazer added a commit that referenced this issue Aug 25, 2021
…positive

[Fix #1184] false positive when block do not have child nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants