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,34 @@
1
+ module Polyrun
2
+ class CLI
3
+ # Canonical start skip flags are +POLYRUN_SKIP_PREPARE+ / +POLYRUN_SKIP_DATABASES+.
4
+ # Legacy +POLYRUN_START_SKIP_*+ aliases remain accepted during the deprecation window.
5
+ module SkipEnv
6
+ module_function
7
+
8
+ def skip_prepare?(env = ENV)
9
+ truthy?(env, "POLYRUN_SKIP_PREPARE") ||
10
+ legacy_truthy?(env, "POLYRUN_START_SKIP_PREPARE", "POLYRUN_SKIP_PREPARE")
11
+ end
12
+
13
+ def skip_databases?(env = ENV)
14
+ truthy?(env, "POLYRUN_SKIP_DATABASES") ||
15
+ legacy_truthy?(env, "POLYRUN_START_SKIP_DATABASES", "POLYRUN_SKIP_DATABASES")
16
+ end
17
+
18
+ def truthy?(env, name)
19
+ %w[1 true yes].include?(env[name].to_s.downcase)
20
+ end
21
+ private_class_method :truthy?
22
+
23
+ def legacy_truthy?(env, legacy, canonical)
24
+ return false unless truthy?(env, legacy)
25
+
26
+ unless truthy?(env, canonical)
27
+ Polyrun::Log.warn "polyrun: #{legacy}=1 is deprecated; use #{canonical}=1"
28
+ end
29
+ true
30
+ end
31
+ private_class_method :legacy_truthy?
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,149 @@
1
+ require "json"
2
+ require "optparse"
3
+
4
+ require_relative "../spec_quality"
5
+
6
+ module Polyrun
7
+ class CLI
8
+ module SpecQualityCommands
9
+ private
10
+
11
+ def cmd_merge_spec_quality(argv)
12
+ inputs = []
13
+ output = "coverage/polyrun-spec-quality.json"
14
+ parser = OptionParser.new do |opts|
15
+ opts.banner = "usage: polyrun merge-spec-quality [-i FILE]... [-o OUT] [FILE...]"
16
+ opts.on("-i", "--input FILE", "Spec quality JSONL fragment (repeatable)") { |f| inputs << f }
17
+ opts.on("-o", "--output PATH", String) { |v| output = v }
18
+ end
19
+ parser.parse!(argv)
20
+ inputs.concat(argv) if inputs.empty?
21
+
22
+ if inputs.empty?
23
+ inputs = Dir.glob(Polyrun::SpecQuality::Fragment.glob_pattern).sort
24
+ end
25
+
26
+ if inputs.empty?
27
+ Polyrun::Log.warn "merge-spec-quality: need -i FILE or coverage/polyrun-spec-quality-fragment-*.jsonl"
28
+ return 2
29
+ end
30
+
31
+ out_abs = File.expand_path(output)
32
+ Polyrun::SpecQuality::Merge.merge_and_write(inputs.map { |p| File.expand_path(p) }, out_abs)
33
+ Polyrun::Log.puts out_abs
34
+ 0
35
+ end
36
+
37
+ # rubocop:disable Metrics/AbcSize -- report argv + gate output
38
+ def cmd_report_spec_quality(argv)
39
+ input = nil
40
+ out_file = nil
41
+ top = 30
42
+ profile = nil
43
+ config_path = nil
44
+ strict = false
45
+ json_out = false
46
+ report_format = nil
47
+ plan_paths = []
48
+
49
+ OptionParser.new do |opts|
50
+ opts.banner = "usage: polyrun report-spec-quality -i FILE [-o PATH] [--top N] [--profile LIST] [--strict] [--format text|json|csv|markdown] [--json]"
51
+ opts.on("-i", "--input PATH", "Merged polyrun-spec-quality.json") { |v| input = v }
52
+ opts.on("-o", "--output PATH", "Write report to file instead of stdout") { |v| out_file = v }
53
+ opts.on("--top N", Integer) { |v| top = v }
54
+ opts.on("--profile LIST", "cpu,mem,io,wall (comma-separated)") { |v| profile = v }
55
+ opts.on("-c", "--config PATH", "polyrun_spec_quality.yml path") { |v| config_path = v }
56
+ opts.on("--plan PATH", "Partition plan JSON (repeatable; polyrun plan output per shard)") { |v| plan_paths << v }
57
+ opts.on("--strict", "Exit 1 when gate thresholds fail") { strict = true }
58
+ opts.on("--format VAL", "text (default), json, csv, or markdown") { |v| report_format = v }
59
+ opts.on("--json", "Write analysis JSON instead of text report") { json_out = true }
60
+ end.parse!(argv)
61
+ input ||= argv.first
62
+
63
+ unless input && File.file?(input)
64
+ Polyrun::Log.warn "report-spec-quality: need -i FILE"
65
+ return 2
66
+ end
67
+
68
+ merged = JSON.parse(File.read(File.expand_path(input)))
69
+ cfg = load_spec_quality_config(config_path)
70
+ strict = true if cfg["strict"] || strict
71
+ plan_shards = Polyrun::SpecQuality::PlanLoader.load_shards(plan_paths)
72
+ resolved_format = report_format || (json_out ? "json" : "text")
73
+
74
+ begin
75
+ text = Polyrun::SpecQuality::Report.render(
76
+ merged,
77
+ format: resolved_format,
78
+ cfg: cfg,
79
+ top: top,
80
+ profile: profile,
81
+ plan_shards: plan_shards
82
+ )
83
+ rescue Polyrun::Error => e
84
+ Polyrun::Log.warn e.message.to_s
85
+ return 2
86
+ end
87
+
88
+ if out_file
89
+ File.write(File.expand_path(out_file), text)
90
+ Polyrun::Log.puts File.expand_path(out_file)
91
+ else
92
+ Polyrun::Log.print text
93
+ end
94
+
95
+ violations = Polyrun::SpecQuality::Report.gate_violations(merged, cfg)
96
+ if strict && violations.any?
97
+ violations.each { |v| Polyrun::Log.warn "polyrun spec-quality gate: #{v}" }
98
+ return 1
99
+ end
100
+
101
+ 0
102
+ end
103
+ # rubocop:enable Metrics/AbcSize
104
+
105
+ def merge_spec_quality_after_shards(ctx)
106
+ files = merge_spec_quality_fragment_files
107
+ if files.empty?
108
+ Polyrun::Log.warn "polyrun run-shards: --merge-spec-quality: no spec-quality fragments found under coverage (enable spec-quality collection in your test setup)"
109
+ return nil
110
+ end
111
+
112
+ out = ctx[:merge_spec_quality_output] || "coverage/polyrun-spec-quality.json"
113
+ Polyrun::Log.warn "polyrun run-shards: merging #{files.size} spec-quality fragment(s) → #{out}"
114
+ Polyrun::SpecQuality::Merge.merge_and_write(files, File.expand_path(out))
115
+ report_spec_quality_after_merge(out, ctx)
116
+ File.expand_path(out)
117
+ end
118
+
119
+ def report_spec_quality_after_merge(merged_path, ctx)
120
+ return unless ctx[:report_spec_quality]
121
+
122
+ cfg = load_spec_quality_config(ctx[:config_path])
123
+ merged = JSON.parse(File.read(File.expand_path(merged_path)))
124
+ text = Polyrun::SpecQuality::Report.format_report(merged, cfg: cfg)
125
+ Polyrun::Log.print text
126
+
127
+ violations = Polyrun::SpecQuality::Report.gate_violations(merged, cfg)
128
+ return if violations.empty?
129
+
130
+ violations.each { |v| Polyrun::Log.warn "polyrun spec-quality gate: #{v}" }
131
+ end
132
+
133
+ def merge_spec_quality_fragment_files
134
+ Dir.glob(Polyrun::SpecQuality::Fragment.glob_pattern).sort
135
+ end
136
+
137
+ def load_spec_quality_config(config_path)
138
+ root = Dir.pwd
139
+ path = config_path
140
+ if path && !path.to_s.empty?
141
+ path = File.expand_path(path, root)
142
+ end
143
+ Polyrun::SpecQuality::Config.load(root: root, config_path: path)
144
+ rescue
145
+ Polyrun::SpecQuality::Config::DEFAULTS.dup
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,126 @@
1
+ require_relative "skip_env"
2
+
3
+ module Polyrun
4
+ class CLI
5
+ # +polyrun start+ only: run +prepare+ and/or Postgres template+shard DBs before parallel RSpec.
6
+ module StartBootstrap
7
+ private
8
+
9
+ def start_bootstrap!(cfg, argv, config_path)
10
+ if start_run_prepare?(cfg) && !SkipEnv.skip_prepare?
11
+ recipe = cfg.prepare["recipe"] || cfg.prepare[:recipe] || "default"
12
+ Polyrun::Log.warn "polyrun start: running prepare (recipe=#{recipe})" if @verbose
13
+ code = cmd_prepare([], config_path)
14
+ return code if code != 0
15
+ end
16
+
17
+ return 0 unless start_databases_provision_wanted?(cfg) && !SkipEnv.skip_databases?
18
+
19
+ dh = cfg.databases
20
+ adapter = Polyrun::Database::UrlBuilder::ConnectionInfer.infer_adapter_name(dh)
21
+ unless adapter == "postgresql"
22
+ return start_non_postgresql_databases_outcome(cfg, adapter)
23
+ end
24
+
25
+ workers = parse_workers_from_start_argv(argv, cfg)
26
+ Polyrun::Log.warn "polyrun start: provisioning test DBs (template + shards 0..#{workers - 1})" if @verbose
27
+ begin
28
+ Polyrun::Database::CloneShards.provision!(
29
+ dh,
30
+ workers: workers,
31
+ rails_root: Dir.pwd,
32
+ migrate: true,
33
+ replace: true,
34
+ force_drop: truthy_env?("POLYRUN_PG_DROP_FORCE"),
35
+ dry_run: false,
36
+ silent: !@verbose
37
+ )
38
+ rescue Polyrun::Error => e
39
+ Polyrun::Log.warn "polyrun start: #{e.message}"
40
+ return 1
41
+ end
42
+ 0
43
+ end
44
+
45
+ def start_run_prepare?(cfg)
46
+ st = cfg.start_config
47
+ prep = cfg.prepare
48
+ return false unless prep.is_a?(Hash) && !prep.empty?
49
+
50
+ return false if st["prepare"] == false || st[:prepare] == false
51
+ return true if st["prepare"] == true || st[:prepare] == true
52
+
53
+ prepare_recipe_has_side_effects?(prep)
54
+ end
55
+
56
+ def prepare_recipe_has_side_effects?(prep)
57
+ recipe = (prep["recipe"] || prep[:recipe] || "default").to_s
58
+ return true if %w[shell assets].include?(recipe)
59
+ return true if prep["command"] || prep[:command] || prep["commands"] || prep[:commands]
60
+
61
+ false
62
+ end
63
+
64
+ def start_databases_provision_wanted?(cfg)
65
+ st = cfg.start_config
66
+ dh = cfg.databases
67
+ return false unless dh.is_a?(Hash)
68
+
69
+ template = (dh["template_db"] || dh[:template_db]).to_s
70
+ return false if template.empty?
71
+
72
+ return false if st["databases"] == false || st[:databases] == false
73
+
74
+ true
75
+ end
76
+
77
+ def start_databases_explicitly_enabled?(cfg)
78
+ st = cfg.start_config
79
+ st["databases"] == true || st[:databases] == true
80
+ end
81
+
82
+ def start_non_postgresql_databases_outcome(cfg, adapter)
83
+ if start_databases_explicitly_enabled?(cfg)
84
+ Polyrun::Log.warn(
85
+ "polyrun start: start.databases is true but databases adapter is #{adapter}; " \
86
+ "CloneShards auto provision is PostgreSQL-only. Set start.databases: false and provision " \
87
+ "shards yourself (for example in prepare), or use a postgresql: connection block."
88
+ )
89
+ return 1
90
+ end
91
+
92
+ Polyrun::Log.warn(
93
+ "polyrun start: skipping database provision (adapter=#{adapter}; CloneShards is PostgreSQL-only). " \
94
+ "Provision shards in prepare, or set start.databases: false to silence this message."
95
+ )
96
+ 0
97
+ end
98
+
99
+ def parse_workers_from_start_argv(argv, cfg = nil)
100
+ sep = argv.index("--")
101
+ head = sep ? argv[0...sep] : argv
102
+ workers = Polyrun::Config::Resolver.parallel_worker_count(cfg)
103
+ i = 0
104
+ while i < head.size
105
+ if head[i] == "--workers" && head[i + 1]
106
+ w = Integer(head[i + 1], exception: false)
107
+ workers = w if w && w >= 1
108
+ i += 2
109
+ elsif head[i].start_with?("--workers=")
110
+ w = Integer(head[i].split("=", 2).last, exception: false)
111
+ workers = w if w && w >= 1
112
+ i += 1
113
+ else
114
+ i += 1
115
+ end
116
+ end
117
+ workers.clamp(1, Polyrun::Config::MAX_PARALLEL_WORKERS)
118
+ end
119
+
120
+ def truthy_env?(name)
121
+ v = ENV[name].to_s.downcase
122
+ %w[1 true yes].include?(v)
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,31 @@
1
+ require "optparse"
2
+
3
+ module Polyrun
4
+ class CLI
5
+ module TimingCommand
6
+ private
7
+
8
+ def cmd_merge_timing(argv)
9
+ inputs = []
10
+ output = "polyrun_timing.json"
11
+ parser = OptionParser.new do |opts|
12
+ opts.banner = "usage: polyrun merge-timing [-i FILE]... [-o OUT] [FILE...]"
13
+ opts.on("-i", "--input FILE", "Timing JSON fragment (repeatable)") { |f| inputs << f }
14
+ opts.on("-o", "--output PATH", String) { |v| output = v }
15
+ end
16
+ parser.parse!(argv)
17
+ inputs.concat(argv) if inputs.empty?
18
+
19
+ if inputs.empty?
20
+ Polyrun::Log.warn "merge-timing: need -i FILE or positional paths"
21
+ return 2
22
+ end
23
+
24
+ out_abs = File.expand_path(output)
25
+ Polyrun::Timing::Merge.merge_and_write(inputs.map { |p| File.expand_path(p) }, out_abs)
26
+ Polyrun::Log.puts out_abs
27
+ 0
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,48 @@
1
+ require_relative "../config"
2
+
3
+ module Polyrun
4
+ class CLI
5
+ module ToolOptions
6
+ private
7
+
8
+ def common_tool_options(parser, options)
9
+ parser.on("--[no-]enable") { |value| options[:enabled] = value }
10
+ parser.on("--root PATH") { |value| options[:root] = value }
11
+ parser.on("--timeout SECONDS", Float) { |value| options[:timeout] = value }
12
+ parser.on("--max-output-bytes N", Integer) { |value| options[:max_output_bytes] = value }
13
+ end
14
+
15
+ def configured_tool(feature, overrides)
16
+ overrides = overrides.dup
17
+ overrides[:root] = File.expand_path(overrides[:root]) if overrides.key?(:root)
18
+ root = File.expand_path(overrides[:root] || Dir.pwd)
19
+ cfg = Config.load(path: ENV["POLYRUN_CONFIG"], root: root)
20
+ settings = Config::Tools.resolve(cfg, feature, overrides: overrides, root: root)
21
+ if feature == "mutation"
22
+ graph = Config::Tools.resolve(cfg, "graph", root: root)
23
+ settings["staging"]["exclude_paths"] |= [graph["state_dir"]]
24
+ end
25
+ unless settings["enabled"]
26
+ raise Graph::Error.new("FEATURE_DISABLED", "#{feature} is disabled; enable it in polyrun.yml or use --enable", exitstatus: 2)
27
+ end
28
+ settings
29
+ end
30
+
31
+ def extract_tool_command!(argv)
32
+ separator = argv.index("--")
33
+ separator ? argv.slice!(separator..).drop(1) : nil
34
+ end
35
+
36
+ def tool_execution_options(settings, *keys)
37
+ settings.slice("root", "timeout", *keys).transform_keys(&:to_sym).merge(max_output: settings["max_output_bytes"])
38
+ end
39
+
40
+ def tool_command(settings, explicit)
41
+ command = explicit || settings.fetch("command")
42
+ raise ArgumentError, "a command is required after -- or in configuration" if command.empty?
43
+
44
+ command
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,26 @@
1
+ require_relative "../config/schema"
2
+
3
+ module Polyrun
4
+ class CLI
5
+ module ValidateCommand
6
+ private
7
+
8
+ def cmd_validate(argv, config_path)
9
+ unless argv.empty?
10
+ Polyrun::Log.warn "polyrun validate: unexpected arguments: #{argv.join(" ")}"
11
+ return 2
12
+ end
13
+
14
+ cfg = Polyrun::Config.load(path: config_path || ENV["POLYRUN_CONFIG"])
15
+ result = Polyrun::Config::Schema.validate(cfg)
16
+ result[:messages].each { |m| Polyrun::Log.warn m }
17
+ if result[:ok]
18
+ Polyrun::Log.puts "polyrun validate: ok (#{cfg.path || "no file"})"
19
+ 0
20
+ else
21
+ 1
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,272 @@
1
+ # rubocop:disable Polyrun/FileLength, Metrics/ClassLength -- CLI dispatch + require wiring
2
+ require "optparse"
3
+
4
+ require_relative "cli/helpers"
5
+ require_relative "cli/partition_diagnostics"
6
+ require_relative "cli/plan_command"
7
+ require_relative "cli/prepare_command"
8
+ require_relative "cli/coverage_commands"
9
+ require_relative "cli/report_commands"
10
+ require_relative "cli/env_commands"
11
+ require_relative "cli/database_commands"
12
+ require_relative "cli/run_shards_command"
13
+ require_relative "cli/run_queue_command"
14
+ require_relative "cli/queue_command"
15
+ require_relative "cli/timing_command"
16
+ require_relative "cli/spec_quality_commands"
17
+ require_relative "cli/benchmark_commands"
18
+ require_relative "cli/init_command"
19
+ require_relative "cli/quick_command"
20
+ require_relative "cli/ci_shard_run_parse"
21
+ require_relative "cli/ci_shard_run_command"
22
+ require_relative "cli/config_command"
23
+ require_relative "cli/validate_command"
24
+ require_relative "cli/doctor_command"
25
+ require_relative "cli/default_run"
26
+ require_relative "cli/hooks_command"
27
+ require_relative "cli/help"
28
+ require_relative "cli/tool_options"
29
+ require_relative "cli/graph_command"
30
+ require_relative "cli/mutation_command"
31
+ require_relative "cli/quality_commands"
32
+
33
+ module Polyrun
34
+ class CLI
35
+ CI_SHARD_COMMANDS = {
36
+ "ci-shard-run" => :cmd_ci_shard_run,
37
+ "ci-shard-rspec" => :cmd_ci_shard_rspec,
38
+ "ci-shard-sus" => :cmd_ci_shard_sus
39
+ }.freeze
40
+
41
+ # Keep in sync with +dispatch_cli_command_subcommands+ (+when+ branches). Used for implicit path routing.
42
+ DISPATCH_SUBCOMMAND_NAMES = %w[
43
+ graph mutate measure observe plan prepare merge-coverage merge-failures merge-spec-quality report-coverage report-junit report-timing report-spec-quality report-benchmark bench
44
+ env config merge-timing db:setup-template db:setup-shard db:clone-shards
45
+ run-shards parallel-rspec parallel-minitest parallel-sus parallel-quick start build-paths init queue run-queue quick hook validate doctor
46
+ ].freeze
47
+
48
+ # First argv token that is a normal subcommand (not a path); if argv[0] is not here but looks like paths, run implicit parallel.
49
+ IMPLICIT_PATH_EXCLUSION_TOKENS = (
50
+ DISPATCH_SUBCOMMAND_NAMES + CI_SHARD_COMMANDS.keys + %w[help version]
51
+ ).freeze
52
+
53
+ include Helpers
54
+ include PartitionDiagnostics
55
+ include PlanCommand
56
+ include PrepareCommand
57
+ include CoverageCommands
58
+ include ReportCommands
59
+ include EnvCommands
60
+ include DatabaseCommands
61
+ include RunShardsCommand
62
+ include RunQueueCommand
63
+ include QueueCommand
64
+ include TimingCommand
65
+ include SpecQualityCommands
66
+ include BenchmarkCommands
67
+ include InitCommand
68
+ include QuickCommand
69
+ include CiShardRunParse
70
+ include CiShardRunCommand
71
+ include ConfigCommand
72
+ include ValidateCommand
73
+ include DoctorCommand
74
+ include DefaultRun
75
+ include HooksCommand
76
+ include Help
77
+ include ToolOptions
78
+ include GraphCommand
79
+ include MutationCommand
80
+ include QualityCommands
81
+
82
+ def self.run(argv = ARGV)
83
+ new.run(argv)
84
+ end
85
+
86
+ def run(argv)
87
+ original_suite = ENV["POLYRUN_SUITE"]
88
+ original_config = ENV["POLYRUN_CONFIG"]
89
+ argv = argv.dup
90
+ config_path = parse_global_cli!(argv)
91
+ return config_path if config_path.is_a?(Integer)
92
+
93
+ ENV["POLYRUN_CONFIG"] = File.expand_path(config_path) if config_path
94
+
95
+ if argv.empty?
96
+ Polyrun::Debug.log_kv(
97
+ command: "(default)",
98
+ cwd: Dir.pwd,
99
+ polyrun_config: config_path,
100
+ argv_rest: [],
101
+ verbose: @verbose
102
+ )
103
+ return dispatch_default_parallel!(config_path)
104
+ end
105
+
106
+ if implicit_parallel_run?(argv)
107
+ Polyrun::Debug.log_kv(
108
+ command: "(paths)",
109
+ cwd: Dir.pwd,
110
+ polyrun_config: config_path,
111
+ argv_rest: argv.dup,
112
+ verbose: @verbose
113
+ )
114
+ return dispatch_implicit_parallel_targets!(argv, config_path)
115
+ end
116
+
117
+ command = argv.shift
118
+
119
+ Polyrun::Debug.log_kv(
120
+ command: command,
121
+ cwd: Dir.pwd,
122
+ polyrun_config: config_path,
123
+ argv_rest: argv.dup,
124
+ verbose: @verbose
125
+ )
126
+
127
+ dispatch_cli_command(command, argv, config_path)
128
+ rescue Polyrun::Error => e
129
+ Polyrun::Log.warn "polyrun: #{e.message}"
130
+ 2
131
+ ensure
132
+ ENV["POLYRUN_SUITE"] = original_suite
133
+ ENV["POLYRUN_CONFIG"] = original_config
134
+ end
135
+
136
+ private
137
+
138
+ def parse_global_cli!(argv)
139
+ config_path = ENV["POLYRUN_CONFIG"]
140
+ @verbose = false
141
+ while (a = argv.first) && a.start_with?("-") && a != "--"
142
+ case a
143
+ when "-c", "--config"
144
+ argv.shift
145
+ config_path = argv.shift or break
146
+ when "--suite"
147
+ argv.shift
148
+ name = argv.shift
149
+ raise Polyrun::Error, "--suite requires a name" if name.nil? || name.strip.empty? || name.start_with?("-")
150
+
151
+ ENV["POLYRUN_SUITE"] = name
152
+ when /\A--suite=(.*)\z/
153
+ name = Regexp.last_match(1)
154
+ raise Polyrun::Error, "--suite requires a name" if name.strip.empty?
155
+
156
+ ENV["POLYRUN_SUITE"] = name
157
+ argv.shift
158
+ when "-v", "--verbose"
159
+ @verbose = true
160
+ ENV["POLYRUN_VERBOSE"] = "1"
161
+ argv.shift
162
+ when "-h", "--help"
163
+ print_help
164
+ return 0
165
+ else
166
+ break
167
+ end
168
+ end
169
+ config_path
170
+ end
171
+
172
+ def dispatch_cli_command(command, argv, config_path)
173
+ case command
174
+ when "help"
175
+ print_help
176
+ 0
177
+ when "version"
178
+ cmd_version
179
+ else
180
+ dispatch_cli_command_subcommands(command, argv, config_path)
181
+ end
182
+ end
183
+
184
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity -- explicit dispatch table
185
+ def dispatch_cli_command_subcommands(command, argv, config_path)
186
+ case command
187
+ when "graph"
188
+ cmd_graph(argv)
189
+ when "mutate"
190
+ cmd_mutate(argv)
191
+ when "measure"
192
+ cmd_measure(argv)
193
+ when "observe"
194
+ cmd_observe(argv)
195
+ when "plan"
196
+ cmd_plan(argv, config_path)
197
+ when "prepare"
198
+ cmd_prepare(argv, config_path)
199
+ when "merge-coverage"
200
+ cmd_merge_coverage(argv, config_path)
201
+ when "merge-failures"
202
+ cmd_merge_failures(argv, config_path)
203
+ when "report-coverage"
204
+ cmd_report_coverage(argv)
205
+ when "report-junit"
206
+ cmd_report_junit(argv)
207
+ when "report-timing"
208
+ cmd_report_timing(argv)
209
+ when "env"
210
+ cmd_env(argv, config_path)
211
+ when "config"
212
+ cmd_config(argv, config_path)
213
+ when "validate"
214
+ cmd_validate(argv, config_path)
215
+ when "doctor"
216
+ cmd_doctor(argv, config_path)
217
+ when "merge-timing"
218
+ cmd_merge_timing(argv)
219
+ when "merge-spec-quality"
220
+ cmd_merge_spec_quality(argv)
221
+ when "report-spec-quality"
222
+ cmd_report_spec_quality(argv)
223
+ when "report-benchmark"
224
+ cmd_report_benchmark(argv)
225
+ when "bench"
226
+ cmd_bench(argv)
227
+ when "db:setup-template"
228
+ cmd_db_setup_template(argv, config_path)
229
+ when "db:setup-shard"
230
+ cmd_db_setup_shard(argv, config_path)
231
+ when "db:clone-shards"
232
+ cmd_db_clone_shards(argv, config_path)
233
+ when "run-shards"
234
+ cmd_run_shards(argv, config_path)
235
+ when "parallel-rspec"
236
+ cmd_parallel_rspec(argv, config_path)
237
+ when "parallel-minitest"
238
+ cmd_parallel_minitest(argv, config_path)
239
+ when "parallel-sus"
240
+ cmd_parallel_sus(argv, config_path)
241
+ when "parallel-quick"
242
+ cmd_parallel_quick(argv, config_path)
243
+ when "start"
244
+ cmd_start(argv, config_path)
245
+ when "build-paths"
246
+ cmd_build_paths(config_path)
247
+ when *CI_SHARD_COMMANDS.keys
248
+ send(CI_SHARD_COMMANDS.fetch(command), argv, config_path)
249
+ when "init"
250
+ cmd_init(argv, config_path)
251
+ when "queue"
252
+ cmd_queue(argv)
253
+ when "run-queue"
254
+ cmd_run_queue(argv, config_path)
255
+ when "quick"
256
+ cmd_quick(argv)
257
+ when "hook"
258
+ cmd_hook(argv, config_path)
259
+ else
260
+ Polyrun::Log.warn "unknown command: #{command}"
261
+ 2
262
+ end
263
+ end
264
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
265
+
266
+ def cmd_version
267
+ Polyrun::Log.puts "polyrun #{Polyrun::VERSION}"
268
+ 0
269
+ end
270
+ end
271
+ end
272
+ # rubocop:enable Polyrun/FileLength, Metrics/ClassLength