Module: Polyrun::SpecQuality

Defined in:
lib/polyrun/spec_quality.rb,
lib/polyrun/spec_quality/merge.rb,
lib/polyrun/spec_quality/config.rb,
lib/polyrun/spec_quality/report.rb,
lib/polyrun/spec_quality/profile.rb,
lib/polyrun/spec_quality/fragment.rb,
lib/polyrun/spec_quality/rspec_hook.rb,
lib/polyrun/spec_quality/plan_loader.rb,
lib/polyrun/spec_quality/sql_counter.rb,
lib/polyrun/spec_quality/minitest_hook.rb

Overview

Per-example spec quality: coverage line deltas, resource use, optional SQL counts. Opt-in with POLYRUN_SPEC_QUALITY=1 or Polyrun::SpecQuality.start!.

Defined Under Namespace

Modules: Config, Fragment, Merge, MinitestHook, PlanLoader, Profile, Report, RspecHook, SqlCounter

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



44
45
46
# File 'lib/polyrun/spec_quality.rb', line 44

def config
  @config
end

.output_pathObject (readonly)

Returns the value of attribute output_path.



46
47
48
# File 'lib/polyrun/spec_quality.rb', line 46

def output_path
  @output_path
end

Class Method Details

.enabled?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/polyrun/spec_quality.rb', line 40

def enabled?
  Config.enabled? && !Config.disabled?
end

.finish_example!(location: nil, pending: false) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/polyrun/spec_quality.rb', line 75

def finish_example!(location: nil, pending: false)
  return unless started?
  cur = @current
  @current = nil
  return if cur.nil? || paused?
  return if pending

  loc = location || cur[:location]
  return if loc.nil? || loc.to_s.empty?

  delta = coverage_delta_for_example(cur)

  profile_after = Profile.snapshot(dimensions: @config["profile"])
  profile_delta = Profile.diff(cur[:profile_before], profile_after)
  wall = Process.clock_gettime(Process::CLOCK_MONOTONIC) - cur[:wall_start]
  profile_delta["wall"] = wall

  factory_counts =
    if defined?(Polyrun::Data::FactoryCounts)
      Polyrun::Data::FactoryCounts.example_counts
    else
      {}
    end

  row = build_row(cur, loc, delta, profile_delta, factory_counts)
  Fragment.append_row!(@output_path, row)
  row
end

.pauseObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/polyrun/spec_quality.rb', line 104

def pause
  @pause_depth += 1
  if block_given?
    begin
      yield
    ensure
      resume
    end
  end
end

.paused?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/polyrun/spec_quality.rb', line 119

def paused?
  @pause_depth.positive?
end

.record_sql!(sql) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/polyrun/spec_quality.rb', line 123

def record_sql!(sql)
  return unless recording?

  @current[:sql_count] += 1
  fp = normalize_sql(sql)
  @current[:sql_fingerprints][fp] += 1
end

.recording?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/polyrun/spec_quality.rb', line 48

def recording?
  started? && @current && @pause_depth.zero?
end

.resumeObject



115
116
117
# File 'lib/polyrun/spec_quality.rb', line 115

def resume
  @pause_depth -= 1 if @pause_depth.positive?
end

.spec_quality_requested_for_quick?(root = Dir.pwd) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
# File 'lib/polyrun/spec_quality.rb', line 131

def spec_quality_requested_for_quick?(root = Dir.pwd)
  return false if Config.disabled?
  return true if %w[1 true yes].include?(ENV["POLYRUN_SPEC_QUALITY"]&.to_s&.downcase)
  return true if %w[1 true yes].include?(ENV["POLYRUN_SPEC_QUALITY_FRAGMENTS"]&.to_s&.downcase)

  path = File.join(File.expand_path(root), Config::DEFAULT_CONFIG_RELATIVE)
  return false unless File.file?(path)

  %w[1 true yes].include?(ENV["POLYRUN_QUICK_SPEC_QUALITY"]&.to_s&.downcase)
end

.start!(root: Dir.pwd, config_path: nil, output_path: nil, **overrides) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/polyrun/spec_quality.rb', line 22

def start!(root: Dir.pwd, config_path: nil, output_path: nil, **overrides)
  return if Config.disabled?

  @config = Config.load(root: root, config_path: config_path, **overrides)
  @output_path = output_path || Fragment.default_fragment_path
  @pause_depth = 0
  @current = nil
  @rng = Random.new(Process.pid ^ Time.now.to_i)

  Fragment.truncate_fragment!(@output_path) unless fragment_append_mode?
  SqlCounter.install! if @config["sql_counter"]
  self
end

.start_example!(location:, wall_start: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/polyrun/spec_quality.rb', line 52

def start_example!(location:, wall_start: nil)
  return unless started?
  return if paused?
  return if Config.ignored_example?(location, @config["ignore_examples"])
  return unless sample_example?

  @current = {
    location: normalize_location(location),
    wall_start: wall_start || Process.clock_gettime(Process::CLOCK_MONOTONIC),
    coverage_before: Coverage::ExampleDiff.peek_blob(
      root: @config["root"],
      track_under: @config["track_under"],
      ignore_paths: @config["ignore_paths"]
    ),
    profile_before: Profile.snapshot(dimensions: @config["profile"]),
    sql_count: 0,
    sql_fingerprints: Hash.new(0),
    factory_counts: {}
  }
  Polyrun::Data::FactoryCounts.reset_example! if defined?(Polyrun::Data::FactoryCounts)
  nil
end

.started?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/polyrun/spec_quality.rb', line 36

def started?
  instance_variable_defined?(:@config) && @config
end