Sha256: fbaed49f23f330cc1add550d55fba711f11f5a5eae0f969011681fbfe4f6b476

Contents?: true

Size: 1.2 KB

Versions: 20

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Checks if test file names start with `test_` or end with `_test.rb`.
      # Files which define classes having names ending with `Test` are checked.
      # Not following this convention may result in tests not being run.
      #
      # @example
      #   # bad
      #   my_class.rb
      #
      #   # good
      #   my_class_test.rb
      #   test_my_class.rb
      #
      class TestFileName < Base
        include MinitestExplorationHelpers

        MSG = 'Test file path should start with `test_` or end with `_test.rb`.'

        def on_new_investigation
          return unless (ast = processed_source.ast)
          return unless test_file?(ast)

          add_global_offense(MSG) unless valid_file_name?
        end

        private

        def test_file?(node)
          return true if node.class_type? && test_class?(node)

          node.each_descendant(:class).any? { |class_node| test_class?(class_node) }
        end

        def valid_file_name?
          basename = File.basename(processed_source.file_path)

          basename.start_with?('test_') || basename.end_with?('_test.rb')
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rubocop-minitest-0.36.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.35.1 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.35.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.34.5 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.34.4 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.34.3 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.34.2 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.34.1 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.34.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.33.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.32.2 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.32.1 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.32.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.31.1 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.29.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.28.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.27.0 lib/rubocop/cop/minitest/test_file_name.rb
rubocop-minitest-0.26.1 lib/rubocop/cop/minitest/test_file_name.rb