polyrun 3.2.0-java

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.
Files changed (225) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +234 -0
  3. data/CODE_OF_CONDUCT.md +26 -0
  4. data/CONTRIBUTING.md +23 -0
  5. data/LICENSE +21 -0
  6. data/README.md +251 -0
  7. data/SECURITY.md +25 -0
  8. data/bin/polyrun +6 -0
  9. data/docs/QUALITY.md +82 -0
  10. data/docs/QUALITY_CONFIGURATION.md +95 -0
  11. data/docs/SETUP_PROFILE.md +139 -0
  12. data/lib/polyrun/benchmark/pressure/cooker.rb +191 -0
  13. data/lib/polyrun/benchmark/pressure/process_sample.rb +127 -0
  14. data/lib/polyrun/benchmark/pressure.rb +144 -0
  15. data/lib/polyrun/benchmark/profile.rb +191 -0
  16. data/lib/polyrun/benchmark/report.rb +87 -0
  17. data/lib/polyrun/cli/benchmark_commands.rb +66 -0
  18. data/lib/polyrun/cli/ci_shard_hooks.rb +129 -0
  19. data/lib/polyrun/cli/ci_shard_run_command.rb +177 -0
  20. data/lib/polyrun/cli/ci_shard_run_parse.rb +68 -0
  21. data/lib/polyrun/cli/config_command.rb +42 -0
  22. data/lib/polyrun/cli/coverage_commands.rb +150 -0
  23. data/lib/polyrun/cli/coverage_merge_io.rb +150 -0
  24. data/lib/polyrun/cli/database_commands.rb +163 -0
  25. data/lib/polyrun/cli/default_run.rb +144 -0
  26. data/lib/polyrun/cli/doctor_command.rb +94 -0
  27. data/lib/polyrun/cli/env_commands.rb +43 -0
  28. data/lib/polyrun/cli/failure_commands.rb +99 -0
  29. data/lib/polyrun/cli/graph_command.rb +68 -0
  30. data/lib/polyrun/cli/help.rb +83 -0
  31. data/lib/polyrun/cli/helpers.rb +162 -0
  32. data/lib/polyrun/cli/hooks_command.rb +97 -0
  33. data/lib/polyrun/cli/init_command.rb +108 -0
  34. data/lib/polyrun/cli/mutation_command.rb +67 -0
  35. data/lib/polyrun/cli/partition_diagnostics.rb +22 -0
  36. data/lib/polyrun/cli/plan_command.rb +184 -0
  37. data/lib/polyrun/cli/prepare_command.rb +70 -0
  38. data/lib/polyrun/cli/prepare_recipe.rb +82 -0
  39. data/lib/polyrun/cli/quality_commands.rb +65 -0
  40. data/lib/polyrun/cli/queue_command.rb +161 -0
  41. data/lib/polyrun/cli/quick_command.rb +13 -0
  42. data/lib/polyrun/cli/report_commands.rb +108 -0
  43. data/lib/polyrun/cli/run_queue_command.rb +127 -0
  44. data/lib/polyrun/cli/run_shards_command.rb +164 -0
  45. data/lib/polyrun/cli/run_shards_parallel_children.rb +112 -0
  46. data/lib/polyrun/cli/run_shards_parallel_wait.rb +252 -0
  47. data/lib/polyrun/cli/run_shards_plan_boot_phases.rb +207 -0
  48. data/lib/polyrun/cli/run_shards_plan_options.rb +74 -0
  49. data/lib/polyrun/cli/run_shards_planning.rb +123 -0
  50. data/lib/polyrun/cli/run_shards_run.rb +171 -0
  51. data/lib/polyrun/cli/run_shards_worker_interrupt.rb +101 -0
  52. data/lib/polyrun/cli/skip_env.rb +34 -0
  53. data/lib/polyrun/cli/spec_quality_commands.rb +149 -0
  54. data/lib/polyrun/cli/start_bootstrap.rb +126 -0
  55. data/lib/polyrun/cli/timing_command.rb +31 -0
  56. data/lib/polyrun/cli/tool_options.rb +48 -0
  57. data/lib/polyrun/cli/validate_command.rb +26 -0
  58. data/lib/polyrun/cli.rb +272 -0
  59. data/lib/polyrun/config/dotted_path.rb +21 -0
  60. data/lib/polyrun/config/effective.rb +82 -0
  61. data/lib/polyrun/config/resolver.rb +114 -0
  62. data/lib/polyrun/config/schema.rb +39 -0
  63. data/lib/polyrun/config/suite.rb +109 -0
  64. data/lib/polyrun/config/tool_defaults.rb +42 -0
  65. data/lib/polyrun/config/tool_validation.rb +79 -0
  66. data/lib/polyrun/config/tools.rb +120 -0
  67. data/lib/polyrun/config.rb +97 -0
  68. data/lib/polyrun/coverage/cobertura_zero_lines.rb +32 -0
  69. data/lib/polyrun/coverage/collector.rb +169 -0
  70. data/lib/polyrun/coverage/collector_blob.rb +53 -0
  71. data/lib/polyrun/coverage/collector_finish.rb +98 -0
  72. data/lib/polyrun/coverage/collector_fragment_meta.rb +57 -0
  73. data/lib/polyrun/coverage/config_resolve.rb +131 -0
  74. data/lib/polyrun/coverage/example_diff.rb +184 -0
  75. data/lib/polyrun/coverage/example_diff_snapshot.rb +44 -0
  76. data/lib/polyrun/coverage/example_diff_track_filter.rb +51 -0
  77. data/lib/polyrun/coverage/file_stats_report.rb +36 -0
  78. data/lib/polyrun/coverage/filter.rb +22 -0
  79. data/lib/polyrun/coverage/formatter.rb +137 -0
  80. data/lib/polyrun/coverage/merge/formatters.rb +188 -0
  81. data/lib/polyrun/coverage/merge/formatters_html.rb +221 -0
  82. data/lib/polyrun/coverage/merge/html/_file_list.html.erb +21 -0
  83. data/lib/polyrun/coverage/merge/html/_file_section.html.erb +26 -0
  84. data/lib/polyrun/coverage/merge/html/_groups_table.html.erb +18 -0
  85. data/lib/polyrun/coverage/merge/html/_overview.html.erb +47 -0
  86. data/lib/polyrun/coverage/merge/html/report.css +147 -0
  87. data/lib/polyrun/coverage/merge/html/report.js +48 -0
  88. data/lib/polyrun/coverage/merge/html/template.html.erb +30 -0
  89. data/lib/polyrun/coverage/merge.rb +160 -0
  90. data/lib/polyrun/coverage/merge_fragment_meta.rb +58 -0
  91. data/lib/polyrun/coverage/merge_merge_two.rb +168 -0
  92. data/lib/polyrun/coverage/merge_native.rb +39 -0
  93. data/lib/polyrun/coverage/rails.rb +126 -0
  94. data/lib/polyrun/coverage/reporting.rb +41 -0
  95. data/lib/polyrun/coverage/result.rb +18 -0
  96. data/lib/polyrun/coverage/track_files.rb +150 -0
  97. data/lib/polyrun/data/cached_fixtures.rb +122 -0
  98. data/lib/polyrun/data/factory_counts.rb +48 -0
  99. data/lib/polyrun/data/factory_instrumentation.rb +50 -0
  100. data/lib/polyrun/data/fixtures.rb +68 -0
  101. data/lib/polyrun/data/parallel_provisioning.rb +93 -0
  102. data/lib/polyrun/data/snapshot.rb +84 -0
  103. data/lib/polyrun/database/clone_shards.rb +106 -0
  104. data/lib/polyrun/database/provision.rb +83 -0
  105. data/lib/polyrun/database/shard.rb +64 -0
  106. data/lib/polyrun/database/url_builder/connection/infer.rb +53 -0
  107. data/lib/polyrun/database/url_builder/connection/url_builders.rb +43 -0
  108. data/lib/polyrun/database/url_builder/connection.rb +191 -0
  109. data/lib/polyrun/database/url_builder/template_prepare.rb +21 -0
  110. data/lib/polyrun/database/url_builder.rb +188 -0
  111. data/lib/polyrun/debug.rb +81 -0
  112. data/lib/polyrun/env/ci.rb +65 -0
  113. data/lib/polyrun/error.rb +3 -0
  114. data/lib/polyrun/export/csv.rb +25 -0
  115. data/lib/polyrun/export/markdown.rb +47 -0
  116. data/lib/polyrun/graph/client.rb +64 -0
  117. data/lib/polyrun/graph/error.rb +13 -0
  118. data/lib/polyrun/graph/index.rb +75 -0
  119. data/lib/polyrun/graph/inspect.rb +58 -0
  120. data/lib/polyrun/graph/plan.rb +134 -0
  121. data/lib/polyrun/graph/planner.rb +117 -0
  122. data/lib/polyrun/graph/syntax.rb +114 -0
  123. data/lib/polyrun/graph/transaction.rb +81 -0
  124. data/lib/polyrun/graph/worker.rb +55 -0
  125. data/lib/polyrun/graph/workspace.rb +117 -0
  126. data/lib/polyrun/hooks/dsl.rb +128 -0
  127. data/lib/polyrun/hooks/shell_runner.rb +33 -0
  128. data/lib/polyrun/hooks/worker_runner.rb +27 -0
  129. data/lib/polyrun/hooks/worker_shell.rb +50 -0
  130. data/lib/polyrun/hooks.rb +190 -0
  131. data/lib/polyrun/log.rb +86 -0
  132. data/lib/polyrun/minitest_support.rb +60 -0
  133. data/lib/polyrun/mutation/candidates.rb +91 -0
  134. data/lib/polyrun/mutation/operators.rb +51 -0
  135. data/lib/polyrun/mutation/runner.rb +133 -0
  136. data/lib/polyrun/mutation/subjects.rb +88 -0
  137. data/lib/polyrun/partition/constraints.rb +80 -0
  138. data/lib/polyrun/partition/hrw.rb +70 -0
  139. data/lib/polyrun/partition/min_heap.rb +64 -0
  140. data/lib/polyrun/partition/paths.rb +175 -0
  141. data/lib/polyrun/partition/paths_build.rb +141 -0
  142. data/lib/polyrun/partition/plan.rb +250 -0
  143. data/lib/polyrun/partition/plan_lpt.rb +91 -0
  144. data/lib/polyrun/partition/plan_sharding.rb +56 -0
  145. data/lib/polyrun/partition/reports.rb +139 -0
  146. data/lib/polyrun/partition/stable_shuffle.rb +18 -0
  147. data/lib/polyrun/partition/suite.rb +120 -0
  148. data/lib/polyrun/partition/timing_diagnostics.rb +138 -0
  149. data/lib/polyrun/partition/timing_keys.rb +86 -0
  150. data/lib/polyrun/prepare/artifacts.rb +40 -0
  151. data/lib/polyrun/prepare/assets.rb +64 -0
  152. data/lib/polyrun/process_stdio.rb +96 -0
  153. data/lib/polyrun/quality/command.rb +84 -0
  154. data/lib/polyrun/quality/measure.rb +38 -0
  155. data/lib/polyrun/quality/observe.rb +53 -0
  156. data/lib/polyrun/quality/observe_hook.rb +103 -0
  157. data/lib/polyrun/quality/sandbox.rb +70 -0
  158. data/lib/polyrun/queue/duration.rb +30 -0
  159. data/lib/polyrun/queue/file_store.rb +413 -0
  160. data/lib/polyrun/queue/file_store_intent.rb +188 -0
  161. data/lib/polyrun/queue/file_store_pending.rb +48 -0
  162. data/lib/polyrun/queue/worker_loop.rb +73 -0
  163. data/lib/polyrun/quick/assertions.rb +32 -0
  164. data/lib/polyrun/quick/config.rb +56 -0
  165. data/lib/polyrun/quick/errors.rb +6 -0
  166. data/lib/polyrun/quick/example_group.rb +111 -0
  167. data/lib/polyrun/quick/example_runner.rb +129 -0
  168. data/lib/polyrun/quick/matchers.rb +159 -0
  169. data/lib/polyrun/quick/matchers_predicates.rb +122 -0
  170. data/lib/polyrun/quick/reporter.rb +76 -0
  171. data/lib/polyrun/quick/runner.rb +224 -0
  172. data/lib/polyrun/quick.rb +1 -0
  173. data/lib/polyrun/railtie.rb +7 -0
  174. data/lib/polyrun/reporting/failure_merge.rb +171 -0
  175. data/lib/polyrun/reporting/junit.rb +126 -0
  176. data/lib/polyrun/reporting/junit_emit.rb +58 -0
  177. data/lib/polyrun/reporting/junit_report.rb +87 -0
  178. data/lib/polyrun/reporting/rspec_failure_fragment_formatter.rb +95 -0
  179. data/lib/polyrun/reporting/rspec_junit.rb +39 -0
  180. data/lib/polyrun/rspec_support/active_record_timeout_recovery.rb +51 -0
  181. data/lib/polyrun/rspec_support/example_debug.rb +167 -0
  182. data/lib/polyrun/rspec_support/example_debug_instrumentation.rb +73 -0
  183. data/lib/polyrun/rspec_support/sharded_formatter_compat.rb +57 -0
  184. data/lib/polyrun/rspec_support.rb +110 -0
  185. data/lib/polyrun/spec_quality/config.rb +139 -0
  186. data/lib/polyrun/spec_quality/fragment.rb +39 -0
  187. data/lib/polyrun/spec_quality/merge.rb +78 -0
  188. data/lib/polyrun/spec_quality/minitest_hook.rb +42 -0
  189. data/lib/polyrun/spec_quality/plan_loader.rb +47 -0
  190. data/lib/polyrun/spec_quality/profile.rb +102 -0
  191. data/lib/polyrun/spec_quality/report.rb +363 -0
  192. data/lib/polyrun/spec_quality/rspec_hook.rb +55 -0
  193. data/lib/polyrun/spec_quality/sql_counter.rb +34 -0
  194. data/lib/polyrun/spec_quality.rb +212 -0
  195. data/lib/polyrun/sus_support.rb +55 -0
  196. data/lib/polyrun/templates/POLYRUN.md +51 -0
  197. data/lib/polyrun/templates/ci_matrix.polyrun.yml +22 -0
  198. data/lib/polyrun/templates/minimal_gem.polyrun.yml +13 -0
  199. data/lib/polyrun/templates/polyrun_hooks_spec_quality.rb +12 -0
  200. data/lib/polyrun/templates/polyrun_spec_quality.yml +20 -0
  201. data/lib/polyrun/templates/rails_prepare.polyrun.yml +51 -0
  202. data/lib/polyrun/templates/rails_prepare_task.rake +16 -0
  203. data/lib/polyrun/timing/merge.rb +36 -0
  204. data/lib/polyrun/timing/rspec_example_formatter.rb +60 -0
  205. data/lib/polyrun/timing/stats.rb +76 -0
  206. data/lib/polyrun/timing/summary.rb +78 -0
  207. data/lib/polyrun/timing/variance_report.rb +51 -0
  208. data/lib/polyrun/version.rb +3 -0
  209. data/lib/polyrun/worker_output.rb +181 -0
  210. data/lib/polyrun/worker_output_forwarders.rb +94 -0
  211. data/lib/polyrun/worker_ping.rb +77 -0
  212. data/lib/polyrun.rb +59 -0
  213. data/polyrun.gemspec +49 -0
  214. data/sig/polyrun/cli.rbs +6 -0
  215. data/sig/polyrun/config.rbs +20 -0
  216. data/sig/polyrun/debug.rbs +12 -0
  217. data/sig/polyrun/log.rbs +12 -0
  218. data/sig/polyrun/minitest_support.rbs +7 -0
  219. data/sig/polyrun/quick.rbs +19 -0
  220. data/sig/polyrun/rspec_support.rbs +21 -0
  221. data/sig/polyrun/sus_support.rbs +7 -0
  222. data/sig/polyrun/worker_output.rbs +11 -0
  223. data/sig/polyrun/worker_ping.rbs +10 -0
  224. data/sig/polyrun.rbs +11 -0
  225. metadata +463 -0
@@ -0,0 +1,167 @@
1
+ require "logger"
2
+ require "timeout"
3
+ require_relative "example_debug_instrumentation"
4
+
5
+ module Polyrun
6
+ module RSpecSupport
7
+ # Per-example RSpec debugging for local investigation (SQL, TracePoint, timeouts).
8
+ #
9
+ # +POLYRUN_EXAMPLE_DEBUG=1+ enables print helpers, Rails logging, and disables per-example timeouts.
10
+ # +POLYRUN_DEBUG_SQL=1+ or legacy +DEBUG_SQL=1+ logs mutating SQL.
11
+ # +POLYRUN_DEBUG_TRACE=1+ or legacy +DEBUG_TRACE=1+ traces :call/:raise under the app root.
12
+ # +DEBUG_PROSOPITE=1+ runs Prosopite N+1 scans when the gem is loaded.
13
+ # +DEBUG_LOG_LEVEL+ accepts Ruby Logger severities as integers (0 debug … 4 fatal) or names.
14
+ module ExampleDebug
15
+ LOG_LEVEL_BY_NAME = {
16
+ "debug" => Logger::DEBUG,
17
+ "info" => Logger::INFO,
18
+ "warn" => Logger::WARN,
19
+ "error" => Logger::ERROR,
20
+ "fatal" => Logger::FATAL
21
+ }.freeze
22
+
23
+ module_function
24
+
25
+ def enabled?
26
+ truthy?(ENV["POLYRUN_EXAMPLE_DEBUG"])
27
+ end
28
+
29
+ def sql_enabled?
30
+ truthy?(ENV["POLYRUN_DEBUG_SQL"]) || truthy?(ENV["DEBUG_SQL"])
31
+ end
32
+
33
+ def trace_enabled?
34
+ truthy?(ENV["POLYRUN_DEBUG_TRACE"]) || truthy?(ENV["DEBUG_TRACE"])
35
+ end
36
+
37
+ def prosopite_enabled?
38
+ truthy?(ENV["DEBUG_PROSOPITE"])
39
+ end
40
+
41
+ def print_spec_enabled?
42
+ enabled? && truthy?(ENV["DEBUG_PRINT_SPEC"])
43
+ end
44
+
45
+ def example_timeout_disabled?
46
+ enabled?
47
+ end
48
+
49
+ def log_level
50
+ parse_log_level(ENV.fetch("DEBUG_LOG_LEVEL", "debug"))
51
+ end
52
+
53
+ def rails_log_level
54
+ case log_level
55
+ when Logger::DEBUG then :debug
56
+ when Logger::INFO then :info
57
+ when Logger::WARN then :warn
58
+ when Logger::ERROR then :error
59
+ else :fatal
60
+ end
61
+ end
62
+
63
+ def parse_log_level(raw)
64
+ normalized = raw.to_s.strip.downcase
65
+ return LOG_LEVEL_BY_NAME.fetch(normalized) if LOG_LEVEL_BY_NAME.key?(normalized)
66
+
67
+ Integer(normalized)
68
+ rescue ArgumentError
69
+ Logger::DEBUG
70
+ end
71
+
72
+ def install!(rspec_config: fetch_rspec_configuration!)
73
+ install_spec_path_helpers!(rspec_config)
74
+ install_sql_debug!(rspec_config) if sql_enabled?
75
+ install_trace_debug!(rspec_config) if trace_enabled?
76
+ end
77
+
78
+ def install_rails_logging!(rspec_config: fetch_rspec_configuration!)
79
+ return unless enabled?
80
+ return unless defined?(Rails)
81
+
82
+ rspec_config.before do |example|
83
+ group = example.metadata[:example_group]
84
+ Polyrun::Log.puts "\n\nRunning #{group[:file_path]}:#{group[:line_number]}"
85
+
86
+ level = ExampleDebug.log_level
87
+ Rails.logger.level = level
88
+ if defined?(ActiveRecord::Base)
89
+ ar_logger = Logger.new(Polyrun::Log.stdout)
90
+ ar_logger.level = level
91
+ ActiveRecord::Base.logger = ar_logger
92
+ end
93
+ end
94
+ end
95
+
96
+ def install_example_timeout!(
97
+ rspec_config,
98
+ seconds: ENV.fetch("RSPEC_EXAMPLE_TIMEOUT_SEC", "30").to_f
99
+ )
100
+ return if seconds <= 0
101
+ return if example_timeout_disabled?
102
+
103
+ require_relative "active_record_timeout_recovery"
104
+
105
+ rspec_config.around(:each) do |example|
106
+ timed_out = false
107
+ begin
108
+ if example.metadata[:benchmark] || example.metadata[:slow]
109
+ example.run
110
+ else
111
+ Timeout.timeout(seconds) { example.run }
112
+ end
113
+ rescue Timeout::Error
114
+ timed_out = true
115
+ raise "Example timed out after #{seconds}s (#{example.location})"
116
+ ensure
117
+ ActiveRecordTimeoutRecovery.disconnect_all_connection_pools! if timed_out
118
+ end
119
+ end
120
+ end
121
+
122
+ def install_prosopite!(rspec_config: fetch_rspec_configuration!)
123
+ return unless prosopite_enabled?
124
+ return unless defined?(Prosopite) && defined?(Rails)
125
+
126
+ log_path = Rails.root.join("tmp", "prosopite_#{Time.current.strftime("%Y%m%d_%H%M%S")}.log")
127
+ Prosopite.custom_logger = Logger.new(log_path)
128
+
129
+ rspec_config.before { Prosopite.scan }
130
+ rspec_config.after { Prosopite.finish }
131
+ end
132
+
133
+ def install_spec_path_helpers!(rspec_config)
134
+ rspec_config.before do |example|
135
+ group = example.metadata[:example_group]
136
+ spec_file_path = group[:file_path]
137
+
138
+ define_singleton_method(:spec_dirname) { File.dirname(spec_file_path) }
139
+ define_singleton_method(:spec_basename) { File.basename(spec_file_path) }
140
+
141
+ if ExampleDebug.print_spec_enabled?
142
+ Polyrun::Log.puts "\nRunning #{spec_file_path}:#{group[:line_number]}\n"
143
+ end
144
+ end
145
+
146
+ rspec_config.after do |example|
147
+ next unless ExampleDebug.print_spec_enabled?
148
+
149
+ group = example.metadata[:example_group]
150
+ Polyrun::Log.puts "\nFinished #{group[:file_path]}:#{group[:line_number]}\n"
151
+ end
152
+ end
153
+
154
+ def fetch_rspec_configuration!
155
+ require "rspec/core"
156
+ ::RSpec.configuration
157
+ end
158
+
159
+ def truthy?(value)
160
+ return false if value.nil?
161
+
162
+ %w[1 true yes on].include?(value.to_s.strip.downcase)
163
+ end
164
+ private_class_method :truthy?, :parse_log_level, :fetch_rspec_configuration!
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,73 @@
1
+ module Polyrun
2
+ module RSpecSupport
3
+ module ExampleDebug
4
+ SKIPPED_SQL_PREFIX = /\A(?:SELECT|SET|SHOW|BEGIN|COMMIT|ROLLBACK|RELEASE|SAVEPOINT)/
5
+
6
+ module_function
7
+
8
+ def install_sql_debug!(rspec_config, io: Polyrun::Log.stdout)
9
+ return unless defined?(ActiveSupport::Notifications)
10
+
11
+ rspec_config.around do |example|
12
+ subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |event|
13
+ payload = event.payload[:sql]
14
+ next unless ExampleDebug.loggable_sql?(payload)
15
+
16
+ line = ExampleDebug.sql_with_interpolated_binds(payload, event.payload[:type_casted_binds])
17
+ io.puts "+ #{line}"
18
+ end
19
+
20
+ example.run
21
+ ensure
22
+ ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
23
+ end
24
+ end
25
+
26
+ def install_trace_debug!(rspec_config, root: trace_root, io: Polyrun::Log.stdout)
27
+ require "pp"
28
+ root_path = File.expand_path(root.to_s)
29
+ trace = TracePoint.new do |trace_point|
30
+ if trace_point.event == :call && trace_point.path.to_s.start_with?(root_path)
31
+ io.puts PP.pp({event: :call, path: "#{trace_point.path}:#{trace_point.lineno}"}, +"")
32
+ elsif trace_point.event == :raise
33
+ io.puts PP.pp(
34
+ {
35
+ event: :raise,
36
+ raised_exception: trace_point.raised_exception,
37
+ path: "#{trace_point.path}:#{trace_point.lineno}",
38
+ method_id: trace_point.method_id
39
+ },
40
+ +""
41
+ )
42
+ end
43
+ end
44
+
45
+ rspec_config.around do |example|
46
+ trace.enable
47
+ example.run
48
+ ensure
49
+ trace.disable
50
+ end
51
+ end
52
+
53
+ def loggable_sql?(sql)
54
+ !sql.to_s.match?(SKIPPED_SQL_PREFIX)
55
+ end
56
+
57
+ def sql_with_interpolated_binds(sql, binds)
58
+ output = sql.to_s.dup
59
+ Array(binds).each_with_index do |bind, index|
60
+ output = output.gsub("$#{index + 1}", "'#{bind}'")
61
+ end
62
+ output
63
+ end
64
+
65
+ def trace_root
66
+ return Rails.root.to_s if defined?(Rails)
67
+
68
+ Dir.pwd
69
+ end
70
+ private_class_method :trace_root
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,57 @@
1
+ module Polyrun
2
+ module RSpecSupport
3
+ # Formatter tweaks for progress formatters under +POLYRUN_SHARD_*+ workers.
4
+ module ShardedFormatterCompat
5
+ TextFormatterSilencer = Module.new do
6
+ def seed(_notification)
7
+ end
8
+
9
+ def dump_summary(_notification)
10
+ end
11
+
12
+ def dump_pending(_notification)
13
+ end
14
+ end
15
+
16
+ FuubarSeedSilencer = Module.new do
17
+ def seed(_notification)
18
+ end
19
+ end
20
+
21
+ module_function
22
+
23
+ def install!(rspec_config: fetch_rspec_configuration!)
24
+ return unless sharded_worker?
25
+
26
+ rspec_config.silence_filter_announcements = true
27
+ silence_text_formatter_noise!
28
+ silence_fuubar_seed! if defined?(Fuubar)
29
+ end
30
+
31
+ def sharded_worker?
32
+ !ENV["POLYRUN_SHARD_TOTAL"].to_s.strip.empty?
33
+ end
34
+
35
+ def silence_text_formatter_noise!
36
+ require "rspec/core/formatters/base_text_formatter"
37
+ klass = ::RSpec::Core::Formatters::BaseTextFormatter
38
+ return if klass.ancestors.include?(TextFormatterSilencer)
39
+
40
+ klass.prepend(TextFormatterSilencer)
41
+ end
42
+
43
+ def silence_fuubar_seed!
44
+ return unless defined?(Fuubar)
45
+ return if Fuubar.ancestors.include?(FuubarSeedSilencer)
46
+
47
+ Fuubar.prepend(FuubarSeedSilencer)
48
+ end
49
+
50
+ def fetch_rspec_configuration!
51
+ require "rspec/core"
52
+ ::RSpec.configuration
53
+ end
54
+ private_class_method :fetch_rspec_configuration!
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,110 @@
1
+ require_relative "../polyrun"
2
+
3
+ module Polyrun
4
+ # Optional RSpec wiring (require +polyrun/rspec_support+ explicitly).
5
+ module RSpecSupport
6
+ module_function
7
+
8
+ # Registers +before(:suite)+ to run {Data::ParallelProvisioning.run_suite_hooks!}.
9
+ def install_parallel_provisioning!(rspec_config)
10
+ rspec_config.before(:suite) do
11
+ Polyrun::Data::ParallelProvisioning.run_suite_hooks!
12
+ end
13
+ end
14
+
15
+ # Experimental: add {Timing::RSpecExampleFormatter} and write per-example JSON (see +timing_granularity: example+).
16
+ # With +output_path:+, that path is used directly (no +ENV+ mutation). Without it, the formatter
17
+ # reads +ENV["POLYRUN_EXAMPLE_TIMING_OUT"]+ or defaults to +polyrun_timing_examples.json+.
18
+ def install_example_timing!(output_path: nil)
19
+ require_relative "timing/rspec_example_formatter"
20
+ fmt =
21
+ if output_path
22
+ op = output_path
23
+ Class.new(Polyrun::Timing::RSpecExampleFormatter) do
24
+ ::RSpec::Core::Formatters.register self, :example_finished, :close
25
+
26
+ define_method(:timing_output_path) { op }
27
+ end
28
+ else
29
+ Polyrun::Timing::RSpecExampleFormatter
30
+ end
31
+ ::RSpec.configure do |config|
32
+ config.add_formatter fmt
33
+ end
34
+ end
35
+
36
+ # Per-worker failure JSONL fragments for +polyrun run-shards --merge-failures+ (parity with coverage shards).
37
+ # Requires +POLYRUN_FAILURE_FRAGMENTS=1+ (set by the parent when --merge-failures is used) unless +only_if+ overrides.
38
+ # Writes +tmp/polyrun_failures/polyrun-failure-fragment-*.jsonl+ (override dir with +POLYRUN_FAILURE_FRAGMENT_DIR+).
39
+ def install_failure_fragments!(only_if: nil)
40
+ pred = only_if || -> { %w[1 true yes].include?(ENV["POLYRUN_FAILURE_FRAGMENTS"].to_s.downcase) }
41
+ return unless pred.call
42
+
43
+ require "rspec/core"
44
+ require_relative "reporting/rspec_failure_fragment_formatter"
45
+ ::RSpec.configure do |config|
46
+ config.add_formatter Polyrun::Reporting::RspecFailureFragmentFormatter
47
+ end
48
+ end
49
+
50
+ # Writes {WorkerPing} after suite start, before/after each example (+location+ is file:line from metadata).
51
+ # Keeps +--worker-idle-timeout+ sensitive to example progress (not only a background thread).
52
+ def install_spec_quality!(only_if: nil, root: nil, output_path: nil)
53
+ pred = only_if || -> { Polyrun::SpecQuality.enabled?(root: root || Dir.pwd) }
54
+ return unless pred.call
55
+
56
+ require_relative "spec_quality/rspec_hook"
57
+ Polyrun::SpecQuality::RspecHook.install!(only_if: pred, root: root, output_path: output_path)
58
+ end
59
+
60
+ def install_worker_ping!
61
+ require "rspec/core"
62
+ require_relative "worker_ping"
63
+ ::RSpec.configure do |config|
64
+ config.before(:suite) { Polyrun::WorkerPing.ping! }
65
+ config.before(:each) do |example|
66
+ Polyrun::WorkerPing.ping!(location: example.metadata[:location] || example.location)
67
+ end
68
+ config.after(:each) do |example|
69
+ Polyrun::WorkerPing.ping!(location: example.metadata[:location] || example.location)
70
+ end
71
+ end
72
+
73
+ Polyrun::WorkerPing.ensure_interval_ping_thread!
74
+ end
75
+
76
+ def install_example_debug!(rspec_config: nil)
77
+ require_relative "rspec_support/example_debug"
78
+ Polyrun::RSpecSupport::ExampleDebug.install!(rspec_config: rspec_config || fetch_rspec_configuration!)
79
+ end
80
+
81
+ def install_example_timeout!(rspec_config: nil, seconds: ENV.fetch("RSPEC_EXAMPLE_TIMEOUT_SEC", "30").to_f)
82
+ require_relative "rspec_support/example_debug"
83
+ Polyrun::RSpecSupport::ExampleDebug.install_example_timeout!(
84
+ rspec_config || fetch_rspec_configuration!,
85
+ seconds: seconds
86
+ )
87
+ end
88
+
89
+ def install_example_rails_logging!(rspec_config: nil)
90
+ require_relative "rspec_support/example_debug"
91
+ Polyrun::RSpecSupport::ExampleDebug.install_rails_logging!(rspec_config: rspec_config || fetch_rspec_configuration!)
92
+ end
93
+
94
+ def install_example_prosopite!(rspec_config: nil)
95
+ require_relative "rspec_support/example_debug"
96
+ Polyrun::RSpecSupport::ExampleDebug.install_prosopite!(rspec_config: rspec_config || fetch_rspec_configuration!)
97
+ end
98
+
99
+ def install_sharded_formatter_compat!(rspec_config: nil)
100
+ require_relative "rspec_support/sharded_formatter_compat"
101
+ Polyrun::RSpecSupport::ShardedFormatterCompat.install!(rspec_config: rspec_config || fetch_rspec_configuration!)
102
+ end
103
+
104
+ def fetch_rspec_configuration!
105
+ require "rspec/core"
106
+ ::RSpec.configuration
107
+ end
108
+ private_class_method :fetch_rspec_configuration!
109
+ end
110
+ end
@@ -0,0 +1,139 @@
1
+ require "yaml"
2
+ require_relative "../config"
3
+
4
+ module Polyrun
5
+ module SpecQuality
6
+ # Loads +config/polyrun_spec_quality.yml+ and +ENV+ overrides.
7
+ module Config
8
+ DEFAULT_CONFIG_RELATIVE = File.join("config", "polyrun_spec_quality.yml").freeze
9
+
10
+ DEFAULTS = {
11
+ "track_under" => %w[lib app],
12
+ "min_line_churn" => 50,
13
+ "min_query_count" => 20,
14
+ "hot_line_example_overlap" => 10,
15
+ "strict" => false,
16
+ "sample" => 1.0,
17
+ "ignore_examples" => [],
18
+ "ignore_paths" => [],
19
+ "ignore_query_patterns" => [],
20
+ "profile" => %w[cpu mem],
21
+ "sql_counter" => false,
22
+ "minimum_unique_lines_per_example" => nil,
23
+ "max_zero_hit_examples" => nil,
24
+ "max_hot_line_overlap" => nil
25
+ }.freeze
26
+
27
+ module_function
28
+
29
+ def enabled?(env = ENV, root: Dir.pwd)
30
+ return false if disabled?(env)
31
+
32
+ return true if truthy?(env["POLYRUN_SPEC_QUALITY"]) || truthy?(env["POLYRUN_SPEC_QUALITY_FRAGMENTS"])
33
+
34
+ Polyrun::Config::Tools.spec_quality(root: root, env: env)["enabled"] == true
35
+ end
36
+
37
+ def disabled?(env = ENV)
38
+ truthy?(env["POLYRUN_SPEC_QUALITY_DISABLE"])
39
+ end
40
+
41
+ def load(root:, config_path: nil, env: ENV, **overrides)
42
+ root = File.expand_path(root)
43
+ file_cfg = load_yaml(root, config_path)
44
+ shared = Polyrun::Config::Tools.spec_quality(root: root, env: env).except("enabled")
45
+ merged = DEFAULTS.merge(stringify_keys(file_cfg)).merge(shared)
46
+ merged.merge!(stringify_keys(file_cfg)) if config_path
47
+ apply_env!(merged, env)
48
+ merged.merge!(stringify_keys(overrides).transform_keys(&:to_s))
49
+ merged["root"] = root
50
+ merged["strict"] = resolve_strict(merged, env)
51
+ merged["sample"] = resolve_sample(merged, env)
52
+ normalize_config!(merged)
53
+ merged
54
+ end
55
+
56
+ def load_yaml(root, config_path)
57
+ path = config_path || File.join(root, DEFAULT_CONFIG_RELATIVE)
58
+ path = File.expand_path(path)
59
+ return {} unless File.file?(path)
60
+
61
+ data = YAML.safe_load_file(path, permitted_classes: [Symbol], aliases: true)
62
+ data.is_a?(Hash) ? data : {}
63
+ end
64
+
65
+ def apply_env!(cfg, env)
66
+ cfg["strict"] = true if truthy?(env["POLYRUN_SPEC_QUALITY_STRICT"])
67
+ if env.key?("POLYRUN_SPEC_QUALITY_SAMPLE")
68
+ cfg["sample"] = Float(env["POLYRUN_SPEC_QUALITY_SAMPLE"])
69
+ end
70
+ if env.key?("POLYRUN_SPEC_QUALITY_SQL_COUNTER")
71
+ cfg["sql_counter"] = truthy?(env["POLYRUN_SPEC_QUALITY_SQL_COUNTER"])
72
+ end
73
+ prof = env["POLYRUN_SPEC_QUALITY_PROFILE"]
74
+ cfg["profile"] = prof.split(",").map(&:strip).reject(&:empty?) if prof && !prof.strip.empty?
75
+ end
76
+
77
+ def resolve_strict(cfg, env)
78
+ return true if truthy?(env["POLYRUN_SPEC_QUALITY_STRICT"])
79
+
80
+ cfg["strict"] == true || truthy?(cfg["strict"])
81
+ end
82
+
83
+ def resolve_sample(cfg, env)
84
+ v = cfg["sample"]
85
+ f = v.is_a?(Numeric) ? v.to_f : Float(v)
86
+ f.clamp(0.0, 1.0)
87
+ rescue ArgumentError, TypeError
88
+ 1.0
89
+ end
90
+
91
+ # rubocop:disable Metrics/AbcSize -- config key normalization
92
+ def normalize_config!(cfg)
93
+ cfg["track_under"] = Array(cfg["track_under"]).map(&:to_s).reject(&:empty?)
94
+ cfg["track_under"] = %w[lib] if cfg["track_under"].empty?
95
+ cfg["ignore_examples"] = Array(cfg["ignore_examples"]).map(&:to_s)
96
+ cfg["ignore_paths"] = Array(cfg["ignore_paths"]).map(&:to_s)
97
+ cfg["ignore_query_patterns"] = Array(cfg["ignore_query_patterns"]).map(&:to_s)
98
+ cfg["profile"] = Array(cfg["profile"]).map(&:to_s).reject(&:empty?)
99
+ %w[min_line_churn min_query_count hot_line_example_overlap].each do |k|
100
+ cfg[k] = Integer(cfg[k]) if cfg[k]
101
+ end
102
+ end
103
+ # rubocop:enable Metrics/AbcSize
104
+
105
+ def ignored_example?(location, ignore_examples)
106
+ loc = location.to_s
107
+ return false if loc.empty?
108
+
109
+ Array(ignore_examples).any? do |pat|
110
+ if pat.start_with?("/") && pat.end_with?("/") && pat.size > 2
111
+ loc.match?(Regexp.new(pat[1..-2]))
112
+ else
113
+ loc.include?(pat)
114
+ end
115
+ rescue RegexpError
116
+ loc.include?(pat)
117
+ end
118
+ end
119
+
120
+ def stringify_keys(obj)
121
+ case obj
122
+ when Hash
123
+ obj.each_with_object({}) { |(k, v), out| out[k.to_s] = stringify_keys(v) }
124
+ when Array
125
+ obj.map { |e| stringify_keys(e) }
126
+ else
127
+ obj
128
+ end
129
+ end
130
+
131
+ def truthy?(value)
132
+ return false if value.nil?
133
+
134
+ %w[1 true yes on].include?(value.to_s.strip.downcase)
135
+ end
136
+ private_class_method :truthy?
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,39 @@
1
+ require "fileutils"
2
+
3
+ require "json"
4
+
5
+ module Polyrun
6
+ module SpecQuality
7
+ module Fragment
8
+ module_function
9
+
10
+ def default_fragment_path(env = ENV)
11
+ dir = env.fetch("POLYRUN_SPEC_QUALITY_FRAGMENT_DIR", default_fragment_dir)
12
+ base = Polyrun::Coverage::CollectorFragmentMeta.fragment_default_basename_from_env(env)
13
+ File.expand_path(File.join(dir, "polyrun-spec-quality-fragment-#{base}.jsonl"))
14
+ end
15
+
16
+ def default_fragment_dir
17
+ File.join(Dir.pwd, "coverage")
18
+ end
19
+
20
+ def glob_pattern(cwd = Dir.pwd)
21
+ File.join(cwd, "coverage", "polyrun-spec-quality-fragment-*.jsonl")
22
+ end
23
+
24
+ def ensure_fragment_dir!(path)
25
+ FileUtils.mkdir_p(File.dirname(path))
26
+ end
27
+
28
+ def truncate_fragment!(path)
29
+ ensure_fragment_dir!(path)
30
+ File.write(path, "")
31
+ end
32
+
33
+ def append_row!(path, row)
34
+ ensure_fragment_dir!(path)
35
+ File.open(path, "a") { |f| f.puts(JSON.generate(row)) }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,78 @@
1
+ require "json"
2
+
3
+ module Polyrun
4
+ module SpecQuality
5
+ module Merge
6
+ module_function
7
+
8
+ def merge_files(paths)
9
+ examples = {}
10
+ paths.each do |path|
11
+ merge_file_into!(examples, path)
12
+ end
13
+ build_merged_payload(examples, paths.size)
14
+ end
15
+
16
+ def merge_file_into!(examples, path)
17
+ File.foreach(path) do |line|
18
+ line = line.strip
19
+ next if line.empty?
20
+
21
+ row = JSON.parse(line)
22
+ key = row["example"].to_s
23
+ next if key.empty?
24
+
25
+ examples[key] = row
26
+ end
27
+ end
28
+
29
+ def build_merged_payload(examples, fragment_count)
30
+ hot_lines = aggregate_hot_lines(examples)
31
+ {
32
+ "examples" => examples,
33
+ "hot_lines" => hot_lines,
34
+ "shard_summary" => shard_summary(examples),
35
+ "meta" => {
36
+ "polyrun_version" => Polyrun::VERSION,
37
+ "fragment_count" => fragment_count,
38
+ "example_count" => examples.size
39
+ }
40
+ }
41
+ end
42
+
43
+ def shard_summary(examples)
44
+ by_shard = Hash.new { |h, k| h[k] = {"examples" => 0, "zero_hit" => 0, "line_churn" => 0} }
45
+ examples.each do |_loc, row|
46
+ shard = row["polyrun_shard_index"]
47
+ shard = shard.nil? ? "?" : shard.to_s
48
+ by_shard[shard]["examples"] += 1
49
+ by_shard[shard]["zero_hit"] += 1 if row["unique_lines"].to_i.zero?
50
+ by_shard[shard]["line_churn"] += row["line_churn"].to_i
51
+ end
52
+ by_shard
53
+ end
54
+
55
+ def aggregate_hot_lines(examples)
56
+ by_line = Hash.new { |h, k| h[k] = {"examples" => [], "total_hits" => 0} }
57
+ examples.each do |example_loc, row|
58
+ Array(row["lines"]).each do |entry|
59
+ path, line_no, delta = entry
60
+ key = "#{path}:#{line_no}"
61
+ by_line[key]["examples"] << example_loc
62
+ by_line[key]["total_hits"] += delta.to_i
63
+ end
64
+ end
65
+ by_line.transform_values do |v|
66
+ v["example_count"] = v["examples"].uniq.size
67
+ v
68
+ end
69
+ end
70
+
71
+ def merge_and_write(paths, output_path)
72
+ merged = merge_files(paths)
73
+ File.write(output_path, JSON.pretty_generate(merged))
74
+ merged
75
+ end
76
+ end
77
+ end
78
+ end