selective-ruby-minitest 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 21ed49908ca7c9dde0483d731253012e7bcad60e19136e1972f4283fb5616569
4
+ data.tar.gz: ac606566b9df73727b87afb88786e6c69263735f57639845139c052586066f69
5
+ SHA512:
6
+ metadata.gz: 05d25cde4b7fb970d4bca1f7cb464f35402202eac76fb6f47b16a7272337fd9feca28f5cf332529c89674464b407c71a0e93564f14f16e61b8da50e65e7693e6
7
+ data.tar.gz: 5e5ff5436b4cdd41c9ba7faee1ba558eacdcef2df6e0d33b448e23bbad73dc8dee56d7966e64dd0c0b7f8279a40a8ee5d65bd63b7d477aeb6663313e23ce0289
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Selective
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,55 @@
1
+ module Selective
2
+ module Ruby
3
+ module Minitest
4
+ module Monkeypatches
5
+ module Minitest
6
+ def autorun
7
+ # Noop. Rails calls autorun from: lib/active_support/testing/autorun.rb
8
+ # when rails/test_help is required in test helper (which is normal rails test setup)
9
+ end
10
+
11
+ # This is the first half of Minitest.run
12
+ def selective_prerun(args = [])
13
+ load_plugins unless args.delete("--no-plugins") || ENV["MT_NO_PLUGINS"]
14
+
15
+ options = process_args args
16
+
17
+ ::Minitest.seed = options[:seed]
18
+ srand ::Minitest.seed
19
+
20
+ reporter = ::Minitest::CompositeReporter.new
21
+ reporter << ::Minitest::SummaryReporter.new(options[:io], options)
22
+ reporter << ::Minitest::ProgressReporter.new(options[:io], options) unless options[:quiet]
23
+
24
+ self.reporter = reporter # this makes it available to plugins
25
+ init_plugins options.merge(report_name: ->(name) { "report-#{name}-#{SecureRandom.uuid}.xml" })
26
+ self.reporter = nil # runnables shouldn't depend on the reporter, ever
27
+
28
+ parallel_executor.start if parallel_executor.respond_to?(:start)
29
+ reporter.start
30
+ reporter
31
+ end
32
+
33
+ # This is the second half of ::Minitest.run
34
+ def selective_postrun(reporter, args = [])
35
+ options = process_args args
36
+
37
+ parallel_executor.shutdown
38
+
39
+ # might have been removed/replaced during init_plugins:
40
+ summary = reporter.reporters.grep(::Minitest::SummaryReporter).first
41
+
42
+ reporter.report
43
+
44
+ return empty_run! options if summary && summary.count == 0
45
+ reporter.passed?
46
+ end
47
+ end
48
+
49
+ def self.apply(config)
50
+ ::Minitest.singleton_class.prepend(Minitest)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,233 @@
1
+ require "tempfile"
2
+ require 'digest'
3
+ require "json"
4
+
5
+ module Selective
6
+ module Ruby
7
+ module Minitest
8
+ class RunnerWrapper
9
+ class TestManifestError < StandardError; end
10
+
11
+ attr_reader :test_case_callback, :reporter, :test_ids, :targeted_test_ids, :minitest_args, :test_map
12
+ attr_accessor :connection_lost
13
+
14
+ FRAMEWORK = "minitest"
15
+ DEFAULT_TEST_DIR = "test"
16
+
17
+ def initialize(args, test_case_callback)
18
+ @test_case_callback = test_case_callback
19
+
20
+ @targeted_test_ids, @minitest_args, wrapper_config_hash = parse_args(args)
21
+ Selective::Ruby::Minitest::Monkeypatches.apply(wrapper_config_hash)
22
+
23
+ configure
24
+ end
25
+
26
+ def manifest
27
+ raise_test_manifest_error("No test cases found") if test_ids.empty?
28
+
29
+ ids = @targeted_test_ids.any? ? @targeted_test_ids : test_ids
30
+ {
31
+ "test_cases" => ids.map do |test_id|
32
+ test = test_map[test_id]
33
+ {
34
+ id: test_id,
35
+ file_path: test[:file_path],
36
+ run_time: 0.0
37
+ }
38
+ end
39
+ }
40
+ end
41
+
42
+ def run_test_cases(test_case_ids)
43
+ test_case_ids.map do |test_id|
44
+ klass, method_name = get_test_from_map(test_id)
45
+ real_time = time { klass.run_one_method(klass, method_name, reporter) }
46
+ foo = format_test_case(test_id, klass, method_name, real_time)
47
+ test_case_callback.call(foo)
48
+ end
49
+ end
50
+
51
+ def remove_test_case_result(test_case_id)
52
+ file_path, method_name = test_case_id.split(":")
53
+
54
+ result = summary_reporter.results.detect do |r|
55
+ r.source_location.first.gsub(root_path, "") == file_path &&
56
+ r.name == method_name
57
+ end
58
+
59
+ result.failures = []
60
+
61
+ summary_reporter.results.delete(result)
62
+ end
63
+
64
+ def base_test_path
65
+ "./#{default_test_glob.split("/").first}"
66
+ end
67
+
68
+ def exit_status
69
+ failures.length.zero? ? 0 : 1
70
+ end
71
+
72
+ def finish
73
+ ::Minitest.selective_postrun(reporter, minitest_args)
74
+
75
+ # This usually happens in at_exit defined in autorun.
76
+ ::Minitest.class_variable_get(:@@after_run).reverse_each(&:call)
77
+ end
78
+
79
+ def framework
80
+ RunnerWrapper::FRAMEWORK
81
+ end
82
+
83
+ def framework_version
84
+ ::Minitest::VERSION
85
+ end
86
+
87
+ def wrapper_version
88
+ ::Minitest::VERSION
89
+ end
90
+
91
+ private
92
+
93
+ def time(&block)
94
+ Benchmark.measure(&block).real
95
+ end
96
+
97
+ def get_test_from_map(test_id)
98
+ t = test_map[test_id]
99
+
100
+ [t[:klass], t[:method_name]]
101
+
102
+ rescue
103
+ puts "Test not found in map: #{test_id}"
104
+ end
105
+
106
+ def summary_reporter
107
+ reporter.reporters.grep(::Minitest::SummaryReporter).first || # Minitest with no 3rd party reporter gems
108
+ reporter.reporters.first.send(:all_reporters)&.grep(::Minitest::Reporters::DefaultReporter)&.first # Minitest with minitest-reporters gem
109
+ end
110
+
111
+ def failures
112
+ summary_reporter.results
113
+ end
114
+
115
+ def failure_for(klass, method_name)
116
+ failures.last.then do |last_failure|
117
+ return if last_failure.nil?
118
+ return unless klass.to_s == last_failure.klass
119
+ return unless last_failure.name == method_name
120
+
121
+ last_failure
122
+ end
123
+ end
124
+
125
+ def format_test_case(test_id, klass, method_name, time)
126
+ failure = failure_for(klass, method_name)
127
+ test = test_map[test_id]
128
+
129
+ status = if failure.nil?
130
+ "passed"
131
+ else
132
+ failure.skipped? ? "pending" : "failed"
133
+ end
134
+
135
+ result = {
136
+ id: test_id,
137
+ description: method_name,
138
+ full_description: method_name,
139
+ status: status,
140
+ file_path: test[:file_path],
141
+ line_number: test[:line_number].to_i,
142
+ run_time: time
143
+ }
144
+
145
+ failure && result.merge!(
146
+ failure_message_lines: failure.to_s.split("\n"),
147
+ failure_formatted_backtrace: failure.failures&.first&.backtrace || []
148
+ )
149
+
150
+ result
151
+ end
152
+
153
+ def default_test_glob
154
+ ENV["DEFAULT_TEST"] || "#{DEFAULT_TEST_DIR}/**/*_test.rb"
155
+ end
156
+
157
+ def list_tests(patterns)
158
+ Rake::FileList[patterns.any? ? patterns : default_test_glob].then do |files|
159
+ include_pattern = ENV["INCLUDE_PATTERN"]
160
+ exclude_pattern = ENV["EXCLUDE_PATTERN"]
161
+
162
+ files = files.select { |file| file.match?(include_pattern) } if include_pattern
163
+ files = files.reject { |file| file.match?(exclude_pattern) } if exclude_pattern
164
+
165
+ files
166
+ end
167
+ end
168
+
169
+ def parse_args(args)
170
+ flag_args, test_ids = args.partition do |arg|
171
+ arg.start_with?("-")
172
+ end
173
+
174
+ supported_wrapper_args = %w[]
175
+ wrapper_args, minitest_args = flag_args.partition do |arg|
176
+ supported_wrapper_args.any? do |p|
177
+ arg.start_with?(p)
178
+ end
179
+ end
180
+
181
+ wrapper_config_hash = wrapper_args.each_with_object({}) do |arg, hash|
182
+ key = arg.sub("--", "").tr("-", "_").to_sym
183
+ hash[key] = true
184
+ end
185
+
186
+ [test_ids, minitest_args, wrapper_config_hash]
187
+ end
188
+
189
+ def normalize_path(path)
190
+ Pathname.new(path).relative_path_from("./")
191
+ end
192
+
193
+ def raise_test_manifest_error(output)
194
+ raise TestManifestError.new("Selective could not generate a test manifest. The output was:\n#{output}")
195
+ end
196
+
197
+ def root_path
198
+ "#{Dir.pwd}/"
199
+ end
200
+
201
+ def configure
202
+ targeted_test_paths = targeted_test_ids.map { |test_id| test_id.split(":").first }.uniq
203
+ tests = targeted_test_paths.any? ? targeted_test_paths : list_tests([])
204
+ $LOAD_PATH.unshift "./#{DEFAULT_TEST_DIR}"
205
+ tests.to_a.each { |path| require File.expand_path(path) }
206
+
207
+ @reporter = ::Minitest.selective_prerun(minitest_args)
208
+ suites = ::Minitest::Runnable.runnables.shuffle
209
+
210
+ @test_ids, @test_map, duplicate_test_ids = suites.each_with_object([[], {}, []]) do |klass, (test_ids, test_map, duplicate_test_ids)|
211
+ klass.runnable_methods.map do |method_name|
212
+ file_path, line_number = klass.instance_method(method_name).source_location
213
+ file_path = file_path.gsub(root_path, "")
214
+ digest = Digest::MD5.hexdigest("#{klass.name}##{method_name}")
215
+ test_id = "#{file_path}:#{digest}"
216
+
217
+ if test_map[test_id]
218
+ duplicate_test_ids << "#{file_path}:#{line_number}"
219
+ else
220
+ test_ids << test_id
221
+ test_map[test_id] = {klass: klass, method_name: method_name, file_path: file_path, line_number: line_number}
222
+ end
223
+ end
224
+ end
225
+
226
+ if duplicate_test_ids.any?
227
+ puts("\e[33mDuplicate test ids found. Please ensure unique test class/method names are used to ensure all tests are run. \n#{duplicate_test_ids.join("\n")}\e[0m")
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
233
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Selective
4
+ module Ruby
5
+ module Minitest
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+ require "minitest"
5
+ require "rake/file_list"
6
+ require "benchmark"
7
+ require "#{__dir__}/selective/ruby/minitest/version"
8
+
9
+ loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
10
+ loader.inflector.inflect("minitest" => "Minitest")
11
+ loader.ignore("#{__dir__}/selective-ruby-minitest.rb")
12
+ loader.ignore("#{__dir__}/selective/ruby/minitest/version.rb")
13
+ loader.setup
14
+
15
+ require "selective-ruby-core"
16
+
17
+ module Selective
18
+ module Ruby
19
+ module Minitest
20
+ class Error < StandardError; end
21
+
22
+ def self.register
23
+ Selective::Ruby::Core.register_runner(
24
+ "minitest", Selective::Ruby::Minitest::RunnerWrapper
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ Selective::Ruby::Minitest.register
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: selective-ruby-minitest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Wood
8
+ - Nate Vick
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2025-04-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: zeitwerk
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 2.6.12
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 2.6.12
28
+ - !ruby/object:Gem::Dependency
29
+ name: selective-ruby-core
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 0.2.5
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 0.2.5
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: Selective is an intelligent test runner for your current CI provider.
57
+ Get real-time test results, intelligent ordering based on code changes, shorter
58
+ run times, automatic flake detection, the ability to re-enqueue failed tests, and
59
+ more.
60
+ email:
61
+ - ben@hint.io
62
+ - nate@hint.io
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - LICENSE
68
+ - Rakefile
69
+ - lib/selective-ruby-minitest.rb
70
+ - lib/selective/ruby/minitest/monkeypatches.rb
71
+ - lib/selective/ruby/minitest/runner_wrapper.rb
72
+ - lib/selective/ruby/minitest/version.rb
73
+ homepage: https://www.selective.ci
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ homepage_uri: https://www.selective.ci
78
+ source_code_uri: http://github.com/selectiveci/selective-ruby-minitest
79
+ changelog_uri: https://github.com/selectiveci/selective-ruby-minitest/blob/main/CHANGELOG.md
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.6.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.5.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Selective Ruby Minitest Client
99
+ test_files: []