git 5.1.0 → 5.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.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +45 -0
  3. data/CONTRIBUTING.md +185 -165
  4. data/LICENSE +1 -1
  5. data/README.md +151 -151
  6. data/git.gemspec +35 -3
  7. data/lib/git/command_line/base.rb +1 -2
  8. data/lib/git/commands/base.rb +1 -1
  9. data/lib/git/commands/cat_file/raw.rb +8 -7
  10. data/lib/git/repository/branching.rb +79 -5
  11. data/lib/git/version.rb +1 -1
  12. metadata +5 -94
  13. data/.claude/commands/address-copilot-reviews.md +0 -14
  14. data/.claude/settings.json +0 -16
  15. data/.claude/skills +0 -1
  16. data/.commitlintrc.yml +0 -38
  17. data/.dockerignore +0 -27
  18. data/.github/copilot-instructions.md +0 -30
  19. data/.github/hooks/bin-setup-on-worktree.json +0 -11
  20. data/.github/hooks/run-bin-setup-once.sh +0 -20
  21. data/.github/issue_template.md +0 -15
  22. data/.github/prompts/iteratively-address-copilot-reviews.prompt.md +0 -188
  23. data/.github/pull_request_template.md +0 -21
  24. data/.github/skills/breaking-change-analysis/SKILL.md +0 -99
  25. data/.github/skills/ci-cd-troubleshooting/SKILL.md +0 -264
  26. data/.github/skills/command-implementation/REFERENCE.md +0 -994
  27. data/.github/skills/command-implementation/SKILL.md +0 -230
  28. data/.github/skills/command-test-conventions/SKILL.md +0 -664
  29. data/.github/skills/command-yard-documentation/SKILL.md +0 -434
  30. data/.github/skills/dependency-management/SKILL.md +0 -72
  31. data/.github/skills/development-workflow/SKILL.md +0 -512
  32. data/.github/skills/facade-implementation/REFERENCE.md +0 -837
  33. data/.github/skills/facade-implementation/SKILL.md +0 -269
  34. data/.github/skills/facade-test-conventions/SKILL.md +0 -391
  35. data/.github/skills/facade-yard-documentation/SKILL.md +0 -435
  36. data/.github/skills/make-skill-template/SKILL.md +0 -226
  37. data/.github/skills/pr-readiness-review/SKILL.md +0 -205
  38. data/.github/skills/project-context/SKILL.md +0 -306
  39. data/.github/skills/pull-request-review/SKILL.md +0 -168
  40. data/.github/skills/rebase/SKILL.md +0 -148
  41. data/.github/skills/refactor-command-to-commandlineresult/SKILL.md +0 -131
  42. data/.github/skills/release-management/SKILL.md +0 -125
  43. data/.github/skills/resolve-feedback/SKILL.md +0 -288
  44. data/.github/skills/review-arguments-dsl/CHECKLIST.md +0 -788
  45. data/.github/skills/review-arguments-dsl/SKILL.md +0 -214
  46. data/.github/skills/review-cross-command-consistency/SKILL.md +0 -139
  47. data/.github/skills/reviewing-skills/SKILL.md +0 -214
  48. data/.github/skills/rspec-unit-testing-standards/SKILL.md +0 -685
  49. data/.github/skills/tdd-refactor-step/SKILL.md +0 -236
  50. data/.github/skills/test-debugging/SKILL.md +0 -161
  51. data/.github/skills/yard-documentation/SKILL.md +0 -981
  52. data/.github/skills/yard-documentation/element-rules.md +0 -162
  53. data/.github/skills-deprecated/README.md +0 -21
  54. data/.github/skills-deprecated/extract-command-from-lib/SKILL.md +0 -487
  55. data/.github/skills-deprecated/extract-facade-from-base-lib/KEYWORD_ARG_REMEDIATION.md +0 -22
  56. data/.github/skills-deprecated/extract-facade-from-base-lib/SKILL.md +0 -600
  57. data/.github/skills-deprecated/review-backward-compatibility/SKILL.md +0 -275
  58. data/.github/workflows/continuous_integration.yml +0 -358
  59. data/.github/workflows/enforce_conventional_commits.yml +0 -35
  60. data/.github/workflows/experimental_continuous_integration.yml +0 -59
  61. data/.github/workflows/release.yml +0 -52
  62. data/.github/workflows/warm_bundler_caches.yml +0 -82
  63. data/.gitignore +0 -30
  64. data/.husky/commit-msg +0 -1
  65. data/.husky/pre-commit +0 -13
  66. data/.release-please-config.json +0 -36
  67. data/.release-please-manifest.json +0 -3
  68. data/.rspec +0 -2
  69. data/.rubocop.yml +0 -44
  70. data/.rubocop_todo.yml +0 -30
  71. data/.yard-lint.yml +0 -75
  72. data/CLAUDE.md +0 -11
  73. data/Gemfile +0 -22
  74. data/Rakefile +0 -41
  75. data/docker/test/Dockerfile +0 -32
  76. data/docker/test/docker-compose.yml +0 -0
  77. data/package.json +0 -10
  78. data/redesign/1_architecture_existing.md +0 -102
  79. data/redesign/2_architecture_redesign.md +0 -449
  80. data/redesign/3_architecture_implementation.md +0 -1623
  81. data/redesign/Phase 4 - Step A.md +0 -366
  82. data/redesign/Phase 4 - Step B.md +0 -921
  83. data/redesign/Phase 4 - Step C.md +0 -833
  84. data/redesign/beta_release.md +0 -107
  85. data/redesign/branch_parse_refactor_plan.md +0 -163
  86. data/redesign/c1a-public-api-scope.tsv +0 -256
  87. data/redesign/c1c2_audit.md +0 -566
  88. data/redesign/c1c2_bucket6_lib_orphans.md +0 -626
  89. data/redesign/config_design.rb +0 -501
  90. data/redesign/index.md +0 -34
  91. data/redesign/info_object_migration_plan.md +0 -126
  92. data/redesign/integration_test_analysis.md +0 -521
  93. data/redesign/phase-4-step-b-test-audit.tsv +0 -485
  94. data/redesign/remote_refactor_plan.md +0 -164
  95. data/redesign/reverse_dependencies.sql +0 -44
  96. data/tasks/gem_tasks.rake +0 -14
  97. data/tasks/npm_tasks.rake +0 -7
  98. data/tasks/rspec.rake +0 -111
  99. data/tasks/rubocop.rake +0 -5
  100. data/tasks/test_gem.rake +0 -12
  101. data/tasks/yard.rake +0 -57
@@ -1,685 +0,0 @@
1
- ---
2
- name: rspec-unit-testing-standards
3
- description: "Defines RSpec unit testing rules for this project covering structure, naming, setup patterns, stubbing, doubles, coverage, and test reliability. Use when writing, reviewing, or auditing RSpec specs under spec/unit/."
4
- ---
5
-
6
- # RSpec Unit Testing Standards
7
-
8
- These rules govern the structure, organization, and quality of all RSpec unit tests
9
- in this project. Apply them when writing new tests, reviewing existing ones, or
10
- auditing test quality.
11
-
12
- ## Priority Levels
13
-
14
- Use RFC-style priority words to reduce ambiguity for AI behavior:
15
-
16
- - **MUST**: mandatory; do not violate without a documented exception
17
- - **SHOULD**: preferred default; may be overridden when a clearer test requires it
18
-
19
- ## Contents
20
-
21
- - [How to use this skill](#how-to-use-this-skill)
22
- - [Related skills](#related-skills)
23
- - [Structure](#structure)
24
- - [Rule 1 (MUST): One top-level `RSpec.describe` block per class](#rule-1-must-one-top-level-rspecdescribe-block-per-class)
25
- - [Rule 2 (MUST): One `describe` block per public method](#rule-2-must-one-describe-block-per-public-method)
26
- - [Rule 3 (SHOULD): Add `# frozen_string_literal: true` at the top of every spec file](#rule-3-should-add--frozen_string_literal-true-at-the-top-of-every-spec-file)
27
- - [Rule 4 (MUST): Spec file location must mirror source file location](#rule-4-must-spec-file-location-must-mirror-source-file-location)
28
- - [Rule 5 (MUST): `require 'spec_helper'` and only the file(s) under test](#rule-5-must-require-spec_helper-and-only-the-files-under-test)
29
- - [Rule 6 (MUST): Test only through the public interface](#rule-6-must-test-only-through-the-public-interface)
30
- - [Naming and Organization](#naming-and-organization)
31
- - [Rule 7 (SHOULD): Use `described_class`](#rule-7-should-use-described_class)
32
- - [Rule 8 (MUST): `context` blocks describe conditions](#rule-8-must-context-blocks-describe-conditions)
33
- - [Rule 9 (MUST): `it` blocks assert one concept, and the description must match the assertion](#rule-9-must-it-blocks-assert-one-concept-and-the-description-must-match-the-assertion)
34
- - [Rule 10 (SHOULD): Use the standard nesting pattern](#rule-10-should-use-the-standard-nesting-pattern)
35
- - [Setup and Subject](#setup-and-subject)
36
- - [Rule 11 (SHOULD): Use a named `subject` at the top of each `describe #method` block](#rule-11-should-use-a-named-subject-at-the-top-of-each-describe-method-block)
37
- - [Rule 12 (SHOULD): Immediately follow `subject` with `let` defaults](#rule-12-should-immediately-follow-subject-with-let-defaults)
38
- - [Rule 13 (SHOULD): Define `let(:described_instance)` at the top level when multiple `describe` blocks share the same instance](#rule-13-should-define-letdescribed_instance-at-the-top-level-when-multiple-describe-blocks-share-the-same-instance)
39
- - [Rule 14 (SHOULD): Prefer `subject` to represent the method call result](#rule-14-should-prefer-subject-to-represent-the-method-call-result)
40
- - [Rule 15 (SHOULD): Do not use `subject` when testing side effects](#rule-15-should-do-not-use-subject-when-testing-side-effects)
41
- - [Rule 16 (MUST): Use `let`/`let!` for inputs and shared setup; use `before` only for side effects](#rule-16-must-use-letlet-for-inputs-and-shared-setup-use-before-only-for-side-effects)
42
- - [Rule 17 (SHOULD): Keep test setup local; extract only for substantial cross-file reuse](#rule-17-should-keep-test-setup-local-extract-only-for-substantial-cross-file-reuse)
43
- - [Doubles and Stubbing](#doubles-and-stubbing)
44
- - [Rule 18 (MUST): Stub calls to non-trivial external objects](#rule-18-must-stub-calls-to-non-trivial-external-objects)
45
- - [Rule 19 (MUST): Use `allow` for incidental stubs; use `expect` for behavioral assertions](#rule-19-must-use-allow-for-incidental-stubs-use-expect-for-behavioral-assertions)
46
- - [Rule 20 (MUST): Use verifying doubles](#rule-20-must-use-verifying-doubles)
47
- - [Coverage](#coverage)
48
- - [Rule 21 (MUST): Achieve 100% branch-level coverage](#rule-21-must-achieve-100-branch-level-coverage)
49
- - [Rule 22 (MUST): Error assertions must specify both the error class and a message pattern](#rule-22-must-error-assertions-must-specify-both-the-error-class-and-a-message-pattern)
50
- - [Rule 23 (MUST): Test edge cases within the relevant `context` block](#rule-23-must-test-edge-cases-within-the-relevant-context-block)
51
- - [Rule 24 (MUST): Assert observable behavior, not implementation details](#rule-24-must-assert-observable-behavior-not-implementation-details)
52
- - [Anti-pattern: structural identity and constant-existence tests](#anti-pattern-structural-identity-and-constant-existence-tests)
53
- - [Test Reliability](#test-reliability)
54
- - [Rule 25 (MUST): Keep unit tests deterministic](#rule-25-must-keep-unit-tests-deterministic)
55
- - [Rule 26 (MUST): Isolate and restore global/process state](#rule-26-must-isolate-and-restore-globalprocess-state)
56
- - [Rule 27 (MUST): Tests must be order-independent](#rule-27-must-tests-must-be-order-independent)
57
- - [Rule 28 (MUST): Avoid `allow_any_instance_of` and `receive_message_chain`](#rule-28-must-avoid-allow_any_instance_of-and-receive_message_chain)
58
- - [Verification](#verification)
59
- - [Output](#output)
60
-
61
- ## How to use this skill
62
-
63
- These rules apply to all RSpec unit specs under `spec/unit/`. Extend this baseline
64
- with domain-specific rules from related skills as needed.
65
-
66
- Adoption and enforcement notes:
67
-
68
- - Apply these rules as hard requirements for new and modified unit specs.
69
- - Legacy specs may violate some rules; treat those as incremental cleanup work.
70
- - Branch and line coverage are both reported by SimpleCov in this repository.
71
- - `minimum_coverage: { line: 100, branch: 100 }` is configured and enforced: a full
72
- `rake spec:unit` run fails when either threshold is missed, and CI fails the pull
73
- request with it. Rule 21 is a build gate, not just a review check. See the
74
- [Test coverage policy](../../../CONTRIBUTING.md#test-coverage-policy) in
75
- `CONTRIBUTING.md` for the full policy.
76
- - Focused runs (`SPEC=<glob> rake spec:unit`, or `rspec <file>`) report coverage but
77
- do not fail on it, since they load all of `lib/` while exercising only a slice.
78
-
79
- ## Related skills
80
-
81
- - [Command Test Conventions](../command-test-conventions/SKILL.md) — additional conventions
82
- for `Git::Commands::*` unit and integration specs, built on top of these rules
83
- - [Development Workflow](../development-workflow/SKILL.md) — TDD process that governs
84
- when and how tests are written
85
- - [PR Readiness Review](../pr-readiness-review/SKILL.md) — final quality gate that
86
- verifies test compliance before opening a pull request
87
- - [Pull Request Review](../pull-request-review/SKILL.md) — PR review process that
88
- checks test quality against these standards
89
-
90
- ## Structure
91
-
92
- ### Rule 1 (MUST): One top-level `RSpec.describe` block per class
93
-
94
- Use the class constant directly:
95
-
96
- ```ruby
97
- RSpec.describe Git::CommandLine::Capturing do
98
- ```
99
-
100
- Never use a string in place of the constant, even for backward-compat aliases:
101
-
102
- ```ruby
103
- # Bad — string describe; described_class is unavailable, coverage tooling may not
104
- # map the spec to the source file, and typos go undetected at load time.
105
- RSpec.describe 'Git::CommandLineResult' do
106
- ```
107
-
108
- If the constant is a backward-compat alias (e.g. `Git::CommandLineResult =
109
- Git::CommandLine::Result`), use the alias constant itself as the describe argument —
110
- the alias is a real Ruby constant and loads without issue. The test content should
111
- verify that the alias points to the correct target using object identity (`be`),
112
- which would not be caught implicitly by a `NameError` on the canonical constant:
113
-
114
- ```ruby
115
- RSpec.describe Git::CommandLineResult do
116
- it 'is a backward-compatible alias for Git::CommandLine::Result' do
117
- expect(described_class).to be(Git::CommandLine::Result)
118
- end
119
- end
120
- ```
121
-
122
- Do not test `#initialize` or other behavior here — that is already covered by the spec for the canonical class.
123
-
124
- ### Rule 2 (MUST): One `describe` block per public method
125
-
126
- Use `#method_name` for instance methods and `.method_name` for class methods.
127
- Include `#initialize`:
128
-
129
- ```ruby
130
- describe '#call' do ...
131
- describe '.build' do ...
132
- describe '#initialize' do ...
133
- ```
134
-
135
- **Inherited `#initialize` in concrete subclasses (SHOULD):** If a class is
136
- directly instantiated by callers but does not override `#initialize`, its spec
137
- SHOULD still include a `describe '#initialize'` block using the minimal
138
- `have_attributes` form (see Rule 13). This serves two purposes:
139
-
140
- 1. The spec is self-contained documentation of the constructor signature — a reader
141
- does not need to consult the ancestor's spec to know what arguments the class
142
- accepts or what attributes it exposes.
143
- 2. It guards against an accidental `def initialize` override in the subclass that
144
- silently drops or misroutes an argument, which the ancestor's spec would not
145
- catch.
146
-
147
- Omit the inherited `#initialize` block only for abstract or internal classes that
148
- callers never instantiate directly — those are sufficiently covered by the
149
- ancestor's spec alone.
150
-
151
- ### Rule 3 (SHOULD): Add `# frozen_string_literal: true` at the top of every spec file
152
-
153
- Matches project-wide convention and catches accidental string mutation.
154
-
155
- ### Rule 4 (MUST): Spec file location must mirror source file location
156
-
157
- `lib/git/foo/bar.rb` maps to `spec/unit/git/foo/bar_spec.rb`. Deviating from this
158
- makes specs hard to find and breaks coverage mapping.
159
-
160
- ### Rule 5 (MUST): `require 'spec_helper'` and only the file(s) under test
161
-
162
- Every unit spec MUST start with `require 'spec_helper'`, then require only the
163
- Ruby file(s) it directly tests. Avoid requiring unrelated libraries or classes —
164
- doing so creates false coupling where a rename or move breaks specs that don't even
165
- test that class.
166
-
167
- ### Rule 6 (MUST): Test only through the public interface
168
-
169
- Never call private methods directly in tests. If private logic is hard to reach
170
- through the public interface, stop and propose one of these remedies to the user:
171
-
172
- - **Extract a class** — move the logic to a new class with its own public interface.
173
- - **Make the method public** — promote it if it is genuinely part of the contract.
174
- - **Redesign the public API** — split the public method into smaller public steps.
175
-
176
- Never use `send`, `instance_variable_get`, or `__send__` to reach private state.
177
-
178
- ## Naming and Organization
179
-
180
- ### Rule 7 (SHOULD): Use `described_class`
181
-
182
- Use `described_class` instead of repeating the class name inside the describe block:
183
-
184
- ```ruby
185
- subject(:result) { described_class.new(args).call }
186
- ```
187
-
188
- ### Rule 8 (MUST): `context` blocks describe conditions
189
-
190
- Use prefixes "when", "with", or "without". Nest them under the relevant `describe`
191
- block for different option combinations, input states, or environmental conditions:
192
-
193
- ```ruby
194
- context 'when the command fails' do ...
195
- context 'with the :force option' do ...
196
- context 'without a timeout' do ...
197
- ```
198
-
199
- ### Rule 9 (MUST): `it` blocks assert one concept, and the description must match the assertion
200
-
201
- Each example tests a single logical behavior. A test described as "raises
202
- ArgumentError when options conflict" must verify both the error class and a message
203
- pattern — not just that any error was raised. Multiple `expect` calls are acceptable
204
- if they all verify the same behavior — that is, a single application code change
205
- would cause them all to fail together. For example, asserting the type, status, and
206
- contents of a single return value is one concept ("the result carries the right
207
- data"), not five separate behaviors:
208
-
209
- ```ruby
210
- # Good — all assertions verify one concept: the returned result is correct
211
- it 'returns a result with the failure details' do
212
- result = described_instance.run('status', raise_on_failure: false)
213
- expect(result).to be_a(Git::CommandLineResult)
214
- expect(result.status.success?).to be false
215
- expect(result.status.exitstatus).to eq(1)
216
- expect(result.stdout).to eq("modified: foo.rb\n")
217
- expect(result.stderr).to eq('fatal: not a git repository')
218
- end
219
- ```
220
-
221
- ### Rule 10 (SHOULD): Use the standard nesting pattern
222
-
223
- ```
224
- describe #method
225
- context "when X"
226
- context "with Y option"
227
- it "does Z"
228
- ```
229
-
230
- > **Exception:** Simple methods with a single execution path and no meaningful input
231
- > variations do not need `context` blocks. A `describe #method` block containing
232
- > `it` directly is fine when there are no conditions worth naming.
233
-
234
- ## Setup and Subject
235
-
236
- ### Rule 11 (SHOULD): Use a named `subject` at the top of each `describe #method` block
237
-
238
- Name the subject after what is returned. For simple cases, inline construction is
239
- fine:
240
-
241
- ```ruby
242
- subject(:result) { described_class.new(command, options).call }
243
- ```
244
-
245
- When construction is complex or shared across multiple `describe` blocks, separate
246
- construction into a `let` and reference it from `subject` (see Rule 14). Naming
247
- the subject allows both `is_expected.to` one-liners and `expect(result).to` in
248
- more descriptive examples. Do not override `subject` in nested `context` blocks
249
- — vary behavior by overriding `let` inputs instead.
250
-
251
- ### Rule 12 (SHOULD): Immediately follow `subject` with `let` defaults
252
-
253
- `subject` must be the first declaration in a `describe` block. Do not place `let`
254
- declarations above `subject` — doing so buries the call under test and inverts the
255
- natural reading order (what is being tested → what inputs it receives).
256
-
257
- Define `let` defaults for all inputs and arguments immediately after `subject`.
258
- Nested `context` blocks override only the `let` values relevant to that scenario —
259
- leave everything else at its default:
260
-
261
- ```ruby
262
- # Good — subject first, then let defaults
263
- describe '#call' do
264
- subject(:result) { described_class.new(command, options).call }
265
-
266
- let(:command) { ['git', 'status'] }
267
- let(:options) { {} }
268
-
269
- context 'when options include timeout' do
270
- let(:options) { { timeout: 10 } }
271
- it { is_expected.to ... }
272
- end
273
- end
274
-
275
- # Bad — let declarations above subject obscure what is under test
276
- describe '#call' do
277
- let(:command) { ['git', 'status'] }
278
- let(:options) { {} }
279
- subject(:result) { described_class.new(command, options).call }
280
- end
281
- ```
282
-
283
- ### Rule 13 (SHOULD): Define `let(:described_instance)` at the top level when multiple `describe` blocks share the same instance
284
-
285
- When two or more `describe #method` blocks instantiate the class identically, define
286
- a single `let(:described_instance)` at the top of the `RSpec.describe` block instead
287
- of repeating construction in every `subject`. Each `describe` block then defines
288
- `subject` as the method call result, referencing `described_instance`:
289
-
290
- ```ruby
291
- RSpec.describe Git::CommandLine do
292
- let(:command) { ['git', 'status'] }
293
- let(:options) { {} }
294
- let(:described_instance) { described_class.new(command, options) }
295
-
296
- describe '#call' do
297
- subject(:result) { described_instance.call }
298
- ...
299
- end
300
-
301
- describe '#to_s' do
302
- subject(:result) { described_instance.to_s }
303
- ...
304
- end
305
- end
306
- ```
307
-
308
- Guidelines:
309
-
310
- - **Use `let`, not `subject`** — avoids making it the implicit assertion target.
311
- - **Reference only `let`-defined arguments** — no inline literals; nested contexts
312
- must be able to override individual inputs.
313
- - **Omit when construction is trivial** or varies between methods.
314
- - For `#initialize`, alias it: `subject(:instance) { described_instance }`.
315
- - **When `#initialize` only stores arguments via `attr_reader`, use a single
316
- `have_attributes` example** — separate `it` blocks for each attribute add noise
317
- without isolation benefit when there is no conditional logic to branch on:
318
-
319
- ```ruby
320
- describe '#initialize' do
321
- subject(:instance) { described_instance }
322
-
323
- it 'stores all constructor arguments' do
324
- expect(instance).to have_attributes(
325
- env: env,
326
- binary_path: binary_path,
327
- global_opts: global_opts,
328
- logger: logger
329
- )
330
- end
331
- end
332
- ```
333
-
334
- Use separate `it` blocks only when `#initialize` performs validation or
335
- conditional logic — each branch then deserves its own example.
336
-
337
- ### Rule 14 (SHOULD): Prefer `subject` to represent the method call result
338
-
339
- Prefer `subject` as the return value of the public method under test. For simple
340
- cases, inline construction is fine (see Rule 11). When construction is complex or
341
- reused across multiple `describe` blocks, separate construction into a `let` so
342
- that nested `context` blocks can override individual inputs without duplicating
343
- the whole call:
344
-
345
- ```ruby
346
- # Good — complex construction is separated so inputs can be overridden
347
- let(:instance) { described_class.new(complex, args, here) }
348
- subject(:result) { instance.call }
349
-
350
- # Avoid when construction is complex — nested contexts cannot vary individual args
351
- subject(:result) { described_class.new(complex, args, here).call }
352
- ```
353
-
354
- > **Exception:** `#initialize` tests — the constructed object *is* the return value,
355
- > so `subject` should be the instance (see Rule 13 aliasing guideline).
356
-
357
- ### Rule 15 (SHOULD): Do not use `subject` when testing side effects
358
-
359
- Use `change` and `raise_error` matchers instead of manual before/after assertions:
360
-
361
- ```ruby
362
- # Good
363
- expect { action }.to change(collection, :size).by(1)
364
- expect { action }.to change(obj, :attr).from('old').to('new')
365
-
366
- # Bad
367
- before_count = collection.size
368
- action
369
- expect(collection.size).to eq(before_count + 1)
370
- ```
371
-
372
- ### Rule 16 (MUST): Use `let`/`let!` for inputs and shared setup; use `before` only for side effects
373
-
374
- Use `let` for values that are referenced directly in examples. Use `let!` when a
375
- value must exist before the example runs but is not referenced directly (e.g., a
376
- precondition that other code depends on). Use `before` only for imperative side
377
- effects (e.g., creating files, setting env vars). Avoid instance variables set in
378
- `before` blocks.
379
-
380
- ### Rule 17 (SHOULD): Keep test setup local; extract only for substantial cross-file reuse
381
-
382
- `shared_context` is appropriate when identical multi-step environment setup must be
383
- shared across multiple spec files and duplicating it inline would be substantial.
384
- Do not use `shared_context` within a single spec file — use the `let` hierarchy
385
- instead. A unit test that needs a `shared_context` to run is almost certainly an
386
- integration test; move it to `spec/integration/`.
387
-
388
- The same restraint applies to shared helper methods. Do not extract a helper to a
389
- support file solely because two spec files contain methods with similar structure.
390
- If the helpers construct different doubles, mock different classes, or carry
391
- different default attributes, the similarity is incidental — not meaningful
392
- duplication. Each spec file should be self-contained and readable without jumping
393
- to external helpers. Extract only when the helpers are truly identical and used
394
- across three or more spec files.
395
-
396
- Defining shared contexts:
397
-
398
- - Define in `spec/support/contexts/`, named after the context string.
399
- - Use only `let`, `let!`, and `before`/`after` — no `subject`, doubles, or
400
- `described_instance`.
401
- - Reference only `let`-defined values; never inline literals.
402
-
403
- Consuming: always use explicit `include_context 'name'` — never metadata-based
404
- auto-inclusion.
405
-
406
- ## Doubles and Stubbing
407
-
408
- ### Rule 18 (MUST): Stub calls to non-trivial external objects
409
-
410
- Stub anything whose real involvement would make the test cross a unit boundary
411
- (e.g., `ProcessExecuter`, file system, network). Do not stub simple value types
412
- like `String`, `Integer`, or `Array`. The guiding question: would running the real
413
- thing make this test not a unit test?
414
-
415
- > **Exception:** Simple value objects with no IO (e.g., `Git::CommandLineResult`)
416
- > can be used directly if doing so keeps the test a unit test.
417
-
418
- ### Rule 19 (MUST): Use `allow` for incidental stubs; use `expect` for behavioral assertions
419
-
420
- Reserve `expect(...).to receive(...)` for cases where the call itself is the
421
- behavior under test. Use `allow` for everything else — overusing `expect` stubs
422
- creates over-specified tests that break on irrelevant refactors:
423
-
424
- ```ruby
425
- # Good — incidental stub; test verifies the return value
426
- allow(executer).to receive(:run).and_return(result)
427
- expect(subject).to eq(expected)
428
-
429
- # Good — the call itself is what's being verified
430
- expect(executer).to receive(:run).with('git', 'status')
431
- subject
432
-
433
- # Good — the call is behavioral and arguments require destructuring;
434
- # use a block with nested expects when .with() cannot cleanly express
435
- # the assertion (e.g., complex kwargs mixed with positional args)
436
- expect(executer).to receive(:run) do |*args, **opts|
437
- expect(opts[:timeout_after]).to eq(5)
438
- mock_result
439
- end
440
- subject
441
- ```
442
-
443
- ### Rule 20 (MUST): Use verifying doubles
444
-
445
- Use `instance_double` / `class_double` rather than plain `double`:
446
-
447
- ```ruby
448
- # Good — ProcessExecuter.run is a class method; use class_double
449
- let(:process_executer) { class_double(ProcessExecuter) }
450
-
451
- # Bad
452
- let(:process_executer) { double('ProcessExecuter') }
453
- ```
454
-
455
- > **Exceptions:**
456
- >
457
- > - Plain `double` is acceptable when the class being stubbed cannot be
458
- > loaded in the test environment (e.g., a C extension or optional dependency
459
- > not available in CI).
460
- > - Plain `double` is acceptable for duck-type collaborators where there is no
461
- > single concrete class to verify against (e.g., `execution_context` in
462
- > command specs implements a duck-type interface, not one specific class).
463
- > - Plain `double` is acceptable when the class delegates methods via
464
- > `SimpleDelegator`, `Delegator`, or `method_missing`. `instance_double`
465
- > only verifies methods that `method_defined?` returns `true` for, so
466
- > delegated methods (e.g., `signaled?`, `exitstatus` forwarded from
467
- > `Process::Status`) would be incorrectly rejected.
468
- >
469
- > In all cases, document the reason with an inline comment so the use of
470
- > `double` is not mistaken for carelessness:
471
- >
472
- > ```ruby
473
- > # Duck-type collaborator: command specs depend on the #command_capturing
474
- > # interface, not a single concrete ExecutionContext class.
475
- > let(:execution_context) { double('ExecutionContext') }
476
- >
477
- > # Plain double: ProcessExecuter result classes delegate to Process::Status
478
- > # via SimpleDelegator/method_missing, so instance_double cannot verify the
479
- > # delegated interface (signaled?, exitstatus, etc.).
480
- > double('ProcessExecuter::ResultWithCapture', success?: true, signaled?: false)
481
- > ```
482
-
483
- ## Coverage
484
-
485
- ### Rule 21 (MUST): Achieve 100% branch-level coverage
486
-
487
- Every conditional path through the public interface must be exercised by at least
488
- one example. If a branch cannot be reached through the public interface, that is a
489
- design smell.
490
-
491
- This is enforced by the build: a full `rake spec:unit` run fails below 100% line or
492
- branch coverage, and the failure output names each uncovered line and branch.
493
-
494
- When a branch is hard to cover, apply these in order:
495
-
496
- 1. **Reach it through the public interface** — if it is reachable, test it.
497
- 2. **Delete it** — a branch unreachable through the public interface is usually dead
498
- code. Removing it is preferable to excluding it.
499
- 3. **Exclude it with `# simplecov:disable`** — the last resort, per the exception below.
500
-
501
- > **Exception:** Defensive guards that require breaking OS-level invariants to reach
502
- > (e.g., `raise "unreachable"` that would require triggering out-of-memory) may be
503
- > excluded. Mark them explicitly with a `# simplecov:disable` directive carrying a
504
- > reason — never leave branches silently uncovered. Cover the smallest possible span,
505
- > and expect a reviewer to challenge it. `lib/` currently contains no coverage
506
- > directives.
507
-
508
- Prefer the inline form for a single line. It applies only to its own line and needs no
509
- matching `enable`, so a region can never be left open by accident:
510
-
511
- ```ruby
512
- raise 'unreachable' # simplecov:disable defensive guard; only reachable on OOM
513
- ```
514
-
515
- Use the block form only for a genuine span, and close it explicitly:
516
-
517
- ```ruby
518
- # simplecov:disable branch platform-specific fallback; not reachable on MRI
519
- ...
520
- # simplecov:enable branch
521
- ```
522
-
523
- Always name the narrowest criterion that solves the problem — `line`, `branch`,
524
- `method`, or a comma-separated combination — and write the reason after it, so the
525
- required justification lives in the directive.
526
-
527
- > **Review check:** verify the criterion is spelled exactly. A word SimpleCov does not
528
- > recognize is parsed as free-form reason text, which silently widens the directive to
529
- > all three criteria rather than failing.
530
-
531
- > **Coverage is a floor on evidence, not a proof of correctness.** Never write an
532
- > example whose only purpose is to execute a line. Such a test turns the report green
533
- > while violating [Rule 24](#rule-24-must-assert-observable-behavior-not-implementation-details),
534
- > and must be rejected in review.
535
-
536
- ### Rule 22 (MUST): Error assertions must specify both the error class and a message pattern
537
-
538
- `raise_error(ErrorClass)` alone is underspecified — any instance of that class
539
- satisfies it regardless of cause. This applies equally when using the block form
540
- to verify properties of the raised error object: the block does not substitute for
541
- a message check, and RSpec allows both together.
542
-
543
- ```ruby
544
- # Good — class + message pattern only
545
- expect { action }.to raise_error(ArgumentError, /cannot combine :force and :dry_run/)
546
-
547
- # Good — class + message pattern + block to verify error properties
548
- expect { action }.to raise_error(Git::FailedError, /git.*status/) do |error|
549
- expect(error.result.status.exitstatus).to eq(1)
550
- end
551
-
552
- # Bad — passes for any ArgumentError, even unrelated ones
553
- expect { action }.to raise_error(ArgumentError)
554
-
555
- # Bad — block form is not an exception to the message requirement;
556
- # still passes for any Git::FailedError regardless of cause
557
- expect { action }.to raise_error(Git::FailedError) do |error|
558
- expect(error.result.status.exitstatus).to eq(1)
559
- end
560
- ```
561
-
562
- > **Version-variance exception:** When the error message is produced by an external
563
- > library or by git itself and is likely to vary across git versions, use the loosest
564
- > regexp that still distinguishes this error from an unrelated one. Anchor the pattern
565
- > on something stable — the invalid input value, the subcommand name, or a keyword
566
- > that appears in all known git versions. Never omit the message check entirely:
567
- >
568
- > ```ruby
569
- > # Good — stable anchor, tolerates phrasing differences across git versions
570
- > expect { command.call('nonexistent.txt') }
571
- > .to raise_error(Git::FailedError, /nonexistent\.txt/)
572
- >
573
- > # Good — subcommand keyword is stable across versions
574
- > expect { command.call('bad-ref') }
575
- > .to raise_error(Git::FailedError, /bad-ref/)
576
- >
577
- > # Bad — passes for any Git::FailedError regardless of cause
578
- > expect { command.call('nonexistent.txt') }.to raise_error(Git::FailedError)
579
- > ```
580
-
581
- ### Rule 23 (MUST): Test edge cases within the relevant `context` block
582
-
583
- `nil`, empty collections, and boundary values belong alongside the normal cases for
584
- the same method and condition — not in a separate "edge cases" block at the bottom.
585
-
586
- ### Rule 24 (MUST): Assert observable behavior, not implementation details
587
-
588
- Every `expect` must verify a meaningful outcome — a return value, a raised error,
589
- a state change, or a message sent to a collaborator. Do not write assertions that
590
- merely confirm the code runs without error or that mirror the implementation:
591
-
592
- ```ruby
593
- # Good — asserts the meaningful return value
594
- expect(subject).to eq('v2.43.0')
595
-
596
- # Bad — passes for any non-nil return, verifies nothing useful
597
- expect(subject).not_to be_nil
598
- ```
599
-
600
- **Decision test — independent failure mode:** Before writing or approving an
601
- assertion, ask: "What application code change would cause *only this test* to fail?"
602
- If the answer is "nothing — every other test would also fail first," the assertion
603
- is redundant and should be removed.
604
-
605
- #### Anti-pattern: structural identity and constant-existence tests
606
-
607
- Do not write tests that only verify a constant exists, that a `require` loaded
608
- successfully, or that a namespace is a `Module` vs a `Class`:
609
-
610
- ```ruby
611
- # Bad — proves nothing about behavior; any real test that uses the class
612
- # would fail with NameError first if the constant were missing
613
- it 'exposes Capturing' do
614
- expect(Git::CommandLine::Capturing).to be_a(Class)
615
- end
616
-
617
- # Bad — structural choice, not observable behavior from a caller's perspective
618
- it 'is a module (not a class)' do
619
- expect(described_class).to be_a(Module)
620
- end
621
- ```
622
-
623
- Constant presence is proven implicitly by every test that instantiates or calls
624
- the class. These tests add no coverage of behavior and should not be written or
625
- approved in review.
626
-
627
- ## Test Reliability
628
-
629
- ### Rule 25 (MUST): Keep unit tests deterministic
630
-
631
- Do not depend on real time, randomness, sleep-based timing, or external process
632
- timing. Stub or freeze `Time.now`, `Process.clock_gettime`, `SecureRandom`, and
633
- `rand` so results are repeatable. Never use `sleep` in unit tests.
634
-
635
- ### Rule 26 (MUST): Isolate and restore global/process state
636
-
637
- Unit tests must not leak state across examples. If a test modifies process or global
638
- state (for example `ENV`, current working directory, locale, or global config),
639
- restore that state before the example ends.
640
-
641
- ### Rule 27 (MUST): Tests must be order-independent
642
-
643
- Every unit test must pass when run alone and when run in randomized order. Do not
644
- rely on side effects from other examples, files, or execution order.
645
-
646
- ### Rule 28 (MUST): Avoid `allow_any_instance_of` and `receive_message_chain`
647
-
648
- Do not use `allow_any_instance_of` or `receive_message_chain` in unit tests. They
649
- hide object boundaries and create brittle tests.
650
-
651
- > **Exception:** Allowed only when there is no practical seam and refactoring is out
652
- > of scope for the current change. If used, add an inline comment explaining why and
653
- > prefer introducing a seam in a follow-up change.
654
-
655
- ## Verification
656
-
657
- After writing or modifying tests, verify compliance before finishing:
658
-
659
- 1. **Run the specs:** `bundle exec rspec spec/unit/path/to_spec.rb`
660
- 2. **Check branch coverage** meets 100% (Rule 21) — open `coverage/index.html` and
661
- confirm no uncovered lines or branches in the class under test. A focused run
662
- reports coverage but does not fail on it; `bundle exec rake spec:unit` is the
663
- authority and will fail below 100%, naming each uncovered line and branch.
664
- 3. **Re-check MUST rules.** Scan the spec against every MUST rule. Fix violations.
665
- 4. **Run in random order** (Rule 27): `bundle exec rspec spec/unit/path/to_spec.rb --order rand`
666
-
667
- Repeat until all checks pass.
668
-
669
- ## Output
670
-
671
- **When writing new tests**, produce the spec file and run through the Verification
672
- checklist above. No additional structured output is required.
673
-
674
- **When reviewing or auditing** existing tests, produce the following:
675
-
676
- 1. A per-rule compliance table:
677
-
678
- | Rule | Status | Issue |
679
- | ---- | ------ | ----- |
680
-
681
- Use **Pass**, **Fail**, or **N/A** for each rule.
682
-
683
- 2. A summary of required fixes (MUST-level violations).
684
-
685
- 3. A list of suggested improvements (SHOULD-level deviations), ordered by impact.