minitest-utils 0.4.10 → 0.5.1

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: 62a63ab4c4bc09bdb1d655a20fd7c393d812a6205563ca37aceee3a8fb167fcc
4
- data.tar.gz: 6465e49d3b9d4e71917e86f91fc5804fd92b1936868df4f57c42e0caaf3b5a17
3
+ metadata.gz: 0b70bd66e3c107ed99a78f3521ed7662a105f13109b54ce4d4c23039676ec63d
4
+ data.tar.gz: 3448ddcfaadfa6c22d61bb2918044b8769aa9c9ca41932caaace1e5d527065dd
5
5
  SHA512:
6
- metadata.gz: 61536bf195760217673850b83e98f504a3477a24c60dedf498186d262ebfcb708b88b5630faeea35786e653c025d727d2b70ce2bc1652f5715fea97ab60c310c
7
- data.tar.gz: 9ddac910a50b8999163936bc4b7aa8e1fb91579f07ed75e3948a4ce6e1564a60936fde92349bed2ecf69997f223a707970c3d79d8e3bc2fc9247a4d13fff57f1
6
+ metadata.gz: 2f2f262a6756480cfbecf5e2facd19edcbbae57a96065043cd5a6a1e30c2b68c073f16b06a1948172fcfcd344200d989d66a538621e28e9f2ed5e9445d08391c
7
+ data.tar.gz: 3d83de0fcb3dc1e2a3b309fcf1eb4a95067abbd94385399ccb59964b5b7dff8a038a6a408f94c3849b1172e033d297c8355a7b350c77a9835b9c229716b94692
data/.rubocop.yml CHANGED
@@ -3,8 +3,11 @@ inherit_gem:
3
3
  rubocop-fnando: .rubocop.yml
4
4
 
5
5
  AllCops:
6
- TargetRubyVersion: 2.7
6
+ TargetRubyVersion: 3.2
7
7
 
8
8
  Style/GlobalVars:
9
9
  Exclude:
10
10
  - test/minitest/utils/let_test.rb
11
+
12
+ Minitest/UselessAssertion:
13
+ Enabled: false
@@ -18,19 +18,40 @@ module Minitest
18
18
  class Test
19
19
  include ::Minitest::Utils::Assertions
20
20
 
21
+ def self.tests
22
+ @tests ||= {}
23
+ end
24
+
21
25
  def self.test(name, &block)
22
- name = name.downcase
23
- .gsub(/[^a-z0-9]+/, "_")
24
- .gsub(/^_+/, "")
25
- .gsub(/_+$/, "")
26
- .gsub(/_+/, "_")
27
- test_name = "test_#{name}".to_sym
28
- defined = method_defined? test_name
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
33
+ method_name = name.downcase
34
+ .gsub(/[^a-z0-9]+/, "_")
35
+ .gsub(/^_+/, "")
36
+ .gsub(/_+$/, "").squeeze("_")
37
+ test_name = "test_#{method_name}".to_sym
38
+ defined = method_defined?(test_name)
39
+
40
+ Test.tests["#{klass}##{test_name}"] = {
41
+ name: name,
42
+ source_location:,
43
+ benchmark: nil
44
+ }
45
+
46
+ testable = proc do
47
+ benchmark = Benchmark.measure { instance_eval(&block) }
48
+ Test.tests["#{klass}##{test_name}"][:benchmark] = benchmark
49
+ end
29
50
 
30
51
  raise "#{test_name} is already defined in #{self}" if defined
31
52
 
32
- if block_given?
33
- define_method(test_name, &block)
53
+ if block
54
+ define_method(test_name, &testable)
34
55
  else
35
56
  define_method(test_name) do
36
57
  flunk "No implementation provided for #{name}"
@@ -28,7 +28,7 @@ module ActionDispatch
28
28
 
29
29
  messages = errors
30
30
  .map(&:message)
31
- .map {|message| message[/(\d+:\d+ .*?)$/, 1] }
31
+ .map! {|message| message[/(\d+:\d+ .*?)$/, 1] }
32
32
  .join("\n")
33
33
 
34
34
  raise "JavaScript Errors\n#{messages}"
@@ -14,7 +14,8 @@ module Minitest
14
14
  red: 31,
15
15
  green: 32,
16
16
  yellow: 33,
17
- blue: 34
17
+ blue: 34,
18
+ gray: 37
18
19
  }.freeze
19
20
 
20
21
  def initialize(*)
@@ -62,11 +63,49 @@ module Minitest
62
63
  io.puts statistics
63
64
  io.puts color(summary, color)
64
65
 
65
- return unless failing_results.any?
66
+ if failing_results.any?
67
+ io.puts "\nFailed Tests:\n"
68
+ failing_results.each {|result| display_replay_command(result) }
69
+ io.puts "\n\n"
70
+ else
71
+ threshold = 0.1 # 100ms
72
+ test_results =
73
+ Test
74
+ .tests
75
+ .values
76
+ .select { _1[:benchmark] }
77
+ .sort_by { _1[:benchmark].total }
78
+ .reverse
79
+ .take(10).filter { _1[:benchmark].total > threshold }
80
+
81
+ return unless test_results.any?
82
+
83
+ io.puts "\nSlow Tests:\n"
84
+
85
+ test_results.each_with_index do |info, index|
86
+ location = info[:source_location].join(":")
87
+ duration = humanize_duration(info[:benchmark].total * 1_000_000_000)
88
+
89
+ prefix = "#{index + 1}) "
90
+ padding = " " * prefix.size
91
+
92
+ io.puts color("#{prefix}#{info[:name]} (#{duration})", :red)
93
+ io.puts color("#{padding}#{location}", :gray)
94
+ io.puts
95
+ end
96
+ end
97
+ end
66
98
 
67
- io.puts "\nFailed Tests:\n"
68
- failing_results.each {|result| display_replay_command(result) }
69
- io.puts "\n\n"
99
+ private def humanize_duration(duration_ns)
100
+ if duration_ns < 1000
101
+ format("%.2f ns", duration_ns)
102
+ elsif duration_ns < 1_000_000
103
+ format("%.2f μs", (duration_ns / 1000))
104
+ elsif duration_ns < 1_000_000_000
105
+ format("%.2f ms", (duration_ns / 1_000_000))
106
+ else
107
+ format("%.2f s", (duration_ns / 1_000_000_000))
108
+ end
70
109
  end
71
110
 
72
111
  private def statistics
@@ -127,15 +166,9 @@ module Minitest
127
166
  end
128
167
 
129
168
  private def find_test_file(result)
130
- location, line = if result.respond_to?(:source_location)
131
- result.source_location
132
- else
133
- result.method(result.name).source_location
134
- end
135
-
136
- location = location.gsub(%r{^.*?/((?:test|spec)/.*?)$}, "\\1")
169
+ info = Test.tests.fetch("#{result.klass}##{result.name}")
137
170
 
138
- [location, line]
171
+ info[:source_location]
139
172
  end
140
173
 
141
174
  private def backtrace(backtrace)
@@ -162,7 +195,8 @@ module Minitest
162
195
  end
163
196
 
164
197
  private def filter_backtrace(backtrace)
165
- Minitest.backtrace_filter.filter(backtrace)
198
+ # drop the last line, which is from benchmark.
199
+ Minitest.backtrace_filter.filter(backtrace)[0..-2]
166
200
  end
167
201
 
168
202
  private def result_name(name)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Minitest
4
4
  module Utils
5
- VERSION = "0.4.10"
5
+ VERSION = "0.5.1"
6
6
  end
7
7
  end
@@ -3,6 +3,8 @@
3
3
  module Minitest
4
4
  module Utils
5
5
  require "minitest"
6
+ require "benchmark"
7
+ require "pathname"
6
8
  require "minitest/utils/version"
7
9
  require "minitest/utils/reporter"
8
10
  require "minitest/utils/extension"
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.summary = "Some utilities for your Minitest day-to-day usage."
11
11
  spec.description = spec.summary
12
12
  spec.homepage = "http://github.com/fnando/minitest-utils"
13
- spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.2.0")
14
14
 
15
15
  spec.files = `git ls-files -z`
16
16
  .split("\x0")
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
+ spec.add_dependency "benchmark"
22
23
  spec.add_dependency "minitest"
23
24
  spec.add_development_dependency "bundler"
24
25
  spec.add_development_dependency "pry-meta"
metadata CHANGED
@@ -1,14 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.10
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-15 00:00:00.000000000 Z
10
+ date: 2025-02-25 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: benchmark
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: minitest
14
28
  requirement: !ruby/object:Gem::Requirement
@@ -155,14 +169,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
169
  requirements:
156
170
  - - ">="
157
171
  - !ruby/object:Gem::Version
158
- version: 2.7.0
172
+ version: 3.2.0
159
173
  required_rubygems_version: !ruby/object:Gem::Requirement
160
174
  requirements:
161
175
  - - ">="
162
176
  - !ruby/object:Gem::Version
163
177
  version: '0'
164
178
  requirements: []
165
- rubygems_version: 3.6.2
179
+ rubygems_version: 3.6.4
166
180
  specification_version: 4
167
181
  summary: Some utilities for your Minitest day-to-day usage.
168
182
  test_files: []