-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Check that `Include` and `Exclude` paths exist - This adds a `PathCleanup` class that removes any paths in the `Include` and `Exclude` lists that don't exist (relative to the `.rubocop.yml`, ignoring paths with globs). - This is useful for when you have a `.rubocop.yml` file that has been around for a while and has lots of paths that people forget to clean up when they delete the files, so doing so manually would be tedious. * More comprehensive `path_cleanup` test * Remove `Symbol` from `PERMITTED_CLASSES` - This was needed in an earlier iteration of the code I wrote, but it seems to work without it now on my test repos. * Rename method for clarity * The YAML parsing makes string checks redundant * Use `filter_map` instead of `each_with_index` and an intermediate array - This is very nice!
- Loading branch information
Showing
9 changed files
with
124 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ruboclean | ||
# Cleans up any `Include` or `Exclude` paths that don't exist. | ||
# The `Include` and `Exclude` paths are relative to the directory | ||
# where the `.rubocop.yml` file is located. If a path includes a | ||
# wildcard, it's assumed to be valid. | ||
class PathCleanup | ||
def initialize(config_hash) | ||
@config_hash = config_hash | ||
end | ||
|
||
def cleanup | ||
%i[Include Exclude].each do |kind| | ||
select_stanzas(kind).each do |cop| | ||
paths = @config_hash.dig(cop, kind) | ||
paths&.select! { |path| regexp_or_wildcard?(path) || File.exist?(path) } | ||
end | ||
end | ||
|
||
@config_hash | ||
end | ||
|
||
def select_stanzas(kind) | ||
@config_hash.filter_map do |cop, value| | ||
next unless value.is_a?(Hash) | ||
|
||
cop if value.key?(kind) | ||
end | ||
end | ||
|
||
def regexp_or_wildcard?(path) | ||
path.is_a?(Regexp) || path.include?("*") | ||
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
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,2 @@ | ||
# frozen_string_literal: true | ||
# test for path cleanup |
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,2 @@ | ||
# frozen_string_literal: true | ||
# test for path cleanup |
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,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "ruboclean/path_cleanup" | ||
|
||
module Ruboclean | ||
class PathCleanupTest < BaseTest | ||
def test_path_cleanup_includes | ||
input = { SomeCop: { Include: ["some_other_file.rb", "test/fixtures/file_exists.rb", "lib/**/*.rb"] } } | ||
output = { SomeCop: { Include: ["test/fixtures/file_exists.rb", "lib/**/*.rb"] } } | ||
|
||
assert_equal output, Ruboclean::PathCleanup.new(input).cleanup | ||
end | ||
|
||
def test_path_cleanup_excludes | ||
input = { SomeCop: { Exclude: ["config/**/*.rb", "test/fixtures/other_file_exists.rb", | ||
"some_other_non_existent_file.rb", "test/fixtures/not_here.rb"] } } | ||
output = { SomeCop: { Exclude: ["config/**/*.rb", "test/fixtures/other_file_exists.rb"] } } | ||
|
||
assert_equal output, Ruboclean::PathCleanup.new(input).cleanup | ||
end | ||
|
||
# rubocop:disable Metrics/MethodLength | ||
def test_path_cleanup_include_and_exclude | ||
input = { | ||
SomeCop: { | ||
Include: ["lib/**/*.rb", "test/fixtures/not_here.rb"], | ||
Exclude: ["config/**/*.rb", "test/fixtures/other_file_exists.rb", "some_other_non_existent_file.rb"], | ||
EnforcedStyle: "something" | ||
}, | ||
SomeOtherCop: { | ||
EnforcedStyle: "something" | ||
} | ||
} | ||
output = { | ||
SomeCop: { | ||
Include: ["lib/**/*.rb"], | ||
Exclude: ["config/**/*.rb", "test/fixtures/other_file_exists.rb"], | ||
EnforcedStyle: "something" | ||
}, | ||
SomeOtherCop: { | ||
EnforcedStyle: "something" | ||
} | ||
} | ||
|
||
assert_equal output, Ruboclean::PathCleanup.new(input).cleanup | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
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