specguard-ruby 0.3.33 → 0.3.35

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f0f1cdce1e2e763d549b0ccb1bd5d9a22b1c5a9da9f63645704af3ecd8149de1
4
- data.tar.gz: aa80ba0c1f264a50d307c83fd1a8c9a543d6b5eef75037f53ac5ffef68c5d812
3
+ metadata.gz: b6af757f15b338064d2ca49b34d49e95d8b07a75076d0f930e4a13db88f0018b
4
+ data.tar.gz: d3f3c8e41eb3abc927641245be8f1f33237e1620eb6fa212caefa207f17e88a6
5
5
  SHA512:
6
- metadata.gz: 4673b30c851eb2324326704ab72539c4a909b068f1addc354cb461de216816a05ee2f195b01c70a9c78cadd3810760d6d59c89947e6c56a31971ca6915e658e4
7
- data.tar.gz: fd2ae5c3fafa5ed48b05c71f5109fea22b8b1796b7d0869cf1fb6bcfe2b8adaad440aa79c2cea493c2984bf7fb2a032fe17a7666d19387f4803852692f2f925d
6
+ metadata.gz: 36137b031aac012cee6f6c8c19d5b91961ebc82d5adc40a1c597d881cc58296f9062c91224ff91464ce236ccb76dd0077e47b9c7f2ab22216e599e9886c07f3a
7
+ data.tar.gz: bdb773396d4161b40637e9bb566c1dbd8d9de7d9a2c8d7f158cf97a5b051659f7930420967bc47583c93c6e6754c66a5b5d9b26cda03a464cf3abf97fb8bdf1a
data/README.md CHANGED
@@ -423,6 +423,19 @@ have named, not against a `spec/support/` helper.
423
423
  `file_path` and `line_number` keep meaning the **definition** site — that is the line the `@intent:`
424
424
  annotation is read from.
425
425
 
426
+ **Paths are relative to the git repository root.** Run from a gem or engine subdirectory of a
427
+ monorepo (`gems/a`, `gems/b`), the formatter and the Minitest reporter ship `gems/a/spec/util_spec.rb`
428
+ — not the working-directory-relative `spec/util_spec.rb` that every sibling package would share, which
429
+ made two packages' same-named specs collide on one run id and silently drop one package's rows. The
430
+ root is `git rev-parse --show-toplevel`, resolved once per run; outside a git repository (or when git
431
+ is missing) it is the working directory, and a run from the repository root ships exactly what it
432
+ always did. Annotations are still read from the working directory, so annotated specs in a
433
+ subdirectory run stay annotated. `specguard-lint` output stays working-directory-relative (display
434
+ only). **One-time discontinuity:** if you already run from a subdirectory of a repository, the
435
+ `file_path`, `spec_file_path` and `id` values change on the first run after upgrading
436
+ (`spec/util_spec.rb` → `gems/a/spec/util_spec.rb`), so per-file history for those specs restarts under
437
+ the new names.
438
+
426
439
  An example counts as **annotated** when an `@intent:` sits on its `it` line, or on the comment line
427
440
  immediately above it:
428
441
 
@@ -612,6 +625,29 @@ that runs `rspec --dry-run` to catch an unparseable spec file inherits the key
612
625
  and would otherwise overwrite your suite's real duration and pass/fail picture
613
626
  with zeroes and green.
614
627
 
628
+ **Errors outside examples are reported, and an aborted run is not delivered.**
629
+ RSpec counts failures that belong to no example — a spec file that raises while
630
+ loading, a raising `before(:context)` / `after(:context)` / `after(:suite)`
631
+ hook — in its own summary ("N errors occurred outside of examples") and exits 1,
632
+ but the example rows the formatter captures say nothing about them. When that
633
+ count is positive the formatter prints one `SpecGuard:` line to stderr:
634
+
635
+ ```
636
+ SpecGuard: RSpec reported 1 error outside of examples (a spec file that failed
637
+ to load, or a failing context or suite hook), so the captured run may not be
638
+ the whole suite; look at the RSpec error blocks above.
639
+ ```
640
+
641
+ If the run also captured **zero examples** — the usual result of a load error —
642
+ it is treated as aborted: nothing is POSTed and nothing is written to
643
+ `log/test_results.jsonl`, and the same line adds *No examples were captured, so
644
+ nothing was sent or written.* Delivering it would post an empty run, which the
645
+ platform accepts and shows as the repository's newest run in place of the last
646
+ real one. A run with errors outside examples **and** at least one captured
647
+ example (a hook that raises after the examples passed) is still delivered, with
648
+ the warning. A clean run prints nothing, a dry run keeps its own single line
649
+ above instead, and parallel shards each print their own.
650
+
615
651
  It **never blocks CI.** RSpec does not sandbox formatters — an exception raised
616
652
  in one escapes the runner and takes RSpec's own exit code with it — so every
617
653
  hook rescues, warns once on stderr, and leaves the exit status to your suite
@@ -1157,8 +1193,8 @@ body is encoded on the wire, never what is in it.
1157
1193
 
1158
1194
  | Field | What it holds | Where it comes from |
1159
1195
  | --- | --- | --- |
1160
- | `id` | RSpec's own example id, `./spec/orders_spec.rb[1:2]` — the re-run argument | `example.id` |
1161
- | `spec_file_path` | the spec file that **ran** the example, relative to the project root when it lives under it — an absolute path when it does not, because a spec outside the working directory has no relative name | `metadata[:rerun_file_path]` |
1196
+ | `id` | RSpec's own example id, `./spec/orders_spec.rb[1:2]` — the re-run argument. From a subdirectory of a git repository the path half is repo-relative (`gems/a/spec/orders_spec.rb[1:2]`) | `example.id` |
1197
+ | `spec_file_path` | the spec file that **ran** the example, relative to the git repository root (the working directory when not in a git repo) when it lives under it — an absolute path when it does not, because a spec outside that root has no relative name | `metadata[:rerun_file_path]` |
1162
1198
  | `file_path` | the spec file the example is **defined** in, on the same terms | `metadata[:file_path]` |
1163
1199
  | `line_number` | the line it is defined on | `metadata[:line_number]` |
1164
1200
  | `name` | the composed `describe`/`context`/`it` string | `example.full_description` |
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "open3"
4
+ require_relative "path_root"
4
5
 
5
6
  module SpecGuard
6
7
  module Client
@@ -401,8 +402,8 @@ module SpecGuard
401
402
  # Empty when git cannot say, in which case the caller falls back to `root`
402
403
  # (the top level *is* root for the common case of running from there).
403
404
  def toplevel(root)
404
- out, ok = git(%w[rev-parse --show-toplevel], root)
405
- ok ? real_path(out.strip) : ""
405
+ # The one probe the reporters' path root shares (SPGD-2109).
406
+ PathRoot.toplevel(root) || ""
406
407
  end
407
408
 
408
409
  # The merge base of HEAD with the default branch.
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require "pathname"
5
+
6
+ module SpecGuard
7
+ module Client
8
+ # The directory a run's row paths are relative to: the git toplevel of the
9
+ # working directory, so a run started from a gem/engine subdirectory of a
10
+ # monorepo (`gems/a`, `gems/b`) ships `gems/a/spec/util_spec.rb`, not
11
+ # `spec/util_spec.rb`. Two packages each holding `spec/util_spec.rb` would
12
+ # otherwise ship identical `file_path`/`id` values, and the platform's
13
+ # per-run upsert keeps one writer's rows and drops the other's.
14
+ #
15
+ # This is the Ruby twin of specguard-ts's `resolveRunRoots()` (SPGD-2105).
16
+ # Like that, it is two values, not one: the clients keep reading spec
17
+ # SOURCE (annotation lookup) from the working directory — the cwd-relative
18
+ # spelling — and only SHIP paths relative to the path root. Collapsing the
19
+ # two would resolve a repo-relative spelling against the cwd and ship
20
+ # every annotated spec of a subdirectory run as `unannotated`.
21
+ #
22
+ # Falls back to the working directory itself when git is absent, the
23
+ # directory is not in a repository, or the reported toplevel does not
24
+ # contain it (compared by real path, so a symlinked tmpdir such as macOS
25
+ # `/var` vs `/private/var` does not read as "outside"). In that case —
26
+ # and when the cwd *is* the toplevel — the answer is the cwd string
27
+ # exactly as given, so output is byte-identical to a run with no path
28
+ # root at all.
29
+ module PathRoot
30
+ module_function
31
+
32
+ # The repository's top level, as a real path.
33
+ # @param dir [String] where to ask git from
34
+ # @return [String, nil] nil when git cannot say
35
+ def toplevel(dir)
36
+ out, _err, status = Open3.capture3("git", "rev-parse", "--show-toplevel", chdir: dir)
37
+ return nil unless status.success?
38
+
39
+ top = out.strip
40
+ top.empty? ? nil : real_path(top)
41
+ rescue SystemCallError
42
+ # git not installed / not executable / `dir` unreadable.
43
+ nil
44
+ end
45
+
46
+ # The path root for a run started in `cwd`.
47
+ #
48
+ # Returned in the spelling of `cwd` (an ancestor of it, so absolute paths
49
+ # the test framework built through the same spelling still start with
50
+ # it) whenever that spelling can be recovered; the real toplevel
51
+ # otherwise.
52
+ # @param cwd [String]
53
+ # @return [String]
54
+ def resolve(cwd)
55
+ top = toplevel(cwd)
56
+ return cwd if top.nil?
57
+
58
+ real_cwd = real_path(cwd)
59
+ return cwd if real_cwd == top
60
+ return cwd unless real_cwd.start_with?(top.end_with?("/") ? top : "#{top}/")
61
+
62
+ suffix = real_cwd.delete_prefix(top)
63
+ cwd.end_with?(suffix) ? cwd.delete_suffix(suffix) : top
64
+ end
65
+
66
+ # The working directory relative to the path root; empty when they are
67
+ # the same directory.
68
+ def prefix(cwd, root)
69
+ return "" if cwd == root
70
+
71
+ cwd.delete_prefix("#{root}/")
72
+ end
73
+
74
+ # A path as the test framework spells it relative to the working
75
+ # directory (or absolute, for a file outside it), re-expressed relative
76
+ # to the path root. With an empty `prefix` the two roots coincide and
77
+ # the path is returned untouched.
78
+ #
79
+ # An absolute path under the root loses the root; an absolute path
80
+ # outside it, or a relative one that climbs out of it, keeps today's
81
+ # spelling.
82
+ def rebase(path, root:, prefix:)
83
+ return path if prefix.empty?
84
+
85
+ if path.start_with?("/")
86
+ path.start_with?("#{root}/") ? path.delete_prefix("#{root}/") : path
87
+ else
88
+ joined = Pathname.new(File.join(prefix, path)).cleanpath.to_s
89
+ joined.start_with?("../") || joined == ".." ? path : joined
90
+ end
91
+ end
92
+
93
+ def real_path(path)
94
+ File.realpath(path)
95
+ rescue SystemCallError
96
+ File.expand_path(path)
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "path_root"
4
+
5
+ module SpecGuard
6
+ module Client
7
+ # The class-level root binding shared by the RSpec formatter and the
8
+ # Minitest reporter: `repo_root`, `path_root` and `path_prefix`.
9
+ #
10
+ # Both clients carried the same three methods, differing only in their
11
+ # comments, and every fix to the one-binding discipline had to land once
12
+ # per client (SPGD-1408/1421 for Minitest, SPGD-1429 for RSpec, SPGD-2109
13
+ # in both). One definition here means a binding fix cannot reach one
14
+ # client and miss the other.
15
+ #
16
+ # Use with `extend`, never `include`: the methods run with the CLASS as
17
+ # `self`, so the memos stay class-level ivars (`@repo_root`,
18
+ # `@path_root_binding`) on each client. Specs that save, clear and restore
19
+ # them on the client class depend on that.
20
+ module RootBinding
21
+ # `Dir.pwd` bound once — by the first client construction (whose
22
+ # `#initialize` reads it, SPGD-1421 / SPGD-1429) or the first direct
23
+ # read — never per row: a suite that changes the working directory after
24
+ # the client was built would otherwise relativize one run's rows against
25
+ # two different roots. The memo is what binds it — an unmemoized
26
+ # `Dir.pwd` here re-reads the cwd on every read and is exactly the
27
+ # two-root split the constructors' comments name, with both spellings
28
+ # individually well-formed so nothing downstream can detect the drift.
29
+ # The memo is also the ONE value the annotation lookup reads against:
30
+ # `#initialize` hands it over, so the relativization root and the
31
+ # lookup's read root cannot drift apart the way they did when each bound
32
+ # `Dir.pwd` at its own moment.
33
+ def repo_root
34
+ @repo_root ||= Dir.pwd
35
+ end
36
+
37
+ # The directory every shipped row path is relative to (SPGD-2109): the
38
+ # git toplevel of {#repo_root}, or {#repo_root} itself outside a
39
+ # repository. Derived from the ONE bound {#repo_root} and probed once
40
+ # per binding — the same one-binding discipline, so a run started from a
41
+ # monorepo package subdirectory ships `gems/a/spec/x_spec.rb` rather
42
+ # than a name every sibling package shares. {#repo_root} stays the READ
43
+ # root (the annotation lookup resolves spec source against it); the two
44
+ # are deliberately not one value.
45
+ def path_root
46
+ bound = repo_root
47
+ @path_root_binding = nil unless @path_root_binding&.first == bound
48
+ (@path_root_binding ||= [bound, SpecGuard::Client::PathRoot.resolve(bound)]).last
49
+ end
50
+
51
+ # {#repo_root} relative to {#path_root}; empty when they coincide.
52
+ def path_prefix
53
+ SpecGuard::Client::PathRoot.prefix(repo_root, path_root)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -21,6 +21,8 @@ require_relative "../client/delivery_warning"
21
21
  # `specguard/client` does not require `rspec/core`, so a minitest-only machine
22
22
  # stays loadable.
23
23
  require_relative "../client/annotation_lookup"
24
+ require_relative "../client/path_root"
25
+ require_relative "../client/root_binding"
24
26
 
25
27
  # The Minitest half of `specguard-ruby`. Everything the RSpec formatter knows
26
28
  # about the platform — the envelope's field names, the transport's
@@ -60,23 +62,7 @@ module SpecGuard
60
62
  # whose file cannot be named fits no by-file aggregate on the platform
61
63
  # and would surface as noise. The RSpec formatter's `capture` makes the
62
64
  # equivalent call about a metadata-less example.
63
- class << self
64
- # `Dir.pwd` bound once — by the first `Reporter.new` (whose
65
- # `#initialize` reads it, SPGD-1421) or the first direct read — never
66
- # per row: a reporter constructed inside a test that changes the
67
- # working directory would otherwise relativize one suite's rows
68
- # against two different roots. The memo is what binds it — an
69
- # unmemoized `Dir.pwd` here re-reads the cwd on every row and is
70
- # exactly the two-root split this comment names, with both spellings
71
- # individually well-formed so nothing downstream can detect the
72
- # drift. The memo is also the ONE value the annotation lookup reads
73
- # against: `#initialize` hands it over, so the relativization root
74
- # and the lookup's read root cannot drift apart the way they did when
75
- # each bound `Dir.pwd` at its own moment.
76
- def repo_root
77
- @repo_root ||= Dir.pwd
78
- end
79
- end
65
+ extend SpecGuard::Client::RootBinding
80
66
 
81
67
  def initialize(configuration: SpecGuard.configuration,
82
68
  transport: nil, output: $stderr,
@@ -108,6 +94,8 @@ module SpecGuard
108
94
  # binds one root, hands it to the default lookup, and pins rspec-core's
109
95
  # lazily memoized relativization regex to the same moment.)
110
96
  root = self.class.repo_root
97
+ # Probed at the same moment, once, never mid-run (SPGD-2109).
98
+ self.class.path_root
111
99
  @annotations = annotations || SpecGuard::Client::AnnotationLookup.new(root: root)
112
100
  @clock = clock
113
101
  @rows = []
@@ -264,10 +252,15 @@ module SpecGuard
264
252
  # the RSpec formatter makes about a metadata-less example.
265
253
  return nil if file.to_s.strip.empty? || line.to_i.zero?
266
254
 
255
+ # `read_file` is the cwd-relative spelling the annotation lookup
256
+ # resolves against the read root; `file` is what ships, relative to
257
+ # the path root (SPGD-2109). Equal unless the run started below the
258
+ # git toplevel.
259
+ read_file = read_relative_path(file)
267
260
  file = relative_path(file)
268
261
  name = "#{result.klass}##{result.name}"
269
262
  line_number = line&.to_i
270
- intent = annotation_for(file, line_number)
263
+ intent = annotation_for(read_file, line_number)
271
264
  {
272
265
  # Minitest has no RSpec-style rerun path or nested ids, so the
273
266
  # definition site is the row's id — the run-local primary key for
@@ -328,7 +321,20 @@ module SpecGuard
328
321
  "passed"
329
322
  end
330
323
 
324
+ # Relative to the path root (the git toplevel; the working directory
325
+ # outside a repository) — the identity a platform aggregate groups by.
331
326
  def relative_path(path)
327
+ root = self.class.path_root
328
+ prefix = self.class.path_prefix
329
+ return SpecGuard::Client::PathRoot.rebase(path, root: root, prefix: prefix) unless prefix.empty?
330
+ return path unless path.start_with?("#{root}/")
331
+
332
+ path.delete_prefix("#{root}/")
333
+ end
334
+
335
+ # The cwd-relative spelling: what the annotation lookup reads against
336
+ # the read root.
337
+ def read_relative_path(path)
332
338
  root = self.class.repo_root
333
339
  return path unless path.start_with?("#{root}/")
334
340
 
@@ -49,6 +49,8 @@ require_relative "../rspec"
49
49
  require_relative "../client/configuration"
50
50
  require_relative "../client/transport"
51
51
  require_relative "../client/delivery_warning"
52
+ require_relative "../client/path_root"
53
+ require_relative "../client/root_binding"
52
54
  # Brings the linter's discovery chain with it (Scanner, Finding, Schema). That
53
55
  # direction is safe — `specguard/rspec` does not require `rspec/core`, so the
54
56
  # linter stays loadable without RSpec; it is only the reverse that would break
@@ -210,7 +212,7 @@ module SpecGuard
210
212
  # that fallback would then print every message a *second* time alongside the
211
213
  # formatter {#seed} restores. Listening here suppresses it and takes over its
212
214
  # job — see {#message}, which is that same fallback under this class's roof.
213
- ::RSpec::Core::Formatters.register self, :example_finished, :stop, :close, :seed, :message
215
+ ::RSpec::Core::Formatters.register self, :example_finished, :stop, :close, :seed, :message, :dump_summary
214
216
 
215
217
  # Prefixed so it is obvious in a CI log which tool is talking, and worded so
216
218
  # a reader knows immediately that it is not their tests that broke.
@@ -230,6 +232,17 @@ module SpecGuard
230
232
  # looking for a bug that is not there.
231
233
  DRY_RUN_WARNING_PREFIX = "SpecGuard: skipped test telemetry for a dry run"
232
234
 
235
+ # The fourth shape: RSpec itself said something went wrong *outside* any
236
+ # example — a spec file that raised while loading, or a raising
237
+ # `before(:context)` / `after(:context)` / `after(:suite)` hook — which
238
+ # `Reporter#notify_non_example_exception` counts into the summary but
239
+ # attributes to no example. Nothing about this formatter failed; the line
240
+ # exists because the captured run may then not be the whole suite, and
241
+ # because a load error leaves RSpec with zero examples, which the platform
242
+ # would otherwise record as the repository's newest (empty) run. See
243
+ # {#warn_errors_outside_examples}.
244
+ OUTSIDE_ERRORS_WARNING_PREFIX = "SpecGuard: RSpec reported"
245
+
233
246
  # The refusal in full, as its own constant rather than a string built at the
234
247
  # call site, because {#skip_dry_run} now needs it in two places: appended to
235
248
  # with the coverage figures on the happy path, and emitted bare as the
@@ -262,23 +275,7 @@ module SpecGuard
262
275
  example_started example_passed example_failed example_pending dump_summary
263
276
  ].freeze
264
277
 
265
- class << self
266
- # `Dir.pwd` bound once — by the first formatter construction (whose
267
- # `#initialize` reads it, SPGD-1429) or the first direct read — never
268
- # per row: a suite that changes the working directory after the
269
- # formatter was built would otherwise relativize one run's rows
270
- # against two different roots. The memo is what binds it — an
271
- # unmemoized `Dir.pwd` here re-reads the cwd on every read and is
272
- # exactly the two-root split the constructor's comment names, with
273
- # both spellings individually well-formed so nothing downstream can
274
- # detect the drift. The memo is also the ONE value the default
275
- # annotation lookup reads against: `#initialize` hands it over, so
276
- # the relativization root and the lookup's read root cannot drift
277
- # apart the way they did when each bound `Dir.pwd` at its own moment.
278
- def repo_root
279
- @repo_root ||= Dir.pwd
280
- end
281
- end
278
+ extend SpecGuard::Client::RootBinding
282
279
 
283
280
  # @param output [IO] the stream RSpec hands every formatter. This class's
284
281
  # product is a file, so nothing in the capture path writes here — but the
@@ -343,10 +340,14 @@ module SpecGuard
343
340
  # is left alone: its memoization is correct for what it does, and
344
341
  # overwriting a bound value would second-guess it.
345
342
  root = self.class.repo_root
343
+ # Probed here too, at the same moment, so the git call happens once at
344
+ # construction and never mid-run (SPGD-2109).
345
+ self.class.path_root
346
346
  ::RSpec::Core::Metadata.relative_path_regex
347
347
  @error_stream = error_stream
348
348
  @annotations = annotations || SpecGuard::Client::AnnotationLookup.new(root: root)
349
349
  @specs = []
350
+ @errors_outside_examples = 0
350
351
  @warned = false
351
352
  # Stamped here rather than from a `start` hook on purpose. `:start` is not
352
353
  # in this formatter's own registered set — it arrives only because
@@ -523,6 +524,23 @@ module SpecGuard
523
524
  never_fail_the_run { relay_message(notification) }
524
525
  end
525
526
 
527
+ # RSpec's end-of-run summary. The only thing read from it is the count of
528
+ # errors RSpec reported outside any example (a spec file raising at load, a
529
+ # raising context or suite hook): `close` turns a positive count into one
530
+ # `SpecGuard:` line, and into a refusal to deliver when no example was
531
+ # captured. Deliberately a different hook from {#message}, whose relay of
532
+ # RSpec's own messages is left alone.
533
+ #
534
+ # `respond_to?`-guarded: an rspec-core whose summary lacks the count reads
535
+ # as "none reported", which is the pre-existing behaviour.
536
+ def dump_summary(notification)
537
+ never_fail_the_run do
538
+ if notification.respond_to?(:errors_outside_of_examples_count)
539
+ @errors_outside_examples = notification.errors_outside_of_examples_count.to_i
540
+ end
541
+ end
542
+ end
543
+
526
544
  # One example finished — passed, failed or pending. Called for every
527
545
  # example in the run, in the order they complete.
528
546
  def example_finished(notification)
@@ -547,6 +565,7 @@ module SpecGuard
547
565
  @duration_seconds ||= elapsed_since_start
548
566
  deliver(payload)
549
567
  end
568
+ never_fail_the_run { warn_errors_outside_examples }
550
569
  never_fail_the_run { super(notification) }
551
570
  end
552
571
 
@@ -748,6 +767,7 @@ module SpecGuard
748
767
  # it. {#dry_run?} has the argument in full.
749
768
  def deliver(data)
750
769
  return skip_dry_run if dry_run?
770
+ return if aborted_run?
751
771
 
752
772
  configuration = SpecGuard::RSpec.configuration
753
773
  return append_local(data) if blank?(configuration.api_key)
@@ -761,6 +781,39 @@ module SpecGuard
761
781
  fall_back(data, result.reason)
762
782
  end
763
783
 
784
+ # A run that captured nothing while RSpec reported errors outside examples
785
+ # is a run that aborted — in practice a spec file that raised at load, so
786
+ # RSpec ran zero examples. Delivering it would post `specs: []`, which the
787
+ # platform accepts and records as the repository's newest run, replacing the
788
+ # last real one with an empty one. It goes to neither sink (nothing worth
789
+ # replaying: the next run will measure the suite properly). A run with the
790
+ # same errors but at least one captured example — a raising hook after
791
+ # passing examples — is real data and is delivered.
792
+ def aborted_run?
793
+ @errors_outside_examples.positive? && @specs.empty?
794
+ end
795
+
796
+ # The one `SpecGuard:` line for errors RSpec reported outside examples.
797
+ #
798
+ # It does not go through {#emit_warning}: it is not a telemetry failure and
799
+ # must not spend the budget a genuine delivery failure needs. A dry run
800
+ # says nothing here — {#skip_dry_run} already printed the run's one
801
+ # `SpecGuard:` line, and a second would only repeat or contradict it.
802
+ # Parallel shards each print their own line; there is nothing to coordinate.
803
+ def warn_errors_outside_examples
804
+ count = @errors_outside_examples
805
+ return unless count.positive?
806
+ return if dry_run?
807
+
808
+ line = "#{OUTSIDE_ERRORS_WARNING_PREFIX} #{count} #{pluralize(count, 'error')} outside of examples " \
809
+ "(a spec file that failed to load, or a failing context or suite hook), so the " \
810
+ "captured run may not be the whole suite; look at the RSpec error blocks above."
811
+ line += " No examples were captured, so nothing was sent or written." if aborted_run?
812
+ @error_stream.puts(line)
813
+ rescue ScriptError, StandardError
814
+ nil
815
+ end
816
+
764
817
  # The checks worth doing *before* spending the run's wall clock on a request
765
818
  # whose answer is already known.
766
819
  #
@@ -1048,6 +1101,11 @@ module SpecGuard
1048
1101
  def capture(example)
1049
1102
  metadata = example.metadata || {}
1050
1103
  result = example.execution_result
1104
+ # `read_path` is rspec-core's cwd-relative spelling — what the
1105
+ # annotation lookup resolves against the read root. `file_path` is the
1106
+ # same file as shipped: relative to the path root (SPGD-2109). They are
1107
+ # equal unless the run started below the git toplevel.
1108
+ read_path = read_relative_path(metadata[:file_path])
1051
1109
  file_path = relative_path(metadata[:file_path])
1052
1110
  line_number = metadata[:line_number]
1053
1111
 
@@ -1057,7 +1115,7 @@ module SpecGuard
1057
1115
  # the intent of the examples in it and not the examples themselves. It
1058
1116
  # also routes through {#warn_once}, so the operator hears about it exactly
1059
1117
  # once however many examples are affected.
1060
- intent = never_fail_the_run { @annotations.intent_for(file: file_path, line: line_number) }
1118
+ intent = never_fail_the_run { @annotations.intent_for(file: read_path, line: line_number) }
1061
1119
 
1062
1120
  {
1063
1121
  # Not wrapped in `never_fail_the_run`, unlike the annotation lookup
@@ -1065,7 +1123,7 @@ module SpecGuard
1065
1123
  # memoized string interpolation over a Hash RSpec has already built, and
1066
1124
  # a guard here could only ever add a way to emit an identity-less row
1067
1125
  # silently. The outer guard at `#example_finished` still covers it.
1068
- "id" => example.id,
1126
+ "id" => shipped_id(example, metadata),
1069
1127
  # `|| file_path` rather than a bare read: RSpec defaults
1070
1128
  # `:rerun_file_path` for every example it builds, so the fallback is
1071
1129
  # unreachable in a real run — but if a producer ever hands us metadata
@@ -1095,7 +1153,19 @@ module SpecGuard
1095
1153
  # definition of "relative to the project root" rather than inventing a
1096
1154
  # second one, then drop the `./` so the value matches what a linter, a
1097
1155
  # `git diff --name-only` and the platform all use as a file's identity.
1156
+ #
1157
+ # When the run started below the git toplevel (a monorepo package),
1158
+ # "relative to the project root" means the toplevel, not the cwd: the cwd
1159
+ # spelling is re-expressed against it (SPGD-2109).
1098
1160
  def relative_path(path)
1161
+ read = read_relative_path(path)
1162
+ return read if read.nil?
1163
+
1164
+ SpecGuard::Client::PathRoot.rebase(read, root: self.class.path_root, prefix: self.class.path_prefix)
1165
+ end
1166
+
1167
+ # rspec-core's own cwd-relative spelling, `./` dropped.
1168
+ def read_relative_path(path)
1099
1169
  return nil if path.nil?
1100
1170
 
1101
1171
  string = path.to_s
@@ -1104,6 +1174,20 @@ module SpecGuard
1104
1174
  (::RSpec::Core::Metadata.relative_path(string) || string).sub(%r{\A\./}, "")
1105
1175
  end
1106
1176
 
1177
+ # RSpec's example id is `"#{rerun_file_path}[#{scoped_id}]"`, with the
1178
+ # rerun path cwd-relative. Below the git toplevel the path half is rebased
1179
+ # onto the path root like every other shipped path; at the toplevel (or
1180
+ # outside a repository) the id is RSpec's own, untouched.
1181
+ def shipped_id(example, metadata)
1182
+ id = example.id
1183
+ return id if self.class.path_prefix.empty?
1184
+
1185
+ rerun = metadata[:rerun_file_path].to_s
1186
+ return id if rerun.empty? || !id.to_s.start_with?(rerun)
1187
+
1188
+ "#{relative_path(rerun)}#{id[rerun.length..]}"
1189
+ end
1190
+
1107
1191
  # Appends one line to the replay queue. Opened in append mode and written
1108
1192
  # with a single call so that two suites sharing an output path (parallel CI
1109
1193
  # shards, say) interleave whole runs rather than halves of one.
@@ -7,5 +7,5 @@
7
7
  # framework-scoped version constant a lie, so the number moved here and the
8
8
  # rspec module now points at it for back-compat.
9
9
  module SpecGuard
10
- VERSION = "0.3.33"
10
+ VERSION = "0.3.35"
11
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: specguard-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.33
4
+ version: 0.3.35
5
5
  platform: ruby
6
6
  authors:
7
7
  - specguard Agent
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-04 00:00:00.000000000 Z
11
+ date: 2026-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -59,7 +59,9 @@ files:
59
59
  - lib/specguard/client/ingest_reporter.rb
60
60
  - lib/specguard/client/json_reporter.rb
61
61
  - lib/specguard/client/linter.rb
62
+ - lib/specguard/client/path_root.rb
62
63
  - lib/specguard/client/payload_normalizer.rb
64
+ - lib/specguard/client/root_binding.rb
63
65
  - lib/specguard/client/scanner.rb
64
66
  - lib/specguard/client/schemas/open-test-intent.v1.json
65
67
  - lib/specguard/client/transport.rb