constable-rails 1.3.2 → 1.4.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/CHANGELOG.md +115 -3
- data/README.md +1 -1
- data/lib/constable/cli.rb +25 -9
- data/lib/constable/cold_case/rspec.rb +35 -0
- data/lib/constable/config.rb +14 -0
- data/lib/constable/jail.rb +8 -1
- data/lib/constable/log_router.rb +66 -7
- data/lib/constable/reporter.rb +46 -11
- data/lib/constable/version.rb +1 -1
- data/lib/constable/worker_databases.rb +71 -0
- data/lib/constable.rb +1 -1
- data/lib/generators/constable/templates/case_helper.rb.tt +1 -0
- data/lib/generators/constable/templates/config.yml.tt +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5fd3b3f867d958ed8f1306a51fb6a001145d88bd4c79839dd6d603f0b565248b
|
|
4
|
+
data.tar.gz: 1dee6cf2bece623c102744cb44e271461907d386715ed435fcc871ee131dd92c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1f6c418f0dd90a2e6e45733b237e8aa3c8e2e0d7c62b55d145dc7596bf047569983f243ba92558050612328f78834971cf5fef19e52efe4eb4deee0f5a751506
|
|
7
|
+
data.tar.gz: 74e6fd7f76215036de95d36ccdd84a068a71b999368e97f998e5e832c2230d3624eaf6aba4aafe0ff5edf2aa0245b15a050e85fb5784347c6407a01d72842711
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,100 @@ All notable changes to this project are documented here. This project adheres to
|
|
|
5
5
|
|
|
6
6
|
## [Unreleased]
|
|
7
7
|
|
|
8
|
+
## [1.4.0]
|
|
9
|
+
|
|
10
|
+
Two problems from the same 1,277-file suite: a docket nobody asked for, and 110 minutes.
|
|
11
|
+
|
|
12
|
+
### A flaky test is no longer jailed by itself
|
|
13
|
+
|
|
14
|
+
**This is the important one, because jailed means skipped.** A first `constable test` on a
|
|
15
|
+
real suite put **29 tests on a docket the user had never asked for** — each one recorded
|
|
16
|
+
as *"passed, then failed with no code change"* — and every one of them was silently
|
|
17
|
+
skipped from then on. A suite with order-dependent tests, which is most large suites and
|
|
18
|
+
exactly what Constable is pitched at, trips that constantly.
|
|
19
|
+
|
|
20
|
+
Automatic flake-jailing is now off by default:
|
|
21
|
+
|
|
22
|
+
```yaml
|
|
23
|
+
jail_flakes: false # was: always on
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`constable test --jail` still jails failures, because that is a thing you asked for.
|
|
27
|
+
Turn the automatic route back on when you want it. Nothing stops being *reported* — a
|
|
28
|
+
flaky test still fails, still shows up, still gets a warrant if warrants are on. It just
|
|
29
|
+
does not remove itself from the suite.
|
|
30
|
+
|
|
31
|
+
### Per-worker databases without a loadable schema
|
|
32
|
+
|
|
33
|
+
The same suite ran **serially for 110 minutes on a 12-core machine**, because its schema
|
|
34
|
+
cannot be loaded from `schema.rb` (Postgres custom types), so `worker_databases: off` was
|
|
35
|
+
the only setting that worked.
|
|
36
|
+
|
|
37
|
+
Postgres can copy a whole database in one statement, and that needs no schema at all:
|
|
38
|
+
|
|
39
|
+
```sql
|
|
40
|
+
CREATE DATABASE "caseflow_test_3" TEMPLATE "caseflow_test"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`worker_databases: reuse` now clones from the test database you already have, dropping a
|
|
44
|
+
stale copy and disconnecting the template first. It falls back to loading the schema when
|
|
45
|
+
there is nothing to clone, and to Postgres only — anything unexpected takes the old path
|
|
46
|
+
rather than failing. It is also simply faster than replaying a large schema once per
|
|
47
|
+
worker, so it is worth having on any Postgres app.
|
|
48
|
+
|
|
49
|
+
For a suite that could only run serially, this is the difference between one core and all
|
|
50
|
+
of them.
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## [1.3.3]
|
|
54
|
+
|
|
55
|
+
**1.3.2 was tagged twice.** The console fix in it was rebuilt after the gem had already
|
|
56
|
+
been pushed, and RubyGems will not accept a second push of the same version — so the
|
|
57
|
+
published 1.3.2 does not contain it. That fix is here, along with two more found on a
|
|
58
|
+
1,277-file suite.
|
|
59
|
+
|
|
60
|
+
### Shared examples survive across cold-case files
|
|
61
|
+
|
|
62
|
+
A suite that keeps shared examples in a plain file beside its specs —
|
|
63
|
+
`require_relative "appeal_shared_examples"` at the top of `appeal_spec.rb` — registers
|
|
64
|
+
them the first time that file loads. `require` never fires again, so every later file
|
|
65
|
+
that shares them died on load:
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
✗ spec/models/legacy_appeal_spec.rb "failed to load"
|
|
69
|
+
ArgumentError: Could not find shared examples "toggle overtime"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Twelve files in one run, every one of which passes in isolation. RSpec never hits this
|
|
73
|
+
because it loads every spec file and *then* runs them; Constable loads one at a time,
|
|
74
|
+
which is what makes a cold case cheap, so the registry has to be carried across by hand.
|
|
75
|
+
|
|
76
|
+
Same two files, before and after: 12 files failing to load → `458 passed, 0 failed`.
|
|
77
|
+
|
|
78
|
+
### Rename suggestions are a section, not a wall
|
|
79
|
+
|
|
80
|
+
They were printed unlabelled after SLOWEST, one long line per suggestion, each carrying
|
|
81
|
+
two full test descriptions and two hashes. A real suite produced two hundred of them —
|
|
82
|
+
several hundred lines with no heading, burying every section above.
|
|
83
|
+
|
|
84
|
+
Now a `RENAMED?` section: eight at a time, three short lines each, with the rest counted.
|
|
85
|
+
Descriptions are truncated to the frame, because an RSpec description built from a matcher
|
|
86
|
+
carries the entire inspected object — every column of a record, ids and timestamps
|
|
87
|
+
included.
|
|
88
|
+
|
|
89
|
+
### The app's stdout, again (from 1.3.2, unpublished)
|
|
90
|
+
|
|
91
|
+
Reassigning the `$stdout` object was not enough: a gem writing through the `STDERR`
|
|
92
|
+
constant, or anything already holding the descriptor, goes straight past it, and in a
|
|
93
|
+
terminal both streams land in the same place. The descriptors are now redirected with
|
|
94
|
+
`IO#reopen`, with an `at_exit` guard so a crash still prints where it can be seen.
|
|
95
|
+
|
|
96
|
+
CLI errors no longer use `Kernel#warn`. Rails apps routinely override `Warning.warn` to
|
|
97
|
+
funnel Ruby warnings into `Rails.logger`, and a real one did: `no tests matched ...`
|
|
98
|
+
arrived in `log/test.log` tagged `[RUBY WARNING]` while the terminal showed nothing at
|
|
99
|
+
all, so the command looked like it had silently done nothing.
|
|
100
|
+
|
|
101
|
+
|
|
8
102
|
## [1.3.2]
|
|
9
103
|
|
|
10
104
|
### stdout is results only — the app's stdout too, not just Constable's
|
|
@@ -27,8 +121,24 @@ noise is emitted *by* booting it. And colour detection was asking `$stdout.tty?`
|
|
|
27
121
|
by then is a file, and a file is never a tty, so colour would have silently switched off
|
|
28
122
|
for everybody.
|
|
29
123
|
|
|
30
|
-
|
|
31
|
-
|
|
124
|
+
Reassigning the `$stdout` *object* turned out not to be enough — a gem writing through
|
|
125
|
+
the `STDERR` constant, or anything already holding the descriptor, goes straight past it,
|
|
126
|
+
and in a terminal both streams land in the same place. So the **descriptors** are
|
|
127
|
+
redirected with `IO#reopen`, which is the only thing that catches every writer, and the
|
|
128
|
+
reporter is handed a dup of the real terminal taken beforehand. An `at_exit` guard puts
|
|
129
|
+
them back, so an uncaught exception still prints its backtrace where you can see it.
|
|
130
|
+
|
|
131
|
+
Two things that fell out of doing it properly:
|
|
132
|
+
|
|
133
|
+
- **CLI errors no longer use `Kernel#warn`.** With the descriptors redirected an error
|
|
134
|
+
would have gone into the log — and `warn` would not have reached the terminal even
|
|
135
|
+
without that, because Rails apps routinely override `Warning.warn` to funnel Ruby
|
|
136
|
+
warnings into `Rails.logger`. A real one did: `no tests matched ...` arrived in
|
|
137
|
+
`log/test.log` tagged `[RUBY WARNING]` while the terminal showed nothing at all. Errors
|
|
138
|
+
now go to the console the reporter kept, on stderr.
|
|
139
|
+
- **Colour detection was asking the wrong stream.** `$stdout.tty?`, when `$stdout` is by
|
|
140
|
+
then a log file and a file is never a tty — colour would have silently switched off for
|
|
141
|
+
everybody.
|
|
32
142
|
|
|
33
143
|
|
|
34
144
|
## [1.3.1]
|
|
@@ -554,7 +664,9 @@ Initial release.
|
|
|
554
664
|
- Diff-based coverage gate — only lines changed in the current diff are held to the
|
|
555
665
|
threshold. `constable beat` for the full picture, `--html` for a browsable report.
|
|
556
666
|
|
|
557
|
-
[Unreleased]: https://github.com/Ray-Hughes/constable/compare/v1.
|
|
667
|
+
[Unreleased]: https://github.com/Ray-Hughes/constable/compare/v1.4.0...HEAD
|
|
668
|
+
[1.4.0]: https://github.com/Ray-Hughes/constable/compare/v1.3.3...v1.4.0
|
|
669
|
+
[1.3.3]: https://github.com/Ray-Hughes/constable/compare/v1.3.2...v1.3.3
|
|
558
670
|
[1.3.2]: https://github.com/Ray-Hughes/constable/compare/v1.3.1...v1.3.2
|
|
559
671
|
[1.3.1]: https://github.com/Ray-Hughes/constable/compare/v1.3.0...v1.3.1
|
|
560
672
|
[1.3.0]: https://github.com/Ray-Hughes/constable/compare/v1.2.0...v1.3.0
|
data/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
**A strict Rails testing framework where fast and non-flaky are structural, not disciplinary.**
|
|
9
9
|
|
|
10
|
-
[](https://badge.fury.io/rb/constable-rails)
|
|
11
11
|
[](https://github.com/Ray-Hughes/constable/actions/workflows/ci.yml)
|
|
12
12
|
[](https://www.ruby-lang.org)
|
|
13
13
|
[](https://rubyonrails.org)
|
data/lib/constable/cli.rb
CHANGED
|
@@ -21,13 +21,29 @@ module Constable
|
|
|
21
21
|
# tier, an unreadable config. Both are the user's mistake rather than a crash, and
|
|
22
22
|
# both deserve one sentence and a usage status instead of a backtrace.
|
|
23
23
|
rescue Thor::Error, Constable::Error => e
|
|
24
|
-
|
|
24
|
+
complain(e.message)
|
|
25
25
|
exit(EXIT_USAGE)
|
|
26
26
|
rescue Interrupt
|
|
27
|
-
|
|
27
|
+
complain("\ninterrupted")
|
|
28
28
|
exit(EXIT_FAILED)
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
# Anything the user has to see, written where they will see it.
|
|
32
|
+
#
|
|
33
|
+
# Not Kernel#warn. A run points stdout and stderr at log/test.log, so an error has to
|
|
34
|
+
# go to the console the reporter kept -- and `warn` would not reach it even without
|
|
35
|
+
# that: Rails apps routinely override Warning.warn to funnel Ruby warnings into
|
|
36
|
+
# Rails.logger, and a real one did. The message arrived in log/test.log tagged
|
|
37
|
+
# "[RUBY WARNING]" while the terminal showed nothing at all.
|
|
38
|
+
def self.complain(message)
|
|
39
|
+
LogRouter.restore!
|
|
40
|
+
io = LogRouter.console_err
|
|
41
|
+
io.puts(message)
|
|
42
|
+
io.flush if io.respond_to?(:flush)
|
|
43
|
+
rescue StandardError
|
|
44
|
+
$stderr.puts(message) # rubocop:disable Style/StderrPuts
|
|
45
|
+
end
|
|
46
|
+
|
|
31
47
|
class_option :"no-color", type: :boolean, default: false, desc: "Disable ANSI color"
|
|
32
48
|
|
|
33
49
|
desc "test [PATH[:LINE]]", "Run the suite -- native and cold cases side by side"
|
|
@@ -184,7 +200,7 @@ module Constable
|
|
|
184
200
|
option :"show-source", type: :boolean, default: false, desc: "Print the rewritten source"
|
|
185
201
|
def modernize(*paths)
|
|
186
202
|
if paths.empty?
|
|
187
|
-
|
|
203
|
+
CLI.complain("modernize needs at least one path")
|
|
188
204
|
exit(EXIT_USAGE)
|
|
189
205
|
end
|
|
190
206
|
|
|
@@ -309,7 +325,7 @@ module Constable
|
|
|
309
325
|
|
|
310
326
|
identity = jail.resolve(locator)
|
|
311
327
|
unless identity
|
|
312
|
-
|
|
328
|
+
CLI.complain("Nothing on the docket at #{locator}")
|
|
313
329
|
exit(EXIT_USAGE)
|
|
314
330
|
end
|
|
315
331
|
yield jail, identity
|
|
@@ -320,9 +336,9 @@ module Constable
|
|
|
320
336
|
def refuse_ambiguous(locator, candidates, noun)
|
|
321
337
|
return if candidates.size <= 1
|
|
322
338
|
|
|
323
|
-
|
|
339
|
+
CLI.complain("#{locator} matches #{candidates.size} tests #{noun}. Name one:")
|
|
324
340
|
candidates.sort_by { |entry| entry.line.to_i }.each do |entry|
|
|
325
|
-
|
|
341
|
+
CLI.complain(" #{entry.location} #{entry.label}")
|
|
326
342
|
end
|
|
327
343
|
exit(EXIT_USAGE)
|
|
328
344
|
end
|
|
@@ -368,7 +384,7 @@ module Constable
|
|
|
368
384
|
|
|
369
385
|
identity = warrants.resolve(locator)
|
|
370
386
|
unless identity
|
|
371
|
-
|
|
387
|
+
CLI.complain("No warrant at #{locator}")
|
|
372
388
|
exit(EXIT_USAGE)
|
|
373
389
|
end
|
|
374
390
|
warrants.release(identity)
|
|
@@ -413,7 +429,7 @@ module Constable
|
|
|
413
429
|
def prepare
|
|
414
430
|
config = load_config
|
|
415
431
|
unless WorkerDatabases.shardable?
|
|
416
|
-
|
|
432
|
+
CLI.complain("This app has no ActiveRecord test databases to prepare.")
|
|
417
433
|
exit(EXIT_USAGE)
|
|
418
434
|
end
|
|
419
435
|
|
|
@@ -429,7 +445,7 @@ module Constable
|
|
|
429
445
|
"rebuilding them."
|
|
430
446
|
0
|
|
431
447
|
rescue Constable::Error => e
|
|
432
|
-
|
|
448
|
+
CLI.complain(e.message)
|
|
433
449
|
exit(EXIT_FAILED)
|
|
434
450
|
end
|
|
435
451
|
|
|
@@ -149,6 +149,7 @@ module Constable
|
|
|
149
149
|
@session_configuration = nil
|
|
150
150
|
@session_prepared = false
|
|
151
151
|
@ran_before_suite_hooks = nil
|
|
152
|
+
@shared_examples = nil
|
|
152
153
|
nil
|
|
153
154
|
end
|
|
154
155
|
|
|
@@ -252,9 +253,11 @@ module Constable
|
|
|
252
253
|
::RSpec.instance_variable_set(:@configuration, @session_configuration)
|
|
253
254
|
prepare_session_configuration(::RSpec.configuration)
|
|
254
255
|
clear_examples
|
|
256
|
+
restore_shared_examples!
|
|
255
257
|
|
|
256
258
|
yield
|
|
257
259
|
ensure
|
|
260
|
+
remember_shared_examples!
|
|
258
261
|
@session_world = ::RSpec.instance_variable_get(:@world)
|
|
259
262
|
@session_configuration = ::RSpec.instance_variable_get(:@configuration)
|
|
260
263
|
clear_examples
|
|
@@ -325,6 +328,38 @@ module Constable
|
|
|
325
328
|
[]
|
|
326
329
|
end
|
|
327
330
|
|
|
331
|
+
# Shared examples are registered on the world, by `require`, once per process.
|
|
332
|
+
#
|
|
333
|
+
# A suite that keeps them in a plain file next to its specs -- `require_relative
|
|
334
|
+
# "appeal_shared_examples"` at the top of appeal_spec.rb -- registers them while
|
|
335
|
+
# the first file runs. `require` never fires again, so if the registry does not
|
|
336
|
+
# survive into the next file, every later file that shares them dies on load with
|
|
337
|
+
# `Could not find shared examples "toggle overtime"`.
|
|
338
|
+
#
|
|
339
|
+
# Under RSpec that never happens: it loads every spec file first, then runs them.
|
|
340
|
+
# Constable loads one file at a time, which is what makes a cold case cheap, so
|
|
341
|
+
# the registry has to be carried across by hand. Observed on a real suite: twelve
|
|
342
|
+
# files failing to load, all of which pass in isolation.
|
|
343
|
+
def remember_shared_examples!
|
|
344
|
+
world = ::RSpec.instance_variable_get(:@world)
|
|
345
|
+
return unless world.respond_to?(:shared_example_group_registry)
|
|
346
|
+
|
|
347
|
+
@shared_examples = world.shared_example_group_registry
|
|
348
|
+
rescue StandardError
|
|
349
|
+
nil
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
def restore_shared_examples!
|
|
353
|
+
return if @shared_examples.nil?
|
|
354
|
+
|
|
355
|
+
world = ::RSpec.world
|
|
356
|
+
return unless world.respond_to?(:shared_example_group_registry)
|
|
357
|
+
|
|
358
|
+
world.instance_variable_set(:@shared_example_group_registry, @shared_examples)
|
|
359
|
+
rescue StandardError
|
|
360
|
+
nil
|
|
361
|
+
end
|
|
362
|
+
|
|
328
363
|
def clear_examples
|
|
329
364
|
world = ::RSpec.instance_variable_get(:@world)
|
|
330
365
|
world.reset if world.respond_to?(:reset)
|
data/lib/constable/config.rb
CHANGED
|
@@ -22,6 +22,7 @@ module Constable
|
|
|
22
22
|
"output" => "concise",
|
|
23
23
|
"parallel_workers" => "auto",
|
|
24
24
|
"worker_databases" => "schema",
|
|
25
|
+
"jail_flakes" => false,
|
|
25
26
|
"tiers" => {
|
|
26
27
|
"unit" => "test/cases/models/**/*",
|
|
27
28
|
"integration" => "test/cases/controllers/**/*",
|
|
@@ -76,6 +77,19 @@ module Constable
|
|
|
76
77
|
|
|
77
78
|
def cold_cases = Array(@raw["cold_cases"])
|
|
78
79
|
def warrants? = truthy(@raw["warrants"])
|
|
80
|
+
|
|
81
|
+
# Should a test that passed last run and failed this one be put on the docket by
|
|
82
|
+
# itself? Off by default, and the reason is what jailing *does*: a jailed test is
|
|
83
|
+
# skipped on every later run. Turning that on automatically means a suite quietly
|
|
84
|
+
# stops running tests nobody chose to stop running.
|
|
85
|
+
#
|
|
86
|
+
# Observed on a real suite: a first `constable test` put 29 tests on a docket the user
|
|
87
|
+
# had never asked for, and every one of them was skipped from then on. A suite with
|
|
88
|
+
# order-dependent tests -- which is most large suites, and exactly the kind Constable
|
|
89
|
+
# is pitched at -- trips this constantly.
|
|
90
|
+
#
|
|
91
|
+
# `constable test --jail` still jails failures, because that is a thing you asked for.
|
|
92
|
+
def jail_flakes? = truthy(@raw["jail_flakes"])
|
|
79
93
|
# Negative retries are a typo for "off", not an instruction to count backwards.
|
|
80
94
|
def warrant_retries = [@raw["warrant_retries"].to_i, 0].max
|
|
81
95
|
def auto_relink? = truthy(@raw["auto_relink"])
|
data/lib/constable/jail.rb
CHANGED
|
@@ -123,6 +123,13 @@ module Constable
|
|
|
123
123
|
|
|
124
124
|
# Consecutive clean runs that earn an automatic release. Floors at 1 -- a period of
|
|
125
125
|
# zero would mean "release on sight", which is not parole.
|
|
126
|
+
# Opt-in. See Config#jail_flakes? for why the default is off: jailing skips the test on
|
|
127
|
+
# every later run, and doing that to a test nobody nominated is how a suite quietly
|
|
128
|
+
# stops testing things.
|
|
129
|
+
def jail_flakes?
|
|
130
|
+
@config.respond_to?(:jail_flakes?) ? @config.jail_flakes? : false
|
|
131
|
+
end
|
|
132
|
+
|
|
126
133
|
def parole_period
|
|
127
134
|
period = @config.respond_to?(:parole_period) ? @config.parole_period.to_i : 0
|
|
128
135
|
period.positive? ? period : 10
|
|
@@ -290,7 +297,7 @@ module Constable
|
|
|
290
297
|
|
|
291
298
|
if jail_mode
|
|
292
299
|
jail_failure(result)
|
|
293
|
-
elsif flake_flip?(result)
|
|
300
|
+
elsif flake_flip?(result) && jail_flakes?
|
|
294
301
|
jail_for_flake(result)
|
|
295
302
|
else
|
|
296
303
|
result
|
data/lib/constable/log_router.rb
CHANGED
|
@@ -121,6 +121,10 @@ module Constable
|
|
|
121
121
|
# else writes to the log.
|
|
122
122
|
def console = @console_out || $stdout
|
|
123
123
|
|
|
124
|
+
# The same, for the stream errors belong on. Convention puts them on stderr, and a
|
|
125
|
+
# run has pointed the real one at the log.
|
|
126
|
+
def console_err = @console_err || $stderr
|
|
127
|
+
|
|
124
128
|
# Rails loggers are not the only thing that writes to a terminal. A gem warning --
|
|
125
129
|
# Faraday's "install the faraday-retry gem", say -- goes straight to $stderr, once
|
|
126
130
|
# per file that triggers it, and lands in the middle of the live stream:
|
|
@@ -132,19 +136,74 @@ module Constable
|
|
|
132
136
|
# terminal it captured beforehand. `--verbose` tees both back, which is the whole
|
|
133
137
|
# point of that flag.
|
|
134
138
|
def capture_console!(file, tee_to)
|
|
135
|
-
|
|
136
|
-
|
|
139
|
+
# Reassigning the $stdout *object* is not enough. A gem that writes through the
|
|
140
|
+
# STDERR constant, a C extension, or anything that already holds file descriptor 2
|
|
141
|
+
# goes straight past it -- and in a terminal both streams land in the same place,
|
|
142
|
+
# so "it was only on stderr" is no comfort at all when it lands mid-glyph.
|
|
143
|
+
#
|
|
144
|
+
# So the descriptors themselves are pointed at the log, and the reporter is handed
|
|
145
|
+
# a dup of the real terminal taken beforehand. #reopen changes where fd 1 and 2
|
|
146
|
+
# write for the whole process, which is the only thing that catches every writer.
|
|
147
|
+
@console_out = $stdout.dup
|
|
148
|
+
@console_err = $stderr.dup
|
|
149
|
+
@reopened = false
|
|
150
|
+
|
|
151
|
+
# --verbose means "show me everything", so nothing is redirected; the object swap
|
|
152
|
+
# is enough to tee. The same fallback covers anything that is not a real IO on
|
|
153
|
+
# both sides -- a StringIO standing in for the terminal in a test, most obviously,
|
|
154
|
+
# where IO#reopen has nothing to reopen onto.
|
|
155
|
+
if tee_to || !redirectable?(file)
|
|
156
|
+
@swapped_out = $stdout
|
|
157
|
+
@swapped_err = $stderr
|
|
158
|
+
replacement = tee_to ? Tee.new(file, tee_to) : file
|
|
159
|
+
$stdout = replacement
|
|
160
|
+
$stderr = replacement
|
|
161
|
+
return
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
$stdout.reopen(file)
|
|
165
|
+
$stderr.reopen(file)
|
|
166
|
+
$stdout.sync = true
|
|
167
|
+
$stderr.sync = true
|
|
168
|
+
@reopened = true
|
|
169
|
+
|
|
170
|
+
# Whatever happens next -- a clean exit, a raise, an interrupt -- the descriptors
|
|
171
|
+
# go back. Without this an uncaught exception prints its backtrace into
|
|
172
|
+
# log/test.log and the terminal shows nothing at all, which is a far worse bug
|
|
173
|
+
# than the one being fixed.
|
|
174
|
+
install_exit_guard!
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def install_exit_guard!
|
|
178
|
+
return if @exit_guard_installed
|
|
137
179
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
180
|
+
@exit_guard_installed = true
|
|
181
|
+
at_exit { restore_console! }
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
# Every party has to be a real IO: the log we are redirecting to, and the two streams
|
|
185
|
+
# we are redirecting away from and will later have to put back.
|
|
186
|
+
def redirectable?(file)
|
|
187
|
+
[file, $stdout, $stderr].all? { |io| io.is_a?(::IO) && io.respond_to?(:fileno) }
|
|
188
|
+
rescue StandardError
|
|
189
|
+
false
|
|
141
190
|
end
|
|
142
191
|
|
|
143
192
|
def restore_console!
|
|
144
|
-
|
|
145
|
-
|
|
193
|
+
if @reopened
|
|
194
|
+
$stdout.reopen(@console_out)
|
|
195
|
+
$stderr.reopen(@console_err)
|
|
196
|
+
elsif @swapped_out
|
|
197
|
+
$stdout = @swapped_out
|
|
198
|
+
$stderr = @swapped_err
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
[@console_out, @console_err].each { |io| io&.close unless io&.closed? } if @reopened
|
|
146
202
|
@console_out = nil
|
|
147
203
|
@console_err = nil
|
|
204
|
+
@swapped_out = nil
|
|
205
|
+
@swapped_err = nil
|
|
206
|
+
@reopened = false
|
|
148
207
|
end
|
|
149
208
|
|
|
150
209
|
# Puts back whatever the app had before route!. Only useful in-process (our own
|
data/lib/constable/reporter.rb
CHANGED
|
@@ -50,6 +50,10 @@ module Constable
|
|
|
50
50
|
# thousand lines that say the same thing.
|
|
51
51
|
COLD_CASE_LIST_LIMIT = 10
|
|
52
52
|
|
|
53
|
+
# How many rename suggestions are listed before the rest are counted instead. A real
|
|
54
|
+
# suite produced two hundred, several hundred lines of output, unlabelled.
|
|
55
|
+
RENAME_LIST_LIMIT = 8
|
|
56
|
+
|
|
53
57
|
DEFAULT_SLOWEST = 5
|
|
54
58
|
|
|
55
59
|
# Column the expanded stream right-aligns durations into. Descriptions are never
|
|
@@ -679,27 +683,58 @@ module Constable
|
|
|
679
683
|
|
|
680
684
|
# Rename detection is the Runner's job; the reporter only says it out loud. Never
|
|
681
685
|
# its own section -- it is a suggestion, not a finding.
|
|
686
|
+
# Renames get a section of their own, a cap, and one line each.
|
|
687
|
+
#
|
|
688
|
+
# They used to be dumped unlabelled after SLOWEST, one long line per suggestion,
|
|
689
|
+
# every line carrying both full test descriptions and two hashes. A real suite
|
|
690
|
+
# produced two hundred of them and several hundred lines of output -- read as a wall
|
|
691
|
+
# of text with no heading, and buried the sections above it. A suggestion nobody can
|
|
692
|
+
# read is not a suggestion.
|
|
682
693
|
def rename_suggestions(suggestions)
|
|
683
|
-
suggestions = Array(suggestions).map { |s|
|
|
694
|
+
suggestions = Array(suggestions).map { |s| suggestion(s) }.compact
|
|
684
695
|
return if suggestions.empty?
|
|
685
696
|
|
|
686
|
-
|
|
687
|
-
suggestions.
|
|
697
|
+
section("RENAMED?")
|
|
698
|
+
shown = suggestions.first(RENAME_LIST_LIMIT)
|
|
699
|
+
|
|
700
|
+
each_entry(shown) do |item|
|
|
701
|
+
writeln(INDENT + paint(item[:from].to_s, :dim))
|
|
702
|
+
writeln(ENTRY_INDENT + paint("→ #{item[:to]}", :dim))
|
|
703
|
+
writeln(ENTRY_INDENT + paint("constable history relink #{item[:old_hash]} #{item[:new_hash]}", :dim))
|
|
704
|
+
end
|
|
705
|
+
|
|
706
|
+
remaining = suggestions.size - shown.size
|
|
707
|
+
hint(if remaining.positive?
|
|
708
|
+
"...and #{remaining} more. A test's history is keyed on its body, so a test " \
|
|
709
|
+
"that was reworded and edited in one commit looks like a new one. Relink the " \
|
|
710
|
+
"ones you recognise; ignore the rest and they age out."
|
|
711
|
+
else
|
|
712
|
+
"A test's history is keyed on its body, so one that was reworded and edited " \
|
|
713
|
+
"in the same commit looks like a new test. Relink it to carry the history over."
|
|
714
|
+
end)
|
|
688
715
|
end
|
|
689
716
|
|
|
690
|
-
def
|
|
691
|
-
return
|
|
692
|
-
return nil unless
|
|
717
|
+
def suggestion(raw)
|
|
718
|
+
return { from: raw.to_s, to: nil, old_hash: nil, new_hash: nil } if raw.is_a?(String)
|
|
719
|
+
return nil unless raw.respond_to?(:to_h)
|
|
693
720
|
|
|
694
|
-
s =
|
|
721
|
+
s = raw.to_h.transform_keys(&:to_sym)
|
|
695
722
|
from = s[:old_label] || s[:from] || label_for(s[:old_case], s[:old_description])
|
|
696
723
|
to = s[:new_label] || s[:to] || label_for(s[:new_case], s[:new_description])
|
|
697
|
-
old_hash = s[:old_hash] || s[:old_identity]
|
|
698
|
-
new_hash = s[:new_hash] || s[:new_identity]
|
|
699
724
|
return nil if from.nil? || to.nil?
|
|
700
725
|
|
|
701
|
-
|
|
702
|
-
|
|
726
|
+
{ from: truncate_label(from), to: truncate_label(to),
|
|
727
|
+
old_hash: s[:old_hash] || s[:old_identity], new_hash: s[:new_hash] || s[:new_identity] }
|
|
728
|
+
end
|
|
729
|
+
|
|
730
|
+
# A description built by RSpec from a matcher carries the whole inspected object --
|
|
731
|
+
# ids, timestamps, every column of a record. Printed in full it is unreadable, and it
|
|
732
|
+
# is the same test either way.
|
|
733
|
+
def truncate_label(label)
|
|
734
|
+
text = label.to_s.tr("\n", " ").squeeze(" ")
|
|
735
|
+
return text if text.length <= RULE_WIDTH - INDENT.length
|
|
736
|
+
|
|
737
|
+
"#{text[0, RULE_WIDTH - INDENT.length - 1]}…"
|
|
703
738
|
end
|
|
704
739
|
|
|
705
740
|
def label_for(case_name, description)
|
data/lib/constable/version.rb
CHANGED
|
@@ -105,6 +105,73 @@ module Constable
|
|
|
105
105
|
.zip(names).each { |config, name| config._database = name if name }
|
|
106
106
|
end
|
|
107
107
|
|
|
108
|
+
# Postgres can copy a whole database in one statement:
|
|
109
|
+
#
|
|
110
|
+
# CREATE DATABASE "caseflow_test_3" TEMPLATE "caseflow_test"
|
|
111
|
+
#
|
|
112
|
+
# That matters because it needs no schema.rb at all. An app whose schema cannot
|
|
113
|
+
# rebuild the database by itself -- custom types, functions, triggers -- can still get
|
|
114
|
+
# per-worker databases this way, cloned from the test database it already has. It is
|
|
115
|
+
# also far faster than replaying a large schema once per worker.
|
|
116
|
+
#
|
|
117
|
+
# Returns true when it cloned, false when this is not Postgres or the source is not
|
|
118
|
+
# there, so the caller can fall back to loading the schema.
|
|
119
|
+
def clone_database(db_config, index)
|
|
120
|
+
return false unless postgres?(db_config)
|
|
121
|
+
|
|
122
|
+
source = db_config.database.to_s.sub(/_#{index}\z/, "")
|
|
123
|
+
target = db_config.database.to_s
|
|
124
|
+
return false if source.empty? || source == target
|
|
125
|
+
|
|
126
|
+
maintenance_connection(db_config) do |connection|
|
|
127
|
+
return false unless database_exists?(connection, source)
|
|
128
|
+
|
|
129
|
+
# A template cannot be copied while anything is connected to it.
|
|
130
|
+
disconnect_everyone_from!(connection, source)
|
|
131
|
+
connection.execute(%(DROP DATABASE IF EXISTS "#{target}"))
|
|
132
|
+
connection.execute(%(CREATE DATABASE "#{target}" TEMPLATE "#{source}"))
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
true
|
|
136
|
+
rescue StandardError
|
|
137
|
+
# Cloning is the fast path, never the only one. Anything unexpected -- a permission,
|
|
138
|
+
# a Postgres version, a connection that will not drop -- falls back to the schema.
|
|
139
|
+
false
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def postgres?(db_config)
|
|
143
|
+
db_config.respond_to?(:adapter) && db_config.adapter.to_s.include?("postgre")
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Postgres will not let you create a database while connected to the one you are
|
|
147
|
+
# copying, so the statements run against the cluster's own maintenance database.
|
|
148
|
+
def maintenance_connection(db_config)
|
|
149
|
+
previous = ::ActiveRecord::Base.connection_db_config
|
|
150
|
+
::ActiveRecord::Base.establish_connection(db_config.configuration_hash.merge(database: "postgres"))
|
|
151
|
+
yield ::ActiveRecord::Base.connection
|
|
152
|
+
ensure
|
|
153
|
+
::ActiveRecord::Base.establish_connection(previous)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def database_exists?(connection, name)
|
|
157
|
+
# Plain Ruby, not #present?: this runs inside a forked worker in somebody else's app,
|
|
158
|
+
# and quietly depending on ActiveSupport being loaded is how a fast path silently
|
|
159
|
+
# turns itself off.
|
|
160
|
+
value = connection.select_value("SELECT 1 FROM pg_database WHERE datname = #{connection.quote(name)}")
|
|
161
|
+
!value.nil?
|
|
162
|
+
rescue StandardError
|
|
163
|
+
false
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def disconnect_everyone_from!(connection, name)
|
|
167
|
+
connection.execute(
|
|
168
|
+
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity " \
|
|
169
|
+
"WHERE datname = #{connection.quote(name)} AND pid <> pg_backend_pid()"
|
|
170
|
+
)
|
|
171
|
+
rescue StandardError
|
|
172
|
+
nil
|
|
173
|
+
end
|
|
174
|
+
|
|
108
175
|
# The :reuse half. Points every database this environment declares at its `_<index>`
|
|
109
176
|
# sibling, and only builds the ones that are not there yet.
|
|
110
177
|
#
|
|
@@ -121,6 +188,10 @@ module Constable
|
|
|
121
188
|
each_worker_config(index) do |db_config|
|
|
122
189
|
next if populated?(db_config)
|
|
123
190
|
|
|
191
|
+
# Clone first: it needs no schema.rb, which is the only thing that works for an
|
|
192
|
+
# app whose schema cannot rebuild the database, and it is faster besides.
|
|
193
|
+
next built << "#{db_config.database} (cloned)" if clone_database(db_config, index)
|
|
194
|
+
|
|
124
195
|
::ActiveRecord::Tasks::DatabaseTasks.reconstruct_from_schema(db_config, nil)
|
|
125
196
|
built << db_config.database
|
|
126
197
|
end
|
data/lib/constable.rb
CHANGED
|
@@ -149,7 +149,7 @@ module Constable
|
|
|
149
149
|
SETTINGS = %i[
|
|
150
150
|
cold_cases warrants warrant_retries auto_relink parole_period
|
|
151
151
|
coverage coverage_threshold coverage_html fail_on_warnings parallel_workers
|
|
152
|
-
worker_databases output tiers
|
|
152
|
+
worker_databases jail_flakes output tiers
|
|
153
153
|
].freeze
|
|
154
154
|
|
|
155
155
|
# `storage` is the one setting that cannot live here, and the reason is ordering, not
|
|
@@ -159,6 +159,7 @@ end
|
|
|
159
159
|
# c.worker_databases = :schema # or :reuse (keep them between runs) / :off
|
|
160
160
|
# c.output = :concise # or :expanded -- a line per test, with timings
|
|
161
161
|
# c.fail_on_warnings = false # CI: fail when the warning count is not trending down
|
|
162
|
+
# c.jail_flakes = false # auto-jail a pass->fail flip (jailed = skipped)
|
|
162
163
|
# c.warrants = false # rerun a failure in isolation before believing it
|
|
163
164
|
# c.warrant_retries = 5
|
|
164
165
|
# c.parole_period = 10 # clean runs before a paroled test releases itself
|
|
@@ -59,6 +59,14 @@ storage:
|
|
|
59
59
|
url: # postgres/mysql only, e.g.
|
|
60
60
|
# postgres://user:pass@host/constable_metadata
|
|
61
61
|
|
|
62
|
+
# Put a test on the docket by itself when it passed last run and failed this one, with no
|
|
63
|
+
# code change in between. Off by default, and the reason is what jailing *does*: a jailed
|
|
64
|
+
# test is skipped on every later run. Turning that on automatically means a suite quietly
|
|
65
|
+
# stops running tests nobody chose to stop running -- and a suite with order-dependent
|
|
66
|
+
# tests, which is most large ones, trips it constantly. `constable test --jail` still
|
|
67
|
+
# jails failures, because that is a thing you asked for.
|
|
68
|
+
jail_flakes: false
|
|
69
|
+
|
|
62
70
|
warrants: false # opt-in flaky detector -- rerun a failure in isolation before believing it
|
|
63
71
|
warrant_retries: 5 # reruns before declaring a warrant (flaky) or a genuine failure
|
|
64
72
|
auto_relink: false # auto-confirm high-confidence rename detection instead of suggesting it
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: constable-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ray Hughes
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-09-
|
|
11
|
+
date: 2026-09-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|