Skip to content

Commit

Permalink
Prefer File.read instead of IO.read
Browse files Browse the repository at this point in the history
  • Loading branch information
koic authored and bbatsov committed Apr 15, 2021
1 parent 02c555a commit c65c5bf
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 107 deletions.
8 changes: 4 additions & 4 deletions lib/rubocop/cli/command/auto_genenerate_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run_line_length_cop
@options.delete(:only)
@config_store = ConfigStore.new
# Save the todo configuration of the LineLength cop.
IO.read(AUTO_GENERATED_FILE).lines.drop_while { |line| line.start_with?('#') }.join
File.read(AUTO_GENERATED_FILE).lines.drop_while { |line| line.start_with?('#') }.join
end

def run_all_cops(line_length_contents)
Expand Down Expand Up @@ -115,9 +115,9 @@ def add_inheritance_from_auto_generated_file(config_file)
end

def existing_configuration(config_file)
IO.read(config_file, encoding: Encoding::UTF_8)
.sub(/^inherit_from: *[^\n]+/, '')
.sub(/^inherit_from: *(\n *- *[^\n]+)+/, '')
File.read(config_file, encoding: Encoding::UTF_8)
.sub(/^inherit_from: *[^\n]+/, '')
.sub(/^inherit_from: *(\n *- *[^\n]+)+/, '')
end

def write_config_file(file_name, file_string, rubocop_yml_contents)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/config_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def check_duplication(yaml_code, absolute_path)
# stderr. Care is taken to use the standard OS exit code for a "file not
# found" error.
def read_file(absolute_path)
IO.read(absolute_path, encoding: Encoding::UTF_8)
File.read(absolute_path, encoding: Encoding::UTF_8)
rescue Errno::ENOENT
raise ConfigNotFoundError, "Configuration file not found: #{absolute_path}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/result_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def valid?

def load
puts "Loading cache from #{@path}" if debug?
@cached_data.from_json(IO.read(@path, encoding: Encoding::UTF_8))
@cached_data.from_json(File.read(@path, encoding: Encoding::UTF_8))
end

def save(offenses)
Expand Down
64 changes: 32 additions & 32 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def f
end
RUBY
expect(cli.run(['--auto-gen-config'])).to eq(0)
expect(IO.readlines('.rubocop_todo.yml')
expect(File.readlines('.rubocop_todo.yml')
.drop_while { |line| line.start_with?('#') }.join)
.to eq(<<~YAML)
Expand All @@ -55,7 +55,7 @@ def f
Layout/LineLength:
Max: 138
YAML
expect(IO.read('.rubocop.yml').strip).to eq(exp_dotfile.join($RS))
expect(File.read('.rubocop.yml').strip).to eq(exp_dotfile.join($RS))
$stdout = StringIO.new
expect(RuboCop::CLI.new.run([])).to eq(0)
expect($stderr.string).to eq('')
Expand Down Expand Up @@ -157,7 +157,7 @@ def f
#
# Note that there is no Style/IfUnlessModifier offense registered due
# to the Max:90 setting.
expect(IO.readlines('.rubocop_todo.yml')
expect(File.readlines('.rubocop_todo.yml')
.drop_while { |line| line.start_with?('#') }.join)
.to eq(<<~YAML)
Expand All @@ -176,7 +176,7 @@ def f
Exclude:
- 'example.rb'
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
inherit_from: .rubocop_todo.yml
Layout/LineLength:
Expand Down Expand Up @@ -212,7 +212,7 @@ def f
# The code base max line length is 99, but the setting Enabled: false
# overrides that so no Layout/LineLength:Max setting is generated in
# .rubocop_todo.yml.
expect(IO.readlines('.rubocop_todo.yml')
expect(File.readlines('.rubocop_todo.yml')
.drop_while { |line| line.start_with?('#') }.join)
.to eq(<<~YAML)
Expand All @@ -230,7 +230,7 @@ def f
Exclude:
- 'example.rb'
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
inherit_from: .rubocop_todo.yml
Layout/LineLength:
Expand Down Expand Up @@ -261,7 +261,7 @@ def f
# The code base max line length is 99, but the setting Max:150
# overrides that so no Layout/LineLength:Max setting is generated in
# .rubocop_todo.yml.
expect(IO.readlines('.rubocop_todo.yml')
expect(File.readlines('.rubocop_todo.yml')
.drop_while { |line| line.start_with?('#') }.join)
.to eq(<<~YAML)
Expand All @@ -279,7 +279,7 @@ def f
Exclude:
- 'example.rb'
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
inherit_from: .rubocop_todo.yml
Layout/LineLength:
Expand Down Expand Up @@ -310,7 +310,7 @@ def f
YAML
create_file('.rubocop.yml', ['inherit_from: .rubocop_todo.yml'])
expect(cli.run(['--auto-gen-config'])).to eq(0)
expect(IO.readlines('.rubocop_todo.yml')[8..-1].map(&:chomp))
expect(File.readlines('.rubocop_todo.yml')[8..-1].map(&:chomp))
.to eq(['# Offense count: 1',
'# Cop supports --auto-correct.',
'# Configuration parameters: AllowForAlignment, ' \
Expand Down Expand Up @@ -352,7 +352,7 @@ def f
create_file('.rubocop.yml', ['inherit_from: .rubocop_todo.yml'])
create_file('.rubocop_todo.yml', [''])
expect(cli.run(['--auto-gen-config'])).to eq(0)
expect(IO.readlines('.rubocop_todo.yml')[8..-1].join)
expect(File.readlines('.rubocop_todo.yml')[8..-1].join)
.to eq(['# Offense count: 1',
'# Cop supports --auto-correct.',
'# Configuration parameters: AllowInHeredoc.',
Expand Down Expand Up @@ -402,7 +402,7 @@ def f
YAML
expect(cli.run(%w[--auto-gen-config --config dir/cop_config.yml])).to eq(0)
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = IO.read('.rubocop_todo.yml').lines[8..-1].join
todo_contents = File.read('.rubocop_todo.yml').lines[8..-1].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
# Cop supports --auto-correct.
Expand All @@ -418,7 +418,7 @@ def f
Exclude:
- 'example1.rb'
YAML
expect(IO.read('dir/cop_config.yml')).to eq(<<~YAML)
expect(File.read('dir/cop_config.yml')).to eq(<<~YAML)
inherit_from: .rubocop_todo.yml
Layout/TrailingWhitespace:
Expand All @@ -443,7 +443,7 @@ def fooBar; end
expect($stderr.string).to eq('')
# expect($stdout.string).to include('Created .rubocop_todo.yml.')
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = IO.read('.rubocop_todo.yml').lines[8..-1].join
todo_contents = File.read('.rubocop_todo.yml').lines[8..-1].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
# Configuration parameters: EnforcedStyle, IgnoredPatterns.
Expand All @@ -460,7 +460,7 @@ def fooBar; end
Exclude:
- 'example1.rb'
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
inherit_from: .rubocop_todo.yml
# The following cop does not support auto-correction.
Expand All @@ -485,7 +485,7 @@ def foo; end
expect(cli.run(%w[--auto-gen-config])).to eq(0)
expect($stderr.string).to eq('')
expect(Dir['.*']).to include('.rubocop_todo.yml')
todo_contents = IO.read('.rubocop_todo.yml').lines[8..-1].join
todo_contents = File.read('.rubocop_todo.yml').lines[8..-1].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
# Cop supports --auto-correct.
Expand All @@ -495,7 +495,7 @@ def foo; end
Exclude:
- 'example1.rb'
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
# rubocop config file
--- # YAML document start
inherit_from: .rubocop_todo.yml
Expand Down Expand Up @@ -523,7 +523,7 @@ def foo; end
expect($stderr.string).to eq('')
# expect($stdout.string).to include('Created .rubocop_todo.yml.')
expect(Dir['dir/.*']).to include('dir/.rubocop_todo.yml')
todo_contents = IO.read('dir/.rubocop_todo.yml').lines[8..-1].join
todo_contents = File.read('dir/.rubocop_todo.yml').lines[8..-1].join
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
# Cop supports --auto-correct.
Expand All @@ -539,7 +539,7 @@ def foo; end
Exclude:
- 'example1.rb'
YAML
expect(IO.read('dir/.rubocop.yml')).to eq(<<~YAML)
expect(File.read('dir/.rubocop.yml')).to eq(<<~YAML)
inherit_from:
- .rubocop_todo.yml
- ../.rubocop.yml
Expand All @@ -566,7 +566,7 @@ def foo; end
expect($stdout.string).to include(<<~YAML)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
inherit_from:
- .rubocop_todo.yml
- #{remote_config_url}
Expand All @@ -586,7 +586,7 @@ def foo; end
expect($stdout.string).to include(<<~YAML)
Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`.
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
expect(File.read('.rubocop.yml')).to eq(<<~YAML)
inherit_from:
- .rubocop_todo.yml
- #{remote_config_url}
Expand Down Expand Up @@ -696,7 +696,7 @@ def a; end
'# URISchemes: http, https',
'Layout/LineLength:',
' Max: 130']
actual = IO.read('.rubocop_todo.yml').split($RS)
actual = File.read('.rubocop_todo.yml').split($RS)
expected.each_with_index do |line, ix|
if line.is_a?(String)
expect(actual[ix]).to eq(line)
Expand Down Expand Up @@ -788,7 +788,7 @@ def a; end
'# URISchemes: http, https',
'Layout/LineLength:',
' Max: 130']
actual = IO.read('.rubocop_todo.yml').split($RS)
actual = File.read('.rubocop_todo.yml').split($RS)
expected.each_with_index do |line, ix|
if line.is_a?(String)
expect(actual[ix]).to eq(line)
Expand Down Expand Up @@ -822,7 +822,7 @@ def a; end
- 'example1.rb'
- 'example2.rb'
YAML
actual = IO.read('.rubocop_todo.yml').lines.reject { |line| line =~ /^(#.*)?$/ }
actual = File.read('.rubocop_todo.yml').lines.reject { |line| line =~ /^(#.*)?$/ }
expect(actual.join).to eq(expected)

$stdout = StringIO.new
Expand All @@ -840,7 +840,7 @@ def a; end
Exclude:
- 'example2.rb'
YAML
actual = IO.read('.rubocop_todo.yml').lines.reject { |line| line =~ /^(#.*)?$/ }
actual = File.read('.rubocop_todo.yml').lines.reject { |line| line =~ /^(#.*)?$/ }
expect(actual.join).to eq(expected)

expect(cli.run([])).to eq(0)
Expand Down Expand Up @@ -882,7 +882,7 @@ def a; end
RUBY
expect(cli.run(['--auto-gen-config'])).to eq(0)
expect($stderr.string).to eq('')
actual = IO.read('.rubocop_todo.yml').split($RS)
actual = File.read('.rubocop_todo.yml').split($RS)
date_stamp = actual.slice!(2)
expect(date_stamp).to match(/# on .* using RuboCop version .*/)
expect(actual.join("\n")).to eq(<<~TEXT.chomp)
Expand Down Expand Up @@ -948,7 +948,7 @@ def a; end
'Style/RegexpLiteral:',
' Exclude:',
" - 'example.rb'"]
actual = IO.read('.rubocop_todo.yml').split($RS)
actual = File.read('.rubocop_todo.yml').split($RS)
expected.each_with_index do |line, ix|
if line.is_a?(String)
expect(actual[ix]).to eq(line)
Expand Down Expand Up @@ -1055,7 +1055,7 @@ def a; end
'# URISchemes: http, https',
'Layout/LineLength:',
' Max: 130']
actual = IO.read('.rubocop_todo.yml').split($RS)
actual = File.read('.rubocop_todo.yml').split($RS)
expected.each_with_index do |line, ix|
if line.is_a?(String)
expect(actual[ix]).to eq(line)
Expand All @@ -1079,7 +1079,7 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)

expect(cli.run(['--auto-gen-config', '--auto-gen-only-exclude',
'--exclude-limit', '1'])).to eq(0)
actual = IO.read('.rubocop_todo.yml').split($RS)
actual = File.read('.rubocop_todo.yml').split($RS)

# With --exclude-limit 1 we get MinDigits generated for NumericLiterals
# because there's one offense in each file. The other cops have offenses
Expand Down Expand Up @@ -1109,13 +1109,13 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
'--exclude-limit', '1'])).to eq(0)

command = '# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 1`'
expect(IO.readlines('.rubocop_todo.yml')[1].chomp).to eq(command)
expect(File.readlines('.rubocop_todo.yml')[1].chomp).to eq(command)
end

it 'does not include a timestamp when --no-auto-gen-timestamp is used' do
create_file('example1.rb', ['$!'])
expect(cli.run(['--auto-gen-config', '--no-auto-gen-timestamp'])).to eq(0)
expect(IO.readlines('.rubocop_todo.yml')[2]).to match(/# using RuboCop version .*/)
expect(File.readlines('.rubocop_todo.yml')[2]).to match(/# using RuboCop version .*/)
end

describe 'when different styles appear in different files' do
Expand All @@ -1127,7 +1127,7 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)

it 'disables cop if --exclude-limit is exceeded' do
expect(cli.run(['--auto-gen-config', '--exclude-limit', '1'])).to eq(0)
expect(IO.readlines('.rubocop_todo.yml')[8..-1].join)
expect(File.readlines('.rubocop_todo.yml')[8..-1].join)
.to eq(<<~YAML)
# Offense count: 3
# Cop supports --auto-correct.
Expand All @@ -1148,7 +1148,7 @@ def function(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
it 'generates Exclude list if --exclude-limit is not exceeded' do
create_file('example4.rb', ['$!'])
expect(cli.run(['--auto-gen-config', '--exclude-limit', '10'])).to eq(0)
expect(IO.readlines('.rubocop_todo.yml')[8..-1].join)
expect(File.readlines('.rubocop_todo.yml')[8..-1].join)
.to eq(<<~YAML)
# Offense count: 4
# Cop supports --auto-correct.
Expand Down

0 comments on commit c65c5bf

Please sign in to comment.