Skip to content

Instantly share code, notes, and snippets.

@zenspider
Created November 26, 2023 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zenspider/32a2a555faf889abf7cf791e0a3c9ef6 to your computer and use it in GitHub Desktop.
Save zenspider/32a2a555faf889abf7cf791e0a3c9ef6 to your computer and use it in GitHub Desktop.
+ Output relative path:line where possible, absolute otherwise.
- BacktraceFilter#filter can now work with caller_locations.
diff -r old/lib/minitest.rb new/lib/minitest.rb
--- old/lib/minitest.rb
+++ new/lib/minitest.rb
@@ -523,12 +523,14 @@
not self.failure
end
+ BASE_DIR = "#{Dir.pwd}/" # :nodoc:
+
##
# The location identifier of this test. Depends on a method
# existing called class_name.
def location
- loc = " [#{self.failure.location}]" unless passed? or error?
+ loc = " [#{self.failure.location.delete_prefix BASE_DIR}]" unless passed? or error?
"#{self.class_name}##{self.name}#{loc}"
end
@@ -833,7 +835,7 @@
io.puts "# Running:"
io.puts
- self.sync = io.respond_to? :"sync=" # stupid emacs
+ self.sync = io.respond_to? :"sync="
self.old_sync, io.sync = io.sync, true if self.sync
end
@@ -991,8 +993,11 @@
self.error.backtrace
end
+ BASE_RE = %r%#{Dir.pwd}/% # :nodoc:
+
def message # :nodoc:
- bt = Minitest.filter_backtrace(self.backtrace).join "\n "
+ bt = Minitest.filter_backtrace(self.backtrace).join("\n ")
+ .gsub(BASE_RE, "")
"#{self.error.class}: #{self.error.message}\n #{bt}"
end
@@ -1085,9 +1090,9 @@
return bt.dup if $DEBUG || ENV["MT_DEBUG"]
- new_bt = bt.take_while { |line| line !~ MT_RE }
- new_bt = bt.select { |line| line !~ MT_RE } if new_bt.empty?
- new_bt = bt.dup if new_bt.empty?
+ new_bt = bt.take_while { |line| line.to_s !~ MT_RE }
+ new_bt = bt.select { |line| line.to_s !~ MT_RE } if new_bt.empty?
+ new_bt = bt.dup if new_bt.empty?
new_bt
end
diff -r old/lib/minitest/assertions.rb new/lib/minitest/assertions.rb
--- old/lib/minitest/assertions.rb
+++ new/lib/minitest/assertions.rb
@@ -198,6 +198,11 @@
assert obj.empty?, msg
end
+ def _where # :nodoc:
+ where = Minitest.filter_backtrace(caller_locations).first
+ [where.path, where.lineno].join ":"
+ end
+
E = "" # :nodoc:
##
@@ -221,10 +226,7 @@
if Minitest::VERSION =~ /^6/ then
refute_nil exp, "Use assert_nil if expecting nil."
else
- where = Minitest.filter_backtrace(caller).first
- where = where.split(/:in /, 2).first # clean up noise
-
- warn "DEPRECATED: Use assert_nil if expecting nil from #{where}. This will fail in Minitest 6."
+ warn "DEPRECATED: Use assert_nil if expecting nil from #{_where}. This will fail in Minitest 6."
end
end
@@ -475,9 +477,7 @@
# Fails unless the call returns a true value
def assert_send send_ary, m = nil
- where = Minitest.filter_backtrace(caller).first
- where = where.split(/:in /, 2).first # clean up noise
- warn "DEPRECATED: assert_send. From #{where}"
+ warn "DEPRECATED: assert_send. From #{_where}"
recv, msg, *args = send_ary
m = message(m) {
diff -r old/lib/minitest/spec.rb new/lib/minitest/spec.rb
--- old/lib/minitest/spec.rb
+++ new/lib/minitest/spec.rb
@@ -13,8 +13,8 @@
# warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
self.class_eval <<-EOM, __FILE__, __LINE__ + 1
def #{new_name} *args
- where = Minitest.filter_backtrace(caller).first
- where = where.split(/:in /, 2).first # clean up noise
+ where = Minitest.filter_backtrace(caller_locations).first
+ where = [where.path, where.lineno].join ":"
Kernel.warn "DEPRECATED: global use of #{new_name} from #\{where}. Use #{target_obj}.#{new_name} instead. This will fail in Minitest 6."
Minitest::Expectation.new(self, Minitest::Spec.current).#{new_name}(*args)
end
diff -r old/test/minitest/metametameta.rb new/test/minitest/metametameta.rb
--- old/test/minitest/metametameta.rb
+++ new/test/minitest/metametameta.rb
@@ -105,15 +105,17 @@
output.gsub!(/0x[A-Fa-f0-9]+/, "0xXXX")
output.gsub!(/ +$/, "")
+ file = ->(s) { s.start_with?("/") ? "FULLFILE" : "FILE" }
+
if windows? then
output.gsub!(/\[(?:[A-Za-z]:)?[^\]:]+:\d+\]/, "[FILE:LINE]")
output.gsub!(/^(\s+)(?:[A-Za-z]:)?[^:]+:\d+:in/, '\1FILE:LINE:in')
else
- output.gsub!(/\[[^\]:]+:\d+\]/, "[FILE:LINE]")
- output.gsub!(/^(\s+)[^:]+:\d+:in/, '\1FILE:LINE:in')
+ output.gsub!(/\[([^\]:]+):\d+\]/) { "[#{file[$1]}:LINE]" }
+ output.gsub!(/^(\s+)([^:]+):\d+:in/) { "#{$1}#{file[$2]}:LINE:in" }
end
- output.gsub!(/( at )[^:]+:\d+/, '\1[FILE:LINE]')
+ output.gsub!(/( at )[^:]+:\d+/) { "#{$1}[#{file[$2]}:LINE]" } # eval?
output
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment