Sha256: e6ac812bf406351d7790ed90f841419464ca2783101bf684502b288be1c61445

Contents?: true

Size: 1.66 KB

Versions: 32

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for opportunities to use `expect { ... }.to output`.
      #
      # @example
      #   # bad
      #   $stdout = StringIO.new
      #   my_app.print_report
      #   $stdout = STDOUT
      #   expect($stdout.string).to eq('Hello World')
      #
      #   # good
      #   expect { my_app.print_report }.to output('Hello World').to_stdout
      #
      class ExpectOutput < Base
        MSG = 'Use `expect { ... }.to output(...).to_%<name>s` ' \
              'instead of mutating $%<name>s.'

        def on_gvasgn(node)
          return unless inside_example_scope?(node)

          # rubocop:disable InternalAffairs/NodeDestructuring
          variable_name, _rhs = *node
          # rubocop:enable InternalAffairs/NodeDestructuring
          name = variable_name[1..]
          return unless name.eql?('stdout') || name.eql?('stderr')

          add_offense(node.loc.name, message: format(MSG, name: name))
        end

        private

        # Detect if we are inside the scope of a single example
        #
        # We want to encourage using `expect { ... }.to output` so
        # we only care about situations where you would replace with
        # an expectation. Therefore, assignments to stderr or stdout
        # within a `before(:all)` or otherwise outside of an example
        # don't matter.
        def inside_example_scope?(node)
          return false if node.nil? || example_group?(node)
          return true if example?(node)
          return RuboCop::RSpec::Hook.new(node).example? if hook?(node)

          inside_example_scope?(node.parent)
        end
      end
    end
  end
end

Version data entries

32 entries across 30 versions & 4 rubygems

Version Path
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.26.1/lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.26.1 lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.26.0 lib/rubocop/cop/rspec/expect_output.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/expect_output.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.25.0 lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.24.1 lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.24.0 lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.23.2 lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.23.1 lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.23.0 lib/rubocop/cop/rspec/expect_output.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/expect_output.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/expect_output.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.13.1/lib/rubocop/cop/rspec/expect_output.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/expect_output.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rspec-2.22.0/lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.22.0 lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.21.0 lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.20.0 lib/rubocop/cop/rspec/expect_output.rb
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/expect_output.rb