Fix an error for `apply_rubocop_autocorrect_after_generate!` with `--pretend`

## Motivation / Background

An issue was identified where an error occurs when executing `apply_rubocop_autocorrect_after_generate!` with `--pretend` option,
according to feedback from https://github.com/rubocop/rubocop-rails/pull/1263.

## Details

This PR fixes the following error when executing `apply_rubocop_autocorrect_after_generate!` with `--pretend` option:

```console
$ bin/rails g migration create_users -p
```

### Before

An `Errno::ENOENT` error occurs:

```console
invoke  active_record
create    db/migrate/20240329060627_create_users.rb
/Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-8e46af8c9396/railties/lib/rails/configuration.rb:138:in
`system': No such file or directory - bin/rubocop (Errno::ENOENT)
```

### After

No errors.
This commit is contained in:
Koichi ITO 2024-03-29 14:58:27 +09:00
parent 8e46af8c93
commit 33c63f0559
2 changed files with 11 additions and 1 deletions

View File

@ -133,7 +133,7 @@ module Rails
def apply_rubocop_autocorrect_after_generate!
after_generate do |files|
parsable_files = files.filter { |file| file.end_with?(".rb") }
parsable_files = files.filter { |file| File.exist?(file) && file.end_with?(".rb") }
unless parsable_files.empty?
system("bin/rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
end

View File

@ -265,6 +265,16 @@ module ApplicationTests
output = rails("generate", "model", "post", "title:string", "body:string")
assert_match(/3 files inspected, no offenses detected/, output)
end
test "generators with apply_rubocop_autocorrect_after_generate! and pretend" do
with_config do |c|
c.generators.apply_rubocop_autocorrect_after_generate!
end
assert_nothing_raised do
rails("generate", "model", "post", "title:string", "body:string", "--pretend")
end
end
end
end
end