File: sample.rb

package info (click to toggle)
ruby-test-prof 0.12.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 508 kB
  • sloc: ruby: 4,075; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,249 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

module TestProf
  # Instance variable writer for RSpec::Core::World
  module RSpecWorldSamplePatch
    def filtered_examples=(val)
      @filtered_examples = val
    end
  end
end

if ENV["SAMPLE"]
  RSpec::Core::World.include(TestProf::RSpecWorldSamplePatch)

  RSpec.configure do |config|
    config.before(:suite) do
      filtered_examples = RSpec.world.filtered_examples.values.flatten
      random = Random.new(RSpec.configuration.seed)
      sample = filtered_examples.sample(ENV["SAMPLE"].to_i, random: random)
      RSpec.world.filtered_examples = Hash.new do |hash, group|
        hash[group] = group.examples & sample
      end
    end
  end
end

if ENV["SAMPLE_GROUPS"]
  RSpec::Core::World.include(TestProf::RSpecWorldSamplePatch)

  RSpec.configure do |config|
    config.before(:suite) do
      filtered_groups = RSpec.world.filtered_examples.reject do |_group, examples|
        examples.empty?
      end.keys
      random = Random.new(RSpec.configuration.seed)
      sample = filtered_groups.sample(ENV["SAMPLE_GROUPS"].to_i, random: random)
      RSpec.world.filtered_examples = Hash.new do |hash, group|
        hash[group] = sample.include?(group) ? group.examples : []
      end
    end
  end
end