selective-ruby-rspec 0.1.13 → 0.1.14

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: 238f5bcf12278f31375b5ec8aa214b5c481f51950e937150456282e32403da2c
4
- data.tar.gz: ed0b17ea2bd15e77415452f35ba220bb300ab4ef415fce39ae13e0eaaa125b04
3
+ metadata.gz: d7524f46dbca9e5e0c0dc307ae761984871502e20878f669984b86248828250a
4
+ data.tar.gz: f324310ab0cda28d8bb37cc1966d8f9de2ba1ddc562dd80f83884cf0aba358a8
5
5
  SHA512:
6
- metadata.gz: 283f02c6092c1f098e6e773a48ac4a3d5b96ad435e229e02d571e87d8c85ef24ce4f68e097ac1f0cff18fd23e03ded321907ad87f17c3cdaf0a6a2a5052b417b
7
- data.tar.gz: c1d52ca30661f273f15201bb33a2f038d231ed5e16e6e80661d948a73f6b781332c8c81289d769eada9fc2e164f6665e6e347af1356b379e43bab2fb16aedee8
6
+ metadata.gz: f1396482d924af9600111524b447b7eeb3608a20d1f9d5e08ef033b8ebf51eac9bffc77102132767520fa8da771bcd27fec5ace5bcfd8069cca8d809f844bf75
7
+ data.tar.gz: 01a843c327ffa4568fb3b8555ce6b84dc69ecb92eaf1b2289e6ecd94eaa882c7a66a9b82cb16876dff363bb0af34a0389e29bb7c65e32cd71c246da0441dabbc
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Selective
4
+ module Ruby
5
+ module RSpec
6
+ # Time in before(:all)/after(:all) hooks, which run outside any example,
7
+ # per group per batch. A group split across batches runs its hooks once
8
+ # per batch; `context_hook_rerun_ms` is the time past each group's first
9
+ # run, the cost of that split.
10
+ module ContextHookTiming
11
+ @seen = {}
12
+ @totals = {"context_hook_ms" => 0.0, "context_hook_rerun_ms" => 0.0, "context_hook_groups" => 0, "context_hook_runs" => 0}
13
+
14
+ class << self
15
+ # One call per group per batch for each of before and after; a
16
+ # group's second before-run is the first rerun.
17
+ def record(group, position, ms)
18
+ @totals["context_hook_ms"] += ms
19
+ if position == :before
20
+ @totals["context_hook_runs"] += 1
21
+ @seen[group] = @seen.key?(group) ? :rerun : :first
22
+ @totals["context_hook_groups"] += 1 if @seen[group] == :first
23
+ end
24
+ @totals["context_hook_rerun_ms"] += ms if @seen[group] == :rerun
25
+ end
26
+
27
+ def report
28
+ @totals.transform_values { |v| v.is_a?(Float) ? v.round : v }
29
+ end
30
+
31
+ def reset
32
+ @seen = {}
33
+ @totals.each_key { |k| @totals[k] = k.end_with?("_ms") ? 0.0 : 0 }
34
+ end
35
+ end
36
+
37
+ # Prepended to ExampleGroup's singleton class: per-group class methods.
38
+ module GroupMethods
39
+ def run_before_context_hooks(example_group_instance)
40
+ context_hooks? ? timed(:before) { super } : super
41
+ end
42
+
43
+ def run_after_context_hooks(example_group_instance)
44
+ context_hooks? ? timed(:after) { super } : super
45
+ end
46
+
47
+ private
48
+
49
+ # RSpec creates these collections when the first hook is registered.
50
+ def context_hooks?
51
+ hooks.instance_variable_get(:@before_context_hooks) || hooks.instance_variable_get(:@after_context_hooks)
52
+ end
53
+
54
+ def timed(position)
55
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
56
+ yield
57
+ ensure
58
+ ContextHookTiming.record(self, position, (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Selective
4
+ module Ruby
5
+ module RSpec
6
+ # The examples a dry run of the loaded suite would list, read from this
7
+ # process's world: RSpec's own filtering over every loaded group, with
8
+ # nothing run. Captured in World#announce_filters, which clears the
9
+ # groups (and can clear inclusion filters) right after.
10
+ class Manifest
11
+ attr_reader :examples
12
+
13
+ def self.capture(world)
14
+ new(world).tap(&:capture)
15
+ end
16
+
17
+ def initialize(world)
18
+ @world = world
19
+ @load_errors = world.selective_load_errors.dup
20
+ @quitting = world.wants_to_quit || (world.respond_to?(:rspec_is_quitting) && world.rspec_is_quitting)
21
+ @examples = []
22
+ end
23
+
24
+ def capture
25
+ filter_manager = @world.filter_manager
26
+ examples = select(filter_manager)
27
+
28
+ if examples.empty? && run_all_when_everything_filtered?
29
+ # announce_filters does the same next, in this process as in a dry run.
30
+ filter_manager.inclusions.clear
31
+ examples = select(filter_manager)
32
+ end
33
+
34
+ @examples = examples.map do |example|
35
+ {
36
+ "id" => example.id,
37
+ "file_path" => example.metadata[:file_path],
38
+ "line_number" => example.metadata[:line_number]
39
+ }
40
+ end
41
+ end
42
+
43
+ def failed_to_load?
44
+ @quitting || @load_errors.any?
45
+ end
46
+
47
+ # As RSpec prints them.
48
+ def load_errors
49
+ @load_errors.map do |exception, context_description|
50
+ example = ::RSpec::Core::Example.new(::RSpec::Core::AnonymousExampleGroup, context_description, {})
51
+ ::RSpec::Core::Formatters::ExceptionPresenter.new(exception, example, indentation: 0).fully_formatted(nil)
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def select(filter_manager)
58
+ @world.example_groups.flat_map(&:descendants).flat_map do |group|
59
+ filter_manager.prune(group.examples)
60
+ end
61
+ end
62
+
63
+ # Removed in RSpec 4.
64
+ def run_all_when_everything_filtered?
65
+ configuration = ::RSpec.configuration
66
+ configuration.respond_to?(:run_all_when_everything_filtered?) &&
67
+ configuration.run_all_when_everything_filtered? && !configuration.only_failures?
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -20,6 +20,11 @@ module Selective
20
20
  end
21
21
 
22
22
  module Reporter
23
+ def notify_non_example_exception(exception, context_description)
24
+ ::RSpec.world.selective_load_errors << [exception, context_description] unless ::RSpec.world.selective_manifest
25
+ super
26
+ end
27
+
23
28
  def finish
24
29
  return if Selective::Ruby::Core::Controller.suppress_reporting?
25
30
  # This handles the scenario of no tests to run
@@ -101,12 +106,22 @@ module Selective
101
106
 
102
107
  module World
103
108
  attr_accessor :example_map
109
+ attr_reader :selective_manifest
104
110
 
105
111
  def initialize(*args)
106
112
  super
107
113
  @example_map = {}
108
114
  end
109
115
 
116
+ def selective_load_errors
117
+ @selective_load_errors ||= []
118
+ end
119
+
120
+ def announce_filters
121
+ @selective_manifest ||= Manifest.capture(self) unless ::RSpec.configuration.dry_run?
122
+ super
123
+ end
124
+
110
125
  def prepare_example_filtering
111
126
  @filtered_examples = Hash.new do |hash, group|
112
127
  hash[group] = if ::RSpec.configuration.dry_run?
@@ -221,6 +236,7 @@ module Selective
221
236
  ::RSpec::Core::ExampleGroup.extend(Hooks)
222
237
  end
223
238
 
239
+ ::RSpec::Core::ExampleGroup.singleton_class.prepend(ContextHookTiming::GroupMethods) if config[:preserve_all_hooks]
224
240
  ::RSpec.singleton_class.prepend(RSpec)
225
241
  ::RSpec::Core::Reporter.prepend(Reporter)
226
242
  ::RSpec::Core::Configuration.prepend(Configuration)
@@ -1,6 +1,3 @@
1
- require "tempfile"
2
- require "json"
3
-
4
1
  module Selective
5
2
  module Ruby
6
3
  module RSpec
@@ -16,6 +13,7 @@ module Selective
16
13
  def initialize(args, example_callback)
17
14
  @example_callback = example_callback
18
15
  rspec_args, wrapper_config_hash = parse_args(args)
16
+ @preserve_all_hooks = wrapper_config_hash[:preserve_all_hooks] == true
19
17
  Selective::Ruby::RSpec::Monkeypatches.apply(wrapper_config_hash)
20
18
  apply_rspec_configuration
21
19
 
@@ -31,19 +29,11 @@ module Selective
31
29
  end
32
30
 
33
31
  def manifest
34
- output = nil
35
- Tempfile.create("selective-rspec-dry-run") do |f|
36
- quoted_paths = config.options[:files_or_directories_to_run].map { |path| "'#{path}'" }.join(" ")
37
- output = `bundle exec selective exec rspec #{quoted_paths} --format=json --out=#{f.path} --dry-run`
38
- JSON.parse(f.read).tap do |content|
39
- if content["examples"].empty?
40
- message = content["messages"]&.first
41
- raise_test_manifest_error(message || "No examples found")
42
- end
43
- end
44
- end
45
- rescue JSON::ParserError => e
46
- raise_test_manifest_error(e.message)
32
+ captured = ::RSpec.world.selective_manifest || Manifest.capture(::RSpec.world)
33
+ raise_test_manifest_error(captured.load_errors.join("\n")) if captured.failed_to_load?
34
+ raise_test_manifest_error("No examples found") if captured.examples.empty?
35
+
36
+ {"examples" => captured.examples}
47
37
  end
48
38
 
49
39
  def run_test_cases(test_case_ids)
@@ -52,6 +42,7 @@ module Selective
52
42
  groups = optimize_test_filtering(test_case_ids).to_a
53
43
  reset_group_memoization(groups)
54
44
  rspec_runner.run_specs(groups)
45
+ Selective::Ruby::Core::Controller.report_at_finish.merge!(ContextHookTiming.report) if @preserve_all_hooks
55
46
  if connection_lost
56
47
  self.connection_lost = false
57
48
  raise Selective::Ruby::Core::ConnectionLostError
@@ -3,7 +3,7 @@
3
3
  module Selective
4
4
  module Ruby
5
5
  module RSpec
6
- VERSION = "0.1.13"
6
+ VERSION = "0.1.14"
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selective-ruby-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Wood
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2026-11-19 00:00:00.000000000 Z
12
+ date: 2026-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: zeitwerk
@@ -75,7 +75,9 @@ files:
75
75
  - LICENSE
76
76
  - Rakefile
77
77
  - lib/selective-ruby-rspec.rb
78
+ - lib/selective/ruby/rspec/context_hook_timing.rb
78
79
  - lib/selective/ruby/rspec/formatter.rb
80
+ - lib/selective/ruby/rspec/manifest.rb
79
81
  - lib/selective/ruby/rspec/monkeypatches.rb
80
82
  - lib/selective/ruby/rspec/runner_wrapper.rb
81
83
  - lib/selective/ruby/rspec/version.rb