Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rubocop_todo link injection when YAML doc start sigil exists #9406

Merged
merged 1 commit into from Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_rubocop_todo_link_injection_when.md
@@ -0,0 +1 @@
* [#9406](https://github.com/rubocop-hq/rubocop/pull/9406): Fix rubocop_todo link injection when YAML doc start sigil exists. ([@dduugg][])
9 changes: 5 additions & 4 deletions lib/rubocop/cli/command/auto_genenerate_config.rb
Expand Up @@ -9,6 +9,7 @@ class AutoGenerateConfig < Base
self.command_name = :auto_gen_config

AUTO_GENERATED_FILE = '.rubocop_todo.yml'
YAML_OPTIONAL_DOC_START = /\A---(\s+#|\s*\z)/.freeze

PHASE_1 = 'Phase 1 of 2: run Layout/LineLength cop'
PHASE_2 = 'Phase 2 of 2: run all cops'
Expand Down Expand Up @@ -130,10 +131,10 @@ def existing_configuration(config_file)
end

def write_config_file(file_name, file_string, rubocop_yml_contents)
File.open(file_name, 'w') do |f|
f.write "inherit_from:#{file_string}\n"
f.write "\n#{rubocop_yml_contents}" if /\S/.match?(rubocop_yml_contents)
end
lines = /\S/.match?(rubocop_yml_contents) ? rubocop_yml_contents.split("\n", -1) : []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity and simplicity:

Suggested change
lines = /\S/.match?(rubocop_yml_contents) ? rubocop_yml_contents.split("\n", -1) : []
lines = rubocop_yml_contents.blank? ? [] : rubocop_yml_contents.split("\n", -1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion @dvandersluis. Unfortunately I see test failures locally with this suggestion (via rspec ./spec/rubocop/cli/cli_auto_gen_config_spec.rb). I think it's due to the nonequivalence when rubocop_yml_contents is nil. (Specifically, rubocop's blank? monkey-patch differs from rails by being undefined on nil.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yeah looks like that's because rubocop_yml_contents can be nil if the file doesn't exist because it's not otherwise initialized in add_inheritance_from_auto_generated_file 🤔

doc_start_index = lines.index { |line| YAML_OPTIONAL_DOC_START.match?(line) } || -1
lines.insert(doc_start_index + 1, "inherit_from:#{file_string}\n")
File.open(file_name, 'w') { |f| f.write lines.join("\n") }
end
end
end
Expand Down
37 changes: 37 additions & 0 deletions spec/rubocop/cli/cli_auto_gen_config_spec.rb
Expand Up @@ -482,6 +482,43 @@ def fooBar; end
end
end

context 'when existing config file has a YAML document start header' do
it 'inserts `inherit_from` key after hearder' do
create_file('example1.rb', <<~RUBY)
def foo; end
RUBY
create_file('.rubocop.yml', <<~YAML)
# rubocop config file
--- # YAML document start
# The following cop does not support auto-correction.
Naming/MethodName:
Enabled: true
YAML
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
expect(todo_contents).to eq(<<~YAML)
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Exclude:
- 'example1.rb'
YAML
expect(IO.read('.rubocop.yml')).to eq(<<~YAML)
# rubocop config file
--- # YAML document start
inherit_from: .rubocop_todo.yml

# The following cop does not support auto-correction.
Naming/MethodName:
Enabled: true
YAML
end
end

context 'when working in a subdirectory' do
it 'can generate a todo list' do
create_file('dir/example1.rb', ['$x = 0 ',
Expand Down