1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
# frozen_string_literal: true
require 'fileutils'
RSpec.describe 'RuboCop::CLI --autocorrect', :isolated_environment do # rubocop:disable RSpec/DescribeClass
subject(:cli) { RuboCop::CLI.new }
include_context 'mock console output'
# Restore injected config as well
around do |ex|
previous_config = RuboCop::ConfigLoader.default_configuration
RuboCop::ConfigLoader.default_configuration = nil
RuboCop::ConfigLoader.default_configuration.for_all_cops['SuggestExtensions'] = false
RuboCop::ConfigLoader.default_configuration.for_all_cops['NewCops'] = 'disable'
ex.call
ensure
RuboCop::ConfigLoader.default_configuration = previous_config
end
it 'corrects `Performance/ConstantRegexp` with `Performance/RegexpMatch`' do
create_file('.rubocop.yml', <<~YAML)
Performance/ConstantRegexp:
Enabled: true
Performance/RegexpMatch:
Enabled: true
YAML
source = <<~RUBY
foo if bar =~ /\#{CONSTANT}/
RUBY
create_file('example.rb', source)
expect(cli.run(['--autocorrect', '--only', 'Performance/ConstantRegexp,Performance/RegexpMatch'])).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
foo if /\#{CONSTANT}/o.match?(bar)
RUBY
end
it 'corrects `Performance/BlockGivenWithExplicitBlock` with `Lint/UnusedMethodArgument`' do
source = <<~RUBY
def foo(&block)
block_given?
end
RUBY
create_file('example.rb', source)
expect(
cli.run(['--autocorrect', '--only', 'Performance/BlockGivenWithExplicitBlock,Lint/UnusedMethodArgument'])
).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
def foo()
block_given?
end
RUBY
end
it 'corrects `Performance/BlockGivenWithExplicitBlock` with `Naming/BlockForwarding`' do
source = <<~RUBY
def foo(&block)
block_given?
bar(&block)
end
RUBY
create_file('example.rb', source)
expect(
cli.run(['--autocorrect', '--only', 'Performance/BlockGivenWithExplicitBlock,Naming/BlockForwarding'])
).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
def foo(&block)
block
bar(&block)
end
RUBY
end
private
def create_file(file_path, content)
file_path = File.expand_path(file_path)
dir_path = File.dirname(file_path)
FileUtils.mkdir_p(dir_path)
File.write(file_path, content)
file_path
end
end
|