From 4b2ffe0e6c2641d3c94f2fb90762bea850a8a5e3 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei Date: Wed, 25 Sep 2024 17:22:16 +0900 Subject: [PATCH] Fix a bug that tagged logger warning doesn't work with Rails 7 GitHub: fix GH-17 Test::Unit::AssertionFailedError doesn't have the raised exception. We need to extract the information from message. Reported by akira yamada. Thanks!!! --- lib/test/unit/active_support.rb | 9 ++++++--- test/test_assertions.rb | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/test/unit/active_support.rb b/lib/test/unit/active_support.rb index a140f89..1707a38 100644 --- a/lib/test/unit/active_support.rb +++ b/lib/test/unit/active_support.rb @@ -1,4 +1,4 @@ -# Copyright (C) 2012-2022 Sutou Kouhei +# Copyright (C) 2012-2024 Sutou Kouhei # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -91,9 +91,12 @@ def mu_pp(object) def _assert_nothing_raised_or_warn(assertion, &block) assert_nothing_raised(&block) rescue Test::Unit::AssertionFailedError => e - if tagged_logger && tagged_logger.warn? + if tagged_logger && + tagged_logger.warn? && + e.message.start_with?("Exception raised:\n") + inspected_exception = e.message.lines[1] || "Unknown exception" warning = <<-MSG.gsub(/^\s+/, "") - #{self.class} - #{name}: #{e.error.class} raised. + #{self.class} - #{name}: #{inspected_exception.strip} raised. If you expected this exception, use `assert_raise` as near to the code that raises as possible. Other block based assertions (e.g. `#{assertion}`) can be used, as long as `assert_raise` is inside their block. MSG diff --git a/test/test_assertions.rb b/test/test_assertions.rb index 7bfec42..1ebaf1e 100644 --- a/test/test_assertions.rb +++ b/test/test_assertions.rb @@ -1,4 +1,4 @@ -# Copyright (C) 2015-2022 Kouhei Sutou +# Copyright (C) 2015-2024 Sutou Kouhei # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -14,6 +14,8 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +require "stringio" + class TestAssertions < ActiveSupport::TestCase test "assert_not" do assert_not(false) @@ -34,12 +36,26 @@ class TestAssertions < ActiveSupport::TestCase raise "Unexpected" end end + elsif ActiveSupport.version < Gem::Version.new("7") + assert_raise(Test::Unit::AssertionFailedError) do + assert_difference("x", 1) do + raise "Unexpected" + end + end else + logdev = StringIO.new + self.tagged_logger = ActiveSupport::TaggedLogging.new(Logger.new(logdev)) assert_raise(Test::Unit::AssertionFailedError) do assert_difference("x", 1) do raise "Unexpected" end end + assert_equal(<<-MESSAGE, logdev.string) +#{self.class} - #{name}: RuntimeError() raised. +If you expected this exception, use `assert_raise` as near to the code that raises as possible. +Other block based assertions (e.g. `assert_difference`) can be used, as long as `assert_raise` is inside their block. + + MESSAGE end end