polyrun 2.2.7 → 2.2.9

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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -1
  3. data/ext/polyrun_coverage_merge/coverage_merge.c +34 -15
  4. data/lib/polyrun/benchmark/pressure/cooker.rb +191 -0
  5. data/lib/polyrun/benchmark/pressure/process_sample.rb +127 -0
  6. data/lib/polyrun/benchmark/pressure.rb +144 -0
  7. data/lib/polyrun/cli/coverage_commands.rb +3 -3
  8. data/lib/polyrun/cli/doctor_command.rb +94 -0
  9. data/lib/polyrun/cli/help.rb +7 -2
  10. data/lib/polyrun/cli/helpers.rb +4 -18
  11. data/lib/polyrun/cli/init_command.rb +2 -0
  12. data/lib/polyrun/cli/run_queue_command.rb +1 -1
  13. data/lib/polyrun/cli/run_shards_plan_boot_phases.rb +2 -2
  14. data/lib/polyrun/cli/run_shards_plan_options.rb +4 -4
  15. data/lib/polyrun/cli/run_shards_planning.rb +1 -1
  16. data/lib/polyrun/cli/skip_env.rb +34 -0
  17. data/lib/polyrun/cli/start_bootstrap.rb +7 -5
  18. data/lib/polyrun/cli/validate_command.rb +26 -0
  19. data/lib/polyrun/cli.rb +30 -3
  20. data/lib/polyrun/config/effective.rb +2 -1
  21. data/lib/polyrun/config/resolver.rb +27 -0
  22. data/lib/polyrun/config/schema.rb +34 -0
  23. data/lib/polyrun/config/suite.rb +104 -0
  24. data/lib/polyrun/config.rb +17 -5
  25. data/lib/polyrun/coverage/config_resolve.rb +131 -0
  26. data/lib/polyrun/coverage/merge.rb +33 -6
  27. data/lib/polyrun/coverage/merge_fragment_meta.rb +7 -1
  28. data/lib/polyrun/coverage/merge_merge_two.rb +2 -0
  29. data/lib/polyrun/coverage/merge_native.rb +2 -0
  30. data/lib/polyrun/coverage/rails.rb +10 -12
  31. data/lib/polyrun/templates/rails_prepare.polyrun.yml +13 -0
  32. data/lib/polyrun/templates/rails_prepare_task.rake +16 -0
  33. data/lib/polyrun/version.rb +1 -1
  34. metadata +11 -1
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "json"
2
4
 
3
5
  module Polyrun
@@ -26,15 +28,15 @@ module Polyrun
26
28
  def merge_fragments(paths)
27
29
  return {blob: {}, meta: {}, groups: nil} if paths.empty?
28
30
 
29
- # Parse one fragment at a time: keep only meta + blob so peak RSS drops after each fold step.
31
+ # Fold one fragment at a time so peak RSS does not retain every blob concurrently.
30
32
  metas = []
31
- blobs = []
33
+ merged_blob = nil
32
34
  paths.each do |path|
33
- doc = JSON.parse(File.read(path))
35
+ doc = JSON.parse(File.binread(path))
34
36
  metas << extract_doc_meta(doc)
35
- blobs << extract_coverage_blob(doc)
37
+ blob = coverage_blob_from_doc(doc)
38
+ merged_blob = merged_blob.nil? ? blob : merge_two(merged_blob, blob)
36
39
  end
37
- merged_blob = merge_blob_tree(blobs)
38
40
  merged_meta = merge_fragment_metas_from_metas(metas)
39
41
  merged_blob = apply_track_files_once_after_merge(merged_blob, merged_meta)
40
42
  groups_payload = recompute_groups_from_meta(merged_blob, merged_meta)
@@ -96,13 +98,33 @@ module Polyrun
96
98
  end
97
99
 
98
100
  def parse_file(path)
99
- text = File.read(path)
101
+ text = File.binread(path)
100
102
  data = JSON.parse(text)
101
103
  extract_coverage_blob(data)
102
104
  end
103
105
 
104
106
  # Picks top-level export `coverage`, merges all suite entries (e.g. RSpec + Minitest),
105
107
  # and combines both when present.
108
+ # Hot path: cooker and worker fragments are { "coverage" => Hash, "meta" => ... } without
109
+ # nested per-tool suites under extra top-level keys.
110
+ def coverage_blob_from_doc(doc)
111
+ return extract_coverage_blob(doc) unless doc.is_a?(Hash)
112
+
113
+ top = doc["coverage"]
114
+ return extract_coverage_blob(doc) unless top.is_a?(Hash)
115
+
116
+ doc.each_key do |k|
117
+ next if k == "coverage"
118
+
119
+ v = doc[k]
120
+ if v.is_a?(Hash) && v["coverage"].is_a?(Hash)
121
+ return extract_coverage_blob(doc)
122
+ end
123
+ end
124
+
125
+ top
126
+ end
127
+
106
128
  def extract_coverage_blob(data)
107
129
  return {} unless data.is_a?(Hash)
108
130
 
@@ -121,6 +143,11 @@ module Polyrun
121
143
  return {}
122
144
  end
123
145
 
146
+ if nested.size == 1
147
+ only = nested.first
148
+ return top.is_a?(Hash) ? merge_two(top, only) : only
149
+ end
150
+
124
151
  merged = nested.reduce { |acc, el| merge_two(acc, el) }
125
152
  top.is_a?(Hash) ? merge_two(top, merged) : merged
126
153
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Polyrun
2
4
  module Coverage
3
5
  module Merge
@@ -9,6 +11,7 @@ module Polyrun
9
11
 
10
12
  def merge_fragment_metas_from_metas(metas)
11
13
  return {} if metas.empty?
14
+ return {} if metas.all? { |m| m.nil? || m.empty? }
12
15
 
13
16
  base = metas.first.dup
14
17
  roots = metas.map { |m| m["polyrun_coverage_root"] }.compact
@@ -43,7 +46,10 @@ module Polyrun
43
46
  end
44
47
 
45
48
  def merge_fragment_meta_warn_track_files!(tfs)
46
- return if tfs.map { |tf| JSON.generate(normalize_track_files_meta(tf)) }.uniq.size <= 1
49
+ return if tfs.empty?
50
+
51
+ normalized = tfs.map { |tf| normalize_track_files_meta(tf) }
52
+ return if normalized.uniq.size <= 1
47
53
 
48
54
  Polyrun::Log.warn "Polyrun merge-coverage: polyrun_track_files differs across fragments; using first."
49
55
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Polyrun
2
4
  module Coverage
3
5
  module Merge
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Polyrun
2
4
  module Coverage
3
5
  module MergeNative
@@ -2,13 +2,15 @@ require "yaml"
2
2
 
3
3
  require_relative "collector"
4
4
  require_relative "formatter"
5
+ require_relative "config_resolve"
5
6
 
6
7
  module Polyrun
7
8
  module Coverage
8
- # Rails integration entry point for {Collector.start!}: optional +config/polyrun_coverage.yml+ under the project
9
- # root, root inference from +spec_helper.rb+ / +rails_helper.rb+ / +test_helper.rb+, and +report_formats+ for
10
- # {Formatter.multi}. Call at the **top** of +spec/spec_helper.rb+ (before +require "rails"+) so stdlib
11
- # +Coverage+ sees application code.
9
+ # Rails integration entry point for {Collector.start!}: coverage settings from
10
+ # +config/polyrun_coverage.yml+ and/or inline +coverage:+ in +polyrun.yml+ (see {ConfigResolve}),
11
+ # root inference from +spec_helper.rb+ / +rails_helper.rb+ / +test_helper.rb+, and +report_formats+
12
+ # for {Formatter.multi}. Call at the **top** of +spec/spec_helper.rb+ (before +require "rails"+) so
13
+ # stdlib +Coverage+ sees application code.
12
14
  #
13
15
  # require "polyrun/coverage/rails"
14
16
  # Polyrun::Coverage::Rails.start!
@@ -16,10 +18,10 @@ module Polyrun
16
18
  module Rails
17
19
  module_function
18
20
 
19
- DEFAULT_CONFIG_RELATIVE = File.join("config", "polyrun_coverage.yml").freeze
21
+ DEFAULT_CONFIG_RELATIVE = ConfigResolve::DEFAULT_FILE
20
22
 
21
23
  # @param root [String, nil] project root (default: infer from caller, or +Rails.root+ when already loaded)
22
- # @param config_path [String, nil] YAML path (default: +<root>/config/polyrun_coverage.yml+ if present)
24
+ # @param config_path [String, nil] YAML path (default: resolve via {ConfigResolve})
23
25
  # @param overrides [Hash] merged over YAML; keys match {Collector.start!} (+report_formats+ builds +formatter+)
24
26
  def start!(root: nil, config_path: nil, **overrides)
25
27
  return if Collector.disabled?
@@ -29,6 +31,7 @@ module Polyrun
29
31
  cfg = load_config(root, config_path)
30
32
  merged = deep_merge_hashes(cfg, stringify_keys(overrides))
31
33
  merged["root"] = root
34
+ ConfigResolve::META_KEYS.each { |k| merged.delete(k) }
32
35
 
33
36
  report_formats = merged.delete("report_formats")
34
37
  unless merged.key?("formatter") || report_formats.nil?
@@ -63,12 +66,7 @@ module Polyrun
63
66
  end
64
67
 
65
68
  def load_config(root, config_path)
66
- path = config_path || File.join(root, DEFAULT_CONFIG_RELATIVE)
67
- path = File.expand_path(path)
68
- return {} unless File.file?(path)
69
-
70
- data = YAML.safe_load_file(path, permitted_classes: [Symbol], aliases: true)
71
- data.is_a?(Hash) ? stringify_keys(data) : {}
69
+ ConfigResolve.load(root: root, config_path: config_path)
72
70
  end
73
71
 
74
72
  def build_formatter(formats, root, merged)
@@ -3,6 +3,8 @@
3
3
  # Gate per-worker duplicate work in application code when POLYRUN_SHARD_TOTAL > 1.
4
4
  # See: docs/SETUP_PROFILE.md
5
5
 
6
+ version: 1
7
+
6
8
  partition:
7
9
  paths_file: spec/spec_paths.txt
8
10
  shard_total: 5
@@ -17,8 +19,19 @@ prepare:
17
19
  rails_root: .
18
20
  command: bundle exec ruby bin/test_prepare.rb
19
21
 
22
+ start:
23
+ prepare: true
24
+ databases: true
25
+
26
+ # Optional suite hooks (uncomment and set commands):
27
+ # hooks:
28
+ # before_suite: bundle exec ruby script/polyrun_before_suite.rb
29
+ # after_suite: bundle exec ruby script/polyrun_after_suite.rb
30
+ hooks: {}
31
+
20
32
  # Optional reporting (see docs/SPEC_QUALITY.md):
21
33
  # reporting:
34
+ # merge_failures: true
22
35
  # merge_spec_quality: true
23
36
  # merge_spec_quality_output: coverage/polyrun-spec-quality.json
24
37
 
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Generated by: polyrun init --profile rails-prepare-task
4
+ # Runs once before parallel workers. Skip: POLYRUN_SKIP_PREPARE=1 (legacy: POLYRUN_START_SKIP_PREPARE=1).
5
+
6
+ namespace :polyrun do
7
+ desc "One-shot Polyrun prepare (delegates to `polyrun prepare`)"
8
+ task prepare: :environment do
9
+ skip = %w[1 true yes].include?(ENV["POLYRUN_SKIP_PREPARE"].to_s.downcase) ||
10
+ %w[1 true yes].include?(ENV["POLYRUN_START_SKIP_PREPARE"].to_s.downcase)
11
+ next if skip
12
+
13
+ ok = system({"RUBYOPT" => nil}, "bundle", "exec", "polyrun", "prepare")
14
+ abort "polyrun prepare failed" unless ok
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Polyrun
2
- VERSION = "2.2.7"
2
+ VERSION = "2.2.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyrun
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.7
4
+ version: 2.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov
@@ -210,6 +210,9 @@ files:
210
210
  - ext/polyrun_coverage_merge/coverage_merge.c
211
211
  - ext/polyrun_coverage_merge/extconf.rb
212
212
  - lib/polyrun.rb
213
+ - lib/polyrun/benchmark/pressure.rb
214
+ - lib/polyrun/benchmark/pressure/cooker.rb
215
+ - lib/polyrun/benchmark/pressure/process_sample.rb
213
216
  - lib/polyrun/benchmark/profile.rb
214
217
  - lib/polyrun/benchmark/report.rb
215
218
  - lib/polyrun/cli.rb
@@ -222,6 +225,7 @@ files:
222
225
  - lib/polyrun/cli/coverage_merge_io.rb
223
226
  - lib/polyrun/cli/database_commands.rb
224
227
  - lib/polyrun/cli/default_run.rb
228
+ - lib/polyrun/cli/doctor_command.rb
225
229
  - lib/polyrun/cli/env_commands.rb
226
230
  - lib/polyrun/cli/failure_commands.rb
227
231
  - lib/polyrun/cli/help.rb
@@ -244,17 +248,22 @@ files:
244
248
  - lib/polyrun/cli/run_shards_planning.rb
245
249
  - lib/polyrun/cli/run_shards_run.rb
246
250
  - lib/polyrun/cli/run_shards_worker_interrupt.rb
251
+ - lib/polyrun/cli/skip_env.rb
247
252
  - lib/polyrun/cli/spec_quality_commands.rb
248
253
  - lib/polyrun/cli/start_bootstrap.rb
249
254
  - lib/polyrun/cli/timing_command.rb
255
+ - lib/polyrun/cli/validate_command.rb
250
256
  - lib/polyrun/config.rb
251
257
  - lib/polyrun/config/dotted_path.rb
252
258
  - lib/polyrun/config/effective.rb
253
259
  - lib/polyrun/config/resolver.rb
260
+ - lib/polyrun/config/schema.rb
261
+ - lib/polyrun/config/suite.rb
254
262
  - lib/polyrun/coverage/cobertura_zero_lines.rb
255
263
  - lib/polyrun/coverage/collector.rb
256
264
  - lib/polyrun/coverage/collector_finish.rb
257
265
  - lib/polyrun/coverage/collector_fragment_meta.rb
266
+ - lib/polyrun/coverage/config_resolve.rb
258
267
  - lib/polyrun/coverage/example_diff.rb
259
268
  - lib/polyrun/coverage/example_diff_snapshot.rb
260
269
  - lib/polyrun/coverage/example_diff_track_filter.rb
@@ -360,6 +369,7 @@ files:
360
369
  - lib/polyrun/templates/polyrun_hooks_spec_quality.rb
361
370
  - lib/polyrun/templates/polyrun_spec_quality.yml
362
371
  - lib/polyrun/templates/rails_prepare.polyrun.yml
372
+ - lib/polyrun/templates/rails_prepare_task.rake
363
373
  - lib/polyrun/timing/merge.rb
364
374
  - lib/polyrun/timing/rspec_example_formatter.rb
365
375
  - lib/polyrun/timing/stats.rb