Class: RuboCop::Cop::Packaging::BundlerSetupInTests
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Packaging::BundlerSetupInTests
- Extended by:
- AutoCorrector
- Includes:
- RangeHelp, Packaging::LibHelperModule
- Defined in:
- lib/rubocop/cop/packaging/bundler_setup_in_tests.rb
Overview
This cop flags the ‘require “bundler/setup”` calls if they’re made from inside the tests directory.
Constant Summary collapse
- MSG =
This is the message that will be displayed when RuboCop::Packaging finds an offense of using ‘require “bundler/setup”` in the tests directory.
"Using `bundler/setup` in tests is redundant. Consider removing it."
Instance Method Summary collapse
-
#autocorrect(corrector, node) ⇒ Object
Called from on_send, this method helps to autocorrect the offenses flagged by this cop.
-
#bundler_setup_in_test_dir?(str) ⇒ Boolean
This method is called from inside ‘#def_node_matcher`.
-
#falls_in_test_dir? ⇒ Boolean
This method determines if the call is made from the tests directory.
-
#on_new_investigation ⇒ Object
Extended from the Base class.
-
#on_send(node) ⇒ Object
Extended from AST::Traversal.
Methods included from Packaging::LibHelperModule
#inspected_file_falls_in_lib?, #inspected_file_is_gemspec?, #inspected_file_is_not_in_lib_or_gemspec?, #root_dir, #target_falls_in_lib?, #target_falls_in_lib_using_file?
Instance Method Details
#autocorrect(corrector, node) ⇒ Object
Called from on_send, this method helps to autocorrect the offenses flagged by this cop.
57 58 59 60 61 |
# 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) ⇒ Boolean
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.
66 67 68 |
# 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? ⇒ Boolean
This method determines if the call is made from the tests directory.
71 72 73 |
# 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.(@file_directory).start_with?("#{root_dir}/#{dir}") } end |
#on_new_investigation ⇒ Object
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.
39 40 41 42 |
# 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) ⇒ Object
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
47 48 49 50 51 52 53 |
# 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 |