Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
--regenerate-todo
option to rebuild the todo file using the pre…
…vious options, or acts as `--auto-gen-config` if there is no valid todo file. Other options that affect `--auto-gen-config` are accepted, and can be passed in to override values in the previous todo. eg. `--regenerate-todo --offense-counts` will supercede `--no-offense-counts`.
- Loading branch information
1 parent
7375130
commit 32ec02c
Showing
8 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
# This class handles collecting the options for regenerating a TODO file. | ||
# @api private | ||
class ConfigRegeneration | ||
AUTO_GENERATED_FILE = RuboCop::CLI::Command::AutoGenerateConfig::AUTO_GENERATED_FILE | ||
COMMAND_REGEX = /(?<=`rubocop )(.*?)(?=`)/.freeze | ||
DEFAULT_OPTIONS = { auto_gen_config: true }.freeze | ||
|
||
# Get options from the comment in the TODO file, and parse them as options | ||
def options | ||
# If there's no existing TODO file, generate one | ||
return DEFAULT_OPTIONS unless todo_exists? | ||
|
||
match = generation_command.match(COMMAND_REGEX) | ||
return DEFAULT_OPTIONS unless match | ||
|
||
options = match[1].split(' ') | ||
Options.new.parse(options).first | ||
end | ||
|
||
private | ||
|
||
def todo_exists? | ||
File.exist?(AUTO_GENERATED_FILE) && !File.empty?(AUTO_GENERATED_FILE) | ||
end | ||
|
||
def generation_command | ||
File.foreach(AUTO_GENERATED_FILE).take(2).last | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::ConfigRegeneration, :isolated_environment do | ||
include FileHelper | ||
|
||
subject(:config_regeneration) { described_class.new } | ||
|
||
describe '#options' do | ||
subject { config_regeneration.options } | ||
|
||
context 'when no todo file exists' do | ||
it { is_expected.to eq(auto_gen_config: true) } | ||
end | ||
|
||
context 'when there is a blank todo file' do | ||
before { create_file('.rubocop_todo.yml', nil) } | ||
|
||
it { is_expected.to eq(auto_gen_config: true) } | ||
end | ||
|
||
context 'when the todo file is malformed' do | ||
before { create_file('.rubocop_todo.yml', 'todo') } | ||
|
||
it { is_expected.to eq(auto_gen_config: true) } | ||
end | ||
|
||
context 'it parses options from the generation comment' do | ||
let(:header) do | ||
<<~HEADER | ||
# This configuration was generated by | ||
# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 100 --no-offense-counts --no-auto-gen-timestamp` | ||
# on 2020-06-12 17:42:47 UTC using RuboCop version 0.85.1. | ||
HEADER | ||
end | ||
|
||
let(:expected_options) do | ||
{ | ||
auto_gen_config: true, | ||
auto_gen_only_exclude: true, | ||
exclude_limit: '100', | ||
offense_counts: false, | ||
auto_gen_timestamp: false | ||
} | ||
end | ||
|
||
before { create_file('.rubocop_todo.yml', header) } | ||
|
||
it { is_expected.to eq(expected_options) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters