Skip to content

Commit e659f46

Browse files
Conditionally print $stdout when invoking run_generator (rails#49448)
* Conditionally print `$stdout` when invoking `run_generator` In an effort to improve the developer experience when debugging generator tests, we add the ability to conditionally print `$stdout` instead of capturing it. This allows for calls to `binding.irb` and `puts` work as expected. ```sh PRINT_STDOUT=true ./bin/test test/generators/actions_test.rb ``` * Update railties/CHANGELOG.md Co-authored-by: Rafael Mendonça França <[email protected]> * Rename environment variable * Update generators guides. * Update guides --------- Co-authored-by: Rafael Mendonça França <[email protected]>
1 parent 71fd543 commit e659f46

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

guides/source/contributing_to_ruby_on_rails.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,12 @@ You can invoke `test_jdbcmysql`, `test_jdbcsqlite3` or `test_jdbcpostgresql` als
495495

496496
To use an external debugger (pry, byebug, etc), install the debugger and use it as normal. If debugger issues occur, run tests in serial by setting `PARALLEL_WORKERS=1` or run a single test with `-n test_long_test_name`.
497497

498+
If running tests against generators you will need to set `RAILS_LOG_TO_STDOUT=true` in order for debugging tools to work.
499+
500+
```sh
501+
RAILS_LOG_TO_STDOUT=true ./bin/test test/generators/actions_test.rb
502+
```
503+
498504
### Warnings
499505

500506
The test suite runs with warnings enabled. Ideally, Ruby on Rails should issue no warnings, but there may be a few, as well as some from third-party libraries. Please ignore (or fix!) them, if any, and submit patches that do not issue new warnings.

guides/source/generators.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,24 @@ In addition to those, Rails also provides many helper methods via
528528
* [`rake`][]
529529
* [`route`][]
530530

531+
Testing Generators
532+
------------------
533+
534+
Rails provides testing helper methods via
535+
[`Rails::Generators::Testing::Behaviour`][], such as:
536+
537+
* [`run_generator`][]
538+
539+
If running tests against generators you will need to set
540+
`RAILS_LOG_TO_STDOUT=true` in order for debugging tools to work.
541+
542+
```sh
543+
RAILS_LOG_TO_STDOUT=true ./bin/test test/generators/actions_test.rb
544+
```
545+
546+
In addition to those, Rails also provides additional assertions via
547+
[`Rails::Generators::Testing::Assertions`][].
548+
531549
[`Rails::Generators::Actions`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html
532550
[`environment`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html#method-i-environment
533551
[`gem`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html#method-i-gem
@@ -541,3 +559,6 @@ In addition to those, Rails also provides many helper methods via
541559
[`rails_command`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html#method-i-rails_command
542560
[`rake`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html#method-i-rake
543561
[`route`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html#method-i-route
562+
[`Rails::Generators::Testing::Behaviour`]: https://api.rubyonrails.org/classes/Rails/Generators/Testing.html
563+
[`run_generator`]: https://api.rubyonrails.org/classes/Rails/Generators/Testing/Behaviour.html#method-i-run_generator
564+
[`Rails::Generators::Testing::Assertions`]: https://api.rubyonrails.org/classes/Rails/Generators/Testing/Assertions.html

railties/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1+
* Conditionally print `$stdout` when invoking `run_generator`
2+
3+
In an effort to improve the developer experience when debugging
4+
generator tests, we add the ability to conditionally print `$stdout`
5+
instead of capturing it.
6+
7+
This allows for calls to `binding.irb` and `puts` work as expected.
8+
9+
```sh
10+
RAILS_LOG_TO_STDOUT=true ./bin/test test/generators/actions_test.rb
11+
```
12+
13+
*Steve Polito*
114

215
Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/railties/CHANGELOG.md) for previous changes.

railties/lib/rails/generators/testing/behavior.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,15 @@ def destination(path)
6565
# You can provide a configuration hash as second argument. This method returns the output
6666
# printed by the generator.
6767
def run_generator(args = default_arguments, config = {})
68-
capture(:stdout) do
69-
args += ["--skip-bundle"] unless args.include?("--no-skip-bundle") || args.include?("--dev")
70-
args += ["--skip-bootsnap"] unless args.include?("--no-skip-bootsnap") || args.include?("--skip-bootsnap")
68+
args += ["--skip-bundle"] unless args.include?("--no-skip-bundle") || args.include?("--dev")
69+
args += ["--skip-bootsnap"] unless args.include?("--no-skip-bootsnap") || args.include?("--skip-bootsnap")
7170

71+
if ENV["RAILS_LOG_TO_STDOUT"] == "true"
7272
generator_class.start(args, config.reverse_merge(destination_root: destination_root))
73+
else
74+
capture(:stdout) do
75+
generator_class.start(args, config.reverse_merge(destination_root: destination_root))
76+
end
7377
end
7478
end
7579

0 commit comments

Comments
 (0)