minitest-utils 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee05007ab6791116cc1ea0e62b39d94b2e11cdfebcd02f66c993717ca620c1f4
4
- data.tar.gz: a535e5c9454fa423faab453f8719ca1978711e9a07c9423de8aba8778c762446
3
+ metadata.gz: 88ad6d0826de366086152cedfef5fabbbc338b4aeb8a664ed62d9762002d841d
4
+ data.tar.gz: 7256702f812f28c4320bf3f42a05733302c963fd6c8093e35b107e21f8248e55
5
5
  SHA512:
6
- metadata.gz: 5cfdbc7786514bcad2dcf4d43ba3f0377018895027b7600c4a8f2aad954588851df558291db149f69c73ab44b0f0026eaf39393a68789809aaa9a69e49e1a0fb
7
- data.tar.gz: 3d2cf46cbc08556df35d4ea73a59782c1ef702a27ad7472b3b79fa145b3c0e01c2efaf5a03254f3a0d86126a3bb32398167d6cee9e39e0610a29b955f070fa68
6
+ metadata.gz: 4979039508fcb466137ac584c0b8f1cae0115f83ddf3866ec92b26384cde28567e72f17bd7f10499d41b23745c66f0fd49945b677daa08cbbcd87ba227134d47
7
+ data.tar.gz: ad984963d7267ece2f9f32f93aa752aeca90ee3d4fdd6e35a86a05fd2108b470e7317ff86c67c3f36f25a1bd6644264c5d58b5d8423dc10a3be3dc0c43ef5ae1
@@ -19,27 +19,33 @@ module Minitest
19
19
  include ::Minitest::Utils::Assertions
20
20
 
21
21
  def self.tests
22
- @tests ||= []
22
+ @tests ||= {}
23
23
  end
24
24
 
25
25
  def self.test(name, &block)
26
26
  source_location = caller_locations(1..1).first
27
+ source_location = [
28
+ Pathname(source_location.path).relative_path_from(Pathname(Dir.pwd)),
29
+ source_location.lineno
30
+ ]
31
+
32
+ klass = self.name
27
33
  method_name = name.downcase
28
34
  .gsub(/[^a-z0-9]+/, "_")
29
35
  .gsub(/^_+/, "")
30
36
  .gsub(/_+$/, "").squeeze("_")
31
37
  test_name = "test_#{method_name}".to_sym
32
- defined = method_defined? test_name
38
+ defined = method_defined?(test_name)
39
+
40
+ Test.tests["#{klass}##{test_name}"] = {
41
+ name: name,
42
+ source_location:,
43
+ benchmark: nil
44
+ }
45
+
33
46
  testable = proc do
34
47
  benchmark = Benchmark.measure { instance_eval(&block) }
35
-
36
- Test.tests << {
37
- class: self.class,
38
- name: name,
39
- test_name: test_name,
40
- benchmark: benchmark,
41
- source_location:
42
- }
48
+ Test.tests["#{klass}##{test_name}"][:benchmark] = benchmark
43
49
  end
44
50
 
45
51
  raise "#{test_name} is already defined in #{self}" if defined
@@ -68,20 +68,22 @@ module Minitest
68
68
  failing_results.each {|result| display_replay_command(result) }
69
69
  io.puts "\n\n"
70
70
  else
71
- io.puts "\nSlow Tests:\n"
72
71
  threshold = 0.1 # 100ms
73
72
  test_results =
74
73
  Test
75
74
  .tests
75
+ .values
76
+ .select { _1[:benchmark] }
76
77
  .sort_by { _1[:benchmark].total }
77
78
  .reverse
78
79
  .take(10).filter { _1[:benchmark].total > threshold }
79
- pwd = Pathname(Dir.pwd)
80
+
81
+ return unless test_results.any?
82
+
83
+ io.puts "\nSlow Tests:\n"
80
84
 
81
85
  test_results.each_with_index do |info, index|
82
- relative_path = Pathname(info[:source_location].absolute_path)
83
- .relative_path_from(pwd)
84
- location = "#{relative_path}:#{info[:source_location].lineno}"
86
+ location = info[:source_location].join(":")
85
87
  duration = humanize_duration(info[:benchmark].total * 1_000_000_000)
86
88
 
87
89
  prefix = "#{index + 1}) "
@@ -164,15 +166,9 @@ module Minitest
164
166
  end
165
167
 
166
168
  private def find_test_file(result)
167
- location, line = if result.respond_to?(:source_location)
168
- result.source_location
169
- else
170
- result.method(result.name).source_location
171
- end
169
+ info = Test.tests.fetch("#{result.klass}##{result.name}")
172
170
 
173
- location = location.gsub(%r{^.*?/((?:test|spec)/.*?)$}, "\\1")
174
-
175
- [location, line]
171
+ info[:source_location]
176
172
  end
177
173
 
178
174
  private def backtrace(backtrace)
@@ -191,7 +187,11 @@ module Minitest
191
187
  return matches[1] if matches
192
188
 
193
189
  regex = include_line_number ? /^([^:]+:\d+)/ : /^([^:]+)/
194
- location = File.expand_path(location[regex, 1])
190
+ path = location[regex, 1]
191
+
192
+ return location unless path
193
+
194
+ location = File.expand_path
195
195
 
196
196
  return location unless location.start_with?(Dir.pwd)
197
197
 
@@ -199,7 +199,8 @@ module Minitest
199
199
  end
200
200
 
201
201
  private def filter_backtrace(backtrace)
202
- Minitest.backtrace_filter.filter(backtrace)
202
+ # drop the last line, which is from benchmark.
203
+ Minitest.backtrace_filter.filter(backtrace)[0..-2]
203
204
  end
204
205
 
205
206
  private def result_name(name)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Minitest
4
4
  module Utils
5
- VERSION = "0.5.0"
5
+ VERSION = "0.5.2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-24 00:00:00.000000000 Z
10
+ date: 2025-02-28 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: benchmark