Skip to content

Commit

Permalink
+ Add metadata lazy accessor to Runnable / Result. (matteeyah)
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//src/minitest/dev/": change = 13875]
  • Loading branch information
zenspider committed Jul 26, 2023
1 parent 4795997 commit de80282
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/minitest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,31 @@ def initialize name # :nodoc:
self.name = name
self.failures = []
self.assertions = 0
# lazy initializer for metadata
end

##
# Metadata you attach to the test results that get sent to the reporter.
#
# Lazily initializes to a hash, to keep memory down.
#
# NOTE: this data *must* be plain (read: marshal-able) data!
# Hashes! Arrays! Strings!

def metadata
@metadata ||= {}
end

##
# Sets metadata, mainly used for +Result.from+.

attr_writer :metadata

##
# Returns true if metadata exists.

def metadata?
defined? @metadata
end

##
Expand Down Expand Up @@ -566,6 +591,7 @@ def self.from runnable
r.assertions = o.assertions
r.failures = o.failures.dup
r.time = o.time
r.metadata = o.metadata if o.metadata?

r.source_location = o.method(o.name).source_location rescue ["unknown", -1]

Expand Down
29 changes: 29 additions & 0 deletions test/minitest/test_minitest_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def passing_test
@pt ||= Minitest::Result.from Minitest::Test.new(:woot)
end

def passing_test_with_metadata
test = Minitest::Test.new(:woot)
test.metadata[:meta] = :data
@pt ||= Minitest::Result.from test
end

def skip_test
unless defined? @st then
@st = Minitest::Test.new(:woot)
Expand Down Expand Up @@ -166,6 +172,29 @@ def test_record_pass
assert_equal 0, r.assertions
end

def test_record_pass_with_metadata
reporter = self.r

def reporter.metadata
@metadata
end

def reporter.record result
super
@metadata = result.metadata if result.metadata?
end

r.record passing_test_with_metadata

exp = { :meta => :data }
assert_equal exp, reporter.metadata

assert_equal ".", io.string
assert_empty r.results
assert_equal 1, r.count
assert_equal 0, r.assertions
end

def test_record_fail
fail_test = self.fail_test
r.record fail_test
Expand Down

2 comments on commit de80282

@casperisfine
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the end of the world, but it's not uncommon for test classes to have some private accessors as shortcuts, and metadata is a fairly common name.

This hard broke a few tests in our app.

@composerinteralia
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broke a few tests at GitHub as well (we Marshal.dump the result when running tests in parallel, and ended up with some objects in the metadata that are not dumpable).

Please sign in to comment.