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,97 @@
1
+ require "yaml"
2
+
3
+ module Polyrun
4
+ # Loads polyrun.yml (or path from POLYRUN_CONFIG / --config).
5
+ class Config
6
+ DEFAULT_FILENAMES = %w[polyrun.yml config/polyrun.yml].freeze
7
+
8
+ # Parallel worker defaults (+run-shards+, +POLYRUN_WORKERS+); single source with {Resolver} and {Effective}.
9
+ DEFAULT_PARALLEL_WORKERS = 5
10
+ MAX_PARALLEL_WORKERS = 10
11
+
12
+ attr_reader :path, :raw, :suite
13
+
14
+ def self.load(path: nil, suite: nil, root: Dir.pwd)
15
+ explicit = path && !path.to_s.strip.empty?
16
+ resolved = resolve_path(path, root: root)
17
+ if explicit && !(resolved && File.file?(resolved))
18
+ raise Polyrun::Error, "polyrun config not found: #{path}"
19
+ end
20
+
21
+ raw =
22
+ if resolved && File.file?(resolved)
23
+ YAML.safe_load_file(resolved, permitted_classes: [Symbol], aliases: true)
24
+ else
25
+ {}
26
+ end
27
+ raw = {} if raw.nil?
28
+ raise Polyrun::Error, "polyrun config root must be a mapping" unless raw.is_a?(Hash)
29
+
30
+ suite_name = (suite.nil? || suite.to_s.strip.empty?) ? ENV["POLYRUN_SUITE"] : suite
31
+ merged, applied = Suite.merge(raw, suite_name)
32
+ new(path: resolved, raw: merged, suite: applied)
33
+ end
34
+
35
+ def self.resolve_path(explicit, root: Dir.pwd)
36
+ return File.expand_path(explicit) if explicit && !explicit.empty?
37
+
38
+ DEFAULT_FILENAMES.each do |name|
39
+ full = File.expand_path(name, root)
40
+ return full if File.file?(full)
41
+ end
42
+ nil
43
+ end
44
+
45
+ def initialize(path:, raw:, suite: nil)
46
+ @path = path
47
+ @suite = suite
48
+ @raw = raw.freeze
49
+ end
50
+
51
+ def partition
52
+ raw["partition"] || raw[:partition] || {}
53
+ end
54
+
55
+ def prepare
56
+ raw["prepare"] || raw[:prepare] || {}
57
+ end
58
+
59
+ def coverage
60
+ raw["coverage"] || raw[:coverage] || {}
61
+ end
62
+
63
+ def databases
64
+ raw["databases"] || raw[:databases] || {}
65
+ end
66
+
67
+ # Optional +start:+ block: +prepare+ / +databases+ booleans override auto-detection for +polyrun start+.
68
+ def start_config
69
+ raw["start"] || raw[:start] || {}
70
+ end
71
+
72
+ def version
73
+ raw["version"] || raw[:version]
74
+ end
75
+
76
+ # Optional +hooks:+ block for +run-shards+ / +parallel-rspec+ / +ci-shard-*+ (see {Hooks}).
77
+ def hooks
78
+ raw["hooks"] || raw[:hooks] || {}
79
+ end
80
+
81
+ # Optional +reporting:+ block (merge-failures output paths, etc.).
82
+ def reporting
83
+ raw["reporting"] || raw[:reporting] || {}
84
+ end
85
+
86
+ def suites
87
+ raw["suites"] || raw[:suites] || {}
88
+ end
89
+ end
90
+ end
91
+
92
+ require_relative "config/suite"
93
+ require_relative "config/resolver"
94
+ require_relative "config/effective"
95
+ require_relative "config/schema"
96
+
97
+ require_relative "config/tools"
@@ -0,0 +1,32 @@
1
+ module Polyrun
2
+ module Coverage
3
+ # Lists Cobertura +line+ elements with +hits="0"+ (optional dev aid). Set +SHOW_ZERO_COVERAGE=1+ and run
4
+ # after Cobertura XML exists (e.g. after {Collector} with Cobertura formatter).
5
+ # Uses a small string scan (no REXML) so the gem stays stdlib-only everywhere REXML may be omitted.
6
+ module CoberturaZeroLines
7
+ module_function
8
+
9
+ def run(xml_path:, filename_prefix: "lib/")
10
+ return unless ENV["SHOW_ZERO_COVERAGE"] == "1"
11
+ return unless File.file?(xml_path)
12
+
13
+ uncovered = extract(File.read(xml_path), filename_prefix: filename_prefix)
14
+ uncovered.sort_by { |e| [e[:file], e[:line]] }.each do |line_info|
15
+ Polyrun::Log.puts "#{line_info[:file]}:#{line_info[:line]}"
16
+ end
17
+ end
18
+
19
+ def extract(xml_text, filename_prefix: "lib/")
20
+ uncovered = []
21
+ xml_text.scan(/<class[^>]+filename="([^"]+)"[^>]*>(.*?)<\/class>/m) do |filename, class_body|
22
+ next unless filename.start_with?(filename_prefix)
23
+
24
+ class_body.scan(/<line number="(\d+)" hits="(\d+)"/) do |num_s, hits_s|
25
+ uncovered << {file: filename, line: num_s.to_i} if hits_s.to_i == 0
26
+ end
27
+ end
28
+ uncovered
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,169 @@
1
+ require "coverage"
2
+ require "fileutils"
3
+ require "json"
4
+
5
+ require_relative "../version"
6
+ require_relative "../error"
7
+ require_relative "cobertura_zero_lines"
8
+ require_relative "filter"
9
+ require_relative "formatter"
10
+ require_relative "merge"
11
+ require_relative "result"
12
+ require_relative "track_files"
13
+ require_relative "collector_fragment_meta"
14
+ require_relative "../debug"
15
+
16
+ module Polyrun
17
+ module Coverage
18
+ # Stdlib +Coverage+ → SimpleCov-compatible JSON for +merge-coverage+ / +report-coverage+.
19
+ # No SimpleCov gem. Enable with +POLYRUN_COVERAGE=1+ or call +start!+ from spec_helper.
20
+ #
21
+ # Disable with +POLYRUN_COVERAGE_DISABLE=1+ or +SIMPLECOV_DISABLE=1+ (migration alias).
22
+ #
23
+ # Branch coverage: set +POLYRUN_COVERAGE_BRANCHES=1+ so stdlib +Coverage.start+ records branches;
24
+ # +merge-coverage+ merges branch keys when present in fragments.
25
+ module Collector
26
+ module_function
27
+
28
+ # @param root [String] project root (absolute or relative)
29
+ # @param reject_patterns [Array<String>] path substrings to drop (like SimpleCov add_filter)
30
+ # @param output_path [String, nil] default see {.fragment_default_basename_from_env}
31
+ # @param minimum_line_percent [Float, nil] exit 1 if below (when strict)
32
+ # @param strict [Boolean] whether to exit non-zero on threshold failure (default true when minimum set)
33
+ # @param track_under [Array<String>] when +track_files+ is nil, only keep coverage keys under these dirs relative to +root+. Default +["lib"]+.
34
+ # @param track_files [String, Array<String>, nil] glob(s) relative to +root+ (e.g. +"{lib,app}/**/*.rb"+). Adds never-loaded files with simulated lines, like SimpleCov +track_files+.
35
+ # @param groups [Hash{String=>String}] group name => glob relative to +root+ (SimpleCov +add_group+); JSON gets +lines.covered_percent+ per group and optional "Ungrouped".
36
+ # @param meta [Hash] extra keys under merged JSON meta
37
+ # @param formatter [Object, nil] Object responding to +format(result, output_dir:, basename:)+ like SimpleCov formatters (e.g. {Formatter.multi} or {Formatter::MultiFormatter})
38
+ # @param report_output_dir [String, nil] directory for +formatter+ outputs (default +coverage/+ under +root+)
39
+ # @param report_basename [String] file prefix for formatter outputs (default +polyrun-coverage+)
40
+ # See {CollectorFragmentMeta.fragment_default_basename_from_env}.
41
+ def self.fragment_default_basename_from_env(env = ENV)
42
+ CollectorFragmentMeta.fragment_default_basename_from_env(env)
43
+ end
44
+
45
+ def start!(root:, reject_patterns: [], track_under: ["lib"], track_files: nil, groups: nil, output_path: nil, minimum_line_percent: nil, strict: nil, meta: {}, formatter: nil, report_output_dir: nil, report_basename: "polyrun-coverage")
46
+ return if disabled?
47
+
48
+ branches = branch_coverage_enabled?
49
+ if branches && !::Coverage.supported?(:branches)
50
+ raise Polyrun::Error, "#{RUBY_ENGINE} does not support branch coverage"
51
+ end
52
+
53
+ root = File.expand_path(root)
54
+ basename = fragment_default_basename_from_env
55
+ output_path ||= File.join(root, "coverage", "polyrun-fragment-#{basename}.json")
56
+ strict = if minimum_line_percent.nil?
57
+ false
58
+ else
59
+ strict.nil? || strict
60
+ end
61
+
62
+ fragment_meta = CollectorFragmentMeta.fragment_meta_from_env(basename)
63
+
64
+ @config = {
65
+ root: root,
66
+ track_under: Array(track_under).map(&:to_s),
67
+ track_files: track_files,
68
+ groups: normalize_groups(groups),
69
+ reject_patterns: reject_patterns,
70
+ output_path: output_path,
71
+ minimum_line_percent: minimum_line_percent,
72
+ strict: strict,
73
+ meta: meta,
74
+ formatter: formatter,
75
+ report_output_dir: report_output_dir,
76
+ report_basename: report_basename,
77
+ shard_total_at_start: ENV["POLYRUN_SHARD_TOTAL"].to_i,
78
+ fragment_meta: fragment_meta
79
+ }
80
+
81
+ unless ::Coverage.running?
82
+ ::Coverage.start(lines: true, branches: branches)
83
+ end
84
+ unless instance_variable_defined?(:@collector_finish_at_exit_registered)
85
+ @collector_finish_at_exit_registered = true
86
+ at_exit { finish }
87
+ end
88
+ nil
89
+ end
90
+
91
+ def branch_coverage_enabled?
92
+ %w[1 true yes].include?(ENV["POLYRUN_COVERAGE_BRANCHES"]&.downcase)
93
+ end
94
+
95
+ def disabled?
96
+ %w[1 true yes].include?(ENV["POLYRUN_COVERAGE_DISABLE"]&.downcase) ||
97
+ %w[1 true yes].include?(ENV["SIMPLECOV_DISABLE"]&.downcase)
98
+ end
99
+
100
+ # True after a successful {start!} in this process (stdlib +Coverage+ is active).
101
+ def self.started?
102
+ instance_variable_defined?(:@config) && @config
103
+ end
104
+
105
+ # Whether +polyrun quick+ / Sus helpers should call {Rails.start!} / {start!} before loading tests:
106
+ # not disabled, and (+POLYRUN_COVERAGE=1+ or Covered-style +COVERAGE+ set, or
107
+ # (+config/polyrun_coverage.yml+ exists and +POLYRUN_QUICK_COVERAGE=1+)).
108
+ def self.coverage_requested_for_quick?(root = Dir.pwd)
109
+ return false if disabled?
110
+ return true if polyrun_coverage_env_enabled?
111
+ return true if covered_env_enabled?
112
+
113
+ path = File.join(File.expand_path(root), "config", "polyrun_coverage.yml")
114
+ return false unless File.file?(path)
115
+
116
+ # Config file alone is for merge/report defaults; opt-in so test suites that only
117
+ # keep polyrun_coverage.yml for gates do not start Collector during `polyrun quick`.
118
+ %w[1 true yes].include?(ENV["POLYRUN_QUICK_COVERAGE"]&.to_s&.downcase)
119
+ end
120
+
121
+ # +POLYRUN_COVERAGE=1/true/yes+.
122
+ def self.polyrun_coverage_env_enabled?
123
+ %w[1 true yes].include?(ENV["POLYRUN_COVERAGE"]&.to_s&.downcase)
124
+ end
125
+
126
+ # Covered sets +COVERAGE+ to a report name (e.g. +PartialSummary+) or +true+.
127
+ # Empty / +0+ / +false+ / +no+ / +off+ stay off.
128
+ def self.covered_env_enabled?
129
+ value = ENV["COVERAGE"].to_s
130
+ return false if value.empty?
131
+ return false if %w[0 false no off].include?(value.downcase)
132
+
133
+ true
134
+ end
135
+
136
+ # When +POLYRUN_SHARD_TOTAL+ > 1, each worker only writes the JSON fragment; merged reports
137
+ # (+merge-coverage+ / +report-coverage+) are authoritative. Set +POLYRUN_COVERAGE_WORKER_FORMATS=1+
138
+ # to force per-worker formatter output (debug only; duplicates work N times).
139
+ def run_formatter_per_worker?
140
+ return true if ENV["POLYRUN_COVERAGE_WORKER_FORMATS"] == "1"
141
+
142
+ ENV["POLYRUN_SHARD_TOTAL"].to_i <= 1
143
+ end
144
+
145
+ def self.finish_debug_time_label
146
+ CollectorFragmentMeta.finish_debug_time_label
147
+ end
148
+
149
+ def build_meta(cfg)
150
+ m = (cfg[:meta] || {}).transform_keys(&:to_s)
151
+ m["polyrun_version"] = Polyrun::VERSION
152
+ m["timestamp"] ||= Time.now.to_i
153
+ m["command_name"] ||= "rspec"
154
+ m["polyrun_coverage_root"] = cfg[:root].to_s
155
+ CollectorFragmentMeta.merge_fragment_meta!(m, cfg[:fragment_meta])
156
+ if cfg[:groups]
157
+ m["polyrun_coverage_groups"] = cfg[:groups].transform_keys(&:to_s).transform_values(&:to_s)
158
+ end
159
+ if cfg[:track_files]
160
+ m["polyrun_track_files"] = cfg[:track_files]
161
+ end
162
+ m
163
+ end
164
+ end
165
+ end
166
+ end
167
+
168
+ require_relative "collector_blob"
169
+ require_relative "collector_finish"
@@ -0,0 +1,53 @@
1
+ module Polyrun
2
+ module Coverage
3
+ module Collector
4
+ module_function
5
+
6
+ # Normalizes stdlib Coverage.result to merge-compatible file entries (lines; branches when collected).
7
+ def result_to_blob(raw)
8
+ out = {}
9
+ raw.each do |path, cov|
10
+ next unless cov.is_a?(Hash)
11
+
12
+ lines = cov[:lines] || cov["lines"]
13
+ next unless lines.is_a?(Array)
14
+
15
+ entry = {"lines" => lines.map { |x| x }}
16
+ br = cov[:branches] || cov["branches"]
17
+ entry["branches"] = br if br
18
+ out[path.to_s] = entry
19
+ end
20
+ out
21
+ end
22
+
23
+ def normalize_blob_paths(blob, root)
24
+ root = File.expand_path(root)
25
+ blob.each_with_object({}) do |(path, entry), acc|
26
+ acc[File.expand_path(path.to_s, root)] = entry
27
+ end
28
+ end
29
+
30
+ def normalize_groups(groups)
31
+ return nil if groups.nil?
32
+
33
+ h = groups.is_a?(Hash) ? groups : {}
34
+ return nil if h.empty?
35
+
36
+ h.transform_keys(&:to_s).transform_values(&:to_s)
37
+ end
38
+
39
+ def keep_under_root(blob, root, track_under)
40
+ return blob if track_under.nil? || track_under.empty?
41
+
42
+ root = File.expand_path(root)
43
+ prefixes = track_under.map { |d| File.join(root, d) }
44
+ blob.each_with_object({}) do |(path, entry), acc|
45
+ p = path.to_s
46
+ next unless prefixes.any? { |pre| p == pre || p.start_with?(pre + "/") }
47
+
48
+ acc[path] = entry
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,98 @@
1
+ require "fileutils"
2
+ require "json"
3
+
4
+ module Polyrun
5
+ module Coverage
6
+ module Collector
7
+ module_function
8
+
9
+ def finish
10
+ cfg = @config || return # rubocop:disable ThreadSafety/ClassInstanceVariable -- Collector stores @config from start! (same process)
11
+ Polyrun::Debug.log_worker_kv(
12
+ collector_finish: "start",
13
+ polyrun_shard_index: ENV["POLYRUN_SHARD_INDEX"],
14
+ polyrun_shard_total: ENV["POLYRUN_SHARD_TOTAL"],
15
+ polyrun_shard_matrix_index: ENV["POLYRUN_SHARD_MATRIX_INDEX"],
16
+ polyrun_shard_matrix_total: ENV["POLYRUN_SHARD_MATRIX_TOTAL"],
17
+ output_path: cfg[:output_path]
18
+ )
19
+ Polyrun::Debug.time(Collector.finish_debug_time_label) do
20
+ blob = Collector.send(:prepare_finish_blob, cfg)
21
+ summary = Merge.console_summary(blob)
22
+ group_payload = cfg[:groups] ? TrackFiles.group_summaries(blob, cfg[:root], cfg[:groups]) : nil
23
+
24
+ Collector.send(:exit_if_below_minimum_line_percent, cfg, summary)
25
+
26
+ Collector.send(:write_finish_fragment!, cfg, blob, group_payload)
27
+ Collector.send(:run_finish_formatter!, cfg, blob, group_payload)
28
+ Polyrun::Log.warn Merge.format_console_summary(summary) if ENV["POLYRUN_COVERAGE_VERBOSE"]
29
+ end
30
+ end
31
+
32
+ def self.prepare_finish_blob(cfg)
33
+ raw = ::Coverage.result
34
+ blob = Collector.result_to_blob(raw)
35
+ blob = Collector.normalize_blob_paths(blob, cfg[:root])
36
+ blob = Collector.send(:track_blob_for_finish, cfg, blob)
37
+ Filter.reject_matching_paths(blob, cfg[:reject_patterns])
38
+ end
39
+ private_class_method :prepare_finish_blob
40
+
41
+ def self.track_blob_for_finish(cfg, blob)
42
+ sharded = ENV["POLYRUN_SHARD_TOTAL"].to_i > 1
43
+ if cfg[:track_files]
44
+ filtered = TrackFiles.keep_tracked_files(blob, cfg[:root], cfg[:track_files])
45
+ return filtered if sharded
46
+
47
+ TrackFiles.merge_untracked_into_blob(filtered, cfg[:root], cfg[:track_files])
48
+ else
49
+ Collector.keep_under_root(blob, cfg[:root], cfg[:track_under])
50
+ end
51
+ end
52
+ private_class_method :track_blob_for_finish
53
+
54
+ def self.write_finish_fragment!(cfg, blob, group_payload)
55
+ FileUtils.mkdir_p(File.dirname(cfg[:output_path]))
56
+ payload = Merge.to_simplecov_json(blob, meta: Collector.build_meta(cfg), groups: group_payload)
57
+ File.write(cfg[:output_path], JSON.generate(payload))
58
+ Polyrun::Debug.log_worker("Collector.finish: wrote #{cfg[:output_path]} bytes=#{File.size(cfg[:output_path])}")
59
+ end
60
+ private_class_method :write_finish_fragment!
61
+
62
+ def self.run_finish_formatter!(cfg, blob, group_payload)
63
+ return unless cfg[:formatter]
64
+
65
+ if Collector.run_formatter_per_worker?
66
+ dir = cfg[:report_output_dir] || File.join(cfg[:root], "coverage")
67
+ base = cfg[:report_basename] || "polyrun-coverage"
68
+ result = Result.new(blob, meta: Collector.build_meta(cfg), groups: group_payload)
69
+ Polyrun::Debug.time("Collector formatter (per-shard reports)") do
70
+ cfg[:formatter].format(result, output_dir: dir, basename: base)
71
+ xml_path = File.join(dir, "#{base}.xml")
72
+ CoberturaZeroLines.run(xml_path: xml_path, filename_prefix: "lib/") if File.file?(xml_path)
73
+ end
74
+ else
75
+ shard_total = ENV.fetch("POLYRUN_SHARD_TOTAL", "nil")
76
+ Polyrun::Debug.log_worker(
77
+ "Collector.finish: skipping per-worker formatter (POLYRUN_SHARD_TOTAL=#{shard_total}); " \
78
+ "use merge-coverage / report-coverage for full LCOV/Cobertura/HTML"
79
+ )
80
+ end
81
+ end
82
+ private_class_method :run_finish_formatter!
83
+
84
+ def self.exit_if_below_minimum_line_percent(cfg, summary)
85
+ shard_total = (cfg[:shard_total_at_start] || ENV["POLYRUN_SHARD_TOTAL"]).to_i
86
+ return unless cfg[:minimum_line_percent] && shard_total <= 1
87
+
88
+ min = cfg[:minimum_line_percent].to_f
89
+ return if summary[:line_percent].round >= min.round
90
+
91
+ Polyrun::Log.warn Merge.format_console_summary(summary)
92
+ Polyrun::Log.warn "Polyrun coverage: #{summary[:line_percent].round(2)}% rounds below minimum #{min}%."
93
+ exit 1 if cfg[:strict]
94
+ end
95
+ private_class_method :exit_if_below_minimum_line_percent
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,57 @@
1
+ module Polyrun
2
+ module Coverage
3
+ # Shard / worker naming for coverage JSON fragments (N×M CI vs run-shards).
4
+ module CollectorFragmentMeta
5
+ module_function
6
+
7
+ # Default fragment basename (no extension) for +coverage/polyrun-fragment-<basename>.json+.
8
+ def fragment_default_basename_from_env(env = ENV)
9
+ local = env.fetch("POLYRUN_SHARD_INDEX", "0")
10
+ mt = env["POLYRUN_SHARD_MATRIX_TOTAL"].to_i
11
+ if mt > 1
12
+ mi = env.fetch("POLYRUN_SHARD_MATRIX_INDEX", "0")
13
+ "shard#{mi}-worker#{local}"
14
+ elsif env["POLYRUN_SHARD_TOTAL"].to_i > 1
15
+ "worker#{local}"
16
+ else
17
+ local
18
+ end
19
+ end
20
+
21
+ def finish_debug_time_label
22
+ mt = ENV["POLYRUN_SHARD_MATRIX_TOTAL"].to_i
23
+ if mt > 1
24
+ "worker pid=#{$$} shard(matrix)=#{ENV.fetch("POLYRUN_SHARD_MATRIX_INDEX", "?")} worker(local)=#{ENV.fetch("POLYRUN_SHARD_INDEX", "?")} Coverage::Collector.finish (write fragment)"
25
+ elsif ENV["POLYRUN_SHARD_TOTAL"].to_i > 1
26
+ "worker pid=#{$$} worker=#{ENV.fetch("POLYRUN_SHARD_INDEX", "?")} Coverage::Collector.finish (write fragment)"
27
+ else
28
+ "Coverage::Collector.finish (write fragment)"
29
+ end
30
+ end
31
+
32
+ def fragment_meta_from_env(basename)
33
+ mt = ENV["POLYRUN_SHARD_MATRIX_TOTAL"].to_i
34
+ {
35
+ basename: basename,
36
+ worker_index: ENV.fetch("POLYRUN_SHARD_INDEX", "0"),
37
+ shard_matrix_index: shard_matrix_index_value(mt)
38
+ }
39
+ end
40
+
41
+ def shard_matrix_index_value(matrix_total)
42
+ return nil if matrix_total <= 1
43
+
44
+ ENV.fetch("POLYRUN_SHARD_MATRIX_INDEX", "0")
45
+ end
46
+
47
+ def merge_fragment_meta!(m, fm)
48
+ return m if fm.nil?
49
+
50
+ m["polyrun_fragment_basename"] = fm[:basename].to_s if fm[:basename]
51
+ m["polyrun_worker_index"] = fm[:worker_index].to_s if fm[:worker_index]
52
+ m["polyrun_shard_matrix_index"] = fm[:shard_matrix_index].to_s if fm[:shard_matrix_index]
53
+ m
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,131 @@
1
+ require "yaml"
2
+ require_relative "../config/suite"
3
+
4
+ module Polyrun
5
+ module Coverage
6
+ # Resolves coverage settings for {Rails.start!} and CLI gates.
7
+ #
8
+ # Order: explicit +config_path+ when that file exists; else +config/polyrun_coverage.yml+
9
+ # when present; else inline +coverage:+ from +polyrun.yml+ (or +POLYRUN_CONFIG+).
10
+ # Aliases: +formats+ -> +report_formats+; directory-like +output_path+ -> +report_output_dir+.
11
+ module ConfigResolve
12
+ module_function
13
+
14
+ DEFAULT_FILE = File.join("config", "polyrun_coverage.yml").freeze
15
+ POLYRUN_YML_NAMES = %w[polyrun.yml config/polyrun.yml].freeze
16
+ META_KEYS = %w[coverage_config_source].freeze
17
+
18
+ def load(root:, config_path: nil, polyrun_config_path: nil)
19
+ root = File.expand_path(root)
20
+ file_path, file_hash = load_file_coverage(root, config_path)
21
+ if file_path
22
+ chosen = file_hash
23
+ source = relative_or_basename(file_path)
24
+ else
25
+ inline_path = resolve_polyrun_yml(root, polyrun_config_path)
26
+ chosen = load_inline_coverage(inline_path)
27
+ source = chosen.empty? ? "none" : relative_or_basename(inline_path)
28
+ end
29
+ normalize_aliases!(chosen, root)
30
+ chosen["coverage_config_source"] = source
31
+ chosen
32
+ end
33
+
34
+ def minimum_line_gate(root: Dir.pwd, config_path: nil, polyrun_config_path: nil)
35
+ data = load(root: root, config_path: config_path, polyrun_config_path: polyrun_config_path)
36
+ min = data["minimum_line_percent"]
37
+ return nil if min.nil?
38
+
39
+ strict = data.key?("strict") ? data["strict"] != false : true
40
+ {minimum: min.to_f, strict: strict}
41
+ end
42
+
43
+ def load_inline_coverage(path)
44
+ return {} unless path
45
+
46
+ raw = YAML.safe_load_file(path, permitted_classes: [Symbol], aliases: true)
47
+ return {} unless raw.is_a?(Hash)
48
+
49
+ raw, = Polyrun::Config::Suite.merge(raw, ENV["POLYRUN_SUITE"])
50
+ cov = raw["coverage"] || raw[:coverage]
51
+ cov.is_a?(Hash) ? stringify_keys(cov) : {}
52
+ rescue Psych::SyntaxError, ArgumentError, TypeError
53
+ {}
54
+ end
55
+ private_class_method :load_inline_coverage
56
+
57
+ def load_file_coverage(root, config_path)
58
+ path =
59
+ if config_path && !config_path.to_s.strip.empty?
60
+ File.expand_path(config_path.to_s)
61
+ else
62
+ File.join(root, DEFAULT_FILE)
63
+ end
64
+ return [nil, {}] unless File.file?(path)
65
+
66
+ data = YAML.safe_load_file(path, permitted_classes: [Symbol], aliases: true)
67
+ hash = data.is_a?(Hash) ? stringify_keys(data) : {}
68
+ [path, hash]
69
+ rescue Psych::SyntaxError, ArgumentError, TypeError
70
+ [path, {}]
71
+ end
72
+ private_class_method :load_file_coverage
73
+
74
+ def resolve_polyrun_yml(root, explicit)
75
+ if explicit && !explicit.to_s.strip.empty?
76
+ p = File.expand_path(explicit.to_s)
77
+ return p if File.file?(p)
78
+
79
+ return nil
80
+ end
81
+
82
+ env = ENV["POLYRUN_CONFIG"]
83
+ if env && !env.empty?
84
+ p = File.expand_path(env)
85
+ return p if File.file?(p)
86
+ end
87
+
88
+ POLYRUN_YML_NAMES.each do |name|
89
+ full = File.expand_path(name, root)
90
+ return full if File.file?(full)
91
+ end
92
+ nil
93
+ end
94
+ private_class_method :resolve_polyrun_yml
95
+
96
+ def normalize_aliases!(h, root)
97
+ if !h.key?("report_formats") && h.key?("formats")
98
+ h["report_formats"] = Array(h.delete("formats"))
99
+ end
100
+ return if h.key?("report_output_dir")
101
+ return unless h.key?("output_path")
102
+
103
+ out = h["output_path"].to_s
104
+ return if out.empty? || out.end_with?(".json")
105
+
106
+ h["report_output_dir"] = File.expand_path(out, root)
107
+ h.delete("output_path")
108
+ end
109
+ private_class_method :normalize_aliases!
110
+
111
+ def relative_or_basename(path)
112
+ return DEFAULT_FILE if path.end_with?(DEFAULT_FILE)
113
+
114
+ File.basename(path)
115
+ end
116
+ private_class_method :relative_or_basename
117
+
118
+ def stringify_keys(obj)
119
+ case obj
120
+ when Hash
121
+ obj.each_with_object({}) { |(k, v), out| out[k.to_s] = stringify_keys(v) }
122
+ when Array
123
+ obj.map { |e| stringify_keys(e) }
124
+ else
125
+ obj
126
+ end
127
+ end
128
+ private_class_method :stringify_keys
129
+ end
130
+ end
131
+ end