slopbouncer 0.5.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 +4 -4
- data/CHANGELOG.md +19 -0
- data/README.md +45 -11
- data/lib/slopbouncer/commit_hook.rb +23 -36
- data/lib/slopbouncer/commit_log.rb +2 -2
- data/lib/slopbouncer/commit_memory.rb +20 -13
- data/lib/slopbouncer/compare.rb +13 -2
- data/lib/slopbouncer/git.rb +22 -2
- data/lib/slopbouncer/install.rb +31 -7
- data/lib/slopbouncer/ruby_tests.rb +10 -2
- data/lib/slopbouncer/test_changes.rb +64 -0
- data/lib/slopbouncer/version.rb +1 -1
- data/lib/slopbouncer.rb +94 -27
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f2e4fa3622013a8c5600752d04c1378838f51ddac48ae014974f55d7da1c03fa
|
|
4
|
+
data.tar.gz: f8e0be9e6cab037a4ebec289cd7bf686f157aa6dfa75347a2b06dfa12e7b0bad
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 90b83a1e6f306e1ebf2d79702f83b293d453dc2e70c70956b99c952759945d1b6f601d13eaaa62afe43dd4a7099238145bd445b3f6dc2747e30eb30b7fb3cc9a
|
|
7
|
+
data.tar.gz: a16a9f3715c8c1c71c8bee7eade4c2bdf3c3a3ed68cf55a61dbfb908e9dfa505b42e502973c1841ddd0b9440d5c308eb04317be27190fc472ae3bdc102513b4a
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
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
|
+
|
|
5
24
|
## 0.5.0 - 2026-11-27
|
|
6
25
|
|
|
7
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.
|
data/README.md
CHANGED
|
@@ -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
|
|
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
|
|
|
@@ -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
|
|
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
|
+
|
|
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.
|
|
66
75
|
|
|
67
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
| `
|
|
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 |
|
|
@@ -174,6 +189,15 @@ equivalents:
|
|
|
174
189
|
- [assert_selector, assert_shown]
|
|
175
190
|
```
|
|
176
191
|
|
|
192
|
+
For RSpec, declare the matchers:
|
|
193
|
+
|
|
194
|
+
```yaml
|
|
195
|
+
equivalents:
|
|
196
|
+
- [have_selector, have_shown]
|
|
197
|
+
```
|
|
198
|
+
|
|
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
|
+
|
|
177
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:
|
|
178
202
|
|
|
179
203
|
```
|
|
@@ -193,7 +217,7 @@ Run it from the root of the repository you want checked:
|
|
|
193
217
|
slopbouncer main..HEAD
|
|
194
218
|
```
|
|
195
219
|
|
|
196
|
-
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:
|
|
197
221
|
|
|
198
222
|
```
|
|
199
223
|
test/basket_test.rb
|
|
@@ -219,10 +243,18 @@ test/checkout_test.rb: Checkout lets a guest pay: assertion_removed: assert_equa
|
|
|
219
243
|
test/refund_test.rb: Refund refunds part of an order: expected_value_changed: 100 → 90
|
|
220
244
|
```
|
|
221
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
|
+
|
|
222
254
|
| Option | What it does |
|
|
223
255
|
|---|---|
|
|
224
256
|
| `--json` | Prints the findings as JSON |
|
|
225
|
-
| `--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 |
|
|
226
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 |
|
|
227
259
|
| `--color` | Uses the terminal layout, in colour, wherever the output goes. For a script whose own output a person reads |
|
|
228
260
|
| `--agent=NAME` | Runs as that coding agent's turn-end hook (see above) |
|
|
@@ -252,6 +284,8 @@ assertions:
|
|
|
252
284
|
|
|
253
285
|
A test that only calls helpers Slopbouncer has not been told about looks like a test with no assertions.
|
|
254
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
|
+
|
|
255
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:
|
|
256
290
|
|
|
257
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
|
|
10
|
+
# in a "Test changes:" section (see TestChanges).
|
|
10
11
|
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
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
19
|
findings = Slopbouncer.check_staged
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
CommitMemory.
|
|
26
|
-
CommitLog.record('checked' => true, 'findings' => findings.size, 'explained_in_message' =>
|
|
27
|
-
'
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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,23 +35,10 @@ 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
38
|
# An assertion swapped for another kind is reported as removed, so a
|
|
53
39
|
# commit stopped for one is told how to say the two make the same check.
|
|
54
|
-
|
|
40
|
+
# Each line comes with the reason remembered for it, when there is one.
|
|
41
|
+
def self.stopped(findings, offered)
|
|
55
42
|
declare = "\nIf these make the same check, declare them under equivalents in #{CONFIG}.\n" if findings.any?(&:swapped)
|
|
56
43
|
<<~MESSAGE
|
|
57
44
|
Slopbouncer: this commit looks to weaken #{findings.size} existing #{findings.size == 1 ? 'test' : 'tests'}.
|
|
@@ -60,10 +47,10 @@ module Slopbouncer
|
|
|
60
47
|
#{declare}
|
|
61
48
|
Put each test back, or add this to the commit message, with a reason on each line:
|
|
62
49
|
|
|
63
|
-
#{SECTION}
|
|
64
|
-
#{findings
|
|
50
|
+
#{TestChanges::SECTION}
|
|
51
|
+
#{TestChanges.lines(findings, offered).join("\n")}
|
|
65
52
|
MESSAGE
|
|
66
53
|
end
|
|
67
|
-
private_class_method :
|
|
54
|
+
private_class_method :stopped
|
|
68
55
|
end
|
|
69
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
|
|
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
|
-
'
|
|
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
|
|
10
|
-
# again, after a reset or an amend,
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
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
|
-
|
|
17
|
+
NEVER_OFFERED = %w[equivalents_changed].freeze
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
23
|
-
|
|
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('
|
|
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))
|
|
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
|
data/lib/slopbouncer/compare.rb
CHANGED
|
@@ -136,8 +136,19 @@ module Slopbouncer
|
|
|
136
136
|
was.form == now.form || (equivalent?(was.method, now.method, equivalents) && was.forms == now.forms)
|
|
137
137
|
end
|
|
138
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.
|
|
139
143
|
def self.equivalent?(one, other, equivalents)
|
|
140
|
-
|
|
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) }
|
|
141
152
|
end
|
|
142
153
|
|
|
143
154
|
# A test with no assertions of its own still asserts when a hook in its
|
|
@@ -162,6 +173,6 @@ module Slopbouncer
|
|
|
162
173
|
differing.first if differing.size == 1
|
|
163
174
|
end
|
|
164
175
|
private_class_method :same_name, :pair, :without, :reworded, :rewritten?, :most_alike, :one_edit?, :likeness, :weaker,
|
|
165
|
-
:asserts_nothing?, :edited_value, :edited_place, :same_check?, :equivalent?, :mark_swapped
|
|
176
|
+
:asserts_nothing?, :edited_value, :edited_place, :same_check?, :equivalent?, :declared?, :mark_swapped
|
|
166
177
|
end
|
|
167
178
|
end
|
data/lib/slopbouncer/git.rb
CHANGED
|
@@ -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
|
-
|
|
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
|
|
117
|
+
private_class_method :repository?, :run
|
|
98
118
|
end
|
|
99
119
|
end
|
data/lib/slopbouncer/install.rb
CHANGED
|
@@ -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
|
-
#
|
|
17
|
-
|
|
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
|
-
|
|
40
|
-
|
|
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
|
|
52
|
-
|
|
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)
|
|
@@ -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
|
data/lib/slopbouncer/version.rb
CHANGED
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,
|
|
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
|
-
|
|
37
|
-
|
|
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?
|
|
50
|
+
|
|
51
|
+
[Git.merge_base(base, head), head]
|
|
52
|
+
end
|
|
38
53
|
|
|
39
|
-
|
|
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
|
|
@@ -51,12 +72,17 @@ module Slopbouncer
|
|
|
51
72
|
equivalents = settings.fetch('equivalents', [])
|
|
52
73
|
changes = Git.changed(base, head)
|
|
53
74
|
results = changes.filter_map do |changed|
|
|
75
|
+
changed = out_of_the_tests(changed)
|
|
54
76
|
next unless [changed.before_path, changed.after_path].compact.all? { |path| path.match?(TEST_FILE) }
|
|
55
77
|
|
|
56
|
-
|
|
57
|
-
|
|
78
|
+
was = Git.show(base, changed.before_path) if changed.before_path
|
|
79
|
+
now = Git.show(head, changed.after_path) if changed.after_path
|
|
58
80
|
file = changed.after_path || changed.before_path
|
|
59
|
-
|
|
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)]
|
|
60
86
|
end
|
|
61
87
|
results.flat_map { |_file, result| result.findings } + deleted(results) +
|
|
62
88
|
equivalents_changed(changes, base, head) + helpers_changed(changes, base, head, helpers)
|
|
@@ -66,18 +92,20 @@ module Slopbouncer
|
|
|
66
92
|
# through, is reported once with how many tests depend on it. Slopbouncer
|
|
67
93
|
# cannot judge such a change, so it asks for it to be looked at.
|
|
68
94
|
def self.helpers_changed(changes, base, head, helpers)
|
|
95
|
+
read = nil
|
|
96
|
+
every_test = -> { read ||= tests_everywhere(head, helpers) }
|
|
69
97
|
changes.select { |changed| changed.before_path && changed.after_path&.end_with?('.rb') && in_tests?(changed.after_path) }
|
|
70
|
-
.flat_map { |changed| changed_helpers(changed, base, head, helpers) }
|
|
98
|
+
.flat_map { |changed| changed_helpers(changed, base, head, helpers, every_test) }
|
|
71
99
|
end
|
|
72
100
|
|
|
73
|
-
def self.changed_helpers(changed, base, head, helpers)
|
|
101
|
+
def self.changed_helpers(changed, base, head, helpers, every_test)
|
|
74
102
|
was = RubyTests.methods_in(Git.show(base, changed.before_path))
|
|
75
103
|
source = Git.show(head, changed.after_path)
|
|
76
104
|
now = RubyTests.methods_in(source)
|
|
77
105
|
altered = now.keys.select { |name| was.key?(name) && was[name] != now[name] }
|
|
78
106
|
return [] if altered.empty?
|
|
79
107
|
|
|
80
|
-
tests = dependants_of(source,
|
|
108
|
+
tests = dependants_of(source, helpers, every_test)
|
|
81
109
|
altered.filter_map do |name|
|
|
82
110
|
count = tests.count { |test| test.assertions.any? { |assertion| assertion.names.include?(name) } }
|
|
83
111
|
next if count.zero?
|
|
@@ -89,11 +117,14 @@ module Slopbouncer
|
|
|
89
117
|
end
|
|
90
118
|
|
|
91
119
|
# The tests that could call a helper: those in its own file when it holds
|
|
92
|
-
# tests, and otherwise those in every test file
|
|
93
|
-
|
|
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)
|
|
94
123
|
own = RubyTests.in(source, helpers: helpers)
|
|
95
|
-
|
|
124
|
+
own.any? ? own : every_test.call
|
|
125
|
+
end
|
|
96
126
|
|
|
127
|
+
def self.tests_everywhere(head, helpers)
|
|
97
128
|
Git.files(head).select { |file| file.match?(TEST_FILE) }.flat_map do |file|
|
|
98
129
|
RubyTests.in(Git.show(head, file), helpers: helpers)
|
|
99
130
|
rescue Errno::ENOENT
|
|
@@ -128,6 +159,26 @@ module Slopbouncer
|
|
|
128
159
|
raise Error, "#{CONFIG} could not be read: #{e.message}"
|
|
129
160
|
end
|
|
130
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
|
+
|
|
131
182
|
# A test that has gone from one file is deleted unless the same test, body
|
|
132
183
|
# for body, is new in another file of the same change.
|
|
133
184
|
def self.deleted(results)
|
|
@@ -137,12 +188,14 @@ module Slopbouncer
|
|
|
137
188
|
end
|
|
138
189
|
end
|
|
139
190
|
|
|
140
|
-
# The project's settings
|
|
141
|
-
#
|
|
142
|
-
#
|
|
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.
|
|
143
196
|
def self.settings(head)
|
|
144
|
-
text = if head
|
|
145
|
-
elsif File.exist?(CONFIG) then
|
|
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)
|
|
146
199
|
end
|
|
147
200
|
text ? YAML.safe_load(text) || {} : {}
|
|
148
201
|
rescue Psych::Exception => e
|
|
@@ -162,9 +215,10 @@ module Slopbouncer
|
|
|
162
215
|
agent = arguments.find { |argument| argument == '--agent' || argument.start_with?('--agent=') }
|
|
163
216
|
return stop_hook(target.to_s, input, agent.split('=', 2)[1] || Agents::DEFAULT) if agent
|
|
164
217
|
|
|
165
|
-
findings =
|
|
166
|
-
|
|
167
|
-
|
|
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
|
|
168
222
|
rescue Error => e
|
|
169
223
|
warn e.message
|
|
170
224
|
2
|
|
@@ -180,7 +234,8 @@ module Slopbouncer
|
|
|
180
234
|
Slopbouncer flags tests that got weaker in a change. It reads the code and never runs it.
|
|
181
235
|
|
|
182
236
|
Usage:
|
|
183
|
-
slopbouncer main..HEAD the commits
|
|
237
|
+
slopbouncer main..HEAD the commits on HEAD that main lacks: from where they
|
|
238
|
+
last met to HEAD (main...HEAD means the same)
|
|
184
239
|
slopbouncer HEAD everything since one commit, committed or not
|
|
185
240
|
slopbouncer install set up the commit hook in this repository
|
|
186
241
|
slopbouncer install AGENT the commit hook, and for claude-code a rule denying --no-verify
|
|
@@ -188,7 +243,7 @@ module Slopbouncer
|
|
|
188
243
|
|
|
189
244
|
Options:
|
|
190
245
|
--json print the findings as JSON
|
|
191
|
-
--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
|
|
192
247
|
--all at a terminal, list every finding however many there are
|
|
193
248
|
--color use the terminal layout, in colour, wherever the output goes
|
|
194
249
|
--agent=NAME run as that coding agent's turn-end hook
|
|
@@ -203,11 +258,23 @@ module Slopbouncer
|
|
|
203
258
|
# a terminal or colour is asked for, and one plain line for each finding
|
|
204
259
|
# when it goes to another program. What a program reads is always complete;
|
|
205
260
|
# what a person reads is counted up when it is long, unless they ask for all.
|
|
206
|
-
|
|
207
|
-
|
|
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
|
|
208
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)
|
|
209
275
|
asked = arguments.intersect?(%w[--color --colour])
|
|
210
276
|
return report(findings) unless asked || $stdout.tty?
|
|
277
|
+
return if findings.empty? && explained.any?
|
|
211
278
|
|
|
212
279
|
TerminalReport.call(findings, colour: asked || ENV['NO_COLOR'].to_s.empty?, all: arguments.include?('--all'))
|
|
213
280
|
end
|
|
@@ -240,7 +307,7 @@ module Slopbouncer
|
|
|
240
307
|
Stopped: #{totals['stopped']}
|
|
241
308
|
Findings: #{totals['findings']}
|
|
242
309
|
Explained in the commit message: #{totals['explained_in_message']}
|
|
243
|
-
|
|
310
|
+
Reason offered from memory: #{totals['reason_offered_from_memory']}
|
|
244
311
|
Not checked, because Slopbouncer could not run: #{totals['not_checked']}
|
|
245
312
|
STATS
|
|
246
313
|
end
|
|
@@ -305,5 +372,5 @@ module Slopbouncer
|
|
|
305
372
|
"#{finding.file}: #{finding.test}: #{finding.rule}: #{what}"
|
|
306
373
|
}.join("\n")
|
|
307
374
|
end
|
|
308
|
-
private_class_method :findings_between, :deleted, :helpers_changed, :changed_helpers, :dependants_of, :in_tests?, :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
|
|
309
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
|
+
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:
|