From e659f46da1822b0fd9cd9c040fa3772e999d21f1 Mon Sep 17 00:00:00 2001 From: Steve Polito Date: Tue, 3 Oct 2023 07:01:16 -0400 Subject: [PATCH] Conditionally print `$stdout` when invoking `run_generator` (#49448) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * Rename environment variable * Update generators guides. * Update guides --------- Co-authored-by: Rafael Mendonça França --- .../source/contributing_to_ruby_on_rails.md | 6 ++++++ guides/source/generators.md | 21 +++++++++++++++++++ railties/CHANGELOG.md | 13 ++++++++++++ .../lib/rails/generators/testing/behavior.rb | 10 ++++++--- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/guides/source/contributing_to_ruby_on_rails.md b/guides/source/contributing_to_ruby_on_rails.md index 8bb21e603c7..ff2559130e6 100644 --- a/guides/source/contributing_to_ruby_on_rails.md +++ b/guides/source/contributing_to_ruby_on_rails.md @@ -495,6 +495,12 @@ You can invoke `test_jdbcmysql`, `test_jdbcsqlite3` or `test_jdbcpostgresql` als 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`. +If running tests against generators you will need to set `RAILS_LOG_TO_STDOUT=true` in order for debugging tools to work. + +```sh +RAILS_LOG_TO_STDOUT=true ./bin/test test/generators/actions_test.rb +``` + ### Warnings 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. diff --git a/guides/source/generators.md b/guides/source/generators.md index d05a8820857..ccf2f8767f8 100644 --- a/guides/source/generators.md +++ b/guides/source/generators.md @@ -528,6 +528,24 @@ In addition to those, Rails also provides many helper methods via * [`rake`][] * [`route`][] +Testing Generators +------------------ + +Rails provides testing helper methods via +[`Rails::Generators::Testing::Behaviour`][], such as: + +* [`run_generator`][] + +If running tests against generators you will need to set +`RAILS_LOG_TO_STDOUT=true` in order for debugging tools to work. + +```sh +RAILS_LOG_TO_STDOUT=true ./bin/test test/generators/actions_test.rb +``` + +In addition to those, Rails also provides additional assertions via +[`Rails::Generators::Testing::Assertions`][]. + [`Rails::Generators::Actions`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html [`environment`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html#method-i-environment [`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 [`rails_command`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html#method-i-rails_command [`rake`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html#method-i-rake [`route`]: https://api.rubyonrails.org/classes/Rails/Generators/Actions.html#method-i-route +[`Rails::Generators::Testing::Behaviour`]: https://api.rubyonrails.org/classes/Rails/Generators/Testing.html +[`run_generator`]: https://api.rubyonrails.org/classes/Rails/Generators/Testing/Behaviour.html#method-i-run_generator +[`Rails::Generators::Testing::Assertions`]: https://api.rubyonrails.org/classes/Rails/Generators/Testing/Assertions.html diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 51387a52b16..af8f6fffa64 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,2 +1,15 @@ +* 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 + RAILS_LOG_TO_STDOUT=true ./bin/test test/generators/actions_test.rb + ``` + + *Steve Polito* Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/railties/CHANGELOG.md) for previous changes. diff --git a/railties/lib/rails/generators/testing/behavior.rb b/railties/lib/rails/generators/testing/behavior.rb index 4e24defd032..53b48ba62c2 100644 --- a/railties/lib/rails/generators/testing/behavior.rb +++ b/railties/lib/rails/generators/testing/behavior.rb @@ -65,11 +65,15 @@ module Rails # You can provide a configuration hash as second argument. This method returns the output # printed by the generator. def run_generator(args = default_arguments, config = {}) - capture(:stdout) do - args += ["--skip-bundle"] unless args.include?("--no-skip-bundle") || args.include?("--dev") - args += ["--skip-bootsnap"] unless args.include?("--no-skip-bootsnap") || args.include?("--skip-bootsnap") + args += ["--skip-bundle"] unless args.include?("--no-skip-bundle") || args.include?("--dev") + args += ["--skip-bootsnap"] unless args.include?("--no-skip-bootsnap") || args.include?("--skip-bootsnap") + if ENV["RAILS_LOG_TO_STDOUT"] == "true" generator_class.start(args, config.reverse_merge(destination_root: destination_root)) + else + capture(:stdout) do + generator_class.start(args, config.reverse_merge(destination_root: destination_root)) + end end end