Sha256: dd2e0b48ed235a1d7d9b52149e99a757e1232b6d82f8f2317e2f78b331d63040

Contents?: true

Size: 1.37 KB

Versions: 21

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Minitest
      # Enforces the test to use `refute_path_exists` instead of using `refute(File.exist?(path))`.
      #
      # @example
      #   # bad
      #   refute(File.exist?(path))
      #   refute(File.exist?(path), 'message')
      #
      #   # good
      #   refute_path_exists(path)
      #   refute_path_exists(path, 'message')
      #
      class RefutePathExists < Base
        extend AutoCorrector

        MSG = 'Prefer using `%<good_method>s`.'
        RESTRICT_ON_SEND = %i[refute].freeze

        def_node_matcher :refute_file_exists, <<~PATTERN
          (send nil? :refute
            (send
              (const _ :File) {:exist? :exists?} $_)
              $...)
        PATTERN

        def on_send(node)
          refute_file_exists(node) do |path, failure_message|
            failure_message = failure_message.first
            good_method = build_good_method(path, failure_message)
            message = format(MSG, good_method: good_method)

            add_offense(node, message: message) do |corrector|
              corrector.replace(node, good_method)
            end
          end
        end

        private

        def build_good_method(path, message)
          args = [path.source, message&.source].compact.join(', ')
          "refute_path_exists(#{args})"
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
rubocop-minitest-0.31.1 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.31.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.30.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.29.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.28.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.27.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.26.1 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.26.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.25.1 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.25.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.24.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.23.2 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.23.1 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.23.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.22.2 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.22.1 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.22.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.21.1 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.21.0 lib/rubocop/cop/minitest/refute_path_exists.rb
rubocop-minitest-0.20.1 lib/rubocop/cop/minitest/refute_path_exists.rb