simplecov 0.22.0 → 1.3.0

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 (210) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +173 -875
  4. data/exe/simplecov +8 -0
  5. data/lib/minitest/simplecov_plugin.rb +18 -7
  6. data/lib/simplecov/atomic_file.rb +67 -0
  7. data/lib/simplecov/autostart.rb +9 -0
  8. data/lib/simplecov/baseline/parser.rb +58 -0
  9. data/lib/simplecov/baseline.rb +170 -0
  10. data/lib/simplecov/cli/affected/changed_files.rb +57 -0
  11. data/lib/simplecov/cli/affected/selection.rb +117 -0
  12. data/lib/simplecov/cli/affected.rb +148 -0
  13. data/lib/simplecov/cli/annotations.rb +166 -0
  14. data/lib/simplecov/cli/badge/svg.rb +70 -0
  15. data/lib/simplecov/cli/badge.rb +62 -0
  16. data/lib/simplecov/cli/clean.rb +86 -0
  17. data/lib/simplecov/cli/command_helpers.rb +91 -0
  18. data/lib/simplecov/cli/completions/scripts.rb +114 -0
  19. data/lib/simplecov/cli/completions.rb +83 -0
  20. data/lib/simplecov/cli/coverage.rb +83 -0
  21. data/lib/simplecov/cli/coverage_file.rb +109 -0
  22. data/lib/simplecov/cli/dead_code/output.rb +100 -0
  23. data/lib/simplecov/cli/dead_code.rb +139 -0
  24. data/lib/simplecov/cli/diff/output.rb +45 -0
  25. data/lib/simplecov/cli/diff.rb +115 -0
  26. data/lib/simplecov/cli/dotfile.rb +156 -0
  27. data/lib/simplecov/cli/git.rb +41 -0
  28. data/lib/simplecov/cli/history/output.rb +135 -0
  29. data/lib/simplecov/cli/history.rb +67 -0
  30. data/lib/simplecov/cli/merge.rb +115 -0
  31. data/lib/simplecov/cli/open.rb +45 -0
  32. data/lib/simplecov/cli/patch/changed_lines.rb +149 -0
  33. data/lib/simplecov/cli/patch/output.rb +151 -0
  34. data/lib/simplecov/cli/patch.rb +175 -0
  35. data/lib/simplecov/cli/ratchet/output.rb +44 -0
  36. data/lib/simplecov/cli/ratchet.rb +83 -0
  37. data/lib/simplecov/cli/report.rb +91 -0
  38. data/lib/simplecov/cli/run.rb +35 -0
  39. data/lib/simplecov/cli/serve/report_preparer.rb +30 -0
  40. data/lib/simplecov/cli/serve/static_file_handler.rb +128 -0
  41. data/lib/simplecov/cli/serve.rb +88 -0
  42. data/lib/simplecov/cli/show/annotator.rb +87 -0
  43. data/lib/simplecov/cli/show/sweep.rb +36 -0
  44. data/lib/simplecov/cli/show.rb +133 -0
  45. data/lib/simplecov/cli/status/facts.rb +87 -0
  46. data/lib/simplecov/cli/status.rb +93 -0
  47. data/lib/simplecov/cli/tests/redundancy.rb +78 -0
  48. data/lib/simplecov/cli/tests.rb +148 -0
  49. data/lib/simplecov/cli/uncovered/misses.rb +33 -0
  50. data/lib/simplecov/cli/uncovered.rb +127 -0
  51. data/lib/simplecov/cli/usage.rb +153 -0
  52. data/lib/simplecov/cli/watch/live_report.rb +73 -0
  53. data/lib/simplecov/cli/watch/narrator.rb +55 -0
  54. data/lib/simplecov/cli/watch/poller.rb +45 -0
  55. data/lib/simplecov/cli/watch/session.rb +134 -0
  56. data/lib/simplecov/cli/watch/test_plan.rb +43 -0
  57. data/lib/simplecov/cli/watch.rb +85 -0
  58. data/lib/simplecov/cli.rb +114 -0
  59. data/lib/simplecov/color.rb +65 -0
  60. data/lib/simplecov/combine/branches_combiner.rb +44 -21
  61. data/lib/simplecov/combine/coverage_accumulator.rb +175 -0
  62. data/lib/simplecov/combine/identity_interner.rb +27 -0
  63. data/lib/simplecov/combine/interned_counts.rb +28 -0
  64. data/lib/simplecov/combine/lines_combiner.rb +35 -29
  65. data/lib/simplecov/combine/methods_combiner.rb +48 -0
  66. data/lib/simplecov/combine/results_combiner.rb +9 -49
  67. data/lib/simplecov/combine.rb +0 -24
  68. data/lib/simplecov/command_guesser.rb +99 -42
  69. data/lib/simplecov/configuration/baseline.rb +31 -0
  70. data/lib/simplecov/configuration/coverage.rb +184 -0
  71. data/lib/simplecov/configuration/coverage_criteria.rb +143 -0
  72. data/lib/simplecov/configuration/deprecations.rb +21 -0
  73. data/lib/simplecov/configuration/eval_coverage.rb +37 -0
  74. data/lib/simplecov/configuration/filters.rb +131 -0
  75. data/lib/simplecov/configuration/formatting.rb +129 -0
  76. data/lib/simplecov/configuration/groups.rb +48 -0
  77. data/lib/simplecov/configuration/history.rb +44 -0
  78. data/lib/simplecov/configuration/ignored_entries.rb +72 -0
  79. data/lib/simplecov/configuration/merging.rb +197 -0
  80. data/lib/simplecov/configuration/missed_caps.rb +51 -0
  81. data/lib/simplecov/configuration/production.rb +23 -0
  82. data/lib/simplecov/configuration/test_tracking.rb +48 -0
  83. data/lib/simplecov/configuration/thresholds.rb +150 -0
  84. data/lib/simplecov/configuration/view_coverage.rb +37 -0
  85. data/lib/simplecov/configuration.rb +100 -443
  86. data/lib/simplecov/configuration_error.rb +5 -0
  87. data/lib/simplecov/context_map/union.rb +81 -0
  88. data/lib/simplecov/context_map.rb +187 -0
  89. data/lib/simplecov/coverage_json.rb +21 -0
  90. data/lib/simplecov/coverage_statistics.rb +24 -28
  91. data/lib/simplecov/coverage_violations.rb +254 -0
  92. data/lib/simplecov/current_run.rb +61 -0
  93. data/lib/simplecov/defaults.rb +20 -28
  94. data/lib/simplecov/deprecation.rb +63 -0
  95. data/lib/simplecov/directive/erb.rb +44 -0
  96. data/lib/simplecov/directive/haml.rb +33 -0
  97. data/lib/simplecov/directive/indented_template.rb +37 -0
  98. data/lib/simplecov/directive/slim.rb +35 -0
  99. data/lib/simplecov/directive/template.rb +29 -0
  100. data/lib/simplecov/directive.rb +152 -0
  101. data/lib/simplecov/exit_codes/baseline_check.rb +46 -0
  102. data/lib/simplecov/exit_codes/check.rb +39 -0
  103. data/lib/simplecov/exit_codes/exit_code_handling.rb +24 -5
  104. data/lib/simplecov/exit_codes/maximum_coverage_drop_check.rb +17 -67
  105. data/lib/simplecov/exit_codes/maximum_missed_check.rb +26 -0
  106. data/lib/simplecov/exit_codes/maximum_missed_per_file_check.rb +34 -0
  107. data/lib/simplecov/exit_codes/maximum_overall_coverage_check.rb +27 -0
  108. data/lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb +20 -38
  109. data/lib/simplecov/exit_codes/minimum_coverage_by_group_check.rb +28 -0
  110. data/lib/simplecov/exit_codes/minimum_overall_coverage_check.rb +37 -34
  111. data/lib/simplecov/exit_codes.rb +26 -0
  112. data/lib/simplecov/exit_handling.rb +163 -0
  113. data/lib/simplecov/file_list.rb +63 -48
  114. data/lib/simplecov/filter.rb +98 -42
  115. data/lib/simplecov/formatter/base.rb +100 -0
  116. data/lib/simplecov/formatter/baseline_formatter.rb +98 -0
  117. data/lib/simplecov/formatter/coverage_json_writer.rb +95 -0
  118. data/lib/simplecov/formatter/html_formatter/public/index.html +93 -0
  119. data/lib/simplecov/formatter/html_formatter/viewer_data_validator.rb +153 -0
  120. data/lib/simplecov/formatter/html_formatter.rb +88 -0
  121. data/lib/simplecov/formatter/json_formatter/errors_formatter.rb +122 -0
  122. data/lib/simplecov/formatter/json_formatter/production_section_formatter.rb +48 -0
  123. data/lib/simplecov/formatter/json_formatter/result_hash_formatter.rb +132 -0
  124. data/lib/simplecov/formatter/json_formatter/source_file_formatter.rb +97 -0
  125. data/lib/simplecov/formatter/json_formatter.rb +40 -0
  126. data/lib/simplecov/formatter/multi_formatter.rb +8 -10
  127. data/lib/simplecov/formatter/simple_formatter.rb +12 -15
  128. data/lib/simplecov/formatter.rb +21 -1
  129. data/lib/simplecov/group_names.rb +33 -0
  130. data/lib/simplecov/history.rb +115 -0
  131. data/lib/simplecov/last_run.rb +16 -5
  132. data/lib/simplecov/lines_classifier.rb +67 -19
  133. data/lib/simplecov/load_global_config.rb +12 -4
  134. data/lib/simplecov/parallel_adapters/base.rb +50 -0
  135. data/lib/simplecov/parallel_adapters/generic.rb +33 -0
  136. data/lib/simplecov/parallel_adapters/parallel_tests.rb +72 -0
  137. data/lib/simplecov/parallel_adapters.rb +54 -0
  138. data/lib/simplecov/parallel_coordination.rb +124 -0
  139. data/lib/simplecov/parallel_result_merger/worker_payload.rb +33 -0
  140. data/lib/simplecov/parallel_result_merger.rb +171 -0
  141. data/lib/simplecov/process.rb +34 -12
  142. data/lib/simplecov/production/error.rb +11 -0
  143. data/lib/simplecov/production/file_sink.rb +130 -0
  144. data/lib/simplecov/production.rb +249 -0
  145. data/lib/simplecov/profiles/bundler_filter.rb +1 -1
  146. data/lib/simplecov/profiles/hidden_filter.rb +1 -1
  147. data/lib/simplecov/profiles/rails.rb +13 -10
  148. data/lib/simplecov/profiles/root_filter.rb +2 -5
  149. data/lib/simplecov/profiles/strict.rb +22 -0
  150. data/lib/simplecov/profiles/test_frameworks.rb +1 -4
  151. data/lib/simplecov/profiles.rb +26 -19
  152. data/lib/simplecov/report_deferral.rb +45 -0
  153. data/lib/simplecov/report_stamp.rb +25 -0
  154. data/lib/simplecov/result/filter_config.rb +19 -0
  155. data/lib/simplecov/result/missing_source_files_reporter.rb +48 -0
  156. data/lib/simplecov/result/serialization.rb +43 -0
  157. data/lib/simplecov/result/source_file_builder.rb +46 -0
  158. data/lib/simplecov/result.rb +123 -47
  159. data/lib/simplecov/result_adapter.rb +130 -11
  160. data/lib/simplecov/result_merger/contexts.rb +22 -0
  161. data/lib/simplecov/result_merger/legacy_format_adapter.rb +24 -0
  162. data/lib/simplecov/result_merger/resultset_file.rb +67 -0
  163. data/lib/simplecov/result_merger/resultset_run_identity.rb +65 -0
  164. data/lib/simplecov/result_merger/resultset_store.rb +62 -0
  165. data/lib/simplecov/result_merger/unloaded_files.rb +95 -0
  166. data/lib/simplecov/result_merger.rb +120 -124
  167. data/lib/simplecov/result_processing.rb +188 -0
  168. data/lib/simplecov/run_identity.rb +75 -0
  169. data/lib/simplecov/simulate_coverage.rb +72 -17
  170. data/lib/simplecov/source_file/branch.rb +8 -35
  171. data/lib/simplecov/source_file/branch_builder.rb +95 -0
  172. data/lib/simplecov/source_file/builder_context.rb +28 -0
  173. data/lib/simplecov/source_file/line.rb +17 -32
  174. data/lib/simplecov/source_file/line_builder.rb +37 -0
  175. data/lib/simplecov/source_file/method.rb +54 -0
  176. data/lib/simplecov/source_file/method_builder.rb +51 -0
  177. data/lib/simplecov/source_file/ruby_data_parser.rb +103 -0
  178. data/lib/simplecov/source_file/skip_chunks.rb +72 -0
  179. data/lib/simplecov/source_file/source_loader.rb +89 -0
  180. data/lib/simplecov/source_file/statistics.rb +76 -0
  181. data/lib/simplecov/source_file.rb +64 -260
  182. data/lib/simplecov/static_coverage_extractor/condition_folding.rb +155 -0
  183. data/lib/simplecov/static_coverage_extractor/location_conventions.rb +191 -0
  184. data/lib/simplecov/static_coverage_extractor/method_collector.rb +45 -0
  185. data/lib/simplecov/static_coverage_extractor/prism_compat.rb +46 -0
  186. data/lib/simplecov/static_coverage_extractor/value_position.rb +61 -0
  187. data/lib/simplecov/static_coverage_extractor/visitor.rb +163 -0
  188. data/lib/simplecov/static_coverage_extractor.rb +78 -0
  189. data/lib/simplecov/test_tracker/accessors.rb +34 -0
  190. data/lib/simplecov/test_tracker/constant_watch.rb +49 -0
  191. data/lib/simplecov/test_tracker/delta.rb +69 -0
  192. data/lib/simplecov/test_tracker/framework_hooks.rb +130 -0
  193. data/lib/simplecov/test_tracker.rb +162 -0
  194. data/lib/simplecov/unloaded_file_injector.rb +62 -0
  195. data/lib/simplecov/useless_results_remover.rb +17 -7
  196. data/lib/simplecov/version.rb +1 -1
  197. data/lib/simplecov/view_coverage/template_compiler.rb +83 -0
  198. data/lib/simplecov/view_coverage.rb +55 -0
  199. data/lib/simplecov-html.rb +5 -0
  200. data/lib/simplecov.rb +143 -394
  201. data/lib/simplecov_json_formatter.rb +5 -0
  202. data/man/simplecov.1 +354 -0
  203. data/sig/simplecov.rbs +1710 -0
  204. metadata +183 -61
  205. data/CHANGELOG.md +0 -191
  206. data/doc/alternate-formatters.md +0 -71
  207. data/doc/commercial-services.md +0 -25
  208. data/doc/editor-integration.md +0 -18
  209. data/lib/simplecov/combine/files_combiner.rb +0 -24
  210. data/lib/simplecov/default_formatter.rb +0 -20
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ module CLI
5
+ module Usage
6
+ extend self
7
+
8
+ def for(cli, command)
9
+ sections = text(cli).split("\n\n")
10
+ row = sections.fetch(1).lines.find { |line| line.match?(/\A #{Regexp.escape(command)}[ \[]/) }
11
+ options = sections.select { |section| section_for?(section, command) }
12
+ ["Usage: simplecov #{command} [options]", row&.strip, *options].compact.join("\n\n")
13
+ end
14
+
15
+ def section_for?(section, command)
16
+ header = heading_of(section)
17
+ header.end_with?("options:") && header.delete_suffix("options:").split(%r{[\s/]+}).include?(command)
18
+ end
19
+
20
+ def heading_of(section)
21
+ section.lines.first.to_s.strip
22
+ end
23
+
24
+ # A method rather than a constant so its default paths resolve at call time
25
+ # against the active `.simplecov`.
26
+ def text(cli)
27
+ <<~USAGE
28
+ Usage: simplecov <command> [options]
29
+
30
+ Commands:
31
+ run <command...> Execute <command> with SimpleCov pre-loaded
32
+ (works without a test_helper hook)
33
+ coverage <path> Print coverage stats for the given file
34
+ show <path> Print the file's source annotated with hit counts and misses
35
+ report Print the overall summary and group totals
36
+ status Report freshness: age, commit distance, recorded tests
37
+ history Print the recorded coverage trend, a sparkline per criterion
38
+ badge Render the coverage percent as an SVG badge
39
+ uncovered List the lowest-coverage files
40
+ tests [<path>[:<line>]] List recorded tests for a file or line (needs track_tests)
41
+ affected List test files touching code changed since a git ref (needs track_tests)
42
+ merge <files...> Merge multiple .resultset.json files
43
+ diff <baseline> Show per-file coverage delta vs baseline
44
+ patch Show coverage of the lines a change touched
45
+ ratchet Rewrite the per-file coverage baseline, only ever tightening
46
+ dead-code Cross production coverage with the report to find dead code
47
+ open Open the HTML report in the default browser
48
+ serve Serve the coverage report over HTTP
49
+ watch <command...> Re-run <command> on save and live-reload the served report
50
+ clean Remove the coverage report directory
51
+ completions <shell> Emit the completion script for fish, bash, or zsh
52
+ help Show this message
53
+ version Print the version (also --version / -v)
54
+
55
+ Every command answers `--help` / `-h`. Default paths follow a project's
56
+ `.simplecov` SimpleCov.coverage_dir (#{cli.coverage_dir} for this run).
57
+
58
+ coverage / show / report / status / uncovered / tests / affected / diff / patch / ratchet / dead-code options:
59
+ --input PATH Read from PATH instead of #{cli.default_input}
60
+ --no-color Disable colorized percentages (also honors NO_COLOR / FORCE_COLOR env)
61
+
62
+ coverage options:
63
+ --json Print the file's JSON entry verbatim
64
+
65
+ show options:
66
+ --uncovered-only Print only <path>:<ranges> of the missed lines
67
+ --json Emit path, missed lines, per-line hits, and markers as JSON
68
+
69
+ report options:
70
+ --json Emit totals and group sections as JSON
71
+
72
+ status options:
73
+ --json Emit the freshness facts as a JSON object
74
+
75
+ history options:
76
+ --input PATH Read from PATH instead of the coverage directory's .history.json
77
+ --file PATH Follow one file's trajectory instead of the totals
78
+ --json Emit the entries (or the file's trajectory) as JSON
79
+ --no-color Disable colorized deltas (also honors NO_COLOR / FORCE_COLOR env)
80
+
81
+ badge options:
82
+ --input PATH Read from PATH instead of #{cli.default_input}
83
+ --output PATH Write the badge to PATH instead of stdout
84
+ --criterion C line, branch, or method (default: line)
85
+ --label TEXT The badge's left-side text (default: the criterion's, like "line coverage")
86
+
87
+ uncovered options:
88
+ --threshold N Only show files below N% coverage
89
+ --top N Show at most N files (default: 10)
90
+ --criterion C line, branch, or method (default: line)
91
+ --missing Append the missed line ranges to each row
92
+ --annotate KIND Emit CI annotations instead of rows: github, gitlab, rdjson, azure, teamcity, or buildkite
93
+ --json Emit results as a JSON array (for CI)
94
+
95
+ tests options:
96
+ --redundant List only tests whose covered lines other tests also cover
97
+ --json Emit the test ids as a JSON array
98
+
99
+ affected options:
100
+ --base REF Diff against the merge base of REF and HEAD (default: origin's HEAD, else main)
101
+ --run <command...> Run <command> (all args after --run) with the selection appended, or bare on a full-suite fallback, exiting with the command's status
102
+ --json Emit {full_suite, triggers, tests} as a JSON object
103
+
104
+ merge options:
105
+ --output PATH Write merged resultset to PATH (default: #{cli.default_resultset})
106
+ --honor-timeout Drop entries older than merge_timeout
107
+ --dry-run Print what would be written without writing
108
+ -q, --quiet Suppress the success status line
109
+
110
+ diff options:
111
+ --fail-on-drop Exit non-zero when any file's coverage dropped vs baseline (deletions don't count)
112
+ --json Emit results as a JSON array (for CI)
113
+ --threshold N Only show files whose absolute delta in any criterion is at least N%
114
+
115
+ patch options:
116
+ --base REF Diff against the merge-base of REF for the touched lines (default: origin's HEAD, else main, or in CI the PR's target branch)
117
+ --minimum N Exit non-zero when patch coverage on any measured criterion is below N%
118
+ --find-renames Follow a renamed file instead of counting the moved file as all-new
119
+ --annotate KIND Emit CI annotations instead of rows: github, gitlab, rdjson, azure, teamcity, or buildkite
120
+ --json Emit results as a JSON array (for CI)
121
+
122
+ dead-code options:
123
+ --production PATH The production coverage file a SimpleCov::Production sink wrote (default: the project's `production_coverage`, required when none is configured)
124
+ --untested-in-production Print code production runs that no test covers, instead of the dead rows
125
+ --json Emit every category (dead, possibly dead, untested in production) as JSON
126
+
127
+ ratchet options:
128
+ --baseline PATH The baseline file to rewrite (default: the project's `baseline_file`, .simplecov_baseline.yml)
129
+ --init Regenerate from scratch: add entries for new files and reset every floor to the current state
130
+ --dry-run Print what would change without writing
131
+ --json Emit the summary (tightened / pruned / regressed entries) as JSON
132
+
133
+ open options:
134
+ --report PATH Open PATH instead of #{cli.default_report}
135
+
136
+ serve options:
137
+ --port N Bind to port N (default: random open port)
138
+ --host HOST Bind to HOST (default: 127.0.0.1)
139
+
140
+ watch options:
141
+ --port N Bind to port N (default: random open port)
142
+ --host HOST Bind to HOST (default: 127.0.0.1)
143
+ --interval SECONDS Poll the tracked files this often (default: 0.5)
144
+ --open Open the served report in the default browser
145
+
146
+ clean options:
147
+ --dry-run Print what would be removed without deleting
148
+ -q, --quiet Suppress status lines
149
+ USAGE
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ module CLI
5
+ module Watch
6
+ # The live half of the watch server: an /events endpoint streaming
7
+ # server-sent events to every open report tab, and an index route that
8
+ # appends the reload listener to the artifact on the way out, so the report
9
+ # on disk stays byte-identical to a plain run's.
10
+ class LiveReport
11
+ RELOAD_SCRIPT = "\n<script>new EventSource('/events').onmessage = () => location.reload();</script>\n"
12
+
13
+ def initialize(dir)
14
+ @dir = dir
15
+ @queues = [] #: Array[Thread::Queue]
16
+ @lock = Mutex.new
17
+ end
18
+
19
+ def routes
20
+ page = method(:page)
21
+ {"/events" => method(:stream), "/" => page, "/index.html" => page}
22
+ end
23
+
24
+ def broadcast(event = "reload")
25
+ with_queues { |queues| queues.each { |queue| queue << event } }
26
+ end
27
+
28
+ private
29
+
30
+ # Holds the connection open, forwarding each broadcast as one SSE message,
31
+ # until the tab goes away and the write fails. A closed tab that never sees
32
+ # another broadcast parks its thread on the queue, a leak the dev-server
33
+ # trade accepts.
34
+ def stream(client)
35
+ queue = Queue.new #: Thread::Queue
36
+ begin
37
+ with_queues { |queues| queues << queue }
38
+ forward(client, queue)
39
+ ensure
40
+ with_queues { |queues| queues.delete(queue) }
41
+ end
42
+ end
43
+
44
+ def forward(client, queue)
45
+ client.write("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\n" \
46
+ "Cache-Control: no-cache\r\n\r\n")
47
+ loop { client.write("data: #{next_event(queue)}\n\n") }
48
+ rescue IOError, SystemCallError
49
+ nil
50
+ end
51
+
52
+ # mutant:disable
53
+ def next_event(queue)
54
+ queue.pop
55
+ end
56
+
57
+ def page(client)
58
+ body = File.binread(File.join(@dir, "index.html")) + RELOAD_SCRIPT
59
+ Serve::StaticFileHandler.respond(client, 200, body, "text/html; charset=utf-8")
60
+ rescue SystemCallError
61
+ Serve::StaticFileHandler.respond(client, 404)
62
+ end
63
+
64
+ # mutant:disable
65
+ # Every touch of the queue registry goes through the lock: an accept thread
66
+ # adds and drops its own queue while the session thread walks them all.
67
+ def with_queues
68
+ @lock.synchronize { yield @queues }
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ module CLI
5
+ module Watch
6
+ class Narrator
7
+ def initialize(stdout, root)
8
+ @stdout = stdout
9
+ @root = root
10
+ end
11
+
12
+ def banner(server, count)
13
+ host = Serve.url_host(server.addr.fetch(3))
14
+ @stdout.puts("watching #{count} file#{"s" unless count.eql?(1)}, " \
15
+ "serving http://#{host}:#{server.addr.fetch(1)}/")
16
+ @stdout.puts("Press Ctrl-C to stop.")
17
+ end
18
+
19
+ def change(changed, plan)
20
+ named = name(changed)
21
+ return @stdout.puts("#{named} changed, no recorded test touches it") unless plan.fetch(:run)
22
+
23
+ @stdout.print("#{named} changed, #{action(plan.fetch(:tests))}...")
24
+ end
25
+
26
+ def result(before, after)
27
+ return @stdout.puts unless after
28
+
29
+ delta = before ? format(" (%+.2f%%)", after - before) : ""
30
+ @stdout.puts(format(" %.2f%%", after) + delta)
31
+ end
32
+
33
+ def failed_refresh
34
+ @stdout.puts(" the report did not regenerate")
35
+ end
36
+
37
+ def stopping
38
+ @stdout.puts("\nsimplecov watch: stopping")
39
+ end
40
+
41
+ private
42
+
43
+ def name(changed)
44
+ relative = changed.map { |path| path.delete_prefix("#{@root}/") }
45
+ named = relative.first
46
+ relative.size.eql?(1) ? named : "#{named} and #{relative.size - 1} more"
47
+ end
48
+
49
+ def action(tests)
50
+ tests ? "running #{tests.size} file#{"s" unless tests.one?}" : "running the full suite"
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ module CLI
5
+ module Watch
6
+ # Watches a fixed set of paths by polling mtimes, the trade chosen over a
7
+ # filesystem-event dependency for a command that is optional to begin with.
8
+ # Editors that save through a temporary file plus rename still move the final
9
+ # path's mtime, which is all this looks at.
10
+ class Poller
11
+ def initialize
12
+ @snapshot = {} #: Hash[String, untyped]
13
+ end
14
+
15
+ # Replaces the watched set, snapshotting current mtimes. A path with no file
16
+ # snapshots as nil, so appearing counts as a change.
17
+ def watch(paths)
18
+ @snapshot = paths.to_h { |path| [path, stamp(path)] }
19
+ end
20
+
21
+ def size
22
+ @snapshot.size
23
+ end
24
+
25
+ def changes
26
+ changed = [] #: Array[String]
27
+ @snapshot.each_key do |path|
28
+ now = stamp(path)
29
+ next if now.eql?(@snapshot.fetch(path))
30
+
31
+ @snapshot[path] = now
32
+ changed << path
33
+ end
34
+ changed
35
+ end
36
+
37
+ def stamp(path)
38
+ File.mtime(path)
39
+ rescue SystemCallError
40
+ nil
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ module CLI
5
+ module Watch
6
+ # One watch session: accept report connections in the background, poll the
7
+ # tracked set for changes, re-run tests on save (selecting over the recorded
8
+ # test map when the report carries one), and push a reload to every open tab
9
+ # when the report regenerates.
10
+ class Session
11
+ # The merge window the child runs get through SIMPLECOV_MERGE_TIMEOUT: a
12
+ # day, so subset re-runs keep merging into a whole report across a long
13
+ # session instead of eroding it after the default ten minutes.
14
+ MERGE_WINDOW = "86400"
15
+
16
+ TOTALS_KEYS = {"line" => "lines", "branch" => "branches", "method" => "methods"}.freeze
17
+
18
+ def initialize(command:, dir:, interval:, stdout:, stderr:)
19
+ @command = command
20
+ @dir = dir
21
+ @interval = interval
22
+ @stderr = stderr
23
+ @root = Dir.pwd
24
+ @narrator = Narrator.new(stdout, @root)
25
+ @live = LiveReport.new(dir)
26
+ @poller = Poller.new
27
+ @document = {} #: Hash[String, untyped]
28
+ end
29
+
30
+ def run(server)
31
+ accept_loop(server)
32
+ run_tests(nil) unless File.exist?(json_path)
33
+ return 1 unless refresh
34
+
35
+ @narrator.banner(server, @poller.size)
36
+ poll_forever
37
+ rescue Interrupt
38
+ @narrator.stopping
39
+ 0
40
+ end
41
+
42
+ def poll_forever
43
+ loop do
44
+ sleep(@interval)
45
+ step
46
+ end
47
+ end
48
+
49
+ def step
50
+ changed = settled_changes
51
+ return if changed.empty?
52
+
53
+ plan = plan_for(changed)
54
+ @narrator.change(changed, plan)
55
+ return unless plan.fetch(:run)
56
+
57
+ before = total_percent
58
+ run_tests(plan.fetch(:tests))
59
+ return @narrator.failed_refresh unless refresh
60
+
61
+ @live.broadcast
62
+ @narrator.result(before, total_percent)
63
+ end
64
+
65
+ private
66
+
67
+ def accept_loop(server)
68
+ Thread.new do
69
+ loop do
70
+ Thread.new(server.accept) do |client|
71
+ Serve::StaticFileHandler.handle_connection(client, @dir, @live.routes)
72
+ end
73
+ end
74
+ rescue IOError, SystemCallError
75
+ nil
76
+ end
77
+ end
78
+
79
+ def plan_for(changed)
80
+ relative = changed.map { |path| path.delete_prefix("#{@root}/") }
81
+ TestPlan.build(relative, @document, root: @root, input: json_path, stderr: @stderr)
82
+ end
83
+
84
+ # The command is the project's own test invocation and must produce the
85
+ # report itself. A selection is appended to it; no selection means the whole
86
+ # suite, which is the command on its own.
87
+ def run_tests(tests)
88
+ env = {"SIMPLECOV_MERGE_TIMEOUT" => MERGE_WINDOW}
89
+ Kernel.system(env, *(tests ? @command + tests : @command))
90
+ end
91
+
92
+ # The regenerated report is both the watch set and the totals the result line
93
+ # diffs against. The previous document is kept when the read fails, so one
94
+ # bad write doesn't blind the session.
95
+ def refresh
96
+ document = CoverageFile.load_document(json_path, command: "watch", stderr: @stderr)
97
+ return false unless document
98
+
99
+ @document = document
100
+ @poller.watch(TestPlan.watched_paths(document, @root))
101
+ document
102
+ end
103
+
104
+ # Editors save in bursts, so after the first detection keep polling until a
105
+ # full interval passes with nothing new.
106
+ def settled_changes
107
+ changed = @poller.changes
108
+ return changed if changed.empty?
109
+
110
+ loop do
111
+ sleep(@interval)
112
+ more = @poller.changes
113
+ break if more.empty?
114
+
115
+ changed |= more
116
+ end
117
+ changed
118
+ end
119
+
120
+ # Follows the report's own primary criterion, falling back to line coverage
121
+ # for documents from before meta carried one.
122
+ def total_percent
123
+ key = TOTALS_KEYS[@document.dig("meta", "primary_coverage")] || "lines"
124
+ value = @document.dig("total", key, "percent")
125
+ (_ = value).to_f if value.is_a?(Numeric)
126
+ end
127
+
128
+ def json_path
129
+ File.join(@dir, "coverage.json")
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SimpleCov
4
+ module CLI
5
+ module Watch
6
+ module TestPlan
7
+ extend self
8
+
9
+ # {run:, tests:} where nil tests mean the full command. A report with no map,
10
+ # a malformed one, or any fail-open trigger runs everything; an empty
11
+ # selection runs nothing.
12
+ def build(changed, document, root:, input:, stderr:)
13
+ contexts = document["contexts"]
14
+ return {run: true, tests: nil} unless contexts.instance_of?(Array) && contexts.all?(String)
15
+
16
+ selection = Affected::Selection.build(changed, document, contexts, {input: input}, stderr, root: root)
17
+ return {run: true, tests: nil} if selection.nil? || !selection.fetch(:triggers).empty?
18
+ return {run: false, tests: []} if selection.fetch(:tests).empty?
19
+
20
+ {run: true, tests: selection.fetch(:tests)}
21
+ end
22
+
23
+ # The watch set a report defines: its tracked files plus the files of its
24
+ # recorded tests.
25
+ def watched_paths(document, root)
26
+ none = [] #: Array[String]
27
+ coverage = document["coverage"]
28
+ sources = coverage.instance_of?(Hash) ? coverage.keys : none
29
+ tests = recorded_test_files(document).map { |path| File.expand_path(path, root) }
30
+ (sources + tests).uniq
31
+ end
32
+
33
+ def recorded_test_files(document)
34
+ none = [] #: Array[String]
35
+ contexts = document["contexts"]
36
+ return none unless contexts.instance_of?(Array)
37
+
38
+ contexts.filter_map { |id| Affected::Selection.test_file_of(id.to_s) }
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+ require_relative "command_helpers"
5
+ require_relative "open"
6
+ require_relative "serve"
7
+ require_relative "affected"
8
+ require_relative "watch/poller"
9
+ require_relative "watch/live_report"
10
+ require_relative "watch/narrator"
11
+ require_relative "watch/test_plan"
12
+ require_relative "watch/session"
13
+
14
+ module SimpleCov
15
+ module CLI
16
+ # `simplecov watch <command...>`: the coverage inner loop. Serves the report
17
+ # the way `serve` does, polls the tracked files for saves, re-runs the given
18
+ # command on change, and pushes a reload to the open tab over server-sent
19
+ # events when the report regenerates. With a `track_tests` recording in the
20
+ # report, a save re-runs only the tests that touch the changed files, by the
21
+ # same selection walk `simplecov affected` uses.
22
+ module Watch
23
+ extend CommandHelpers
24
+
25
+ extend self
26
+
27
+ def run(args, stdout:, stderr:, **)
28
+ opts, command = parse(args)
29
+ return error(stderr, "missing command to run (e.g. `simplecov watch bundle exec rspec`)") if command.empty?
30
+
31
+ Serve.require_socket
32
+ server = bind(opts, stderr)
33
+ return 1 unless server
34
+
35
+ serve_session(server, command, opts, stdout, stderr)
36
+ end
37
+
38
+ def serve_session(server, command, opts, stdout, stderr)
39
+ launch_browser(server, stderr) if opts.fetch(:open)
40
+ session_for(command, opts, stdout, stderr).run(server)
41
+ ensure
42
+ server.close
43
+ end
44
+
45
+ # Fire-and-forget through the same platform opener `simplecov open` uses,
46
+ # detached so the session never waits on a browser. A platform with no known
47
+ # opener notes the URL instead of failing the watch.
48
+ def launch_browser(server, stderr)
49
+ url = "http://#{Serve.url_host(server.addr.fetch(3))}:#{server.addr.fetch(1)}/"
50
+ opener = Open.browser_opener
51
+ unless opener
52
+ return stderr.puts("simplecov watch: no known browser opener for this platform, open it yourself: #{url}")
53
+ end
54
+
55
+ Process.detach(spawn(*opener, url, out: File::NULL, err: File::NULL))
56
+ end
57
+
58
+ # `order` rather than `parse` stops at the first positional, so the runner
59
+ # command keeps its own flags: `simplecov watch bundle exec rspec --seed 1`
60
+ # passes --seed through untouched.
61
+ def parse(args)
62
+ opts = {port: 0, host: "127.0.0.1", interval: 0.5, open: false} #: Hash[Symbol, untyped]
63
+ rest =
64
+ build_parser do |parser|
65
+ parser.on("--port N", Integer) { |v| opts[:port] = v }
66
+ parser.on("--host HOST") { |v| opts[:host] = v }
67
+ parser.on("--interval SECONDS", Float) { |v| opts[:interval] = v }
68
+ parser.on("--open") { opts[:open] = true }
69
+ end.order(args)
70
+ [opts, rest]
71
+ end
72
+
73
+ def bind(opts, stderr)
74
+ (_ = TCPServer).new(opts.fetch(:host), opts.fetch(:port)) #: TCPServer
75
+ rescue SystemCallError, SocketError => e
76
+ error_nil(stderr, "cannot bind to #{opts.fetch(:host)}:#{opts.fetch(:port)} (#{e})")
77
+ end
78
+
79
+ def session_for(command, opts, stdout, stderr)
80
+ Session.new(command: command, dir: CLI.coverage_dir,
81
+ interval: opts.fetch(:interval), stdout: stdout, stderr: stderr)
82
+ end
83
+ end
84
+ end
85
+ end