-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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!!!
- Loading branch information
Showing
2 changed files
with
23 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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(<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 | ||
|
||
|