slopbouncer 0.4.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7ae3e83e53cde3d529fcfe6d61a45569b1437012f0cbc0e6419fa3870773bf5
4
- data.tar.gz: 2a5e54814bf04382ab07924353fccd2fc4c9cf3124596a2dfeaff3e589b28a5f
3
+ metadata.gz: f2e4fa3622013a8c5600752d04c1378838f51ddac48ae014974f55d7da1c03fa
4
+ data.tar.gz: f8e0be9e6cab037a4ebec289cd7bf686f157aa6dfa75347a2b06dfa12e7b0bad
5
5
  SHA512:
6
- metadata.gz: aab2c9d00992a192944112d05bfc8d38c9482405810f2ee940e00513679e58eeff09ce08fe0a326b7ae1a0c024c57fa65ba892ca4f61eabd7355fafbfeb23908
7
- data.tar.gz: 85a0b413d120416d88806af0eda0a2c64805dd06cd29158d572890bbf4fd13633822ca95777404cabb667fd1d6e12dacd3892ce5471648645058c2fb4bfc7d63
6
+ metadata.gz: 90b83a1e6f306e1ebf2d79702f83b293d453dc2e70c70956b99c952759945d1b6f601d13eaaa62afe43dd4a7099238145bd445b3f6dc2747e30eb30b7fb3cc9a
7
+ data.tar.gz: a16a9f3715c8c1c71c8bee7eade4c2bdf3c3a3ed68cf55a61dbfb908e9dfa505b42e502973c1841ddd0b9440d5c308eb04317be27190fc472ae3bdc102513b4a
data/CHANGELOG.md CHANGED
@@ -2,6 +2,33 @@
2
2
 
3
3
  What changed in each version of Slopbouncer.
4
4
 
5
+ ## 0.6.0 - 2026-12-04
6
+
7
+ - The commit hook no longer lets a change through on the strength of an earlier commit. The reason has to be in the commit that lands. When a commit goes through, the reason given for each finding is remembered, and when the same change is stopped again, after a reset or an amend, the "Test changes:" lines it prints come with those reasons filled in, ready to paste.
8
+ - A commit stopped for some of its findings no longer has the others remembered. Before, a first try that explained one test and a second that explained another went through, with neither message explaining both.
9
+ - `slopbouncer stats` counts "Reason offered from memory" in place of "Already explained". In `--json` the key is `reason_offered_from_memory`. A memory file from an earlier version kept no reasons, so it has none to offer.
10
+ - `slopbouncer install` adds its line only to a `commit-msg` hook run by `sh`, `bash`, `zsh` or `dash`. A hook in another language, or with no `#!` line, is left unchanged; install prints the line to add by hand and exits with 2. Before, it put a shell line into a Python or Node hook, which then failed on every commit.
11
+ - A range reads the commits in it. `main..HEAD` and `main...HEAD` both compare the commit where the two last met, their merge base, with `HEAD`. Before, `main...HEAD` failed, and `main..HEAD` compared the two tips, so a branch behind `main` reported tests added on `main` since as deleted. Two histories with no commit in common are said to have none.
12
+ - A test file moved out of the test directories, such as `test/basket_test.rb` to `lib/basket.rb`, reports each of its tests as `test_deleted`, unless the same tests are new in another test file of the change. Before, the move was not read at all.
13
+ - The range command keeps the commit hook's policy. It reads the "Test changes:" section of every commit message in the range, and a finding any of them explains is listed after the others, with ": explained: " and its reason. In `--json` an explained finding carries a `reason`. `--strict` exits with 1 only when something is unexplained, so a commit made with the hook passes the pipeline, and one made with `--no-verify` does not.
14
+ - A "Test changes:" line that names only a file, `- test/till_test.rb: the till is its own gem now`, explains every test deleted from that file, and nothing else. When every finding in a file is a deleted test, the commit hook offers that one line in place of one for each test.
15
+ - `equivalents` work for RSpec. Declare the matchers, such as `[have_selector, have_shown]`, and `expect(page).to have_selector('h1')` swapped for `expect(page).to have_shown('h1')` is not flagged. The subject and the direction must stay the same, so `to` turned into `not_to` is still flagged. Before, a declared matcher name never matched an RSpec expectation.
16
+ - A test file that does not parse, before or after the change, is reported once as `unreadable`, with the line where parsing stopped, and nothing else is said about it. Before, the part that parsed was compared as if it were the whole file, so a file cut short reported its missing assertions as removed, or missed what it could not see.
17
+ - `.slopbouncer.yml` is read as the change has it. For a range it comes from the range's last commit, and for work not yet committed from the root of the repository, wherever the command is run. Before, it was read from the directory the command ran in, so a settings file left uncommitted changed what a range reported, and running from a subdirectory found no settings at all.
18
+ - When several helper files outside the tests change in one go, every test file is read once, where before it was read again for each helper file.
19
+ - `expect_any_instance_of(Till).to receive(:charge)` counts as an expectation, so removing one is flagged as `assertion_removed`. Before, it was not counted at all.
20
+ - The README no longer mentions a pre-push stage, which Slopbouncer does not have.
21
+ - The scenarios run on Ruby 4.0 as well as 3.2, 3.3 and 3.4.
22
+ - `slopbouncer install claude-code` denies more ways round the hook: `--no-verify` or `-n` later in a `git commit` command, as in `git commit -m "x" --no-verify`, and any git command naming `core.hooksPath`, as in `git -c core.hooksPath=/dev/null commit`. Before, only the flag straight after `git commit` was denied. The README says what the rules still miss, such as `git -C . commit -n`, and that the pipeline command is the backstop.
23
+
24
+ ## 0.5.0 - 2026-11-27
25
+
26
+ - The allowance for a change that only rewords tests is gone. In 0.4.0, when a change touched nothing but tests, an assertion swapped one for one for another kind was taken to be reworded: the commit hook let the commit through, the turn-end hook let the agent finish, and a terminal showed one line, "Test refactor: 23 assertions reworded in 6 files, no other code changed." So `assert_equal 5, total` changed to `assert true`, committed with only the test file, was not stopped. Slopbouncer cannot tell whether two different assertions make the same check, and a commit that weakens a test often changes nothing else, so every swap is now reported as `assertion_removed`, wherever the change is read.
27
+ - A swap is quiet only when the two assertions are declared under `equivalents` in `.slopbouncer.yml` and their arguments are the same. Declaring them is flagged as `equivalents_changed`, so a real refactor, such as every `assert_selector` moved to a helper that wraps it, goes through with that one reason in the commit message.
28
+ - A commit stopped for a swap is told: "If these make the same check, declare them under equivalents in .slopbouncer.yml."
29
+ - At commit time `.slopbouncer.yml` is read as staged, like the tests. Before, the hook read it from the working tree, so a settings file left out of the commit could declare two assertions equivalent and quiet a finding, with no `equivalents_changed` to show for it.
30
+ - In `--json`, the finding for a swapped assertion carries `"swapped": true`. The `reworded` key is gone.
31
+
5
32
  ## 0.4.0 - 2026-11-17
6
33
 
7
34
  - Slopbouncer checks commits. `slopbouncer install` sets up git's `commit-msg` hook, wherever git runs hooks from, `.git/hooks` or `core.hooksPath`. A `commit-msg` hook the repository already has gets one line, after its first.
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Gem version](https://img.shields.io/gem/v/slopbouncer)](https://rubygems.org/gems/slopbouncer)
4
4
  [![Test](https://github.com/mainline-dev/slopbouncer/actions/workflows/test.yml/badge.svg)](https://github.com/mainline-dev/slopbouncer/actions/workflows/test.yml)
5
5
 
6
- A commit hook that catches weakened tests, whoever weakened them.
6
+ A commit hook that catches weakened Ruby tests, whoever weakened them.
7
7
 
8
8
  A coding agent's job is to make the checks pass. One way is to weaken the check. When a commit weakens an existing test, Slopbouncer stops it until the commit message says which behaviour changed and why. It reads code and never runs it, so it takes a fraction of a second.
9
9
 
@@ -23,7 +23,7 @@ From the root of your repository:
23
23
  slopbouncer install
24
24
  ```
25
25
 
26
- This sets up git's `commit-msg` hook, so every commit is checked, whoever or whatever makes it. It goes wherever git runs hooks from, `.git/hooks` or the directory `core.hooksPath` names, as Husky and Lefthook set. If the repository already has a `commit-msg` hook, Slopbouncer adds one line to it, after its first, and the rest of the hook runs as before. Running it again changes nothing.
26
+ This sets up git's `commit-msg` hook, so every commit is checked, whoever or whatever makes it. It goes wherever git runs hooks from, `.git/hooks` or the directory `core.hooksPath` names, as Husky and Lefthook set. If the repository already has a `commit-msg` hook run by `sh`, `bash`, `zsh` or `dash`, Slopbouncer adds one line to it, after its first, and the rest of the hook runs as before. A hook in any other language, or with no `#!` line, is left as it is: install prints the shell line for you to have the hook run, with the message file as its argument, and exits with 2. Running it again changes nothing.
27
27
 
28
28
  Name your coding agent to set it up for that agent too:
29
29
 
@@ -40,7 +40,7 @@ The hook needs the `slopbouncer` command on the `PATH` of whatever commits. When
40
40
 
41
41
  ## When it stops a commit
42
42
 
43
- It reads only what the commit will hold: the test files as staged, whatever else is in the working tree. A commit that changes no tests, or only adds them, goes through without a word. One that weakens an existing test is stopped, with a line for each finding and the lines to add to the commit message:
43
+ It reads only what the commit will hold: the test files and `.slopbouncer.yml` as staged, whatever else is in the working tree. A commit that changes no tests, or only adds them, goes through without a word. One that weakens an existing test is stopped, with a line for each finding and the lines to add to the commit message:
44
44
 
45
45
  ```
46
46
  Slopbouncer: this commit looks to weaken 1 existing test.
@@ -62,13 +62,22 @@ Test changes:
62
62
  - test/basket_test.rb: Basket adds up: size moved to its own test
63
63
  ```
64
64
 
65
- A test named with no reason does not count. Once a message has explained a finding, Slopbouncer remembers it, so the same change committed again, after a reset or an amend, goes through quietly. A change to the assertions declared equivalent is asked about every time, however often it was explained. The memory is `.git/slopbouncer/explained.json`, in the git directory, and is never committed.
65
+ A test file deleted whole, or any tests deleted from a file, can be explained in one line that names only the file:
66
66
 
67
- A change that only rewords tests, with no other code changed, is not stopped.
67
+ ```
68
+ Test changes:
69
+ - test/till_test.rb: the till is its own gem now
70
+ ```
71
+
72
+ That line explains the deleted tests in that file and nothing else. When every finding in a file is a deleted test, the hook offers that one line in place of a line for each test.
73
+
74
+ A test named with no reason does not count. The reason has to be in the commit that lands. When a commit goes through, Slopbouncer remembers the reason given for each finding, and if the same change is stopped again, after a reset or an amend, the "Test changes:" lines it prints come with those reasons filled in, ready to paste into the message. A change to the assertions declared equivalent never comes filled in. The memory is `.git/slopbouncer/explained.json`, in the git directory, and is never committed.
75
+
76
+ An assertion swapped for another kind, such as `assert_equal 5, total` for `assert true`, is reported as removed, and the commit is stopped like any other, whether or not it changes anything besides tests. When a commit is stopped for a swap, the message adds one line: "If these make the same check, declare them under equivalents in .slopbouncer.yml." See [Moving tests from one assertion to another](#moving-tests-from-one-assertion-to-another).
68
77
 
69
78
  ## How often it stops a commit
70
79
 
71
- Every commit the hook checks is logged in `.git/slopbouncer/log.jsonl`. After a week or so, see how often it stopped one and why the others went through:
80
+ Every commit the hook checks is logged in `.git/slopbouncer/log.jsonl`. After a week or so, see how often it stopped one, how often a reason was given, and how often a stopped commit was offered a reason from memory:
72
81
 
73
82
  ```shell
74
83
  slopbouncer stats
@@ -79,7 +88,7 @@ Commits checked: 214
79
88
  Stopped: 9
80
89
  Findings: 31
81
90
  Explained in the commit message: 17
82
- Already explained (repeats not asked about): 6
91
+ Reason offered from memory: 6
83
92
  Not checked, because Slopbouncer could not run: 0
84
93
  ```
85
94
 
@@ -87,20 +96,25 @@ Not checked, because Slopbouncer could not run: 0
87
96
 
88
97
  ## Getting round it
89
98
 
90
- `git commit --no-verify`, or `-n`, skips every hook, this one included. A person can choose to do that. To stop Claude Code doing it, `slopbouncer install claude-code` adds these rules to `.claude/settings.json`, and you can add them by hand:
99
+ `git commit --no-verify`, or `-n`, skips every hook, this one included, and `git -c core.hooksPath=...` can point git at no hooks at all. A person can choose to do that. To stop Claude Code doing it, `slopbouncer install claude-code` adds these rules to `.claude/settings.json`, and you can add them by hand:
91
100
 
92
101
  ```json
93
102
  {
94
103
  "permissions": {
95
104
  "deny": [
96
105
  "Bash(git commit --no-verify:*)",
97
- "Bash(git commit -n:*)"
106
+ "Bash(git commit -n:*)",
107
+ "Bash(git commit * --no-verify*)",
108
+ "Bash(git commit * -n*)",
109
+ "Bash(git *core.hooksPath*)"
98
110
  ]
99
111
  }
100
112
  }
101
113
  ```
102
114
 
103
- Keep running the plain command in a pipeline as the backstop (see below).
115
+ They deny `--no-verify` or `-n` anywhere in a command that starts `git commit`, including inside a longer command joined with `&&` or `;`, and any git command that names `core.hooksPath`. Claude Code matches a rule against the command as written, so they do not catch every spelling: `git -C . commit -n`, a flag bundled as `-an`, git called by its full path or through `sh -c`. Claude Code's own documentation says such rules are not a security boundary. A commit message containing ` -n` is denied too, and can be reworded.
116
+
117
+ So keep running the plain command in a pipeline as the backstop (see below). It reads every commit, however it was made.
104
118
 
105
119
  ## Turn-end hooks for coding agents
106
120
 
@@ -140,7 +154,7 @@ For each one, do one of two things before you finish:
140
154
  2. Leave the change, and tell the person which behaviour changed and why the test had to change with it.
141
155
  ```
142
156
 
143
- The turn-end hook remembers what it has shown the agent for the work in hand, and sends it back only for findings it has not been shown. When nothing is new it lets the agent finish and says nothing. A finding that is put right and later brought back is shown again, and once HEAD moves, as it does on a commit, the memory starts empty. It keeps this in `slopbouncer-explained.json` in the git directory, never in the working tree. A change to the assertions declared equivalent is shown on every stop. The command run by hand, the pre-push stage and `--json` never use this memory and always report everything.
157
+ The turn-end hook remembers what it has shown the agent for the work in hand, and sends it back only for findings it has not been shown. When nothing is new it lets the agent finish and says nothing. A finding that is put right and later brought back is shown again, and once HEAD moves, as it does on a commit, the memory starts empty. It keeps this in `slopbouncer-explained.json` in the git directory, never in the working tree. A change to the assertions declared equivalent is shown on every stop. The command run by hand and `--json` never use this memory and always report everything.
144
158
 
145
159
  ## What it flags
146
160
 
@@ -150,7 +164,8 @@ The turn-end hook remembers what it has shown the agent for the work in hand, an
150
164
  | `expected_value_changed` | The value an assertion expects was edited |
151
165
  | `subject_changed` | The thing an assertion checks was edited, so the test now checks something else |
152
166
  | `assertion_changed` | An argument was edited in an assertion Slopbouncer cannot read sides into, such as a helper of your own, or the message an assertion shows when it fails |
153
- | `test_deleted` | A test, or its whole file, was deleted |
167
+ | `unreadable` | A version of a test file could not be parsed, so it could not be compared. Reported once for the file, with the line where parsing stopped |
168
+ | `test_deleted` | A test, or its whole file, was deleted, or the file was moved out of the test directories, where its tests no longer run |
154
169
  | `skip_added` | A test was marked to be skipped or pending |
155
170
  | `no_assertions` | A test the change touched asserts nothing |
156
171
  | `equivalents_changed` | The assertions declared equivalent in `.slopbouncer.yml` changed, naming each group added or removed |
@@ -163,15 +178,34 @@ The turn-end hook remembers what it has shown the agent for the work in hand, an
163
178
  - A test that was reworded, moved to another file, or both, with its checks unchanged.
164
179
  - Files that are not tests.
165
180
 
166
- ## A change that only rewords tests
181
+ ## Moving tests from one assertion to another
167
182
 
168
- When a change touches nothing but tests, their directories and `.slopbouncer.yml`, an assertion removed from a test that gained as many as it lost, and has as many in all as before or more, is taken to be reworded. At a terminal and in an agent's hook those are one line:
183
+ Slopbouncer cannot tell whether two different assertions make the same check, so one swapped for another is reported as `assertion_removed`, at commit time, at the end of an agent's turn and in the plain command. A change that touches only tests is read the same way as any other.
169
184
 
185
+ When you move tests to a helper that makes the same check, declare the pair equivalent in the same commit:
186
+
187
+ ```yaml
188
+ equivalents:
189
+ - [assert_selector, assert_shown]
170
190
  ```
171
- Test refactor: 23 assertions reworded in 6 files, no other code changed.
191
+
192
+ For RSpec, declare the matchers:
193
+
194
+ ```yaml
195
+ equivalents:
196
+ - [have_selector, have_shown]
172
197
  ```
173
198
 
174
- `--all` lists them, and piped output and `--json` always do. Everything else is still reported in full, among them a deleted test, a test that asserts nothing, a test with fewer assertions than before, `expected_value_changed`, `subject_changed` and `equivalents_changed`. An agent is not sent back for the line alone. As soon as the same change touches a file outside the tests, every finding is reported as usual.
199
+ Then `expect(page).to have_selector('h1')` swapped for `expect(page).to have_shown('h1')` is not flagged. The expectation must stay about the same thing and keep its direction: `to` turned into `not_to` is flagged whatever is declared.
200
+
201
+ A test that swaps one for the other with the same arguments is then not flagged, however many files the change covers. The declaration is flagged, once, as `equivalents_changed`, so the commit goes through with one reason:
202
+
203
+ ```
204
+ Assert through assert_shown
205
+
206
+ Test changes:
207
+ - .slopbouncer.yml: equivalent assertions: assert_shown wraps assert_selector
208
+ ```
175
209
 
176
210
  ## Running it yourself and in a pipeline
177
211
 
@@ -183,7 +217,7 @@ Run it from the root of the repository you want checked:
183
217
  slopbouncer main..HEAD
184
218
  ```
185
219
 
186
- The argument is a range of commits. Give one commit instead, such as `HEAD` or `origin/main`, and it checks everything since then, committed or not. At a terminal it lays the findings out by file, with each rule in colour:
220
+ The argument is a range of commits, written `main..HEAD` or `main...HEAD`; both mean the same. Slopbouncer compares the commit where the two last met, their merge base, with the second, so it reads only the commits on `HEAD` that `main` lacks. Work merged to `main` since the branch began, such as a teammate's new test, is not taken for the branch deleting it. Give one commit instead, such as `HEAD` or `origin/main`, and it checks everything since then, committed or not. At a terminal it lays the findings out by file, with each rule in colour:
187
221
 
188
222
  ```
189
223
  test/basket_test.rb
@@ -209,10 +243,18 @@ test/checkout_test.rb: Checkout lets a guest pay: assertion_removed: assert_equa
209
243
  test/refund_test.rb: Refund refunds part of an order: expected_value_changed: 100 → 90
210
244
  ```
211
245
 
246
+ The range command keeps the commit hook's policy. A finding is explained when the "Test changes:" section of any commit message in the range names it with a reason, as the hook asks, so a commit made with the hook passes the pipeline too, and one made with `--no-verify` does not. Explained findings are listed after the others, each with its reason:
247
+
248
+ ```
249
+ test/basket_test.rb: Basket adds up: assertion_removed: assert_equal 2, size: explained: size has its own test
250
+ ```
251
+
252
+ In `--json` an explained finding carries a `reason`. `--strict` fails only on what nothing explains. Given one commit instead of a range, there are no messages to read, and nothing is explained.
253
+
212
254
  | Option | What it does |
213
255
  |---|---|
214
256
  | `--json` | Prints the findings as JSON |
215
- | `--strict` | Exits with 1 when anything is flagged |
257
+ | `--strict` | Exits with 1 when anything is flagged that no commit message in the range explains |
216
258
  | `--all` | At a terminal, lists every finding. Without it, more than 30 findings are shown as a count of each rule in each file |
217
259
  | `--color` | Uses the terminal layout, in colour, wherever the output goes. For a script whose own output a person reads |
218
260
  | `--agent=NAME` | Runs as that coding agent's turn-end hook (see above) |
@@ -242,6 +284,8 @@ assertions:
242
284
 
243
285
  A test that only calls helpers Slopbouncer has not been told about looks like a test with no assertions.
244
286
 
287
+ The file is read as the change has it, like the tests: as staged at commit time, from the last commit of a range, and from the root of the working tree for work not yet committed, wherever in the repository the command runs. A settings file left uncommitted does not change what a range reports.
288
+
245
289
  When two assertions make the same check, as a framework's assertion and the helper you wrap round it might, declare them equivalent in the same file:
246
290
 
247
291
  ```yaml
@@ -2,32 +2,32 @@
2
2
 
3
3
  require_relative 'commit_log'
4
4
  require_relative 'commit_memory'
5
+ require_relative 'test_changes'
5
6
 
6
7
  module Slopbouncer
7
8
  # Run as git's commit-msg hook. It reads the tests the commit changes, as
8
9
  # staged, and stops a commit that weakens one until the message says why,
9
- # in a "Test changes:" section with a line for each such test:
10
+ # in a "Test changes:" section (see TestChanges).
10
11
  #
11
- # Test changes:
12
- # - test/basket_test.rb: Basket adds up: size moved to its own test
13
- #
14
- # What a message has explained is remembered, so the same change committed
15
- # again goes through quietly. When it cannot check a commit at all, it says
16
- # so and lets the commit through: a broken setup never stops anyone.
12
+ # The reason has to be in the commit that lands. The reasons in a commit
13
+ # that went through are remembered, so when the same change is stopped
14
+ # again, after a reset or an amend, its lines come filled in, ready to
15
+ # paste. When it cannot check a commit at all, it says so and lets the
16
+ # commit through: a broken setup never stops anyone.
17
17
  module CommitHook
18
- SECTION = 'Test changes:'
19
-
20
18
  def self.call(message_path)
21
- message = File.read(message_path)
22
- findings = Slopbouncer.check_staged.reject(&:reworded)
23
- remembered, unknown = findings.partition { |finding| CommitMemory.known?(finding) }
24
- explained, unexplained = unknown.partition { |finding| explains?(message, finding) }
25
- CommitMemory.remember(explained)
26
- CommitLog.record('checked' => true, 'findings' => findings.size, 'explained_in_message' => explained.size,
27
- 'already_explained' => remembered.size, 'stopped' => unexplained.any?)
28
- return 0 if unexplained.empty?
29
-
30
- warn stopped(unexplained)
19
+ findings = Slopbouncer.check_staged
20
+ reasons = TestChanges.reasons([File.read(message_path)], findings)
21
+ unexplained = findings.zip(reasons).filter_map { |finding, reason| finding unless reason }
22
+ offered = unexplained.map { |finding| CommitMemory.reason(finding) }
23
+ CommitLog.record('checked' => true, 'findings' => findings.size, 'explained_in_message' => reasons.compact.size,
24
+ 'reason_offered_from_memory' => offered.compact.size, 'stopped' => unexplained.any?)
25
+ if unexplained.empty?
26
+ CommitMemory.remember(findings.zip(reasons))
27
+ return 0
28
+ end
29
+
30
+ warn stopped(unexplained, offered)
31
31
  1
32
32
  rescue StandardError => e
33
33
  warn "Slopbouncer could not run: #{e.message}. This commit was not checked."
@@ -35,32 +35,22 @@ module Slopbouncer
35
35
  0
36
36
  end
37
37
 
38
- # Whether the message's "Test changes:" section names the finding's file
39
- # and test, with a reason after them. Lines git would strip as comments
40
- # are not read.
41
- def self.explains?(message, finding)
42
- start = "- #{finding.file}: #{finding.test}:"
43
- section(message).any? { |line| line.start_with?(start) && !line.delete_prefix(start).strip.empty? }
44
- end
45
-
46
- def self.section(message)
47
- lines = message.lines.map(&:rstrip).reject { |line| line.start_with?('#') }
48
- after = lines.drop_while { |line| line.strip != SECTION }.drop(1)
49
- after.take_while { |line| line.start_with?('- ') }
50
- end
51
-
52
- def self.stopped(findings)
38
+ # An assertion swapped for another kind is reported as removed, so a
39
+ # commit stopped for one is told how to say the two make the same check.
40
+ # Each line comes with the reason remembered for it, when there is one.
41
+ def self.stopped(findings, offered)
42
+ declare = "\nIf these make the same check, declare them under equivalents in #{CONFIG}.\n" if findings.any?(&:swapped)
53
43
  <<~MESSAGE
54
44
  Slopbouncer: this commit looks to weaken #{findings.size} existing #{findings.size == 1 ? 'test' : 'tests'}.
55
45
 
56
46
  #{Slopbouncer.report(findings)}
57
-
47
+ #{declare}
58
48
  Put each test back, or add this to the commit message, with a reason on each line:
59
49
 
60
- #{SECTION}
61
- #{findings.map { |finding| "- #{finding.file}: #{finding.test}: " }.join("\n")}
50
+ #{TestChanges::SECTION}
51
+ #{TestChanges.lines(findings, offered).join("\n")}
62
52
  MESSAGE
63
53
  end
64
- private_class_method :explains?, :section, :stopped
54
+ private_class_method :stopped
65
55
  end
66
56
  end
@@ -11,7 +11,7 @@ module Slopbouncer
11
11
  # went through.
12
12
  module CommitLog
13
13
  FILE = 'log.jsonl'
14
- COUNTS = %w[checked stopped findings explained_in_message already_explained not_checked].freeze
14
+ COUNTS = %w[checked stopped findings explained_in_message reason_offered_from_memory not_checked].freeze
15
15
 
16
16
  def self.record(entry)
17
17
  FileUtils.mkdir_p(File.dirname(path))
@@ -26,7 +26,7 @@ module Slopbouncer
26
26
  { 'checked' => checked.size, 'stopped' => checked.count { |entry| entry['stopped'] },
27
27
  'findings' => checked.sum { |entry| entry['findings'].to_i },
28
28
  'explained_in_message' => checked.sum { |entry| entry['explained_in_message'].to_i },
29
- 'already_explained' => checked.sum { |entry| entry['already_explained'].to_i },
29
+ 'reason_offered_from_memory' => checked.sum { |entry| entry['reason_offered_from_memory'].to_i },
30
30
  'not_checked' => entries.count { |entry| !entry['checked'] } }
31
31
  rescue Errno::ENOENT
32
32
  COUNTS.to_h { |count| [count, 0] }
@@ -6,24 +6,28 @@ require 'json'
6
6
  require_relative 'git'
7
7
 
8
8
  module Slopbouncer
9
- # The findings a commit message has explained, so the same change committed
10
- # again, after a reset or an amend, goes through quietly. It is kept in the
11
- # git directory, out of the working tree, and lasts across commits. A
12
- # change to the assertions declared equivalent quiets Slopbouncer, so
13
- # nothing quiets that change in turn.
9
+ # The reasons given in commits that went through, each kept with the finding
10
+ # it explained, so the same change stopped again, after a reset or an amend,
11
+ # is offered its reason ready to paste. A reason is only offered: the commit
12
+ # that lands has to give it. It is kept in the git directory, out of the
13
+ # working tree, and lasts across commits. A change to the assertions
14
+ # declared equivalent quiets Slopbouncer, so no reason is offered for one.
14
15
  module CommitMemory
15
16
  FILE = 'explained.json'
16
- ALWAYS = %w[equivalents_changed].freeze
17
+ NEVER_OFFERED = %w[equivalents_changed].freeze
17
18
 
18
- def self.known?(finding)
19
- !ALWAYS.include?(finding.rule) && recall.include?(fingerprint(finding))
19
+ # The reason remembered for the finding, if there is one to offer.
20
+ def self.reason(finding)
21
+ recall[fingerprint(finding)] unless NEVER_OFFERED.include?(finding.rule)
20
22
  end
21
23
 
22
- def self.remember(findings)
23
- return if findings.empty?
24
+ # Findings, each with the reason a commit that went through gave it.
25
+ def self.remember(explained)
26
+ return if explained.empty?
24
27
 
28
+ reasons = recall.merge(explained.to_h { |finding, reason| [fingerprint(finding), reason] })
25
29
  FileUtils.mkdir_p(File.dirname(path))
26
- File.write(path, JSON.generate('fingerprints' => (recall + findings.map { |finding| fingerprint(finding) }).uniq))
30
+ File.write(path, JSON.generate('reasons' => reasons))
27
31
  end
28
32
 
29
33
  # The same finding: the same file, test and kind, changing the same thing
@@ -33,10 +37,13 @@ module Slopbouncer
33
37
  Digest::SHA256.hexdigest(JSON.generate(said.map { |part| part.to_s.gsub(/\s+/, ' ').strip }))
34
38
  end
35
39
 
40
+ # Reasons by fingerprint. A file from before 0.6.0 kept no reasons, so it
41
+ # has none to offer.
36
42
  def self.recall
37
- JSON.parse(File.read(path)).fetch('fingerprints', [])
43
+ reasons = JSON.parse(File.read(path))['reasons']
44
+ reasons.is_a?(Hash) ? reasons : {}
38
45
  rescue Errno::ENOENT, JSON::ParserError
39
- []
46
+ {}
40
47
  end
41
48
 
42
49
  def self.path
@@ -6,9 +6,9 @@ module Slopbouncer
6
6
  # tell a stronger assertion from a weaker one, so an assertion swapped for
7
7
  # another kind is reported as removed, for a person to judge.
8
8
  module Compare
9
- # reworded: an assertion removed from a test that gained as many as it
10
- # lost, so it may only have been written another way.
11
- Finding = Struct.new(:file, :test, :rule, :detail, :before, :after, :reworded, keyword_init: true)
9
+ # swapped: an assertion removed from a test that gained another in its
10
+ # place. Slopbouncer cannot tell whether the two make the same check.
11
+ Finding = Struct.new(:file, :test, :rule, :detail, :before, :after, :swapped, keyword_init: true)
12
12
  Result = Struct.new(:findings, :gone, :fresh, keyword_init: true)
13
13
 
14
14
  # A reworded test is the same test: one that has gone is taken to be a
@@ -116,19 +116,17 @@ module Slopbouncer
116
116
  finding.call(EDITED.fetch(edit.sides[edited_place(assertion, edit, equivalents)], 'assertion_changed'),
117
117
  detail: edit.source, before: before, after: after)
118
118
  end
119
- mark_reworded(found, gained, was, now)
119
+ mark_swapped(found, gained)
120
120
  found << finding.call('skip_added', detail: 'marked to be skipped') if now.skipped && !was.skipped
121
121
  found << finding.call('no_assertions', detail: 'asserts nothing') if asserts_nothing?(now) && was.form != now.form
122
122
  found
123
123
  end
124
124
 
125
- # The assertions a test lost are marked reworded when it gained at least as
126
- # many as it lost, and has as many in all as before or more.
127
- def self.mark_reworded(found, gained, was, now)
125
+ # The assertions a test lost are marked swapped, one for each assertion
126
+ # it gained that no edit accounts for.
127
+ def self.mark_swapped(found, gained)
128
128
  removed = found.select { |finding| finding.rule == 'assertion_removed' }
129
- return unless gained.size >= removed.size && now.assertions.size >= was.assertions.size
130
-
131
- removed.each { |finding| finding.reworded = true }
129
+ removed.first(gained.size).each { |finding| finding.swapped = true }
132
130
  end
133
131
 
134
132
  # Two assertions make the same check when they parse the same, or when the
@@ -138,8 +136,19 @@ module Slopbouncer
138
136
  was.form == now.form || (equivalent?(was.method, now.method, equivalents) && was.forms == now.forms)
139
137
  end
140
138
 
139
+ # An RSpec expectation is named for its subject, direction and matcher,
140
+ # such as "expect to have_selector". A declared name matches the matcher
141
+ # when the subject and the direction are the same: declaring
142
+ # [have_selector, have_shown] never lets to become not_to.
141
143
  def self.equivalent?(one, other, equivalents)
142
- one == other || equivalents.any? { |group| group.include?(one) && group.include?(other) }
144
+ *about, matcher = one.split
145
+ *also_about, other_matcher = other.split
146
+ one == other || declared?(one, other, equivalents) ||
147
+ (about.any? && about == also_about && declared?(matcher, other_matcher, equivalents))
148
+ end
149
+
150
+ def self.declared?(one, other, equivalents)
151
+ equivalents.any? { |group| group.include?(one) && group.include?(other) }
143
152
  end
144
153
 
145
154
  # A test with no assertions of its own still asserts when a hook in its
@@ -164,6 +173,6 @@ module Slopbouncer
164
173
  differing.first if differing.size == 1
165
174
  end
166
175
  private_class_method :same_name, :pair, :without, :reworded, :rewritten?, :most_alike, :one_edit?, :likeness, :weaker,
167
- :asserts_nothing?, :edited_value, :edited_place, :same_check?, :equivalent?, :mark_reworded
176
+ :asserts_nothing?, :edited_value, :edited_place, :same_check?, :equivalent?, :declared?, :mark_swapped
168
177
  end
169
178
  end
@@ -34,6 +34,11 @@ module Slopbouncer
34
34
  end
35
35
  end
36
36
 
37
+ # The message of each commit on head that base lacks.
38
+ def self.messages(base, head)
39
+ run('log', '--format=%B%x00', "#{base}..#{head}").split("\0").map(&:strip).reject(&:empty?)
40
+ end
41
+
37
42
  # What a file holds at a commit, as staged, or in the working tree when no
38
43
  # commit is given.
39
44
  def self.show(revision, path)
@@ -56,8 +61,23 @@ module Slopbouncer
56
61
  run('rev-parse', '--git-path', "hooks/#{name}").strip
57
62
  end
58
63
 
64
+ # The commit where two lines of work last met. Git says nothing when they
65
+ # never met, so that is said here.
66
+ def self.merge_base(first, second)
67
+ raise Error, NOT_A_REPOSITORY unless repository?
68
+
69
+ output, error, status = Open3.capture3('git', 'merge-base', first, second)
70
+ return output.strip if status.success?
71
+
72
+ raise Error, error.empty? ? "#{first} and #{second} have no commit in common." : error.lines.first.strip
73
+ end
74
+
75
+ # The top of the working tree, for the directory the command is in now:
76
+ # a hook may change directory before it asks.
59
77
  def self.root
60
- @root ||= run('rev-parse', '--show-toplevel').strip
78
+ raise Error, NOT_A_REPOSITORY unless repository?
79
+
80
+ (@roots ||= {})[Dir.pwd] ||= run('rev-parse', '--show-toplevel').strip
61
81
  end
62
82
 
63
83
  # Where git keeps its own files for this repository, outside the working
@@ -94,6 +114,6 @@ module Slopbouncer
94
114
 
95
115
  output
96
116
  end
97
- private_class_method :repository?, :run, :root
117
+ private_class_method :repository?, :run
98
118
  end
99
119
  end
@@ -13,8 +13,15 @@ module Slopbouncer
13
13
  # the commit was not checked and lets it through.
14
14
  LINE = 'command -v slopbouncer >/dev/null 2>&1 && { slopbouncer commit-msg "$1" || exit 1; } || ' \
15
15
  'echo "Slopbouncer could not run: the slopbouncer command was not found. This commit was not checked." >&2'
16
- # What Claude Code may not run, since --no-verify skips every hook.
17
- DENY = ['Bash(git commit --no-verify:*)', 'Bash(git commit -n:*)'].freeze
16
+ # The shells whose hooks Slopbouncer adds its line to.
17
+ SHELLS = %w[sh bash zsh dash].freeze
18
+ # What Claude Code may not run, since --no-verify skips every hook and
19
+ # core.hooksPath can point git at no hooks: --no-verify or -n straight
20
+ # after git commit or later in it, and any git command naming
21
+ # core.hooksPath. A Bash rule matches the command as written, so another
22
+ # spelling, such as git -C . commit -n or -an, gets past them.
23
+ DENY = ['Bash(git commit --no-verify:*)', 'Bash(git commit -n:*)', 'Bash(git commit * --no-verify*)',
24
+ 'Bash(git commit * -n*)', 'Bash(git *core.hooksPath*)'].freeze
18
25
 
19
26
  # Sets up the commit hook, and for Claude Code the rules that deny it
20
27
  # --no-verify. Says what it did, a line for each.
@@ -25,9 +32,10 @@ module Slopbouncer
25
32
  done.join("\n")
26
33
  end
27
34
 
28
- # Writes the commit-msg hook wherever git runs hooks from. A hook the
35
+ # Writes the commit-msg hook wherever git runs hooks from. A shell hook the
29
36
  # project already has gets one line, after its first, so it runs before
30
- # anything that might end the hook early.
37
+ # anything that might end the hook early. A hook in any other language is
38
+ # left as it is, with the line to add by hand.
31
39
  def self.hook
32
40
  path = Git.hook_path('commit-msg')
33
41
  where = File.dirname(path)
@@ -36,8 +44,9 @@ module Slopbouncer
36
44
  FileUtils.mkdir_p(where)
37
45
  if File.exist?(path)
38
46
  lines = File.read(path).lines
39
- at = lines.first.to_s.start_with?('#!') ? 1 : 0
40
- File.write(path, lines.insert(at, "#{LINE}\n").join)
47
+ raise Error, by_hand(path) unless shell?(lines.first.to_s)
48
+
49
+ File.write(path, lines.insert(1, "#{LINE}\n").join)
41
50
  File.chmod(File.stat(path).mode | 0o111, path)
42
51
  return "Added Slopbouncer to the commit-msg hook in #{where}. Both run on every commit."
43
52
  end
@@ -47,6 +56,21 @@ module Slopbouncer
47
56
  "Set up the commit-msg hook in #{where}. Every commit that weakens a test now needs a reason."
48
57
  end
49
58
 
59
+ # Whether a hook's first line runs it with a shell the line can go in:
60
+ # #!/bin/sh, #!/bin/bash -e or #!/usr/bin/env zsh, say.
61
+ def self.shell?(first)
62
+ return false unless first.start_with?('#!')
63
+
64
+ words = first.delete_prefix('#!').split
65
+ words = words.drop(1).reject { |word| word.start_with?('-') } if File.basename(words.first.to_s) == 'env'
66
+ SHELLS.include?(File.basename(words.first.to_s))
67
+ end
68
+
69
+ def self.by_hand(path)
70
+ "#{path} is not a shell script, so Slopbouncer cannot add itself to it. Nothing changed. " \
71
+ "To have it check commits, make the hook run this shell line, with the message file as its argument:\n\n#{LINE}"
72
+ end
73
+
50
74
  def self.deny(path)
51
75
  settings = read(path)
52
76
  denied = Array(settings.dig('permissions', 'deny'))
@@ -81,6 +105,6 @@ module Slopbouncer
81
105
  rescue JSON::ParserError
82
106
  raise Error, "#{path} could not be read as JSON. Nothing changed."
83
107
  end
84
- private_class_method :read, :hook, :deny
108
+ private_class_method :read, :hook, :shell?, :by_hand, :deny
85
109
  end
86
110
  end
@@ -48,8 +48,9 @@ module Slopbouncer
48
48
  HOOKS = %i[before after around].freeze
49
49
  ASSERTION = /\A(assert|refute|must|wont)(_|\z)|\A(expect|flunk)\z/
50
50
  # An RSpec expectation: expect(total).to eq(5), is_expected.not_to be_nil,
51
- # and the older total.should eq(5).
52
- SUBJECTS = %i[expect is_expected].freeze
51
+ # expect_any_instance_of(Till).to receive(:charge), and the older
52
+ # total.should eq(5).
53
+ SUBJECTS = %i[expect is_expected expect_any_instance_of].freeze
53
54
  DIRECTIONS = %i[to not_to to_not].freeze
54
55
  SHOULDS = %i[should should_not].freeze
55
56
 
@@ -58,6 +59,13 @@ module Slopbouncer
58
59
  Reader.new(helpers).read(Prism.parse(source).value)
59
60
  end
60
61
 
62
+ # Where a file stops parsing, as "line 3: expected an `end`", or nil
63
+ # when it parses.
64
+ def self.unreadable(source)
65
+ error = Prism.parse(source).errors.first
66
+ "line #{error.location.start_line}: #{error.message}" if error
67
+ end
68
+
61
69
  # The methods a file defines, other than tests written as methods, each
62
70
  # with what it parses to.
63
71
  def self.methods_in(source)
@@ -5,8 +5,7 @@ module Slopbouncer
5
5
  # tests flagged in it underneath, each rule with its detail under its test,
6
6
  # and a count at the end. Colour is optional; the layout is the same without.
7
7
  # Past LIMIT findings it gives a count of each rule in each file, unless
8
- # asked for them all. Assertions a change that only touches tests rewrote
9
- # are one line at the end, unless asked for them all.
8
+ # asked for them all.
10
9
  module TerminalReport
11
10
  LIMIT = 30
12
11
  BOLD = 1
@@ -18,16 +17,13 @@ module Slopbouncer
18
17
  'expected_value_changed' => YELLOW, 'skip_added' => YELLOW }.freeze
19
18
 
20
19
  def self.call(findings, colour:, all: false)
21
- reworded = all ? [] : findings.select(&:reworded)
22
- return [listed(findings - reworded, colour, all, quiet: true), Slopbouncer.test_refactor(reworded)].compact.join("\n\n") if reworded.any?
23
-
24
20
  listed(findings, colour, all)
25
21
  end
26
22
 
27
23
  # The findings as a person reads them, or nothing for none.
28
- def self.listed(findings, colour, all, quiet: false)
24
+ def self.listed(findings, colour, all)
29
25
  paint = ->(text, code) { colour ? "\e[#{code}m#{text}\e[0m" : text }
30
- return (quiet ? nil : paint.call('No tests look weaker.', GREEN)) if findings.empty?
26
+ return paint.call('No tests look weaker.', GREEN) if findings.empty?
31
27
 
32
28
  files = findings.group_by(&:file)
33
29
  tests = files.sum { |_file, found| found.map(&:test).uniq.size }
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slopbouncer
4
+ # The "Test changes:" section of a commit message, where whoever commits
5
+ # says why each test the commit weakens had to change, a line for each:
6
+ #
7
+ # Test changes:
8
+ # - test/basket_test.rb: Basket adds up: size moved to its own test
9
+ #
10
+ # A line naming only a file explains every test deleted from it, and
11
+ # nothing else, so a test file deleted whole needs one line:
12
+ #
13
+ # - test/till_test.rb: the till is its own gem now
14
+ #
15
+ # The commit hook reads the message being committed, and a range reads the
16
+ # messages of every commit in it. Either way a finding is explained by a
17
+ # line that names it and gives a reason.
18
+ module TestChanges
19
+ SECTION = 'Test changes:'
20
+
21
+ # The reason given for each finding, in the same order, or nil for a
22
+ # finding no message explains.
23
+ def self.reasons(messages, findings)
24
+ lines = messages.flat_map { |message| section(message) }
25
+ findings.map { |finding| for_the_test(lines, finding) || for_the_file(lines, finding, findings) }
26
+ end
27
+
28
+ # The lines to add to a message to explain the findings, each with the
29
+ # reason offered for it, if there is one. A file whose findings are all
30
+ # deleted tests gets one line, with a reason when they share one.
31
+ def self.lines(findings, offered)
32
+ findings.zip(offered).group_by { |finding, _reason| finding.file }.flat_map do |file, found|
33
+ next "- #{file}: #{found.map(&:last).uniq.one? ? found.first.last : ''}" if found.all? { |finding, _reason| finding.rule == 'test_deleted' }
34
+
35
+ found.map { |finding, reason| "- #{file}: #{finding.test}: #{reason}" }
36
+ end
37
+ end
38
+
39
+ def self.for_the_test(lines, finding)
40
+ said(lines, "- #{finding.file}: #{finding.test}:").first
41
+ end
42
+
43
+ # A line that names the file and then no test reported in it.
44
+ def self.for_the_file(lines, finding, findings)
45
+ return unless finding.rule == 'test_deleted'
46
+
47
+ named = findings.select { |other| other.file == finding.file }.map { |other| "#{other.test}:" }
48
+ said(lines, "- #{finding.file}:").find { |reason| named.none? { |test| reason.start_with?(test) } }
49
+ end
50
+
51
+ # The reasons in the lines that start so, those with one.
52
+ def self.said(lines, start)
53
+ lines.filter_map { |line| line.delete_prefix(start).strip if line.start_with?(start) }.reject(&:empty?)
54
+ end
55
+
56
+ # The section's lines. Lines git would strip as comments are not read.
57
+ def self.section(message)
58
+ lines = message.lines.map(&:rstrip).reject { |line| line.start_with?('#') }
59
+ after = lines.drop_while { |line| line.strip != SECTION }.drop(1)
60
+ after.take_while { |line| line.start_with?('- ') }
61
+ end
62
+ private_class_method :for_the_test, :for_the_file, :said, :section
63
+ end
64
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slopbouncer
4
- VERSION = '0.4.0'
4
+ VERSION = '0.6.0'
5
5
  end
data/lib/slopbouncer.rb CHANGED
@@ -17,7 +17,8 @@ require_relative 'slopbouncer/terminal_report'
17
17
  module Slopbouncer
18
18
  class Error < StandardError; end
19
19
 
20
- # A project's own settings, in the directory the command is run from:
20
+ # A project's own settings, at the root of the repository, read as the
21
+ # change has them:
21
22
  # assertions: the names of its helper methods that count as assertions.
22
23
  # equivalents: groups of assertion names that make the same check.
23
24
  CONFIG = '.slopbouncer.yml'
@@ -33,10 +34,30 @@ module Slopbouncer
33
34
  # The findings for the commits in a range such as main..HEAD, or for
34
35
  # everything since one commit, committed or not, when given just that.
35
36
  def self.check(target)
36
- base, head = target.split('..', 2)
37
- raise Error, USAGE if base.to_s.empty? || (target.include?('..') && head.to_s.empty?)
37
+ findings_between(*range(target))
38
+ end
39
+
40
+ # What a target compares: one commit with the working tree, or, for a
41
+ # range written a..b or a...b, the commit where a and b last met with b.
42
+ # Either way it is the commits on b that a lacks, as git log reads a..b,
43
+ # so work done on a meanwhile is not taken for b undoing it.
44
+ def self.range(target)
45
+ raise Error, USAGE if target.empty?
46
+ return [target, nil] unless target.include?('..')
47
+
48
+ base, head = target.split(/\.{2,3}/, 2)
49
+ raise Error, USAGE if base.empty? || head.to_s.empty?
38
50
 
39
- findings_between(base, head)
51
+ [Git.merge_base(base, head), head]
52
+ end
53
+
54
+ # The findings for a target, each with the reason the "Test changes:"
55
+ # section of a commit in the range gives it, or nil. Changes since one
56
+ # commit, not all of them committed, have no messages to read.
57
+ def self.explained(target)
58
+ base, head = range(target)
59
+ findings = findings_between(base, head)
60
+ [findings, TestChanges.reasons(head ? Git.messages(base, head) : [], findings)]
40
61
  end
41
62
 
42
63
  # The findings for what is staged for the next commit, as the commit hook
@@ -46,39 +67,45 @@ module Slopbouncer
46
67
  end
47
68
 
48
69
  def self.findings_between(base, head)
70
+ settings = settings(head)
49
71
  helpers = settings.fetch('assertions', [])
50
72
  equivalents = settings.fetch('equivalents', [])
51
73
  changes = Git.changed(base, head)
52
74
  results = changes.filter_map do |changed|
75
+ changed = out_of_the_tests(changed)
53
76
  next unless [changed.before_path, changed.after_path].compact.all? { |path| path.match?(TEST_FILE) }
54
77
 
55
- before = changed.before_path ? RubyTests.in(Git.show(base, changed.before_path), helpers: helpers) : []
56
- after = changed.after_path ? RubyTests.in(Git.show(head, changed.after_path), helpers: helpers) : []
78
+ was = Git.show(base, changed.before_path) if changed.before_path
79
+ now = Git.show(head, changed.after_path) if changed.after_path
57
80
  file = changed.after_path || changed.before_path
58
- [file, Compare.call(file, before, after, equivalents: equivalents)]
81
+ broken = unreadable(file, was, now)
82
+ next [file, broken] if broken
83
+
84
+ [file, Compare.call(file, was ? RubyTests.in(was, helpers: helpers) : [], now ? RubyTests.in(now, helpers: helpers) : [],
85
+ equivalents: equivalents)]
59
86
  end
60
- findings = results.flat_map { |_file, result| result.findings } + deleted(results) +
61
- equivalents_changed(changes, base, head) + helpers_changed(changes, base, head, helpers)
62
- findings.each { |finding| finding.reworded = nil } unless tests_only?(changes)
63
- findings
87
+ results.flat_map { |_file, result| result.findings } + deleted(results) +
88
+ equivalents_changed(changes, base, head) + helpers_changed(changes, base, head, helpers)
64
89
  end
65
90
 
66
91
  # A method the tests define whose body changed, and that tests assert
67
92
  # through, is reported once with how many tests depend on it. Slopbouncer
68
93
  # cannot judge such a change, so it asks for it to be looked at.
69
94
  def self.helpers_changed(changes, base, head, helpers)
95
+ read = nil
96
+ every_test = -> { read ||= tests_everywhere(head, helpers) }
70
97
  changes.select { |changed| changed.before_path && changed.after_path&.end_with?('.rb') && in_tests?(changed.after_path) }
71
- .flat_map { |changed| changed_helpers(changed, base, head, helpers) }
98
+ .flat_map { |changed| changed_helpers(changed, base, head, helpers, every_test) }
72
99
  end
73
100
 
74
- def self.changed_helpers(changed, base, head, helpers)
101
+ def self.changed_helpers(changed, base, head, helpers, every_test)
75
102
  was = RubyTests.methods_in(Git.show(base, changed.before_path))
76
103
  source = Git.show(head, changed.after_path)
77
104
  now = RubyTests.methods_in(source)
78
105
  altered = now.keys.select { |name| was.key?(name) && was[name] != now[name] }
79
106
  return [] if altered.empty?
80
107
 
81
- tests = dependants_of(source, head, helpers)
108
+ tests = dependants_of(source, helpers, every_test)
82
109
  altered.filter_map do |name|
83
110
  count = tests.count { |test| test.assertions.any? { |assertion| assertion.names.include?(name) } }
84
111
  next if count.zero?
@@ -90,11 +117,14 @@ module Slopbouncer
90
117
  end
91
118
 
92
119
  # The tests that could call a helper: those in its own file when it holds
93
- # tests, and otherwise those in every test file.
94
- def self.dependants_of(source, head, helpers)
120
+ # tests, and otherwise those in every test file, read once however many
121
+ # helpers changed.
122
+ def self.dependants_of(source, helpers, every_test)
95
123
  own = RubyTests.in(source, helpers: helpers)
96
- return own if own.any?
124
+ own.any? ? own : every_test.call
125
+ end
97
126
 
127
+ def self.tests_everywhere(head, helpers)
98
128
  Git.files(head).select { |file| file.match?(TEST_FILE) }.flat_map do |file|
99
129
  RubyTests.in(Git.show(head, file), helpers: helpers)
100
130
  rescue Errno::ENOENT
@@ -106,22 +136,6 @@ module Slopbouncer
106
136
  path.match?(TEST_FILE) || path.match?(TEST_DIRECTORY)
107
137
  end
108
138
 
109
- # A change touches only the tests when every file it changed is a test, is
110
- # in a test directory, or is the settings file that says how tests are read.
111
- def self.tests_only?(changes)
112
- changes.all? do |changed|
113
- [changed.before_path, changed.after_path].compact.all? { |path| in_tests?(path) || path == CONFIG }
114
- end
115
- end
116
-
117
- # One line for the assertions a change that only touches tests rewrote.
118
- def self.test_refactor(reworded)
119
- count = reworded.size
120
- files = reworded.map(&:file).uniq.size
121
- "Test refactor: #{count} #{count == 1 ? 'assertion' : 'assertions'} reworded in #{files} #{files == 1 ? 'file' : 'files'}, " \
122
- 'no other code changed.'
123
- end
124
-
125
139
  # Declaring assertions equivalent quiets Slopbouncer about them, so a
126
140
  # change to what is declared is a finding of its own, naming each group of
127
141
  # names added and removed.
@@ -145,6 +159,26 @@ module Slopbouncer
145
159
  raise Error, "#{CONFIG} could not be read: #{e.message}"
146
160
  end
147
161
 
162
+ # A version of a test file that does not parse cannot be compared, since
163
+ # what parses is only part of it, so the file is reported once as
164
+ # unreadable, with where it stopped parsing, and nothing else.
165
+ def self.unreadable(file, was, now)
166
+ side, error = { 'before' => was, 'after' => now }.filter_map { |name, source| [name, RubyTests.unreadable(source)] if source }
167
+ .find(&:last)
168
+ return unless error
169
+
170
+ finding = Compare::Finding.new(file: file, test: 'whole file', rule: 'unreadable', detail: "could not be parsed #{side} the change, at #{error}")
171
+ Compare::Result.new(findings: [finding], gone: [], fresh: [])
172
+ end
173
+
174
+ # A test file moved to where no test is looked for, such as lib/, is read as
175
+ # deleted: its tests no longer run.
176
+ def self.out_of_the_tests(changed)
177
+ return changed unless changed.before_path&.match?(TEST_FILE) && changed.after_path && !changed.after_path.match?(TEST_FILE)
178
+
179
+ Git::Changed.new(before_path: changed.before_path)
180
+ end
181
+
148
182
  # A test that has gone from one file is deleted unless the same test, body
149
183
  # for body, is new in another file of the same change.
150
184
  def self.deleted(results)
@@ -154,8 +188,16 @@ module Slopbouncer
154
188
  end
155
189
  end
156
190
 
157
- def self.settings
158
- File.exist?(CONFIG) ? YAML.safe_load_file(CONFIG) || {} : {}
191
+ # The project's settings, as the change has them, like the tests: staged at
192
+ # commit time, in the last commit of a range, and at the root of the working
193
+ # tree for work not yet committed, wherever the command runs. A settings file
194
+ # left out of what is checked would otherwise quiet a finding with no record
195
+ # of it.
196
+ def self.settings(head)
197
+ text = if head then (Git.show(head, CONFIG) if Git.files(head).include?(CONFIG))
198
+ elsif File.exist?(File.join(Git.root, CONFIG)) then Git.show(nil, CONFIG)
199
+ end
200
+ text ? YAML.safe_load(text) || {} : {}
159
201
  rescue Psych::Exception => e
160
202
  raise Error, "#{CONFIG} could not be read: #{e.message}"
161
203
  end
@@ -173,9 +215,10 @@ module Slopbouncer
173
215
  agent = arguments.find { |argument| argument == '--agent' || argument.start_with?('--agent=') }
174
216
  return stop_hook(target.to_s, input, agent.split('=', 2)[1] || Agents::DEFAULT) if agent
175
217
 
176
- findings = check(target.to_s)
177
- puts printed(findings, arguments)
178
- arguments.include?('--strict') && findings.any? ? 1 : 0
218
+ findings, reasons = explained(target.to_s)
219
+ unexplained = findings.zip(reasons).filter_map { |finding, reason| finding unless reason }
220
+ puts printed(unexplained, findings.zip(reasons).select(&:last), arguments)
221
+ arguments.include?('--strict') && unexplained.any? ? 1 : 0
179
222
  rescue Error => e
180
223
  warn e.message
181
224
  2
@@ -191,7 +234,8 @@ module Slopbouncer
191
234
  Slopbouncer flags tests that got weaker in a change. It reads the code and never runs it.
192
235
 
193
236
  Usage:
194
- slopbouncer main..HEAD the commits in a range
237
+ slopbouncer main..HEAD the commits on HEAD that main lacks: from where they
238
+ last met to HEAD (main...HEAD means the same)
195
239
  slopbouncer HEAD everything since one commit, committed or not
196
240
  slopbouncer install set up the commit hook in this repository
197
241
  slopbouncer install AGENT the commit hook, and for claude-code a rule denying --no-verify
@@ -199,7 +243,7 @@ module Slopbouncer
199
243
 
200
244
  Options:
201
245
  --json print the findings as JSON
202
- --strict exit with 1 when anything is flagged
246
+ --strict exit with 1 when anything is flagged that no commit message in the range explains
203
247
  --all at a terminal, list every finding however many there are
204
248
  --color use the terminal layout, in colour, wherever the output goes
205
249
  --agent=NAME run as that coding agent's turn-end hook
@@ -214,11 +258,23 @@ module Slopbouncer
214
258
  # a terminal or colour is asked for, and one plain line for each finding
215
259
  # when it goes to another program. What a program reads is always complete;
216
260
  # what a person reads is counted up when it is long, unless they ask for all.
217
- def self.printed(findings, arguments)
218
- return JSON.pretty_generate(findings.map { |finding| finding.to_h.compact }) if arguments.include?('--json')
261
+ # Explained findings come after the rest, each with its reason.
262
+ def self.printed(findings, explained, arguments)
263
+ if arguments.include?('--json')
264
+ return JSON.pretty_generate(findings.map { |finding| finding.to_h.compact } +
265
+ explained.map { |finding, reason| finding.to_h.compact.merge(reason: reason) })
266
+ end
267
+
268
+ reasons = explained.map { |finding, reason| "#{report([finding])}: explained: #{reason}" }
269
+ [unexplained_shown(findings, explained, arguments), reasons.join("\n")].compact.reject(&:empty?).join("\n\n")
270
+ end
219
271
 
272
+ # The findings nothing explains, as printed or laid out. At a terminal,
273
+ # with only explained findings, there is nothing to lay out.
274
+ def self.unexplained_shown(findings, explained, arguments)
220
275
  asked = arguments.intersect?(%w[--color --colour])
221
276
  return report(findings) unless asked || $stdout.tty?
277
+ return if findings.empty? && explained.any?
222
278
 
223
279
  TerminalReport.call(findings, colour: asked || ENV['NO_COLOR'].to_s.empty?, all: arguments.include?('--all'))
224
280
  end
@@ -251,7 +307,7 @@ module Slopbouncer
251
307
  Stopped: #{totals['stopped']}
252
308
  Findings: #{totals['findings']}
253
309
  Explained in the commit message: #{totals['explained_in_message']}
254
- Already explained (repeats not asked about): #{totals['already_explained']}
310
+ Reason offered from memory: #{totals['reason_offered_from_memory']}
255
311
  Not checked, because Slopbouncer could not run: #{totals['not_checked']}
256
312
  STATS
257
313
  end
@@ -266,9 +322,7 @@ module Slopbouncer
266
322
  told = hook_input(input)
267
323
  return 0 if agent.stopped_before?(told)
268
324
 
269
- # A rewording in a change that only touches tests is not worth sending
270
- # the agent back for.
271
- findings = Dir.chdir(working_directory(told)) { Explained.unseen(check(target)) }.reject(&:reworded)
325
+ findings = Dir.chdir(working_directory(told)) { Explained.unseen(check(target)) }
272
326
  return 0 if findings.empty?
273
327
 
274
328
  output, error, status = agent.block(to_the_agent(findings, target))
@@ -318,5 +372,5 @@ module Slopbouncer
318
372
  "#{finding.file}: #{finding.test}: #{finding.rule}: #{what}"
319
373
  }.join("\n")
320
374
  end
321
- private_class_method :findings_between, :deleted, :helpers_changed, :changed_helpers, :dependants_of, :in_tests?, :tests_only?, :equivalents_changed, :declared, :settings, :said, :help, :printed, :install, :stats, :stop_hook, :working_directory, :hook_input, :to_the_agent
375
+ private_class_method :range, :explained, :unexplained_shown, :findings_between, :unreadable, :out_of_the_tests, :deleted, :helpers_changed, :changed_helpers, :dependants_of, :tests_everywhere, :in_tests?, :equivalents_changed, :declared, :settings, :said, :help, :printed, :install, :stats, :stop_hook, :working_directory, :hook_input, :to_the_agent
322
376
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slopbouncer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kipple Pty Ltd
@@ -54,6 +54,7 @@ files:
54
54
  - lib/slopbouncer/install.rb
55
55
  - lib/slopbouncer/ruby_tests.rb
56
56
  - lib/slopbouncer/terminal_report.rb
57
+ - lib/slopbouncer/test_changes.rb
57
58
  - lib/slopbouncer/version.rb
58
59
  homepage: https://github.com/mainline-dev/slopbouncer
59
60
  licenses: