From d3398516dcf6a9268e2dfd7279d15b24e34d26c7 Mon Sep 17 00:00:00 2001 From: Craig Webster Date: Mon, 18 May 2020 14:55:32 +0800 Subject: [PATCH] Fix generating ignore list when one exists already Generating an ignore list is done by running Rubocop over all the files in the project to detect which have errors, and then writing that list of files to `.standard_todo.yml`. When a file was already ignored in `.standard_todo.yml`, Rubocop would be configured to not run cops against it. In that case, no errors were reported, so the file would not be written to `.standard_todo.yml`. This leads to a situation where if a file is ignored, running `standardrb --generated-todo` would remove it from the todo list, and add all other files with errors. Running the same command again would then flip the file list. We fix this by setting the list of ignored files to an empty array before running Rubocop in the `genignore` runner. --- lib/standard/runners/genignore.rb | 9 +++++++++ test/standard/runners/genignore_test.rb | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/standard/runners/genignore.rb b/lib/standard/runners/genignore.rb index 74236749..e6556f49 100644 --- a/lib/standard/runners/genignore.rb +++ b/lib/standard/runners/genignore.rb @@ -13,6 +13,7 @@ def call(config) config.rubocop_options[:formatters] = [["files", temp_file.path]] config.rubocop_options[:format] = "files" config.rubocop_options[:out] = temp_file.path + remove_project_files_from_ignore_list(config) Runners::Rubocop.new.call(config) # Read in the files with errors. It will have the absolute paths @@ -43,6 +44,14 @@ def call(config) temp_file.unlink end end + + # FIXME: This will also remove files which are in + # `Standard::CreatesConfigStore::ConfiguresIgnoredPaths::DEFAULT_IGNORES`. + def remove_project_files_from_ignore_list(config) + options_config = config.rubocop_config_store.instance_variable_get("@options_config") + options_config["AllCops"] ||= [] + options_config["AllCops"]["Exclude"] = [] + end end end end diff --git a/test/standard/runners/genignore_test.rb b/test/standard/runners/genignore_test.rb index 407634fa..49527ef6 100644 --- a/test/standard/runners/genignore_test.rb +++ b/test/standard/runners/genignore_test.rb @@ -30,6 +30,17 @@ def test_todo_generated def create_config(config_path) store = RuboCop::ConfigStore.new.tap do |config_store| config_store.options_config = config_path + + # Simulate a `.standard_todo.yml` that contains an ignore file: + # + # --- + # ignore: + # - errors_one.rb + # + options_config = config_store.instance_variable_get("@options_config") + options_config["AllCops"] ||= [] + options_config["AllCops"]["Exclude"] ||= [] + options_config["AllCops"]["Exclude"] |= [path("errors_one.rb")] end Standard::Config.new(nil, ["."], {}, store)