slopbouncer 0.4.0 → 0.5.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: cf15ac41208be921538621451a991e941da50e9a1067a14be4f143628d02873b
4
+ data.tar.gz: e19bf239389683de982f9a73b001ad51b8543393950c542f19727fd70c2f7ff6
5
5
  SHA512:
6
- metadata.gz: aab2c9d00992a192944112d05bfc8d38c9482405810f2ee940e00513679e58eeff09ce08fe0a326b7ae1a0c024c57fa65ba892ca4f61eabd7355fafbfeb23908
7
- data.tar.gz: 85a0b413d120416d88806af0eda0a2c64805dd06cd29158d572890bbf4fd13633822ca95777404cabb667fd1d6e12dacd3892ce5471648645058c2fb4bfc7d63
6
+ metadata.gz: c9d1953b5085efad693627020a12296454d9d90705ca35488878021f5b554a2db55eaa114aa3096eb4e0a13beb95982d6aee6a9afea7fabbd5bf638eba5fe338
7
+ data.tar.gz: 5f0572369485163b6e3da6e6bd2fee05ae9a8c730a49f3c3ec0f6bdd0a1005dad7398ff655525656150bc1f2d1667680a89d6c358e117768839dc7de8e960311
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  What changed in each version of Slopbouncer.
4
4
 
5
+ ## 0.5.0 - 2026-11-27
6
+
7
+ - 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.
8
+ - 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.
9
+ - A commit stopped for a swap is told: "If these make the same check, declare them under equivalents in .slopbouncer.yml."
10
+ - 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.
11
+ - In `--json`, the finding for a swapped assertion carries `"swapped": true`. The `reworded` key is gone.
12
+
5
13
  ## 0.4.0 - 2026-11-17
6
14
 
7
15
  - 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
 
@@ -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.
@@ -64,7 +64,7 @@ Test changes:
64
64
 
65
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.
66
66
 
67
- A change that only rewords tests, with no other code changed, is not stopped.
67
+ 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
68
 
69
69
  ## How often it stops a commit
70
70
 
@@ -163,15 +163,25 @@ The turn-end hook remembers what it has shown the agent for the work in hand, an
163
163
  - A test that was reworded, moved to another file, or both, with its checks unchanged.
164
164
  - Files that are not tests.
165
165
 
166
- ## A change that only rewords tests
166
+ ## Moving tests from one assertion to another
167
167
 
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:
168
+ 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
169
 
170
+ When you move tests to a helper that makes the same check, declare the pair equivalent in the same commit:
171
+
172
+ ```yaml
173
+ equivalents:
174
+ - [assert_selector, assert_shown]
170
175
  ```
171
- Test refactor: 23 assertions reworded in 6 files, no other code changed.
176
+
177
+ 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
+
172
179
  ```
180
+ Assert through assert_shown
173
181
 
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.
182
+ Test changes:
183
+ - .slopbouncer.yml: equivalent assertions: assert_shown wraps assert_selector
184
+ ```
175
185
 
176
186
  ## Running it yourself and in a pipeline
177
187
 
@@ -19,7 +19,7 @@ module Slopbouncer
19
19
 
20
20
  def self.call(message_path)
21
21
  message = File.read(message_path)
22
- findings = Slopbouncer.check_staged.reject(&:reworded)
22
+ findings = Slopbouncer.check_staged
23
23
  remembered, unknown = findings.partition { |finding| CommitMemory.known?(finding) }
24
24
  explained, unexplained = unknown.partition { |finding| explains?(message, finding) }
25
25
  CommitMemory.remember(explained)
@@ -49,12 +49,15 @@ module Slopbouncer
49
49
  after.take_while { |line| line.start_with?('- ') }
50
50
  end
51
51
 
52
+ # An assertion swapped for another kind is reported as removed, so a
53
+ # commit stopped for one is told how to say the two make the same check.
52
54
  def self.stopped(findings)
55
+ declare = "\nIf these make the same check, declare them under equivalents in #{CONFIG}.\n" if findings.any?(&:swapped)
53
56
  <<~MESSAGE
54
57
  Slopbouncer: this commit looks to weaken #{findings.size} existing #{findings.size == 1 ? 'test' : 'tests'}.
55
58
 
56
59
  #{Slopbouncer.report(findings)}
57
-
60
+ #{declare}
58
61
  Put each test back, or add this to the commit message, with a reason on each line:
59
62
 
60
63
  #{SECTION}
@@ -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
@@ -164,6 +162,6 @@ module Slopbouncer
164
162
  differing.first if differing.size == 1
165
163
  end
166
164
  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
165
+ :asserts_nothing?, :edited_value, :edited_place, :same_check?, :equivalent?, :mark_swapped
168
166
  end
169
167
  end
@@ -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 }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slopbouncer
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
data/lib/slopbouncer.rb CHANGED
@@ -46,6 +46,7 @@ module Slopbouncer
46
46
  end
47
47
 
48
48
  def self.findings_between(base, head)
49
+ settings = settings(head)
49
50
  helpers = settings.fetch('assertions', [])
50
51
  equivalents = settings.fetch('equivalents', [])
51
52
  changes = Git.changed(base, head)
@@ -57,10 +58,8 @@ module Slopbouncer
57
58
  file = changed.after_path || changed.before_path
58
59
  [file, Compare.call(file, before, after, equivalents: equivalents)]
59
60
  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
61
+ results.flat_map { |_file, result| result.findings } + deleted(results) +
62
+ equivalents_changed(changes, base, head) + helpers_changed(changes, base, head, helpers)
64
63
  end
65
64
 
66
65
  # A method the tests define whose body changed, and that tests assert
@@ -106,22 +105,6 @@ module Slopbouncer
106
105
  path.match?(TEST_FILE) || path.match?(TEST_DIRECTORY)
107
106
  end
108
107
 
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
108
  # Declaring assertions equivalent quiets Slopbouncer about them, so a
126
109
  # change to what is declared is a finding of its own, naming each group of
127
110
  # names added and removed.
@@ -154,8 +137,14 @@ module Slopbouncer
154
137
  end
155
138
  end
156
139
 
157
- def self.settings
158
- File.exist?(CONFIG) ? YAML.safe_load_file(CONFIG) || {} : {}
140
+ # The project's settings. At commit time they are read as staged, like the
141
+ # tests: a settings file left in the working tree and out of the commit
142
+ # would otherwise quiet a finding with no record of it.
143
+ def self.settings(head)
144
+ text = if head == Git::STAGED then (Git.show(head, CONFIG) if Git.files(head).include?(CONFIG))
145
+ elsif File.exist?(CONFIG) then File.read(CONFIG)
146
+ end
147
+ text ? YAML.safe_load(text) || {} : {}
159
148
  rescue Psych::Exception => e
160
149
  raise Error, "#{CONFIG} could not be read: #{e.message}"
161
150
  end
@@ -266,9 +255,7 @@ module Slopbouncer
266
255
  told = hook_input(input)
267
256
  return 0 if agent.stopped_before?(told)
268
257
 
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)
258
+ findings = Dir.chdir(working_directory(told)) { Explained.unseen(check(target)) }
272
259
  return 0 if findings.empty?
273
260
 
274
261
  output, error, status = agent.block(to_the_agent(findings, target))
@@ -318,5 +305,5 @@ module Slopbouncer
318
305
  "#{finding.file}: #{finding.test}: #{finding.rule}: #{what}"
319
306
  }.join("\n")
320
307
  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
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
322
309
  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.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kipple Pty Ltd