rspec-collapsing-formatter 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/README.md +72 -4
- data/lib/rspec/collapsing_formatter/display.rb +98 -0
- data/lib/rspec/collapsing_formatter/events.rb +38 -0
- data/lib/rspec/collapsing_formatter/layout.rb +51 -0
- data/lib/rspec/collapsing_formatter/live_output.rb +72 -8
- data/lib/rspec/collapsing_formatter/parallel.rb +199 -0
- data/lib/rspec/collapsing_formatter/runners/parallel_tests.rb +54 -0
- data/lib/rspec/collapsing_formatter/runners.rb +33 -0
- data/lib/rspec/collapsing_formatter/symbols.rb +25 -6
- data/lib/rspec/collapsing_formatter/transport.rb +176 -0
- data/lib/rspec/collapsing_formatter/tree.rb +24 -17
- data/lib/rspec/collapsing_formatter.rb +56 -24
- metadata +7 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f96339bcb40833f172b025723b8ae2cf7722c3ec858e3f9d4423ffcede4d40f2
|
|
4
|
+
data.tar.gz: f6a9ad9f1931664c2438326f172b0cca248aa74e27d20cc4de9e19caa862f6b4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 44fda669cca4338da96716eee8610873ff02fd8ed1cd7afcddccfedaa8e76b89af70630d2db1d9f8518c43a5b3c995e31373fb988db237c36c3b918cc6c87ac3
|
|
7
|
+
data.tar.gz: 7cc520401823bcab55731eebbe7e47036bee212aa59d44749b040f2a93b524216901e65e5162026f858d443993bbde79aaa3f3c98b4ae5caeab20ef8c8eca9e9
|
data/README.md
CHANGED
|
@@ -55,14 +55,16 @@ The `(FAILED - n)` numbers match the numbers in that failure list.
|
|
|
55
55
|
|
|
56
56
|
## Installation
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
Add it to your Gemfile:
|
|
59
59
|
|
|
60
60
|
```ruby
|
|
61
61
|
group :test do
|
|
62
|
-
gem "rspec-collapsing-formatter"
|
|
62
|
+
gem "rspec-collapsing-formatter"
|
|
63
63
|
end
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
+
or install it with `gem install rspec-collapsing-formatter`.
|
|
67
|
+
|
|
66
68
|
It requires Ruby 3.1 or later and rspec-core 3.10 or later. Its only other dependency is
|
|
67
69
|
`unicode-display_width` (2.4 up to 4), to measure line widths. It is tested on Ruby 3.1
|
|
68
70
|
and 3.4, with rspec-core 3.10 and 3.13 and `unicode-display_width` 2.x and 3.x.
|
|
@@ -139,6 +141,60 @@ starts or finishes.
|
|
|
139
141
|
The proxies pass everything else (`tty?`, `sync`, `winsize`, …) through to the real streams.
|
|
140
142
|
The real streams are restored before RSpec prints its summary.
|
|
141
143
|
|
|
144
|
+
## Parallel runs
|
|
145
|
+
|
|
146
|
+
With [parallel_tests](https://github.com/grosser/parallel_tests), the formatter shows the
|
|
147
|
+
progress of every process in one tree. No configuration is needed beyond the usual
|
|
148
|
+
`--format RSpec::CollapsingFormatter`:
|
|
149
|
+
|
|
150
|
+
```sh
|
|
151
|
+
bundle exec parallel_rspec -n 4
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Each process's running group is expanded at the same time, and finished groups collapse
|
|
155
|
+
above them in the order they finish:
|
|
156
|
+
|
|
157
|
+
```text
|
|
158
|
+
✓ Bid (12 examples)
|
|
159
|
+
✓ Search (13 examples)
|
|
160
|
+
|
|
161
|
+
Auction
|
|
162
|
+
✗ rejects low bids (FAILED - 1, worker 3)
|
|
163
|
+
✓ when active (4 examples)
|
|
164
|
+
when suspended
|
|
165
|
+
▸ does thing 2
|
|
166
|
+
|
|
167
|
+
Catalog
|
|
168
|
+
✓ when active (4 examples)
|
|
169
|
+
when suspended
|
|
170
|
+
✓ does thing 1
|
|
171
|
+
▸ does thing 3
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
When the running groups don't fit on the screen, each is reduced to the path to its
|
|
175
|
+
running example, and if that doesn't fit either, to one line:
|
|
176
|
+
|
|
177
|
+
```text
|
|
178
|
+
Auction › when active › ▸ does thing 3
|
|
179
|
+
Catalog › when suspended › ▸ does thing 2
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Each process still prints its own failure list and summary, one process after the other,
|
|
183
|
+
once the tree is complete; parallel_tests then adds up the totals as usual. Failures are
|
|
184
|
+
numbered per process, so `(FAILED - 1, worker 3)` refers to failure 1 in the list printed
|
|
185
|
+
by process 3.
|
|
186
|
+
|
|
187
|
+
How it works: the first process draws the tree on stderr, which parallel_tests leaves
|
|
188
|
+
connected to the terminal, and the other processes send it their progress over a Unix
|
|
189
|
+
socket. No process writes to stdout until the tree is complete, since parallel_tests
|
|
190
|
+
copies each process's stdout to the terminal as it arrives.
|
|
191
|
+
|
|
192
|
+
Each process falls back to the normal behavior if it can't take part: when stderr is not
|
|
193
|
+
a terminal (as in CI, or with `--combine-stderr`), or when it can't reach the first
|
|
194
|
+
process within 30 seconds.
|
|
195
|
+
|
|
196
|
+
Support for other parallel runners (turbo_tests, flatware) is planned.
|
|
197
|
+
|
|
142
198
|
## Limitations
|
|
143
199
|
|
|
144
200
|
The first version makes these simplifying assumptions where terminal behavior gets
|
|
@@ -162,6 +218,15 @@ complicated:
|
|
|
162
218
|
- **Wide characters.** Emoji widths differ between versions of `unicode-display_width` and
|
|
163
219
|
between terminals. If they disagree, a line can be cut a little short, or in rare cases
|
|
164
220
|
wrap.
|
|
221
|
+
- **Parallel runs:**
|
|
222
|
+
- Output that bypasses `$stdout`/`$stderr` in any process can corrupt the tree, as above.
|
|
223
|
+
- On Ctrl-C, RSpec's "shutting down" notice is printed once per process.
|
|
224
|
+
- If the first process crashes, the tree stops updating; the other processes still
|
|
225
|
+
print their results when they finish.
|
|
226
|
+
- parallel_tests' `--test-file-limit` option, which runs several RSpec processes one
|
|
227
|
+
after another for each worker, is not supported.
|
|
228
|
+
- With only stdout redirected (`parallel_rspec | tee log`), the tree still appears on the
|
|
229
|
+
terminal, and the log gets each process's results.
|
|
165
230
|
|
|
166
231
|
## Development
|
|
167
232
|
|
|
@@ -171,9 +236,12 @@ bundle exec rspec
|
|
|
171
236
|
```
|
|
172
237
|
|
|
173
238
|
The design is described in [PLAN.md](PLAN.md), and the original brief is in
|
|
174
|
-
[REQUIREMENTS.md](REQUIREMENTS.md). In short,
|
|
239
|
+
[REQUIREMENTS.md](REQUIREMENTS.md). In short, the formatter turns RSpec notifications into
|
|
240
|
+
events (`events.rb`), and a display (`display.rb`) applies them to a tree (`tree.rb`).
|
|
175
241
|
`layout.rb` turns the tree into lines without any IO, and `live_output.rb` or
|
|
176
|
-
`stream_output.rb` writes them. Only `live_output.rb` knows about cursor movement.
|
|
242
|
+
`stream_output.rb` writes them. Only `live_output.rb` knows about cursor movement. For
|
|
243
|
+
parallel runs, `runners/` detects the runner, `parallel.rb` holds the coordinator and
|
|
244
|
+
worker roles, and `transport.rb` carries events between processes.
|
|
177
245
|
|
|
178
246
|
Most tests run without a real terminal. `spec/support/virtual_terminal.rb` is a small
|
|
179
247
|
terminal emulator that interprets the bytes the formatter writes, so specs can assert on
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "monitor"
|
|
4
|
+
|
|
5
|
+
module RSpec
|
|
6
|
+
class CollapsingFormatter < Core::Formatters::BaseTextFormatter
|
|
7
|
+
# Applies events to the tree and tells the output what changed.
|
|
8
|
+
#
|
|
9
|
+
# Knows nothing about RSpec notifications or where events come from; the formatter
|
|
10
|
+
# translates notifications into events, possibly in other processes. Knows nothing
|
|
11
|
+
# about terminals either; that is the output's job.
|
|
12
|
+
#
|
|
13
|
+
# Events may arrive from several threads; they are applied one at a time.
|
|
14
|
+
class Display
|
|
15
|
+
# A display for `io`: live redrawing if it is a terminal, append-only otherwise.
|
|
16
|
+
# In parallel mode, every worker's running group is shown at once.
|
|
17
|
+
def self.for(io, parallel: false)
|
|
18
|
+
tree = Tree.new
|
|
19
|
+
symbols = Symbols.for(io)
|
|
20
|
+
output =
|
|
21
|
+
if LiveOutput.supported?(io)
|
|
22
|
+
LiveOutput.new(io, symbols, tree, parallel: parallel)
|
|
23
|
+
else
|
|
24
|
+
StreamOutput.new(io, symbols)
|
|
25
|
+
end
|
|
26
|
+
new(tree, output, parallel: parallel)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
attr_reader :tree
|
|
30
|
+
|
|
31
|
+
# In parallel mode a worker's Finished event only ends its pending output; the
|
|
32
|
+
# display as a whole is finished by calling #finish once every worker is done.
|
|
33
|
+
def initialize(tree, output, parallel: false)
|
|
34
|
+
@tree = tree
|
|
35
|
+
@output = output
|
|
36
|
+
@parallel = parallel
|
|
37
|
+
@partial = Hash.new { |partial, worker| partial[worker] = +"" } # Output not yet ending in a newline
|
|
38
|
+
@lock = Monitor.new
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def call(event)
|
|
42
|
+
@lock.synchronize { apply(event) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Completes the output: prints pending partial lines and the final tree.
|
|
46
|
+
def finish
|
|
47
|
+
@lock.synchronize do
|
|
48
|
+
@partial.each_key { |worker| end_output(worker) }
|
|
49
|
+
@output.finish
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def apply(event)
|
|
56
|
+
worker = event.worker
|
|
57
|
+
case event
|
|
58
|
+
when Events::Started
|
|
59
|
+
@output.start
|
|
60
|
+
when Events::GroupStarted
|
|
61
|
+
group = @tree.group_started(event.description, worker: worker)
|
|
62
|
+
@output.group_started(group, @tree.depth(worker) - 1)
|
|
63
|
+
when Events::GroupFinished
|
|
64
|
+
group = @tree.group_finished(worker: worker)
|
|
65
|
+
@output.group_finished(group, @tree.depth(worker))
|
|
66
|
+
when Events::ExampleStarted
|
|
67
|
+
example = @tree.example_started(event.description, worker: worker)
|
|
68
|
+
@output.example_started(example, @tree.depth(worker))
|
|
69
|
+
when Events::ExampleFinished
|
|
70
|
+
example = @tree.example_finished(event.description, event.status, event.detail, worker: worker)
|
|
71
|
+
@output.example_finished(example, @tree.depth(worker))
|
|
72
|
+
when Events::Message
|
|
73
|
+
@output.message(event.text)
|
|
74
|
+
when Events::Output
|
|
75
|
+
output(worker, event.text)
|
|
76
|
+
when Events::Finished
|
|
77
|
+
@parallel ? end_output(worker) : finish
|
|
78
|
+
else
|
|
79
|
+
raise ArgumentError, "unknown event #{event.inspect}"
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Prints the worker's complete lines; the rest waits for its newline.
|
|
84
|
+
def output(worker, text)
|
|
85
|
+
buffer = @partial[worker] << text
|
|
86
|
+
newline = buffer.rindex("\n")
|
|
87
|
+
@output.message(buffer.slice!(0..newline).chomp) if newline
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Prints the worker's partial line, if any, as a line of its own.
|
|
91
|
+
def end_output(worker)
|
|
92
|
+
buffer = @partial[worker]
|
|
93
|
+
@output.message(buffer.dup) unless buffer.empty?
|
|
94
|
+
buffer.clear
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RSpec
|
|
4
|
+
class CollapsingFormatter < Core::Formatters::BaseTextFormatter
|
|
5
|
+
# The events a Display consumes: what happened in a test run, independent of where
|
|
6
|
+
# it happened. They are plain values, so they can come from the formatter in the
|
|
7
|
+
# same process or, later, from other processes.
|
|
8
|
+
#
|
|
9
|
+
# Every event names the worker it came from. A run in a single process has one
|
|
10
|
+
# worker, nil.
|
|
11
|
+
module Events
|
|
12
|
+
def self.define(*fields)
|
|
13
|
+
Struct.new(*fields, :worker, keyword_init: true)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# The run started. Sent once per worker.
|
|
17
|
+
Started = define
|
|
18
|
+
|
|
19
|
+
GroupStarted = define(:description)
|
|
20
|
+
GroupFinished = define
|
|
21
|
+
|
|
22
|
+
ExampleStarted = define(:description)
|
|
23
|
+
# `status` is :passed, :failed or :pending; `detail` e.g. "FAILED - 1". The
|
|
24
|
+
# description is sent again because RSpec may generate it only after the example ran.
|
|
25
|
+
ExampleFinished = define(:description, :status, :detail)
|
|
26
|
+
|
|
27
|
+
# A message from RSpec, e.g. "Run options: ...", without a trailing newline.
|
|
28
|
+
Message = define(:text)
|
|
29
|
+
|
|
30
|
+
# Text that specs wrote to $stdout or $stderr in another process. It may end in the
|
|
31
|
+
# middle of a line; the rest follows in the worker's next Output event.
|
|
32
|
+
Output = define(:text)
|
|
33
|
+
|
|
34
|
+
# The worker finished running examples; its failure and summary output follows.
|
|
35
|
+
Finished = define
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -63,6 +63,28 @@ module RSpec
|
|
|
63
63
|
lines
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
+
# Lines for several running top-level groups (one per worker) in at most `height`
|
|
67
|
+
# lines, as detailed as fits:
|
|
68
|
+
# 1. everything, as `lines` would show it;
|
|
69
|
+
# 2. for each group, the path to where it is now: its open groups and its latest
|
|
70
|
+
# example or finished subgroup;
|
|
71
|
+
# 3. one line per group, e.g. "User › when suspended › ▸ cannot log in";
|
|
72
|
+
# 4. as many of those as fit, and a line counting the rest.
|
|
73
|
+
def fitted_lines(groups, height, previous: nil)
|
|
74
|
+
full = lines(groups, previous: previous)
|
|
75
|
+
return full if full.size <= height
|
|
76
|
+
|
|
77
|
+
paths = groups.flat_map { |group| path_lines(group) }
|
|
78
|
+
return paths if paths.size <= height
|
|
79
|
+
|
|
80
|
+
summaries = groups.map { |group| one_line(group) }
|
|
81
|
+
return summaries if summaries.size <= height
|
|
82
|
+
return [] unless height.positive?
|
|
83
|
+
|
|
84
|
+
hidden = summaries.size - (height - 1)
|
|
85
|
+
[*summaries.first(height - 1), line(0, "#{@symbols.ellipsis} #{hidden} more", @symbols.style(:running), nil)]
|
|
86
|
+
end
|
|
87
|
+
|
|
66
88
|
# Whether a finished group is shown as a single summary line, or not at all.
|
|
67
89
|
def hides_children?(group)
|
|
68
90
|
%i[passed pending empty].include?(group.status)
|
|
@@ -100,6 +122,35 @@ module RSpec
|
|
|
100
122
|
node.is_a?(GroupNode) ? group_lines(node, depth) : [example_line(node, depth)]
|
|
101
123
|
end
|
|
102
124
|
|
|
125
|
+
# The group's chain of open (running) groups, outermost first, and the last child
|
|
126
|
+
# of the innermost one: the running example, or whatever finished last.
|
|
127
|
+
def open_path(group)
|
|
128
|
+
chain = [group]
|
|
129
|
+
while (last = chain.last.children.last).is_a?(GroupNode) && !last.finished?
|
|
130
|
+
chain << last
|
|
131
|
+
end
|
|
132
|
+
[chain, chain.last.children.last]
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def path_lines(group)
|
|
136
|
+
chain, latest = open_path(group)
|
|
137
|
+
headers = chain.each_with_index.map { |node, depth| header_line(node, depth) }
|
|
138
|
+
return headers unless latest
|
|
139
|
+
|
|
140
|
+
depth = chain.size
|
|
141
|
+
latest_line = latest.is_a?(GroupNode) ? header_line(latest, depth) : example_line(latest, depth)
|
|
142
|
+
latest.is_a?(GroupNode) && latest.status == :empty ? headers : [*headers, latest_line]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def one_line(group)
|
|
146
|
+
chain, latest = open_path(group)
|
|
147
|
+
parts = chain.map(&:description)
|
|
148
|
+
if latest.is_a?(ExampleNode) && !latest.finished?
|
|
149
|
+
parts << "#{@symbols.glyph(:running)} #{latest.description}"
|
|
150
|
+
end
|
|
151
|
+
line(0, parts.join(" #{@symbols.path_separator} "), nil, group)
|
|
152
|
+
end
|
|
153
|
+
|
|
103
154
|
def expanded?(group)
|
|
104
155
|
%i[running failed].include?(group.status)
|
|
105
156
|
end
|
|
@@ -26,6 +26,12 @@ module RSpec
|
|
|
26
26
|
# later fails, and if a group whose lines were committed collapses, its summary and
|
|
27
27
|
# everything after it is written below what is already on screen.
|
|
28
28
|
#
|
|
29
|
+
# In parallel mode (several workers sharing one tree) every running top-level group
|
|
30
|
+
# is live at once, one per worker. The live region is then always fitted to the
|
|
31
|
+
# screen (Layout#fitted_lines) instead of committing lines that do not fit, and a
|
|
32
|
+
# top-level group is committed when it finishes, by moving it above the groups that
|
|
33
|
+
# are still running. Groups are thus committed in the order they finish.
|
|
34
|
+
#
|
|
29
35
|
# While running, $stdout and $stderr (when they are terminals) are replaced by
|
|
30
36
|
# StrayOutput proxies. Complete lines written to them, and RSpec messages, are
|
|
31
37
|
# printed above the live region: it is cleared, the text is written through the
|
|
@@ -41,8 +47,10 @@ module RSpec
|
|
|
41
47
|
output.respond_to?(:tty?) && output.tty? && env["TERM"] != "dumb"
|
|
42
48
|
end
|
|
43
49
|
|
|
44
|
-
def initialize(output, symbols, tree, env: ENV)
|
|
50
|
+
def initialize(output, symbols, tree, env: ENV, parallel: false)
|
|
45
51
|
@output = output
|
|
52
|
+
@parallel = parallel
|
|
53
|
+
@committed = {}.compare_by_identity # parallel mode: committed top-level groups
|
|
46
54
|
@symbols = symbols
|
|
47
55
|
@tree = tree
|
|
48
56
|
@env = env
|
|
@@ -72,10 +80,14 @@ module RSpec
|
|
|
72
80
|
@lock.synchronize { render }
|
|
73
81
|
end
|
|
74
82
|
|
|
75
|
-
def group_finished(
|
|
83
|
+
def group_finished(group, depth)
|
|
76
84
|
@lock.synchronize do
|
|
77
|
-
|
|
78
|
-
|
|
85
|
+
if @parallel && depth.zero?
|
|
86
|
+
commit_group(group)
|
|
87
|
+
else
|
|
88
|
+
render
|
|
89
|
+
commit if depth.zero?
|
|
90
|
+
end
|
|
79
91
|
end
|
|
80
92
|
end
|
|
81
93
|
|
|
@@ -112,8 +124,12 @@ module RSpec
|
|
|
112
124
|
def finish
|
|
113
125
|
@lock.synchronize do
|
|
114
126
|
release
|
|
115
|
-
|
|
116
|
-
|
|
127
|
+
if @parallel
|
|
128
|
+
commit_remaining
|
|
129
|
+
else
|
|
130
|
+
render
|
|
131
|
+
commit
|
|
132
|
+
end
|
|
117
133
|
end
|
|
118
134
|
end
|
|
119
135
|
|
|
@@ -167,7 +183,9 @@ module RSpec
|
|
|
167
183
|
columns, rows = terminal_size
|
|
168
184
|
layout = Layout.new(@symbols, width: columns)
|
|
169
185
|
lines =
|
|
170
|
-
if @
|
|
186
|
+
if @parallel
|
|
187
|
+
layout.fitted_lines(live_groups, rows - 1, previous: @last_committed)
|
|
188
|
+
elsif @start
|
|
171
189
|
@start = topmost_hidden_ancestor(layout, @start) || @start
|
|
172
190
|
layout.lines_from(@start)
|
|
173
191
|
else
|
|
@@ -184,7 +202,53 @@ module RSpec
|
|
|
184
202
|
@output.flush if @output.respond_to?(:flush)
|
|
185
203
|
@frame = frame
|
|
186
204
|
@frame_nodes = lines.map(&:node)
|
|
187
|
-
commit_overflow(rows - 1) # one row is left for the cursor
|
|
205
|
+
commit_overflow(rows - 1) unless @parallel # one row is left for the cursor
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Parallel mode: the top-level groups that have not been committed, in start order.
|
|
209
|
+
def live_groups
|
|
210
|
+
@tree.groups.reject { |group| @committed.key?(group) }
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Parallel mode: commits a finished top-level group by drawing it in full at the
|
|
214
|
+
# top of the live region, followed by the groups that are still running. When it
|
|
215
|
+
# already is the first group, only its own lines change.
|
|
216
|
+
def commit_group(group)
|
|
217
|
+
flush_pending
|
|
218
|
+
columns, rows = terminal_size
|
|
219
|
+
layout = Layout.new(@symbols, width: columns)
|
|
220
|
+
@committed[group] = true
|
|
221
|
+
lines = layout.lines([group], previous: @last_committed)
|
|
222
|
+
@last_committed = group unless lines.empty?
|
|
223
|
+
live = layout.fitted_lines(live_groups, rows - 1, previous: @last_committed)
|
|
224
|
+
draw([*lines, *live].map { |line| @symbols.paint(line) })
|
|
225
|
+
@frame = @frame.drop(lines.size)
|
|
226
|
+
@frame_nodes = live.map(&:node)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
# Parallel mode, at the end of the run: commits every group still on screen (only
|
|
230
|
+
# those of interrupted workers), in full.
|
|
231
|
+
def commit_remaining
|
|
232
|
+
flush_pending
|
|
233
|
+
layout = Layout.new(@symbols, width: terminal_size.first)
|
|
234
|
+
remaining = live_groups
|
|
235
|
+
draw(layout.lines(remaining, previous: @last_committed).map { |line| @symbols.paint(line) })
|
|
236
|
+
remaining.each { |group| @committed[group] = true }
|
|
237
|
+
@last_committed = remaining.reverse.find { |group| group.status != :empty } || @last_committed
|
|
238
|
+
@frame = []
|
|
239
|
+
@frame_nodes = []
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# Turns the live region into `frame`.
|
|
243
|
+
def draw(frame)
|
|
244
|
+
data = redraw(frame)
|
|
245
|
+
unless @started
|
|
246
|
+
data = "\n#{data}"
|
|
247
|
+
@started = true
|
|
248
|
+
end
|
|
249
|
+
@output.write(data) unless data.empty?
|
|
250
|
+
@output.flush if @output.respond_to?(:flush)
|
|
251
|
+
@frame = frame
|
|
188
252
|
end
|
|
189
253
|
|
|
190
254
|
# The outermost group above `node` that now collapsed or turned out empty, which
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "monitor"
|
|
4
|
+
|
|
5
|
+
module RSpec
|
|
6
|
+
class CollapsingFormatter < Core::Formatters::BaseTextFormatter
|
|
7
|
+
# The two roles of a process in a parallel run, whatever the runner: one
|
|
8
|
+
# Coordinator displays the progress of every worker, and the other processes are
|
|
9
|
+
# Workers that send it their events. Both use the Runners::Context an adapter
|
|
10
|
+
# provides, and the Transport.
|
|
11
|
+
#
|
|
12
|
+
# Neither writes to the process's stdout during the run, since runners such as
|
|
13
|
+
# parallel_tests relay it to the terminal whenever it arrives. Output from specs is
|
|
14
|
+
# sent to the coordinator, and RSpec's failure and summary output is held back
|
|
15
|
+
# until the display is complete; then each process prints it in turn.
|
|
16
|
+
module Parallel
|
|
17
|
+
# How long a worker keeps trying to reach the coordinator, which may still be
|
|
18
|
+
# starting, before it falls back to working on its own.
|
|
19
|
+
CONNECT_TIMEOUT = 30
|
|
20
|
+
|
|
21
|
+
# Replaces $stdout and/or $stderr by StrayOutput proxies for the duration of the
|
|
22
|
+
# run, handing what is written to them to #stray_output.
|
|
23
|
+
module StreamCapture
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def capture_streams(stdout: true, stderr: true)
|
|
27
|
+
@captured = []
|
|
28
|
+
if stdout
|
|
29
|
+
@captured << [:stdout, $stdout]
|
|
30
|
+
$stdout = StrayOutput.new($stdout, self)
|
|
31
|
+
end
|
|
32
|
+
if stderr
|
|
33
|
+
@captured << [:stderr, $stderr]
|
|
34
|
+
$stderr = StrayOutput.new($stderr, self)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Restores the streams, unless something else has replaced them since.
|
|
39
|
+
def release_streams
|
|
40
|
+
(@captured || []).each do |name, original|
|
|
41
|
+
current = name == :stdout ? $stdout : $stderr
|
|
42
|
+
next unless current.is_a?(StrayOutput) && current.io.equal?(original)
|
|
43
|
+
|
|
44
|
+
name == :stdout ? $stdout = original : $stderr = original
|
|
45
|
+
end
|
|
46
|
+
@captured = []
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def text_of(data)
|
|
50
|
+
data.to_s.dup.force_encoding(Encoding::UTF_8).scrub
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# A process that sends its events to the coordinator.
|
|
55
|
+
class Worker
|
|
56
|
+
include StreamCapture
|
|
57
|
+
|
|
58
|
+
# The worker, or nil if the coordinator cannot be reached.
|
|
59
|
+
def self.start(context, timeout: CONNECT_TIMEOUT)
|
|
60
|
+
client = Transport::Client.connect(context.address, worker: context.worker, timeout: timeout)
|
|
61
|
+
client && new(client, context.worker)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def initialize(client, worker)
|
|
65
|
+
@client = client
|
|
66
|
+
@worker = worker
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def call(event)
|
|
70
|
+
capture_streams if event.is_a?(Events::Started) # stderr too: it is the terminal
|
|
71
|
+
release_streams if event.is_a?(Events::Finished)
|
|
72
|
+
@client.send_event(event)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Called by StrayOutput.
|
|
76
|
+
def stray_output(_proxy, data)
|
|
77
|
+
@client.send_event(Events::Output.new(text: text_of(data), worker: @worker))
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Waits until the coordinator lets this worker print its failure and summary
|
|
81
|
+
# output, which the block does.
|
|
82
|
+
def close
|
|
83
|
+
release_streams
|
|
84
|
+
@client.wait_for_release
|
|
85
|
+
yield
|
|
86
|
+
ensure
|
|
87
|
+
@client.close
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# The process that displays the progress of every worker, its own included.
|
|
92
|
+
class Coordinator
|
|
93
|
+
include StreamCapture
|
|
94
|
+
|
|
95
|
+
# The coordinator, or nil if it cannot listen for workers.
|
|
96
|
+
def self.start(context, io, connect_timeout: CONNECT_TIMEOUT)
|
|
97
|
+
new(context, io, connect_timeout: connect_timeout)
|
|
98
|
+
rescue IOError, SystemCallError
|
|
99
|
+
nil
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def initialize(context, io, connect_timeout:)
|
|
103
|
+
@context = context
|
|
104
|
+
@worker = context.worker
|
|
105
|
+
@display = Display.for(io, parallel: true)
|
|
106
|
+
@states = {} # worker => :running, :finished or :gone (disconnected)
|
|
107
|
+
@lock = Monitor.new
|
|
108
|
+
@changed = @lock.new_cond
|
|
109
|
+
@deferred = [] # own output written from signal handlers, which cannot take locks
|
|
110
|
+
# Workers that have not connected by then have given up and work on their own.
|
|
111
|
+
@newcomer_deadline = now + connect_timeout + 1
|
|
112
|
+
@server = Transport::Server.new(context.address) { |worker, event| received(worker, event) }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# An event of this process.
|
|
116
|
+
def call(event)
|
|
117
|
+
# Only stdout: stderr is the display's terminal, which the live output captures.
|
|
118
|
+
capture_streams(stderr: false) if event.is_a?(Events::Started)
|
|
119
|
+
flush_deferred
|
|
120
|
+
@display.call(event)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Called by StrayOutput for this process's stdout.
|
|
124
|
+
def stray_output(_proxy, data)
|
|
125
|
+
event = Events::Output.new(text: text_of(data), worker: @worker)
|
|
126
|
+
begin
|
|
127
|
+
flush_deferred
|
|
128
|
+
@display.call(event)
|
|
129
|
+
rescue ThreadError => e
|
|
130
|
+
raise unless e.message.include?("trap context")
|
|
131
|
+
|
|
132
|
+
@deferred << event # Array#<< needs no lock
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Waits for every worker, completes the display, then lets this process (the
|
|
137
|
+
# block) and each worker in turn print its failure and summary output.
|
|
138
|
+
def close
|
|
139
|
+
release_streams
|
|
140
|
+
flush_deferred
|
|
141
|
+
wait_for_workers
|
|
142
|
+
@display.finish
|
|
143
|
+
yield
|
|
144
|
+
release_workers
|
|
145
|
+
ensure
|
|
146
|
+
@server.close
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
private
|
|
150
|
+
|
|
151
|
+
def received(worker, event)
|
|
152
|
+
@lock.synchronize do
|
|
153
|
+
@states[worker] =
|
|
154
|
+
if event.nil? then :gone
|
|
155
|
+
elsif event.is_a?(Events::Finished) then :finished
|
|
156
|
+
else @states[worker] || :running
|
|
157
|
+
end
|
|
158
|
+
@changed.broadcast
|
|
159
|
+
end
|
|
160
|
+
return unless event
|
|
161
|
+
|
|
162
|
+
event.worker = worker # trust the connection, not the message
|
|
163
|
+
@display.call(event)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def wait_for_workers
|
|
167
|
+
@lock.synchronize { @changed.wait(0.1) until workers_done? }
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Every connected worker has finished, and no other worker process is running,
|
|
171
|
+
# or none can still connect.
|
|
172
|
+
def workers_done?
|
|
173
|
+
return false if @states.value?(:running)
|
|
174
|
+
|
|
175
|
+
running = @context.running_processes&.call
|
|
176
|
+
return true if running.nil? || now > @newcomer_deadline
|
|
177
|
+
|
|
178
|
+
running - 1 - @states.count { |_worker, state| state != :gone } <= 0
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def release_workers
|
|
182
|
+
@states.keys.sort.each do |worker|
|
|
183
|
+
next unless @states[worker] == :finished && @server.release(worker)
|
|
184
|
+
|
|
185
|
+
@lock.synchronize { @changed.wait(0.1) until @states[worker] == :gone }
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def flush_deferred
|
|
190
|
+
@display.call(@deferred.shift) until @deferred.empty?
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def now
|
|
194
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
require "tmpdir"
|
|
6
|
+
|
|
7
|
+
module RSpec
|
|
8
|
+
class CollapsingFormatter < Core::Formatters::BaseTextFormatter
|
|
9
|
+
module Runners
|
|
10
|
+
# parallel_tests (https://github.com/grosser/parallel_tests) starts one RSpec
|
|
11
|
+
# process per group of spec files and sets, in each:
|
|
12
|
+
#
|
|
13
|
+
# - TEST_ENV_NUMBER: "" or "1" for the first process, then "2", "3", ...
|
|
14
|
+
# - PARALLEL_TEST_GROUPS: the number of processes requested (more may have been
|
|
15
|
+
# requested than are running, if there were fewer spec files);
|
|
16
|
+
# - PARALLEL_PID_FILE: a file, unique to the run, listing the processes still
|
|
17
|
+
# running as a JSON array of process ids.
|
|
18
|
+
#
|
|
19
|
+
# The first process is the coordinator. The socket is named after the pid file,
|
|
20
|
+
# so that every process of a run finds it and concurrent runs do not meet.
|
|
21
|
+
module ParallelTests
|
|
22
|
+
def self.detect(env)
|
|
23
|
+
pid_file = env["PARALLEL_PID_FILE"]
|
|
24
|
+
requested = Integer(env.fetch("PARALLEL_TEST_GROUPS", ""), exception: false)
|
|
25
|
+
number = env["TEST_ENV_NUMBER"]
|
|
26
|
+
return unless pid_file && number && requested && requested > 1
|
|
27
|
+
|
|
28
|
+
worker = number.empty? ? 1 : Integer(number, exception: false)
|
|
29
|
+
return unless worker&.positive?
|
|
30
|
+
|
|
31
|
+
Context.new(
|
|
32
|
+
worker: worker,
|
|
33
|
+
coordinator: worker == 1,
|
|
34
|
+
address: socket_path(pid_file),
|
|
35
|
+
running_processes: -> { running_processes(pid_file) }
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.socket_path(pid_file)
|
|
40
|
+
name = "rspec-collapsing-formatter-#{Digest::SHA256.hexdigest(pid_file)[0, 16]}.sock"
|
|
41
|
+
path = File.join(Dir.tmpdir, name)
|
|
42
|
+
path.bytesize < 100 ? path : File.join("/tmp", name) # socket paths are limited to about 104 bytes
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.running_processes(pid_file)
|
|
46
|
+
pids = JSON.parse(File.read(pid_file))
|
|
47
|
+
pids.size if pids.is_a?(Array)
|
|
48
|
+
rescue SystemCallError, JSON::ParserError
|
|
49
|
+
nil # missing, or being rewritten
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RSpec
|
|
4
|
+
class CollapsingFormatter < Core::Formatters::BaseTextFormatter
|
|
5
|
+
# Recognizes parallel test runners. This is the only place that lists them, and
|
|
6
|
+
# the only runner-related code the formatter calls.
|
|
7
|
+
#
|
|
8
|
+
# Each adapter detects its runner from the environment, without loading the
|
|
9
|
+
# runner's gem, and describes this process's place in the run as a Context.
|
|
10
|
+
module Runners
|
|
11
|
+
# `worker`: this process's worker id, from 1.
|
|
12
|
+
# `coordinator`: whether this process displays the progress of the whole run.
|
|
13
|
+
# `address`: the Unix socket path through which the run's processes meet.
|
|
14
|
+
# `running_processes`: a callable returning how many of the run's worker
|
|
15
|
+
# processes are still running, this one included, or nil if unknown.
|
|
16
|
+
Context = Struct.new(:worker, :coordinator, :address, :running_processes, keyword_init: true)
|
|
17
|
+
|
|
18
|
+
ADAPTERS = {
|
|
19
|
+
"rspec/collapsing_formatter/runners/parallel_tests" => :ParallelTests
|
|
20
|
+
}.freeze
|
|
21
|
+
|
|
22
|
+
# The context of this process if it is a worker in a parallel run, else nil.
|
|
23
|
+
def self.detect(env = ENV)
|
|
24
|
+
ADAPTERS.each do |path, name|
|
|
25
|
+
require path
|
|
26
|
+
context = const_get(name).detect(env)
|
|
27
|
+
return context if context
|
|
28
|
+
end
|
|
29
|
+
nil
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -7,17 +7,24 @@ module RSpec
|
|
|
7
7
|
# Colors are named by RSpec's configurable roles (:success, :failure, ...), so they
|
|
8
8
|
# follow the user's RSpec color configuration when wrapped with ConsoleCodes.
|
|
9
9
|
class Symbols
|
|
10
|
-
UNICODE = {
|
|
11
|
-
|
|
10
|
+
UNICODE = {
|
|
11
|
+
passed: "✓", failed: "✗", pending: "•", running: "▸", ellipsis: "…", path_separator: "›"
|
|
12
|
+
}.freeze
|
|
13
|
+
ASCII = {
|
|
14
|
+
passed: "+", failed: "x", pending: "*", running: ">", ellipsis: "...", path_separator: ">"
|
|
15
|
+
}.freeze
|
|
12
16
|
STYLES = { passed: :success, failed: :failure, pending: :pending, running: :detail }.freeze
|
|
13
17
|
|
|
14
18
|
def self.for(output)
|
|
15
19
|
encoding = (output.external_encoding if output.respond_to?(:external_encoding)) || Encoding.default_external
|
|
16
|
-
new(encoding == Encoding::UTF_8 ? UNICODE : ASCII)
|
|
20
|
+
new(encoding == Encoding::UTF_8 ? UNICODE : ASCII, io: output)
|
|
17
21
|
end
|
|
18
22
|
|
|
19
|
-
|
|
23
|
+
# `io` is the stream the lines are written to; whether to color them depends on it
|
|
24
|
+
# when RSpec's color mode is automatic. Without it, RSpec's output stream decides.
|
|
25
|
+
def initialize(glyphs, io: nil)
|
|
20
26
|
@glyphs = glyphs
|
|
27
|
+
@io = io
|
|
21
28
|
end
|
|
22
29
|
|
|
23
30
|
def glyph(status)
|
|
@@ -28,6 +35,11 @@ module RSpec
|
|
|
28
35
|
@glyphs.fetch(:ellipsis)
|
|
29
36
|
end
|
|
30
37
|
|
|
38
|
+
# Between the descriptions in a one-line path, e.g. "User › when active".
|
|
39
|
+
def path_separator
|
|
40
|
+
@glyphs.fetch(:path_separator)
|
|
41
|
+
end
|
|
42
|
+
|
|
31
43
|
def style(status)
|
|
32
44
|
STYLES.fetch(status)
|
|
33
45
|
end
|
|
@@ -35,8 +47,15 @@ module RSpec
|
|
|
35
47
|
# The line as it is written to the terminal, colored according to RSpec's
|
|
36
48
|
# color configuration.
|
|
37
49
|
def paint(line)
|
|
38
|
-
|
|
39
|
-
|
|
50
|
+
return line.indent + line.text unless line.style && color?
|
|
51
|
+
|
|
52
|
+
"#{line.indent}\e[#{Core::Formatters::ConsoleCodes.console_code_for(line.style)}m#{line.text}\e[0m"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def color?
|
|
58
|
+
@io ? RSpec.configuration.color_enabled?(@io) : RSpec.configuration.color_enabled?
|
|
40
59
|
end
|
|
41
60
|
end
|
|
42
61
|
end
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "socket"
|
|
5
|
+
|
|
6
|
+
module RSpec
|
|
7
|
+
class CollapsingFormatter < Core::Formatters::BaseTextFormatter
|
|
8
|
+
# Carries events from worker processes to the process that displays them, over a
|
|
9
|
+
# Unix socket, as one JSON object per line. Knows nothing about any particular
|
|
10
|
+
# test runner.
|
|
11
|
+
#
|
|
12
|
+
# A worker connects, says which worker it is and sends its events. When it has
|
|
13
|
+
# finished it waits to be released: the server sends "go", the worker prints its
|
|
14
|
+
# failure and summary output, then disconnects. That keeps each worker's output in
|
|
15
|
+
# one piece, after the display is complete.
|
|
16
|
+
module Transport
|
|
17
|
+
RELEASE = "go"
|
|
18
|
+
|
|
19
|
+
def self.encode(event)
|
|
20
|
+
"#{JSON.generate({ "type" => event.class.name.split("::").last, **event.to_h.transform_keys(&:to_s) })}\n"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The event in `line`, or nil if it isn't one.
|
|
24
|
+
def self.decode(line)
|
|
25
|
+
fields = JSON.parse(line)
|
|
26
|
+
type = fields.delete("type")
|
|
27
|
+
return unless fields.is_a?(Hash) && Events.constants.include?(type&.to_sym)
|
|
28
|
+
|
|
29
|
+
event_class = Events.const_get(type)
|
|
30
|
+
fields = fields.slice(*event_class.members.map(&:to_s)).transform_keys(&:to_sym)
|
|
31
|
+
fields[:status] = fields[:status].to_sym if fields[:status].is_a?(String)
|
|
32
|
+
event_class.new(**fields)
|
|
33
|
+
rescue JSON::ParserError, TypeError
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Accepts workers' connections and hands their events to the block, from one
|
|
38
|
+
# thread per connection: `block.call(worker, event)`, and `block.call(worker, nil)`
|
|
39
|
+
# when the worker disconnects.
|
|
40
|
+
class Server
|
|
41
|
+
def initialize(path, &on_event)
|
|
42
|
+
File.delete(path) if File.socket?(path) # left over from a run that crashed
|
|
43
|
+
@path = path
|
|
44
|
+
@server = UNIXServer.new(path)
|
|
45
|
+
@on_event = on_event
|
|
46
|
+
@connections = {}
|
|
47
|
+
@lock = Mutex.new
|
|
48
|
+
@acceptor = Thread.new { accept_connections }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Tells the worker it may print its output. False if it isn't connected.
|
|
52
|
+
def release(worker)
|
|
53
|
+
socket = @lock.synchronize { @connections[worker] }
|
|
54
|
+
return false unless socket
|
|
55
|
+
|
|
56
|
+
socket.write("#{RELEASE}\n")
|
|
57
|
+
true
|
|
58
|
+
rescue IOError, SystemCallError
|
|
59
|
+
false
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Stops accepting workers and disconnects those still connected, which stops
|
|
63
|
+
# them waiting to be released.
|
|
64
|
+
def close
|
|
65
|
+
@server.close
|
|
66
|
+
@acceptor.join(1)
|
|
67
|
+
@lock.synchronize { @connections.values }.each { |socket| socket.close unless socket.closed? }
|
|
68
|
+
File.delete(@path) if File.socket?(@path)
|
|
69
|
+
rescue IOError, SystemCallError
|
|
70
|
+
nil
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
def accept_connections
|
|
76
|
+
loop do
|
|
77
|
+
socket = @server.accept
|
|
78
|
+
Thread.new { serve(socket) }
|
|
79
|
+
end
|
|
80
|
+
rescue IOError, SystemCallError
|
|
81
|
+
nil # closed
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def serve(socket)
|
|
85
|
+
worker = JSON.parse(socket.gets || "{}")["worker"]
|
|
86
|
+
return socket.close unless worker
|
|
87
|
+
|
|
88
|
+
@lock.synchronize { @connections[worker] = socket }
|
|
89
|
+
while (line = socket.gets)
|
|
90
|
+
event = Transport.decode(line)
|
|
91
|
+
@on_event.call(worker, event) if event
|
|
92
|
+
end
|
|
93
|
+
rescue IOError, SystemCallError, JSON::ParserError
|
|
94
|
+
nil
|
|
95
|
+
ensure
|
|
96
|
+
if worker
|
|
97
|
+
@lock.synchronize { @connections.delete(worker) }
|
|
98
|
+
@on_event.call(worker, nil)
|
|
99
|
+
end
|
|
100
|
+
socket.close unless socket.closed?
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# A worker's connection to the server.
|
|
105
|
+
class Client
|
|
106
|
+
# Connects, retrying until `timeout` seconds have passed (the server's process
|
|
107
|
+
# may start later). Nil if it never succeeds.
|
|
108
|
+
def self.connect(path, worker:, timeout:)
|
|
109
|
+
deadline = monotonic_now + timeout
|
|
110
|
+
begin
|
|
111
|
+
socket = UNIXSocket.new(path)
|
|
112
|
+
rescue Errno::ENOENT, Errno::ECONNREFUSED
|
|
113
|
+
return nil if monotonic_now > deadline
|
|
114
|
+
|
|
115
|
+
sleep 0.05
|
|
116
|
+
retry
|
|
117
|
+
end
|
|
118
|
+
socket.write("#{JSON.generate({ "worker" => worker })}\n")
|
|
119
|
+
new(socket)
|
|
120
|
+
rescue IOError, SystemCallError
|
|
121
|
+
nil
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def self.monotonic_now
|
|
125
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def initialize(socket)
|
|
129
|
+
@socket = socket
|
|
130
|
+
@lock = Mutex.new
|
|
131
|
+
@deferred = [] # events sent from signal handlers, which cannot take the lock
|
|
132
|
+
@connected = true
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def connected?
|
|
136
|
+
@connected
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def send_event(event)
|
|
140
|
+
line = Transport.encode(event)
|
|
141
|
+
@lock.synchronize do
|
|
142
|
+
write(@deferred.shift) until @deferred.empty?
|
|
143
|
+
write(line)
|
|
144
|
+
end
|
|
145
|
+
rescue ThreadError => e
|
|
146
|
+
raise unless e.message.include?("trap context")
|
|
147
|
+
|
|
148
|
+
@deferred << line # Array#<< needs no lock; sent with the next event
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Blocks until the server releases this worker, or goes away.
|
|
152
|
+
def wait_for_release
|
|
153
|
+
@lock.synchronize { write(@deferred.shift) until @deferred.empty? }
|
|
154
|
+
while (line = @socket.gets)
|
|
155
|
+
return if line.chomp == RELEASE
|
|
156
|
+
end
|
|
157
|
+
rescue IOError, SystemCallError
|
|
158
|
+
nil
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def close
|
|
162
|
+
@connected = false
|
|
163
|
+
@socket.close unless @socket.closed?
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
private
|
|
167
|
+
|
|
168
|
+
def write(line)
|
|
169
|
+
@socket.write(line) if @connected
|
|
170
|
+
rescue IOError, SystemCallError
|
|
171
|
+
@connected = false # the server is gone; carry on without it
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
end
|
|
@@ -103,39 +103,46 @@ module RSpec
|
|
|
103
103
|
|
|
104
104
|
# The hierarchy of groups and examples seen so far, built from RSpec events.
|
|
105
105
|
#
|
|
106
|
-
#
|
|
107
|
-
# as a stack and the currently running example.
|
|
106
|
+
# Each worker runs examples one at a time, so the tree tracks, per worker, the
|
|
107
|
+
# currently open groups as a stack and the currently running example. All workers
|
|
108
|
+
# share one list of top-level groups, in the order they started. A run in a single
|
|
109
|
+
# process has one worker, nil, the default.
|
|
108
110
|
class Tree
|
|
111
|
+
# Where a worker is in the tree.
|
|
112
|
+
Position = Struct.new(:open_groups, :current_example)
|
|
113
|
+
|
|
109
114
|
attr_reader :groups
|
|
110
115
|
|
|
111
116
|
def initialize
|
|
112
117
|
@groups = []
|
|
113
|
-
@
|
|
114
|
-
@current_example = nil
|
|
118
|
+
@positions = Hash.new { |positions, worker| positions[worker] = Position.new([], nil) }
|
|
115
119
|
end
|
|
116
120
|
|
|
117
|
-
# Nesting depth of the innermost open group (0 when no group is open).
|
|
118
|
-
def depth
|
|
119
|
-
@open_groups.size
|
|
121
|
+
# Nesting depth of the worker's innermost open group (0 when no group is open).
|
|
122
|
+
def depth(worker = nil)
|
|
123
|
+
@positions[worker].open_groups.size
|
|
120
124
|
end
|
|
121
125
|
|
|
122
|
-
def group_started(description)
|
|
123
|
-
|
|
124
|
-
|
|
126
|
+
def group_started(description, worker: nil)
|
|
127
|
+
open_groups = @positions[worker].open_groups
|
|
128
|
+
group = add(GroupNode.new(description), open_groups.last)
|
|
129
|
+
open_groups.push(group)
|
|
125
130
|
group
|
|
126
131
|
end
|
|
127
132
|
|
|
128
|
-
def group_finished
|
|
129
|
-
@open_groups.pop.tap(&:finish)
|
|
133
|
+
def group_finished(worker: nil)
|
|
134
|
+
@positions[worker].open_groups.pop.tap(&:finish)
|
|
130
135
|
end
|
|
131
136
|
|
|
132
|
-
def example_started(description)
|
|
133
|
-
|
|
137
|
+
def example_started(description, worker: nil)
|
|
138
|
+
position = @positions[worker]
|
|
139
|
+
position.current_example = add(ExampleNode.new(description), position.open_groups.last)
|
|
134
140
|
end
|
|
135
141
|
|
|
136
|
-
def example_finished(description, status, detail = nil)
|
|
137
|
-
|
|
138
|
-
|
|
142
|
+
def example_finished(description, status, detail = nil, worker: nil)
|
|
143
|
+
position = @positions[worker]
|
|
144
|
+
example = position.current_example || example_started(description, worker: worker)
|
|
145
|
+
position.current_example = nil
|
|
139
146
|
example.finish(status, detail, description: description)
|
|
140
147
|
example
|
|
141
148
|
end
|
|
@@ -8,16 +8,25 @@ require "rspec/collapsing_formatter/layout"
|
|
|
8
8
|
require "rspec/collapsing_formatter/stream_output"
|
|
9
9
|
require "rspec/collapsing_formatter/stray_output"
|
|
10
10
|
require "rspec/collapsing_formatter/live_output"
|
|
11
|
+
require "rspec/collapsing_formatter/events"
|
|
12
|
+
require "rspec/collapsing_formatter/display"
|
|
13
|
+
require "rspec/collapsing_formatter/transport"
|
|
14
|
+
require "rspec/collapsing_formatter/runners"
|
|
15
|
+
require "rspec/collapsing_formatter/parallel"
|
|
11
16
|
|
|
12
17
|
module RSpec
|
|
13
18
|
# A documentation-style formatter that collapses finished example groups.
|
|
14
19
|
#
|
|
15
20
|
# Failure, pending and summary output is inherited unchanged from RSpec's
|
|
16
|
-
# BaseTextFormatter. This class only translates RSpec
|
|
17
|
-
#
|
|
21
|
+
# BaseTextFormatter. This class only translates RSpec notifications into events and
|
|
22
|
+
# hands them to a Display, which owns the tree and everything about how lines reach
|
|
18
23
|
# the terminal.
|
|
24
|
+
#
|
|
25
|
+
# In a parallel run (see Runners) the events go to a Parallel::Coordinator or
|
|
26
|
+
# Parallel::Worker instead, and RSpec's failure and summary output is held back
|
|
27
|
+
# until they let this process print it.
|
|
19
28
|
class CollapsingFormatter < Core::Formatters::BaseTextFormatter
|
|
20
|
-
VERSION = "0.
|
|
29
|
+
VERSION = "0.2.0"
|
|
21
30
|
|
|
22
31
|
Core::Formatters.register self,
|
|
23
32
|
:start, :message,
|
|
@@ -27,40 +36,39 @@ module RSpec
|
|
|
27
36
|
|
|
28
37
|
def initialize(output)
|
|
29
38
|
super
|
|
30
|
-
@tree = Tree.new
|
|
31
|
-
symbols = Symbols.for(output)
|
|
32
|
-
@renderer =
|
|
33
|
-
if LiveOutput.supported?(output)
|
|
34
|
-
LiveOutput.new(output, symbols, @tree)
|
|
35
|
-
else
|
|
36
|
-
StreamOutput.new(output, symbols)
|
|
37
|
-
end
|
|
38
39
|
@failure_count = 0
|
|
40
|
+
@worker = nil
|
|
41
|
+
context = parallel_context
|
|
42
|
+
@sink = context && parallel_role(context)
|
|
43
|
+
if @sink
|
|
44
|
+
@worker = context.worker
|
|
45
|
+
@held_output = output
|
|
46
|
+
@output = StringIO.new(+"") # the inherited dump methods write here until released
|
|
47
|
+
else
|
|
48
|
+
@sink = Display.for(output)
|
|
49
|
+
end
|
|
39
50
|
end
|
|
40
51
|
|
|
41
52
|
def start(notification)
|
|
42
53
|
super
|
|
43
|
-
|
|
54
|
+
emit(Events::Started)
|
|
44
55
|
end
|
|
45
56
|
|
|
46
57
|
# RSpec messages (e.g. "Run options: ...") are printed above the live region.
|
|
47
58
|
def message(notification)
|
|
48
|
-
|
|
59
|
+
emit(Events::Message, text: notification.message)
|
|
49
60
|
end
|
|
50
61
|
|
|
51
62
|
def example_group_started(notification)
|
|
52
|
-
|
|
53
|
-
@renderer.group_started(group, @tree.depth - 1)
|
|
63
|
+
emit(Events::GroupStarted, description: notification.group.description.strip)
|
|
54
64
|
end
|
|
55
65
|
|
|
56
66
|
def example_group_finished(_notification)
|
|
57
|
-
|
|
58
|
-
@renderer.group_finished(group, @tree.depth)
|
|
67
|
+
emit(Events::GroupFinished)
|
|
59
68
|
end
|
|
60
69
|
|
|
61
70
|
def example_started(notification)
|
|
62
|
-
|
|
63
|
-
@renderer.example_started(example, @tree.depth)
|
|
71
|
+
emit(Events::ExampleStarted, description: notification.example.description.strip)
|
|
64
72
|
end
|
|
65
73
|
|
|
66
74
|
def example_passed(notification)
|
|
@@ -69,7 +77,9 @@ module RSpec
|
|
|
69
77
|
|
|
70
78
|
def example_failed(notification)
|
|
71
79
|
@failure_count += 1
|
|
72
|
-
|
|
80
|
+
detail = "FAILED - #{@failure_count}"
|
|
81
|
+
detail += ", worker #{@worker}" if @worker # numbered as in this worker's failure list
|
|
82
|
+
example_finished(notification.example, :failed, detail)
|
|
73
83
|
end
|
|
74
84
|
|
|
75
85
|
def example_pending(notification)
|
|
@@ -80,14 +90,20 @@ module RSpec
|
|
|
80
90
|
# Called before RSpec's failure, pending and summary output, which is then
|
|
81
91
|
# appended below the tree by the inherited dump methods.
|
|
82
92
|
def start_dump(_notification)
|
|
83
|
-
|
|
93
|
+
emit(Events::Finished)
|
|
84
94
|
end
|
|
85
95
|
|
|
86
96
|
# Also finishes the output when the run ends without reaching start_dump.
|
|
87
97
|
# Finishing twice is harmless.
|
|
88
98
|
def close(notification)
|
|
89
|
-
|
|
99
|
+
emit(Events::Finished)
|
|
90
100
|
super
|
|
101
|
+
return unless @held_output
|
|
102
|
+
|
|
103
|
+
@sink.close do
|
|
104
|
+
@held_output.write(output.string)
|
|
105
|
+
@held_output.flush
|
|
106
|
+
end
|
|
91
107
|
end
|
|
92
108
|
|
|
93
109
|
private
|
|
@@ -95,8 +111,24 @@ module RSpec
|
|
|
95
111
|
# Descriptions are read again on finish: RSpec generates descriptions for
|
|
96
112
|
# examples like `it { is_expected.to be_valid }` only after they have run.
|
|
97
113
|
def example_finished(example, status, detail = nil)
|
|
98
|
-
|
|
99
|
-
|
|
114
|
+
emit(Events::ExampleFinished, description: example.description.strip, status: status, detail: detail)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def emit(type, **fields)
|
|
118
|
+
@sink.call(type.new(worker: @worker, **fields))
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# This process's place in a parallel run, if it is one whose progress can be shown
|
|
122
|
+
# live: the display goes to stderr, which runners leave connected to the terminal.
|
|
123
|
+
def parallel_context
|
|
124
|
+
context = Runners.detect(ENV)
|
|
125
|
+
context if context && LiveOutput.supported?(STDERR)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# The coordinator or worker for this process; nil (so it works on its own) if the
|
|
129
|
+
# coordinator cannot listen, or a worker cannot reach it.
|
|
130
|
+
def parallel_role(context)
|
|
131
|
+
context.coordinator ? Parallel::Coordinator.start(context, STDERR) : Parallel::Worker.start(context)
|
|
100
132
|
end
|
|
101
133
|
end
|
|
102
134
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rspec-collapsing-formatter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Adam Lett
|
|
@@ -52,11 +52,17 @@ files:
|
|
|
52
52
|
- LICENSE
|
|
53
53
|
- README.md
|
|
54
54
|
- lib/rspec/collapsing_formatter.rb
|
|
55
|
+
- lib/rspec/collapsing_formatter/display.rb
|
|
56
|
+
- lib/rspec/collapsing_formatter/events.rb
|
|
55
57
|
- lib/rspec/collapsing_formatter/layout.rb
|
|
56
58
|
- lib/rspec/collapsing_formatter/live_output.rb
|
|
59
|
+
- lib/rspec/collapsing_formatter/parallel.rb
|
|
60
|
+
- lib/rspec/collapsing_formatter/runners.rb
|
|
61
|
+
- lib/rspec/collapsing_formatter/runners/parallel_tests.rb
|
|
57
62
|
- lib/rspec/collapsing_formatter/stray_output.rb
|
|
58
63
|
- lib/rspec/collapsing_formatter/stream_output.rb
|
|
59
64
|
- lib/rspec/collapsing_formatter/symbols.rb
|
|
65
|
+
- lib/rspec/collapsing_formatter/transport.rb
|
|
60
66
|
- lib/rspec/collapsing_formatter/tree.rb
|
|
61
67
|
homepage: https://github.com/KaptajnKold/rspec-collapsing-formatter
|
|
62
68
|
licenses:
|