Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test.filter_backtrace method doesn't exist #2

Closed
nevans opened this issue Aug 21, 2023 · 3 comments
Closed

Test.filter_backtrace method doesn't exist #2

nevans opened this issue Aug 21, 2023 · 3 comments

Comments

@nevans
Copy link

nevans commented Aug 21, 2023

In #assert_nothing_raised, Test.filter_backtrace is called in one of the rescue clauses:

rescue *(args.empty? ? Exception : args) => e
msg = message(msg) {
"Exception raised:\n<#{mu_pp(e)}>\n""Backtrace:\n" <<
Test.filter_backtrace(e.backtrace).map{|frame| " #{frame}"}.join("\n")
}
raise Test::Unit::AssertionFailedError, msg.call, e.backtrace
end

But that method isn't part of test/unit. It looks to me like that method is defined here:

https://github.com/ruby/ruby/blob/7d26c03267cf1be643963355fc52efde1574b37a/tool/lib/test/unit.rb#L27-L61

  class << self
    ##
    # Filter object for backtraces.

    attr_accessor :backtrace_filter
  end

  class BacktraceFilter # :nodoc:
    def filter bt
      return ["No backtrace"] unless bt

      new_bt = []
      pattern = %r[/(?:lib\/test/|core_assertions\.rb:)]

      unless $DEBUG then
        bt.each do |line|
          break if pattern.match?(line)
          new_bt << line
        end

        new_bt = bt.reject { |line| pattern.match?(line) } if new_bt.empty?
        new_bt = bt.dup if new_bt.empty?
      else
        new_bt = bt.dup
      end

      new_bt
    end
  end

  self.backtrace_filter = BacktraceFilter.new

  def self.filter_backtrace bt # :nodoc:
    backtrace_filter.filter bt
  end

I assume that will work when running tests in the ruby repo, but it doesn't seem to work from individual gem repos. I temporarily copied the above into my local test/lib/helper.rb and it fixed the issue. Should it simply be moved into core_assertions? Or should more of tool/lib/test/unit.rb be migrated into a different file in this repo?

Thanks!

@hsbt
Copy link
Member

hsbt commented Aug 25, 2023

Thank you to catch this. I'm considering to copy BacktraceFilter to this repo from ruby/ruby.

@hsbt
Copy link
Member

hsbt commented Aug 29, 2023

@nevans Can you try with v1.0.2? It supports Test.filter_backtrace with c669064.

@nevans
Copy link
Author

nevans commented Aug 29, 2023

@hsbt I verified it with an explicit raise inside an assert_nothing_raised block:

With core-assertions v1.0.1: NoMethodError: undefined method `filter_backtrace' for Test:Module
╭╼ <nick@anarres> ~/src/net-imap git:[tls-verified {4} U:2]
╰──╼ $) bundle exec rake TEST="test/net/imap/test_imap.rb" TESTOPTS='--name=/test_imaps_verify_none/'
Loaded suite /home/nick/.local/share/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rake-13.0.6/lib
/rake/rake_test_loader
Started
E
========================================================================================================================
Error: test_imaps_verify_none(IMAPTest): NoMethodError: undefined method `filter_backtrace' for Test:Module
/home/nick/.local/share/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/test-unit-ruby-core-
cbb4532eae10/lib/core_assertions.rb:188:in `block in assert_nothing_raised'
/home/nick/.local/share/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/test-unit-ruby-core-
cbb4532eae10/lib/core_assertions.rb:26:in `block in message'
/home/nick/.local/share/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/test-unit-ruby-core-
cbb4532eae10/lib/core_assertions.rb:190:in `rescue in assert_nothing_raised'
/home/nick/.local/share/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/test-unit-ruby-core-
cbb4532eae10/lib/core_assertions.rb:181:in `assert_nothing_raised'
/home/nick/src/net-imap/test/net/imap/test_imap.rb:66:in `test_imaps_verify_none'
     63:       # Assert verified *after* the imaps_test and assert_nothing_raised blocks.
     64:       # Otherwise, failures can't logout and need to wait for the timeout.
     65:       verified, imap = :unknown, nil
  => 66:       assert_nothing_raised do
     67:         imaps_test do |port|
     68:           imap = Net::IMAP.new(
     69:             server_addr,
========================================================================================================================
Finished in 0.20029998 seconds.
------------------------------------------------------------------------------------------------------------------------
1 tests, 0 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
------------------------------------------------------------------------------------------------------------------------
4.99 tests/s, 0.00 assertions/s
rake aborted!
Command failed with status (1)
/home/nick/.local/share/rbenv/versions/3.2.2/bin/bundle:25:in `load'
/home/nick/.local/share/rbenv/versions/3.2.2/bin/bundle:25:in `<main>'
Tasks: TOP => default => test
(See full trace by running task with --trace)
With core-assertions v1.0.2: Exception raised: <#<RuntimeError: oops>>
╭╼ <nick@anarres> ~/src/net-imap git:[tls-verified {4} U:2]
╰──╼ $) bundle exec rake TEST="test/net/imap/test_imap.rb" TESTOPTS='--name=/test_imaps_verify_none/'
Loaded suite /home/nick/.local/share/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rake-13.0.6/lib
/rake/rake_test_loader
Started
F
========================================================================================================================
Failure: test_imaps_verify_none(IMAPTest):
  Exception raised:
  <#<RuntimeError: oops>>
  Backtrace:
    /home/nick/src/net-imap/test/net/imap/test_imap.rb:76:in `block in test_imaps_verify_none'.
/home/nick/src/net-imap/test/net/imap/test_imap.rb:76:in `block in test_imaps_verify_none'
     73:           verified = imap.tls_verified?
     74:           imap
     75:         end
  => 76:         raise "oops"
     77:       end
     78:       assert_equal false, verified
     79:       assert_equal false, imap.tls_verified?
/home/nick/.local/share/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/bundler/gems/test-unit-ruby-core-
c6f0dda672b3/lib/core_assertions.rb:219:in `assert_nothing_raised'
/home/nick/src/net-imap/test/net/imap/test_imap.rb:66:in `test_imaps_verify_none'
========================================================================================================================
Finished in 0.199629887 seconds.
------------------------------------------------------------------------------------------------------------------------
1 tests, 0 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
0% passed
------------------------------------------------------------------------------------------------------------------------
5.01 tests/s, 0.00 assertions/s
rake aborted!
Command failed with status (1)
/home/nick/.local/share/rbenv/versions/3.2.2/bin/bundle:25:in `load'
/home/nick/.local/share/rbenv/versions/3.2.2/bin/bundle:25:in `<main>'
Tasks: TOP => default => test
(See full trace by running task with --trace)

Thanks!

@nevans nevans closed this as completed Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants