diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG.md | 8 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/app/app_generator.rb | 8 | ||||
-rw-r--r-- | railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt | 2 | ||||
-rw-r--r-- | railties/lib/rails/test_unit/minitest_plugin.rb | 20 | ||||
-rw-r--r-- | railties/lib/rails/test_unit/reporter.rb | 32 | ||||
-rw-r--r-- | railties/test/generators/app_generator_test.rb | 6 | ||||
-rw-r--r-- | railties/test/test_unit/reporter_test.rb | 38 |
7 files changed, 95 insertions, 19 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index ed8f74ee9b..501c7d2ce5 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -201,7 +201,7 @@ * Fix STATS_DIRECTORIES already defined warning when running rake from within the top level directory of an engine that has a test app. - Fixes #20510 + Fixes #20510. *Ersin Akinci* @@ -249,13 +249,13 @@ middleware for API apps & generators generates the right files, folders and configurations. - *Santiago Pastorino & Jorge Bejar* + *Santiago Pastorino*, *Jorge Bejar* * Make generated scaffold functional tests work inside engines. *Yuji Yaginuma* -* Generator a `.keep` file in the `tmp` folder by default as many scripts +* Generate a `.keep` file in the `tmp` folder by default as many scripts assume the existence of this folder and most would fail if it is absent. See #20299. @@ -321,7 +321,7 @@ * Created rake restart task. Restarts your Rails app by touching the `tmp/restart.txt`. - Fixes #18876. + See #18876. *Hyonjee Joo* diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 0819e6c01f..f4deec7135 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -57,7 +57,7 @@ module Rails directory 'app' keep_file 'app/assets/images' - keep_file 'app/assets/javascripts/channels' unless options[:skip_action_cable] + empty_directory_with_keep_file 'app/assets/javascripts/channels' unless options[:skip_action_cable] keep_file 'app/controllers/concerns' keep_file 'app/models/concerns' @@ -329,6 +329,12 @@ module Rails end end + def delete_api_initializers + unless options[:api] + remove_file 'config/initializers/cors.rb' + end + end + def finish_template build(:leftovers) end diff --git a/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt b/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt index cfadd24017..1beea2accd 100644 --- a/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt @@ -1,5 +1,5 @@ # This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the rails db:seed (or created alongside the db with db:setup). +# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). # # Examples: # diff --git a/railties/lib/rails/test_unit/minitest_plugin.rb b/railties/lib/rails/test_unit/minitest_plugin.rb index d39d2f32bf..d4ab2ada66 100644 --- a/railties/lib/rails/test_unit/minitest_plugin.rb +++ b/railties/lib/rails/test_unit/minitest_plugin.rb @@ -3,16 +3,13 @@ require "rails/test_unit/reporter" require "rails/test_unit/test_requirer" module Minitest - mattr_accessor(:hide_aggregated_results) { false } - - module AggregatedResultSuppresion + class SuppressedSummaryReporter < SummaryReporter + # Disable extra failure output after a run if output is inline. def aggregated_results - super unless Minitest.hide_aggregated_results + super unless options[:output_inline] end end - SummaryReporter.prepend AggregatedResultSuppresion - def self.plugin_rails_options(opts, options) executable = ::Rails::TestUnitReporter.executable opts.separator "" @@ -49,6 +46,12 @@ module Minitest options[:fail_fast] = true end + opts.on("-c", "--[no-]color", + "Enable color in the output") do |value| + options[:color] = value + end + + options[:color] = true options[:output_inline] = true options[:patterns] = opts.order! end @@ -77,9 +80,8 @@ module Minitest Minitest.backtrace_filter = ::Rails.backtrace_cleaner if ::Rails.respond_to?(:backtrace_cleaner) end - # Disable the extra failure output after a run, unless output is deferred. - self.hide_aggregated_results = options[:output_inline] - + self.reporter.reporters.clear # Replace progress reporter for colors. + self.reporter << SuppressedSummaryReporter.new(options[:io], options) self.reporter << ::Rails::TestUnitReporter.new(options[:io], options) end diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb index 695c67756b..73b8d7d27b 100644 --- a/railties/lib/rails/test_unit/reporter.rb +++ b/railties/lib/rails/test_unit/reporter.rb @@ -9,10 +9,16 @@ module Rails def record(result) super + if options[:verbose] + io.puts color_output(format_line(result), by: result) + else + io.print color_output(result.result_code, by: result) + end + if output_inline? && result.failure && (!result.skipped? || options[:verbose]) io.puts io.puts - io.puts format_failures(result) + io.puts format_failures(result).map { |line| color_output(line, by: result) } io.puts io.puts format_rerun_snippet(result) io.puts @@ -56,6 +62,10 @@ module Rails options[:fail_fast] end + def format_line(result) + "%s#%s = %.2f s = %s" % [result.class, result.name, result.time, result.result_code] + end + def format_failures(result) result.failures.map do |failure| "#{failure.result_label}:\n#{result.class}##{result.name}:\n#{failure.message}\n" @@ -76,5 +86,25 @@ module Rails def app_root @app_root ||= defined?(ENGINE_ROOT) ? ENGINE_ROOT : Rails.root end + + def colored_output? + options[:color] && io.respond_to?(:tty?) && io.tty? + end + + codes = { red: 31, green: 32, yellow: 33 } + COLOR_BY_RESULT_CODE = { + "." => codes[:green], + "E" => codes[:red], + "F" => codes[:red], + "S" => codes[:yellow] + } + + def color_output(string, by:) + if colored_output? + "\e[#{COLOR_BY_RESULT_CODE[by.result_code]}m#{string}\e[0m" + else + string + end + end end end diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 66997c5f5e..314d8c50b0 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -151,6 +151,12 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file("config/initializers/cookies_serializer.rb", /Rails\.application\.config\.action_dispatch\.cookies_serializer = :json/) end + def test_new_application_not_include_api_initializers + run_generator + + assert_no_file 'config/initializers/cors.rb' + end + def test_rails_update_keep_the_cookie_serializer_if_it_is_already_configured app_root = File.join(destination_root, 'myapp') run_generator [app_root] diff --git a/railties/test/test_unit/reporter_test.rb b/railties/test/test_unit/reporter_test.rb index 3ff7cd18cd..2d08d4ec30 100644 --- a/railties/test/test_unit/reporter_test.rb +++ b/railties/test/test_unit/reporter_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require 'rails/test_unit/reporter' +require 'minitest/mock' class TestUnitReporterTest < ActiveSupport::TestCase class ExampleTest < Minitest::Test @@ -61,7 +62,7 @@ class TestUnitReporterTest < ActiveSupport::TestCase @reporter.record(failed_test) @reporter.report - expect = %r{\A\n\nFailure:\nTestUnitReporterTest::ExampleTest#woot:\nboo\n\nbin/rails test test/test_unit/reporter_test.rb:\d+\n\n\z} + expect = %r{\AF\n\nFailure:\nTestUnitReporterTest::ExampleTest#woot:\nboo\n\nbin/rails test test/test_unit/reporter_test.rb:\d+\n\n\z} assert_match expect, @output.string end @@ -69,7 +70,7 @@ class TestUnitReporterTest < ActiveSupport::TestCase @reporter.record(errored_test) @reporter.report - expect = %r{\A\n\nError:\nTestUnitReporterTest::ExampleTest#woot:\nArgumentError: wups\n No backtrace\n\nbin/rails test .*test/test_unit/reporter_test.rb:6\n\n\z} + expect = %r{\AE\n\nError:\nTestUnitReporterTest::ExampleTest#woot:\nArgumentError: wups\n No backtrace\n\nbin/rails test .*test/test_unit/reporter_test.rb:\d+\n\n\z} assert_match expect, @output.string end @@ -78,7 +79,7 @@ class TestUnitReporterTest < ActiveSupport::TestCase verbose.record(skipped_test) verbose.report - expect = %r{\A\n\nSkipped:\nTestUnitReporterTest::ExampleTest#woot:\nskipchurches, misstemples\n\nbin/rails test test/test_unit/reporter_test.rb:\d+\n\n\z} + expect = %r{\ATestUnitReporterTest::ExampleTest#woot = 10\.00 s = S\n\n\nSkipped:\nTestUnitReporterTest::ExampleTest#woot:\nskipchurches, misstemples\n\nbin/rails test test/test_unit/reporter_test.rb:\d+\n\n\z} assert_match expect, @output.string end @@ -113,6 +114,36 @@ class TestUnitReporterTest < ActiveSupport::TestCase assert_no_match 'Failed tests:', @output.string end + test "outputs colored passing results" do + @output.stub(:tty?, true) do + colored = Rails::TestUnitReporter.new @output, color: true, output_inline: true + colored.record(passing_test) + + expect = %r{\e\[32m\.\e\[0m} + assert_match expect, @output.string + end + end + + test "outputs colored skipped results" do + @output.stub(:tty?, true) do + colored = Rails::TestUnitReporter.new @output, color: true, output_inline: true + colored.record(skipped_test) + + expect = %r{\e\[33mS\e\[0m} + assert_match expect, @output.string + end + end + + test "outputs colored failed results" do + @output.stub(:tty?, true) do + colored = Rails::TestUnitReporter.new @output, color: true, output_inline: true + colored.record(errored_test) + + expected = %r{\e\[31mE\e\[0m\n\n\e\[31mError:\nTestUnitReporterTest::ExampleTest#woot:\nArgumentError: wups\n No backtrace\n\e\[0m} + assert_match expected, @output.string + end + end + private def assert_rerun_snippet_count(snippet_count) assert_equal snippet_count, @output.string.scan(%r{^bin/rails test }).size @@ -145,6 +176,7 @@ class TestUnitReporterTest < ActiveSupport::TestCase rescue Minitest::Assertion => e e end + st.time = 10 st end end |