Skip to content

Commit

Permalink
Fix an error for apply_rubocop_autocorrect_after_generate! with `--…
Browse files Browse the repository at this point in the history
…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 rubocop/rubocop-rails#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.
  • Loading branch information
koic committed Mar 29, 2024
1 parent 8e46af8 commit 5d55fc2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion railties/lib/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def after_generate(&block)

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
Expand Down
10 changes: 10 additions & 0 deletions railties/test/application/generators_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ def check_expected
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

0 comments on commit 5d55fc2

Please sign in to comment.