Skip to content

Commit

Permalink
Fix a bug that tagged logger warning doesn't work with Rails 7
Browse files Browse the repository at this point in the history
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!!!
  • Loading branch information
kou committed Sep 25, 2024
1 parent e2244ed commit 4b2ffe0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/test/unit/active_support.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2012-2022 Sutou Kouhei <[email protected]>
# Copyright (C) 2012-2024 Sutou Kouhei <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion test/test_assertions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2015-2022 Kouhei Sutou <[email protected]>
# Copyright (C) 2015-2024 Sutou Kouhei <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand All @@ -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)
Expand All @@ -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(<Unexpected>) 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

Expand Down

0 comments on commit 4b2ffe0

Please sign in to comment.