Skip to content

Commit

Permalink
Support searching for rubocop/config.yml in compliance with dot-config
Browse files Browse the repository at this point in the history
Follow rubocop#12700 (comment)

This PR supports searching for `rubocop/config.yml` in compliance with dot-config.

This PR adds `project_root/.config/rubocop/config.yml` to the list of paths to search,
matching XDG path format of the already targeted `~/.config/rubocop/config.yml`.

However, support for `~/.config/rubocop.yml` is not included in this PR,
as the outcome of inquiry dot-config/dot-config.github.io#17
regarding it is still pending.
  • Loading branch information
koic authored and bbatsov committed Feb 19, 2024
1 parent 4478266 commit 2b013e1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion changelog/new_support_dot_config.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* [#12699](https://github.com/rubocop/rubocop/issues/12699): Support searching for `.rubocop.yml` in compliance with dot-config. ([@koic][])
* [#12699](https://github.com/rubocop/rubocop/issues/12699): Support searching for `.rubocop.yml` and `rubocop/config.yml` in compliance with dot-config. ([@koic][])
3 changes: 2 additions & 1 deletion docs/modules/ROOT/pages/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ and the user's global config locations. The user's global config locations consi
dotfile or a config file inside the https://specifications.freedesktop.org/basedir-spec/latest/index.html[XDG Base Directory
specification].

* `.config/.rubocop.yml` of the project root
* `.config/.rubocop.yml` or `.config/rubocop/config.yml` at the project root
* `~/.rubocop.yml`
* `$XDG_CONFIG_HOME/rubocop/config.yml` (expands to `~/.config/rubocop/config.yml`
if `$XDG_CONFIG_HOME` is not set)
Expand All @@ -49,6 +49,7 @@ files:
* `/path/to/project/lib/.rubocop.yml`
* `/path/to/project/.rubocop.yml`
* `/path/to/project/.config/.rubocop.yml`
* `/path/to/project/.config/rubocop/config.yml`
* `/.rubocop.yml`
* `~/.rubocop.yml`
* `~/.config/rubocop/config.yml`
Expand Down
6 changes: 4 additions & 2 deletions lib/rubocop/config_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ def find_project_dotfile(target_dir)
def find_project_root_dot_config
return unless project_root

file = File.join(project_root, '.config', DOTFILE)
dotfile = File.join(project_root, '.config', DOTFILE)
return dotfile if File.exist?(dotfile)

file if File.exist?(file)
xdg_config = File.join(project_root, '.config', 'rubocop', XDG_CONFIG)
xdg_config if File.exist?(xdg_config)
end

def find_user_dotfile
Expand Down
43 changes: 39 additions & 4 deletions spec/rubocop/config_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,64 @@

before { create_empty_file('dir/example.rb') }

context 'but a config file exists in .config directory of the project root' do
context 'but a config file exists in .config/.rubocop.yml of the project root' do
before do
create_empty_file('Gemfile')
create_empty_file('.config/.rubocop.yml')
end

it 'returns the path to the file in home directory' do
it 'returns the path to the file in .config directory' do
expect(configuration_file_for).to end_with('.config/.rubocop.yml')
end
end

context 'but a config file exists in both .config of the project root and home directories' do
context 'but a config file exists in both .config/.rubocop.yml of the project root and home directory' do
before do
create_empty_file('Gemfile')
create_empty_file('.config/.rubocop.yml')
create_empty_file('~/.rubocop.yml')
end

it 'returns the path to the file in home directory' do
it 'returns the path to the file in .config directory' do
expect(configuration_file_for).to end_with('.config/.rubocop.yml')
end
end

context 'but a config file exists in .config/rubocop/config.yml of the project root' do
before do
create_empty_file('Gemfile')
create_empty_file('.config/rubocop/config.yml')
end

it 'returns the path to the file in .config/rubocop directory' do
expect(configuration_file_for).to end_with('.config/rubocop/config.yml')
end
end

context 'but a config file exists in both .config/.rubocop.yml and .config/rubocop/config.yml of the project root' do
before do
create_empty_file('Gemfile')
create_empty_file('.config/.rubocop.yml')
create_empty_file('.config/rubocop/config.yml')
end

it 'returns the path to the file in .config directory' do
expect(configuration_file_for).to end_with('.config/.rubocop.yml')
end
end

context 'but a config file exists in both .config//rubocop/config.yml of the project root and home directory' do
before do
create_empty_file('Gemfile')
create_empty_file('.config/rubocop/config.yml')
create_empty_file('~/.rubocop.yml')
end

it 'returns the path to the file in .config/rubocop directory' do
expect(configuration_file_for).to end_with('.config/rubocop/config.yml')
end
end

context 'but a config file exists in home directory' do
before { create_empty_file('~/.rubocop.yml') }

Expand Down

0 comments on commit 2b013e1

Please sign in to comment.