specguard-ruby 0.3.32 → 0.3.34

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: 4e2f8dd966880d2075d3989184c5f9df89d702445edcdf88583c0b8976021a46
4
- data.tar.gz: 4e5d985eda3fb38fc4832412585f6f389a5d9429c915bc9797d02d349576a713
3
+ metadata.gz: 68ead3c5ec7600c6f7443687863287223702e80d321e75ca530b73962fbc6845
4
+ data.tar.gz: baf5152ce8cf58b6d34a2404c2f5c901804886475890f9ed47c90fdae8d0da4e
5
5
  SHA512:
6
- metadata.gz: dfc1753a08f2e9c713c61ed12ac18b557f5e32fbaa3577735383ac6adea9bf237ff6374baf6fe276e6735c0e2da38344574c2ab6b02de1a8f42fe41c3fc51aa9
7
- data.tar.gz: eca2722d9dcfa6117c21ee589b2b2afdfa422c9983a49d66ee80974574e33f8163f10c2ccab65345badef6dec01d94ad25b436980d5d87beb81b224c3c2c6d1b
6
+ metadata.gz: 1e97bb03e49080fae408556ad78b8f41df57e4c502c9d29e082d27b9d484c5c8db7856e0b889dc13b278590b66f5552c0dbeaf74e4a5a84770de5c265f7ab788
7
+ data.tar.gz: af29badb353672a54662352ba3b7f6af3e612ccd31121154828e98bbaf29eafeb3b456f6db8a2cf72577390a5e58009195f37aab9568990ec79209d550a0e572
data/README.md CHANGED
@@ -612,6 +612,29 @@ that runs `rspec --dry-run` to catch an unparseable spec file inherits the key
612
612
  and would otherwise overwrite your suite's real duration and pass/fail picture
613
613
  with zeroes and green.
614
614
 
615
+ **Errors outside examples are reported, and an aborted run is not delivered.**
616
+ RSpec counts failures that belong to no example — a spec file that raises while
617
+ loading, a raising `before(:context)` / `after(:context)` / `after(:suite)`
618
+ hook — in its own summary ("N errors occurred outside of examples") and exits 1,
619
+ but the example rows the formatter captures say nothing about them. When that
620
+ count is positive the formatter prints one `SpecGuard:` line to stderr:
621
+
622
+ ```
623
+ SpecGuard: RSpec reported 1 error outside of examples (a spec file that failed
624
+ to load, or a failing context or suite hook), so the captured run may not be
625
+ the whole suite; look at the RSpec error blocks above.
626
+ ```
627
+
628
+ If the run also captured **zero examples** — the usual result of a load error —
629
+ it is treated as aborted: nothing is POSTed and nothing is written to
630
+ `log/test_results.jsonl`, and the same line adds *No examples were captured, so
631
+ nothing was sent or written.* Delivering it would post an empty run, which the
632
+ platform accepts and shows as the repository's newest run in place of the last
633
+ real one. A run with errors outside examples **and** at least one captured
634
+ example (a hook that raises after the examples passed) is still delivered, with
635
+ the warning. A clean run prints nothing, a dry run keeps its own single line
636
+ above instead, and parallel shards each print their own.
637
+
615
638
  It **never blocks CI.** RSpec does not sandbox formatters — an exception raised
616
639
  in one escapes the runner and takes RSpec's own exit code with it — so every
617
640
  hook rescues, warns once on stderr, and leaves the exit status to your suite
@@ -1146,8 +1169,8 @@ body is encoded on the wire, never what is in it.
1146
1169
 
1147
1170
  | Field | What it holds | Where it comes from |
1148
1171
  | --- | --- | --- |
1149
- | `commit_sha` | the commit the suite ran against | `SPECGUARD_COMMIT_SHA` if you set it, else `GITHUB_SHA`, `CI_COMMIT_SHA`, `CIRCLE_SHA1`, `BUILDKITE_COMMIT`, `GIT_COMMIT`, else `git rev-parse HEAD` |
1150
- | `branch` | the branch name; `null` on a detached checkout | `SPECGUARD_BRANCH` if you set it, else `GITHUB_REF_NAME`, `CI_COMMIT_REF_NAME`, `CIRCLE_BRANCH`, `BUILDKITE_BRANCH`, `GIT_BRANCH`, else `git symbolic-ref --short -q HEAD` |
1172
+ | `commit_sha` | the commit the suite ran against | `SPECGUARD_COMMIT_SHA` if you set it, else — on a same-repository GitHub Actions `pull_request` / `pull_request_target` run — the pushed head sha from the event payload at `GITHUB_EVENT_PATH` (`GITHUB_SHA` there is an ephemeral merge commit), else `GITHUB_SHA`, `CI_COMMIT_SHA`, `CIRCLE_SHA1`, `BUILDKITE_COMMIT`, `GIT_COMMIT`, else `git rev-parse HEAD` |
1173
+ | `branch` | the branch name; `null` on a detached checkout | `SPECGUARD_BRANCH` if you set it, else — on a same-repository GitHub Actions `pull_request` / `pull_request_target` run — `GITHUB_HEAD_REF` (`GITHUB_REF_NAME` there is `<n>/merge`), else `GITHUB_REF_NAME`, `CI_COMMIT_REF_NAME`, `CIRCLE_BRANCH`, `BUILDKITE_BRANCH`, `GIT_BRANCH`, else `git symbolic-ref --short -q HEAD` |
1151
1174
  | `ci_run_id` | your provider's build id, so shards of one run fold together; `null` on a laptop | `SPECGUARD_RUN_ID` if you set it, else `GITHUB_RUN_ID`, `CI_PIPELINE_ID`, `CIRCLE_WORKFLOW_ID`, `BUILDKITE_BUILD_ID`, `BUILD_TAG` |
1152
1175
  | `shard_id` | which slice of that run this process is; `null` when unsharded | `SPECGUARD_SHARD_ID` if you set it, else `TEST_ENV_NUMBER`, `CI_NODE_INDEX`, `CIRCLE_NODE_INDEX`, `BUILDKITE_PARALLEL_JOB` |
1153
1176
  | `duration_seconds` | wall clock for the whole run | measured by the formatter |
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
4
+
3
5
  module SpecGuard
4
6
  module Client
5
7
  # Where the checkout is sitting — which commit, and which branch — asked of
@@ -180,6 +182,12 @@ module SpecGuard
180
182
  # every GitLab, CircleCI, Buildkite and Jenkins run 400ing as a matter of
181
183
  # course — the *default* outcome on four of the five providers this gem is
182
184
  # likely to meet.
185
+ #
186
+ # On a GitHub Actions `pull_request` / `pull_request_target` run from the
187
+ # same repository, the pushed head commit in the event payload
188
+ # (`#pull_request_head`) is consulted *between* `SPECGUARD_COMMIT_SHA` and
189
+ # the rest of this list: `GITHUB_SHA` there is the ephemeral merge commit
190
+ # (the base tip on `_target`), a sha nobody pushed.
183
191
  COMMIT_SHA_KEYS = %w[
184
192
  SPECGUARD_COMMIT_SHA
185
193
  GITHUB_SHA
@@ -195,6 +203,11 @@ module SpecGuard
195
203
  # (`origin/main` on many setups), because `branch` is a free-form string
196
204
  # to the platform and inventing a normalization here would be this gem
197
205
  # guessing at somebody's ref layout.
206
+ #
207
+ # On a same-repository GitHub Actions `pull_request` /
208
+ # `pull_request_target` run, {PULL_REQUEST_BRANCH_KEYS} (`GITHUB_HEAD_REF`,
209
+ # the source branch) is consulted between `SPECGUARD_BRANCH` and the rest
210
+ # of this list, because `GITHUB_REF_NAME` there is `<n>/merge`.
198
211
  BRANCH_KEYS = %w[
199
212
  SPECGUARD_BRANCH
200
213
  GITHUB_REF_NAME
@@ -314,6 +327,22 @@ module SpecGuard
314
327
  # than absent. See trap 1 above.
315
328
  BLANK_MEANS_FIRST_SHARD_KEY = "TEST_ENV_NUMBER"
316
329
 
330
+ # GitHub Actions only: the event that triggered the run, the file holding
331
+ # its payload, and the source branch of a pull request. They are read
332
+ # only together, by `#pull_request_head`, and only when `GITHUB_EVENT_NAME`
333
+ # is one of {PULL_REQUEST_EVENTS}.
334
+ EVENT_NAME_KEYS = %w[GITHUB_EVENT_NAME].freeze
335
+ EVENT_PATH_KEYS = %w[GITHUB_EVENT_PATH].freeze
336
+ PULL_REQUEST_BRANCH_KEYS = %w[GITHUB_HEAD_REF].freeze
337
+
338
+ # The GitHub events whose `GITHUB_SHA` / `GITHUB_REF_NAME` describe a
339
+ # merge ref rather than anything a person pushed.
340
+ PULL_REQUEST_EVENTS = %w[pull_request pull_request_target].freeze
341
+
342
+ # Largest `GITHUB_EVENT_PATH` payload read; a bigger file is treated as
343
+ # unreadable (mirrors specguard-ts).
344
+ MAX_EVENT_PAYLOAD_BYTES = 1024 * 1024
345
+
317
346
  OUTPUT_PATH_KEYS = %w[SPECGUARD_OUTPUT_PATH].freeze
318
347
  LOCAL_OUTPUT_PATH_KEYS = %w[SPECGUARD_LOCAL_OUTPUT_PATH].freeze
319
348
  ENDPOINT_KEYS = %w[SPECGUARD_ENDPOINT].freeze
@@ -377,10 +406,22 @@ module SpecGuard
377
406
  # reason, and so a test does not silently pick up the sha or the branch
378
407
  # of whatever repository it runs in.
379
408
  def initialize(env: ENV, git: GitCheckout)
380
- @commit_sha = first_present(env, COMMIT_SHA_KEYS) || blank_to_nil(git.commit_sha)
409
+ explicit_sha = first_present(env, COMMIT_SHA_KEYS.first(1))
410
+ explicit_branch = first_present(env, BRANCH_KEYS.first(1))
411
+ # One read of the event payload decides branch and sha together, so
412
+ # they cannot disagree; skipped when both were set explicitly.
413
+ pr_head = explicit_sha && explicit_branch ? nil : pull_request_head(env)
414
+
381
415
  # `||` short-circuits, which is the whole of "do not spawn a subprocess
382
416
  # when a provider already answered".
383
- @branch = first_present(env, BRANCH_KEYS) || blank_to_nil(git.branch)
417
+ @commit_sha = explicit_sha ||
418
+ pr_head&.fetch(:sha) ||
419
+ first_present(env, COMMIT_SHA_KEYS) ||
420
+ blank_to_nil(git.commit_sha)
421
+ @branch = explicit_branch ||
422
+ (pr_head && first_present(env, PULL_REQUEST_BRANCH_KEYS)) ||
423
+ first_present(env, BRANCH_KEYS) ||
424
+ blank_to_nil(git.branch)
384
425
  @run_id = first_present(env, RUN_ID_KEYS)
385
426
  @shard_id = resolve_shard_id(env)
386
427
  @output_path = first_present(env, OUTPUT_PATH_KEYS) || DEFAULT_OUTPUT_PATH
@@ -393,6 +434,65 @@ module SpecGuard
393
434
 
394
435
  private
395
436
 
437
+ # The pushed head of a GitHub Actions pull-request run, or nil when it
438
+ # cannot be established as a SAME-REPOSITORY pull request.
439
+ #
440
+ # On `pull_request` / `pull_request_target`, `GITHUB_SHA` is the
441
+ # ephemeral merge commit (the base tip on `_target`) and
442
+ # `GITHUB_REF_NAME` is `<n>/merge` — neither is anything a person pushed.
443
+ # `git rev-parse HEAD` does not rescue it: `actions/checkout` puts the
444
+ # merge commit at HEAD. The real head sha lives only in the event payload
445
+ # at `GITHUB_EVENT_PATH`.
446
+ #
447
+ # A fork PR yields nil: `GITHUB_HEAD_REF` is then the FORK's branch name
448
+ # (`main` on a `contrib:main` PR would be filed under the base
449
+ # repository's own `main`), so the merge-ref behaviour is kept. A
450
+ # missing, unreadable, oversized or garbled payload also yields nil.
451
+ # Never raises and shells out to nothing.
452
+ #
453
+ # @return [Hash{Symbol=>String,nil}, nil] `{sha:}` — sha is nil when the
454
+ # payload's head sha is not 40 hex characters (the branch is still the
455
+ # source branch then) — or nil when this is not a same-repo PR.
456
+ def pull_request_head(env)
457
+ return nil unless PULL_REQUEST_EVENTS.include?(first_present(env, EVENT_NAME_KEYS))
458
+
459
+ path = first_present(env, EVENT_PATH_KEYS)
460
+ return nil if path.nil?
461
+
462
+ pull_request = read_event_payload(path)&.fetch("pull_request", nil)
463
+ return nil unless pull_request.is_a?(Hash)
464
+
465
+ head = pull_request["head"]
466
+ base = pull_request["base"]
467
+ head_repo = repo_full_name(head)
468
+ return nil if head_repo.nil? || head_repo != repo_full_name(base)
469
+
470
+ sha = head["sha"]
471
+ { sha: sha.is_a?(String) && sha.match?(/\A[0-9a-f]{40}\z/i) ? sha : nil }
472
+ rescue ScriptError, StandardError
473
+ nil
474
+ end
475
+
476
+ def repo_full_name(side)
477
+ return nil unless side.is_a?(Hash) && side["repo"].is_a?(Hash)
478
+
479
+ name = side["repo"]["full_name"]
480
+ name.is_a?(String) && !name.empty? ? name : nil
481
+ end
482
+
483
+ # @return [Hash, nil] the parsed payload, or nil if it is absent, not a
484
+ # regular file, larger than {MAX_EVENT_PAYLOAD_BYTES}, or not a JSON
485
+ # object.
486
+ def read_event_payload(path)
487
+ return nil unless File.file?(path)
488
+
489
+ raw = File.open(path, "rb") { |file| file.read(MAX_EVENT_PAYLOAD_BYTES + 1) }
490
+ return nil if raw.nil? || raw.bytesize > MAX_EVENT_PAYLOAD_BYTES
491
+
492
+ parsed = JSON.parse(raw)
493
+ parsed.is_a?(Hash) ? parsed : nil
494
+ end
495
+
396
496
  # {SHARD_ID_KEYS} in order, first non-blank wins — except that a
397
497
  # {BLANK_MEANS_FIRST_SHARD_KEY} which is *set* counts as a hit even when
398
498
  # it is empty, and resolves to `"1"`.
@@ -210,7 +210,7 @@ module SpecGuard
210
210
  # that fallback would then print every message a *second* time alongside the
211
211
  # formatter {#seed} restores. Listening here suppresses it and takes over its
212
212
  # 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
213
+ ::RSpec::Core::Formatters.register self, :example_finished, :stop, :close, :seed, :message, :dump_summary
214
214
 
215
215
  # Prefixed so it is obvious in a CI log which tool is talking, and worded so
216
216
  # a reader knows immediately that it is not their tests that broke.
@@ -230,6 +230,17 @@ module SpecGuard
230
230
  # looking for a bug that is not there.
231
231
  DRY_RUN_WARNING_PREFIX = "SpecGuard: skipped test telemetry for a dry run"
232
232
 
233
+ # The fourth shape: RSpec itself said something went wrong *outside* any
234
+ # example — a spec file that raised while loading, or a raising
235
+ # `before(:context)` / `after(:context)` / `after(:suite)` hook — which
236
+ # `Reporter#notify_non_example_exception` counts into the summary but
237
+ # attributes to no example. Nothing about this formatter failed; the line
238
+ # exists because the captured run may then not be the whole suite, and
239
+ # because a load error leaves RSpec with zero examples, which the platform
240
+ # would otherwise record as the repository's newest (empty) run. See
241
+ # {#warn_errors_outside_examples}.
242
+ OUTSIDE_ERRORS_WARNING_PREFIX = "SpecGuard: RSpec reported"
243
+
233
244
  # The refusal in full, as its own constant rather than a string built at the
234
245
  # call site, because {#skip_dry_run} now needs it in two places: appended to
235
246
  # with the coverage figures on the happy path, and emitted bare as the
@@ -347,6 +358,7 @@ module SpecGuard
347
358
  @error_stream = error_stream
348
359
  @annotations = annotations || SpecGuard::Client::AnnotationLookup.new(root: root)
349
360
  @specs = []
361
+ @errors_outside_examples = 0
350
362
  @warned = false
351
363
  # Stamped here rather than from a `start` hook on purpose. `:start` is not
352
364
  # in this formatter's own registered set — it arrives only because
@@ -523,6 +535,23 @@ module SpecGuard
523
535
  never_fail_the_run { relay_message(notification) }
524
536
  end
525
537
 
538
+ # RSpec's end-of-run summary. The only thing read from it is the count of
539
+ # errors RSpec reported outside any example (a spec file raising at load, a
540
+ # raising context or suite hook): `close` turns a positive count into one
541
+ # `SpecGuard:` line, and into a refusal to deliver when no example was
542
+ # captured. Deliberately a different hook from {#message}, whose relay of
543
+ # RSpec's own messages is left alone.
544
+ #
545
+ # `respond_to?`-guarded: an rspec-core whose summary lacks the count reads
546
+ # as "none reported", which is the pre-existing behaviour.
547
+ def dump_summary(notification)
548
+ never_fail_the_run do
549
+ if notification.respond_to?(:errors_outside_of_examples_count)
550
+ @errors_outside_examples = notification.errors_outside_of_examples_count.to_i
551
+ end
552
+ end
553
+ end
554
+
526
555
  # One example finished — passed, failed or pending. Called for every
527
556
  # example in the run, in the order they complete.
528
557
  def example_finished(notification)
@@ -547,6 +576,7 @@ module SpecGuard
547
576
  @duration_seconds ||= elapsed_since_start
548
577
  deliver(payload)
549
578
  end
579
+ never_fail_the_run { warn_errors_outside_examples }
550
580
  never_fail_the_run { super(notification) }
551
581
  end
552
582
 
@@ -748,6 +778,7 @@ module SpecGuard
748
778
  # it. {#dry_run?} has the argument in full.
749
779
  def deliver(data)
750
780
  return skip_dry_run if dry_run?
781
+ return if aborted_run?
751
782
 
752
783
  configuration = SpecGuard::RSpec.configuration
753
784
  return append_local(data) if blank?(configuration.api_key)
@@ -761,6 +792,39 @@ module SpecGuard
761
792
  fall_back(data, result.reason)
762
793
  end
763
794
 
795
+ # A run that captured nothing while RSpec reported errors outside examples
796
+ # is a run that aborted — in practice a spec file that raised at load, so
797
+ # RSpec ran zero examples. Delivering it would post `specs: []`, which the
798
+ # platform accepts and records as the repository's newest run, replacing the
799
+ # last real one with an empty one. It goes to neither sink (nothing worth
800
+ # replaying: the next run will measure the suite properly). A run with the
801
+ # same errors but at least one captured example — a raising hook after
802
+ # passing examples — is real data and is delivered.
803
+ def aborted_run?
804
+ @errors_outside_examples.positive? && @specs.empty?
805
+ end
806
+
807
+ # The one `SpecGuard:` line for errors RSpec reported outside examples.
808
+ #
809
+ # It does not go through {#emit_warning}: it is not a telemetry failure and
810
+ # must not spend the budget a genuine delivery failure needs. A dry run
811
+ # says nothing here — {#skip_dry_run} already printed the run's one
812
+ # `SpecGuard:` line, and a second would only repeat or contradict it.
813
+ # Parallel shards each print their own line; there is nothing to coordinate.
814
+ def warn_errors_outside_examples
815
+ count = @errors_outside_examples
816
+ return unless count.positive?
817
+ return if dry_run?
818
+
819
+ line = "#{OUTSIDE_ERRORS_WARNING_PREFIX} #{count} #{pluralize(count, 'error')} outside of examples " \
820
+ "(a spec file that failed to load, or a failing context or suite hook), so the " \
821
+ "captured run may not be the whole suite; look at the RSpec error blocks above."
822
+ line += " No examples were captured, so nothing was sent or written." if aborted_run?
823
+ @error_stream.puts(line)
824
+ rescue ScriptError, StandardError
825
+ nil
826
+ end
827
+
764
828
  # The checks worth doing *before* spending the run's wall clock on a request
765
829
  # whose answer is already known.
766
830
  #
@@ -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.32"
10
+ VERSION = "0.3.34"
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.32
4
+ version: 0.3.34
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-10-07 00:00:00.000000000 Z
11
+ date: 2026-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json