Skip to content

Commit 33c63f0

Browse files
committed
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 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.
1 parent 8e46af8 commit 33c63f0

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

railties/lib/rails/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def after_generate(&block)
133133

134134
def apply_rubocop_autocorrect_after_generate!
135135
after_generate do |files|
136-
parsable_files = files.filter { |file| file.end_with?(".rb") }
136+
parsable_files = files.filter { |file| File.exist?(file) && file.end_with?(".rb") }
137137
unless parsable_files.empty?
138138
system("bin/rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
139139
end

railties/test/application/generators_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,16 @@ def check_expected
265265
output = rails("generate", "model", "post", "title:string", "body:string")
266266
assert_match(/3 files inspected, no offenses detected/, output)
267267
end
268+
269+
test "generators with apply_rubocop_autocorrect_after_generate! and pretend" do
270+
with_config do |c|
271+
c.generators.apply_rubocop_autocorrect_after_generate!
272+
end
273+
274+
assert_nothing_raised do
275+
rails("generate", "model", "post", "title:string", "body:string", "--pretend")
276+
end
277+
end
268278
end
269279
end
270280
end

0 commit comments

Comments
 (0)