class RuboCop::Cop::Packaging::BundlerSetupInTests

This cop flags the `require “bundler/setup”` calls if they're made from inside the tests directory.

@example

# bad
require "foo"
require "bundler/setup"

# good
require "foo"

Constants

MSG

This is the message that will be displayed when RuboCop::Packaging finds an offense of using `require “bundler/setup”` in the tests directory.

Public Instance Methods

autocorrect(corrector, node) click to toggle source

Called from on_send, this method helps to autocorrect the offenses flagged by this cop.

# File lib/rubocop/cop/packaging/bundler_setup_in_tests.rb, line 57
def autocorrect(corrector, node)
  range = range_by_whole_lines(node.source_range, include_final_newline: true)

  corrector.remove(range)
end
bundler_setup_in_test_dir?(str) click to toggle source

This method is called from inside `#def_node_matcher`. It flags an offense if the `require “bundler/setup”` call is made from the tests directory.

# File lib/rubocop/cop/packaging/bundler_setup_in_tests.rb, line 66
def bundler_setup_in_test_dir?(str)
  str.eql?("bundler/setup") && falls_in_test_dir?
end
falls_in_test_dir?() click to toggle source

This method determines if the call is made from the tests directory.

# File lib/rubocop/cop/packaging/bundler_setup_in_tests.rb, line 71
def falls_in_test_dir?
  %w[spec specs test tests].any? { |dir| File.expand_path(@file_directory).start_with?("#{root_dir}/#{dir}") }
end
on_new_investigation() click to toggle source

Extended from the Base class. More about the `#on_new_investigation` method can be found here: github.com/rubocop-hq/rubocop/blob/343f62e4555be0470326f47af219689e21c61a37/lib/rubocop/cop/base.rb

Processing of the AST happens here.

# File lib/rubocop/cop/packaging/bundler_setup_in_tests.rb, line 39
def on_new_investigation
  @file_path = processed_source.file_path
  @file_directory = File.dirname(@file_path)
end
on_send(node) click to toggle source

Extended from AST::Traversal. More about the `#on_send` method can be found here: github.com/rubocop-hq/rubocop-ast/blob/08d0f49a47af1e9a30a6d8f67533ba793c843d67/lib/rubocop/ast/traversal.rb#L112

# File lib/rubocop/cop/packaging/bundler_setup_in_tests.rb, line 47
def on_send(node)
  return unless bundler_setup?(node)

  add_offense(node) do |corrector|
    autocorrect(corrector, node)
  end
end