Module: Polyrun::RSpec

Defined in:
lib/polyrun/rspec.rb,
sig/polyrun/rspec.rbs

Overview

Optional RSpec wiring (require polyrun/rspec explicitly).

Class Method Summary collapse

Class Method Details

.install_example_timing!(output_path: nil) ⇒ void

This method returns an undefined value.

Experimental: add Timing::RSpecExampleFormatter and write per-example JSON (see timing_granularity: example). With output_path:, that path is used directly (no ENV mutation). Without it, the formatter reads ENV["POLYRUN_EXAMPLE_TIMING_OUT"] or defaults to polyrun_timing_examples.json.

Parameters:

  • output_path: (String, nil) (defaults to: nil)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/polyrun/rspec.rb', line 18

def install_example_timing!(output_path: nil)
  require_relative "timing/rspec_example_formatter"
  fmt =
    if output_path
      op = output_path
      Class.new(Polyrun::Timing::RSpecExampleFormatter) do
        ::RSpec::Core::Formatters.register self, :example_finished, :close

        define_method(:timing_output_path) { op }
      end
    else
      Polyrun::Timing::RSpecExampleFormatter
    end
  ::RSpec.configure do |config|
    config.add_formatter fmt
  end
end

.install_failure_fragments!(only_if: nil) ⇒ void

This method returns an undefined value.

Per-worker failure JSONL fragments for polyrun run-shards --merge-failures (parity with coverage shards). Requires POLYRUN_FAILURE_FRAGMENTS=1 (set by the parent when --merge-failures is used) unless only_if overrides. Writes tmp/polyrun_failures/polyrun-failure-fragment-*.jsonl (override dir with POLYRUN_FAILURE_FRAGMENT_DIR).

Parameters:

  • only_if: (Object, nil) (defaults to: nil)


39
40
41
42
43
44
45
46
47
48
# File 'lib/polyrun/rspec.rb', line 39

def install_failure_fragments!(only_if: nil)
  pred = only_if || -> { %w[1 true yes].include?(ENV["POLYRUN_FAILURE_FRAGMENTS"].to_s.downcase) }
  return unless pred.call

  require "rspec/core"
  require_relative "reporting/rspec_failure_fragment_formatter"
  ::RSpec.configure do |config|
    config.add_formatter Polyrun::Reporting::RspecFailureFragmentFormatter
  end
end

.install_parallel_provisioning!(rspec_config) ⇒ void

This method returns an undefined value.

Registers before(:suite) to run Data::ParallelProvisioning.run_suite_hooks!.

Parameters:

  • rspec_config (Object)


9
10
11
12
13
# File 'lib/polyrun/rspec.rb', line 9

def install_parallel_provisioning!(rspec_config)
  rspec_config.before(:suite) do
    Polyrun::Data::ParallelProvisioning.run_suite_hooks!
  end
end

.install_spec_quality!(only_if: nil, root: nil, output_path: nil) ⇒ Object

Writes WorkerPing after suite start, before/after each example (+location+ is file:line from metadata). Keeps --worker-idle-timeout sensitive to example progress (not only a background thread).



52
53
54
55
56
57
58
# File 'lib/polyrun/rspec.rb', line 52

def install_spec_quality!(only_if: nil, root: nil, output_path: nil)
  pred = only_if || -> { Polyrun::SpecQuality.enabled? }
  return unless pred.call

  require_relative "spec_quality/rspec_hook"
  Polyrun::SpecQuality::RspecHook.install!(only_if: pred, root: root, output_path: output_path)
end

.install_worker_ping!void

This method returns an undefined value.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/polyrun/rspec.rb', line 60

def install_worker_ping!
  require "rspec/core"
  require_relative "worker_ping"
  ::RSpec.configure do |config|
    config.before(:suite) { Polyrun::WorkerPing.ping! }
    config.before(:each) do |example|
      Polyrun::WorkerPing.ping!(location: example.[:location] || example.location)
    end
    config.after(:each) do |example|
      Polyrun::WorkerPing.ping!(location: example.[:location] || example.location)
    end
  end

  Polyrun::WorkerPing.ensure_interval_ping_thread!
end