slopbouncer 0.3.0 → 0.4.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.
@@ -4,7 +4,11 @@ module Slopbouncer
4
4
  # The findings laid out for a person at a terminal: each file once, the
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
+ # 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.
7
10
  module TerminalReport
11
+ LIMIT = 30
8
12
  BOLD = 1
9
13
  RED = 31
10
14
  GREEN = 32
@@ -13,18 +17,37 @@ module Slopbouncer
13
17
  COLOURS = { 'test_deleted' => RED, 'assertion_removed' => RED, 'no_assertions' => RED,
14
18
  'expected_value_changed' => YELLOW, 'skip_added' => YELLOW }.freeze
15
19
 
16
- def self.call(findings, colour:)
20
+ 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
+ listed(findings, colour, all)
25
+ end
26
+
27
+ # The findings as a person reads them, or nothing for none.
28
+ def self.listed(findings, colour, all, quiet: false)
17
29
  paint = ->(text, code) { colour ? "\e[#{code}m#{text}\e[0m" : text }
18
- return paint.call('No tests got weaker.', GREEN) if findings.empty?
30
+ return (quiet ? nil : paint.call('No tests look weaker.', GREEN)) if findings.empty?
19
31
 
20
32
  files = findings.group_by(&:file)
21
33
  tests = files.sum { |_file, found| found.map(&:test).uniq.size }
22
- [*files.map { |file, found| file(file, found, paint) }, count(tests, files.size)].join("\n\n")
34
+ return [*files.map { |file, found| file(file, found, paint) }, count(tests, files.size)].join("\n\n") if all || findings.size <= LIMIT
35
+
36
+ [*files.map { |file, found| counted(file, found, paint) }, "#{count(tests, files.size)}\n--all lists every finding."].join("\n\n")
37
+ end
38
+
39
+ # A file with how many times each rule was broken in it.
40
+ def self.counted(file, found, paint)
41
+ [paint.call(file, BOLD), *found.group_by(&:rule).map { |rule, broken| " #{rule(rule, paint)} #{broken.size}" }].join("\n")
42
+ end
43
+
44
+ def self.rule(rule, paint)
45
+ paint.call(rule, COLOURS.fetch(rule, YELLOW))
23
46
  end
24
47
 
25
48
  def self.file(file, found, paint)
26
49
  tests = found.group_by(&:test).flat_map do |test, about|
27
- [" #{test}", *about.map { |finding| " #{paint.call(finding.rule, COLOURS.fetch(finding.rule, YELLOW))} #{detail(finding)}" }]
50
+ [" #{test}", *about.map { |finding| " #{rule(finding.rule, paint)} #{detail(finding)}" }]
28
51
  end
29
52
  [paint.call(file, BOLD), *tests].join("\n")
30
53
  end
@@ -34,8 +57,8 @@ module Slopbouncer
34
57
  end
35
58
 
36
59
  def self.count(tests, files)
37
- "#{tests} #{tests == 1 ? 'test' : 'tests'} got weaker in #{files} #{files == 1 ? 'file' : 'files'}"
60
+ "#{tests} #{tests == 1 ? 'test looks' : 'tests look'} weaker in #{files} #{files == 1 ? 'file' : 'files'}"
38
61
  end
39
- private_class_method :file, :detail, :count
62
+ private_class_method :listed, :counted, :rule, :file, :detail, :count
40
63
  end
41
64
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slopbouncer
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/slopbouncer.rb CHANGED
@@ -6,8 +6,10 @@ require_relative 'slopbouncer/version'
6
6
  require_relative 'slopbouncer/git'
7
7
  require_relative 'slopbouncer/ruby_tests'
8
8
  require_relative 'slopbouncer/compare'
9
+ require_relative 'slopbouncer/explained'
9
10
  require_relative 'slopbouncer/agents'
10
11
  require_relative 'slopbouncer/install'
12
+ require_relative 'slopbouncer/commit_hook'
11
13
  require_relative 'slopbouncer/terminal_report'
12
14
 
13
15
  # A static check that flags tests which got weaker in a change, so a person
@@ -17,9 +19,14 @@ module Slopbouncer
17
19
 
18
20
  # A project's own settings, in the directory the command is run from:
19
21
  # assertions: the names of its helper methods that count as assertions.
22
+ # equivalents: groups of assertion names that make the same check.
20
23
  CONFIG = '.slopbouncer.yml'
21
24
 
22
25
  TEST_FILE = %r{(\A|/)(test|spec)/.*\.rb\z|(\A|/)(test_[^/]*|[^/]*_(test|spec))\.rb\z}
26
+ TEST_DIRECTORY = %r{(\A|/)(test|spec)/}
27
+
28
+ # How many findings a coding agent is told about in full.
29
+ TOLD_TO_AN_AGENT = 20
23
30
 
24
31
  USAGE = 'Give a range of commits, such as main..HEAD, or one commit to compare the working tree with, such as HEAD'
25
32
 
@@ -29,15 +36,113 @@ module Slopbouncer
29
36
  base, head = target.split('..', 2)
30
37
  raise Error, USAGE if base.to_s.empty? || (target.include?('..') && head.to_s.empty?)
31
38
 
39
+ findings_between(base, head)
40
+ end
41
+
42
+ # The findings for what is staged for the next commit, as the commit hook
43
+ # reads it: only what git will commit, whatever else is in the working tree.
44
+ def self.check_staged
45
+ findings_between(Git.staged_base, Git::STAGED)
46
+ end
47
+
48
+ def self.findings_between(base, head)
32
49
  helpers = settings.fetch('assertions', [])
33
- results = Git.changed(base, head).filter_map do |changed|
50
+ equivalents = settings.fetch('equivalents', [])
51
+ changes = Git.changed(base, head)
52
+ results = changes.filter_map do |changed|
34
53
  next unless [changed.before_path, changed.after_path].compact.all? { |path| path.match?(TEST_FILE) }
35
54
 
36
55
  before = changed.before_path ? RubyTests.in(Git.show(base, changed.before_path), helpers: helpers) : []
37
56
  after = changed.after_path ? RubyTests.in(Git.show(head, changed.after_path), helpers: helpers) : []
38
- [changed.after_path || changed.before_path, Compare.call(changed.after_path || changed.before_path, before, after)]
57
+ file = changed.after_path || changed.before_path
58
+ [file, Compare.call(file, before, after, equivalents: equivalents)]
59
+ 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
64
+ end
65
+
66
+ # A method the tests define whose body changed, and that tests assert
67
+ # through, is reported once with how many tests depend on it. Slopbouncer
68
+ # cannot judge such a change, so it asks for it to be looked at.
69
+ def self.helpers_changed(changes, base, head, helpers)
70
+ 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) }
72
+ end
73
+
74
+ def self.changed_helpers(changed, base, head, helpers)
75
+ was = RubyTests.methods_in(Git.show(base, changed.before_path))
76
+ source = Git.show(head, changed.after_path)
77
+ now = RubyTests.methods_in(source)
78
+ altered = now.keys.select { |name| was.key?(name) && was[name] != now[name] }
79
+ return [] if altered.empty?
80
+
81
+ tests = dependants_of(source, head, helpers)
82
+ altered.filter_map do |name|
83
+ count = tests.count { |test| test.assertions.any? { |assertion| assertion.names.include?(name) } }
84
+ next if count.zero?
85
+
86
+ Compare::Finding.new(file: changed.after_path, test: name, rule: 'helper_changed',
87
+ detail: "#{name} changed, and #{count} #{count == 1 ? 'test asserts' : 'tests assert'} through it. " \
88
+ 'Slopbouncer cannot judge a change to a helper, so look at this one.')
89
+ end
90
+ end
91
+
92
+ # 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)
95
+ own = RubyTests.in(source, helpers: helpers)
96
+ return own if own.any?
97
+
98
+ Git.files(head).select { |file| file.match?(TEST_FILE) }.flat_map do |file|
99
+ RubyTests.in(Git.show(head, file), helpers: helpers)
100
+ rescue Errno::ENOENT
101
+ []
39
102
  end
40
- results.flat_map { |_file, result| result.findings } + deleted(results)
103
+ end
104
+
105
+ def self.in_tests?(path)
106
+ path.match?(TEST_FILE) || path.match?(TEST_DIRECTORY)
107
+ end
108
+
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
+ # Declaring assertions equivalent quiets Slopbouncer about them, so a
126
+ # change to what is declared is a finding of its own, naming each group of
127
+ # names added and removed.
128
+ def self.equivalents_changed(changes, base, head)
129
+ changed = changes.find { |file| [file.before_path, file.after_path].include?(CONFIG) }
130
+ return [] unless changed
131
+
132
+ was = declared(changed.before_path && Git.show(base, changed.before_path))
133
+ now = declared(changed.after_path && Git.show(head, changed.after_path))
134
+ said = (now - was).map { |group| "added #{group.join(' = ')}" } + (was - now).map { |group| "removed #{group.join(' = ')}" }
135
+ return [] if said.empty?
136
+
137
+ [Compare::Finding.new(file: CONFIG, test: 'equivalent assertions', rule: 'equivalents_changed', detail: said.join('; '))]
138
+ end
139
+
140
+ # The groups of equivalent names a settings file declares, each in order.
141
+ def self.declared(text)
142
+ groups = text ? (YAML.safe_load(text) || {}).fetch('equivalents', []) : []
143
+ groups.map { |group| Array(group).map(&:to_s).sort }
144
+ rescue Psych::Exception => e
145
+ raise Error, "#{CONFIG} could not be read: #{e.message}"
41
146
  end
42
147
 
43
148
  # A test that has gone from one file is deleted unless the same test, body
@@ -56,8 +161,13 @@ module Slopbouncer
56
161
  end
57
162
 
58
163
  def self.run(arguments, input: $stdin)
164
+ return said(help) if arguments.intersect?(%w[--help -h])
165
+ return said("slopbouncer #{VERSION}") if arguments.intersect?(%w[--version -v])
166
+
59
167
  words = arguments.reject { |argument| argument.start_with?('--') }
60
168
  return install(words.drop(1), arguments) if words.first == 'install'
169
+ return CommitHook.call(words[1].to_s) if words.first == 'commit-msg'
170
+ return stats(arguments) if words.first == 'stats'
61
171
 
62
172
  target = words.first
63
173
  agent = arguments.find { |argument| argument == '--agent' || argument.start_with?('--agent=') }
@@ -71,42 +181,97 @@ module Slopbouncer
71
181
  2
72
182
  end
73
183
 
184
+ def self.said(text)
185
+ puts text
186
+ 0
187
+ end
188
+
189
+ def self.help
190
+ <<~HELP
191
+ Slopbouncer flags tests that got weaker in a change. It reads the code and never runs it.
192
+
193
+ Usage:
194
+ slopbouncer main..HEAD the commits in a range
195
+ slopbouncer HEAD everything since one commit, committed or not
196
+ slopbouncer install set up the commit hook in this repository
197
+ slopbouncer install AGENT the commit hook, and for claude-code a rule denying --no-verify
198
+ slopbouncer stats how often the commit hook has stopped a commit
199
+
200
+ Options:
201
+ --json print the findings as JSON
202
+ --strict exit with 1 when anything is flagged
203
+ --all at a terminal, list every finding however many there are
204
+ --color use the terminal layout, in colour, wherever the output goes
205
+ --agent=NAME run as that coding agent's turn-end hook
206
+ --turn-end with install AGENT, set up its turn-end hook instead: #{Agents::ALL.keys.join(', ')}
207
+ --local with install claude-code, keep what it adds out of the shared settings
208
+ -h, --help print this
209
+ -v, --version print the version
210
+ HELP
211
+ end
212
+
74
213
  # JSON when asked for it. Otherwise laid out for a person when the output is
75
214
  # a terminal or colour is asked for, and one plain line for each finding
76
- # when it goes to another program.
215
+ # when it goes to another program. What a program reads is always complete;
216
+ # what a person reads is counted up when it is long, unless they ask for all.
77
217
  def self.printed(findings, arguments)
78
218
  return JSON.pretty_generate(findings.map { |finding| finding.to_h.compact }) if arguments.include?('--json')
79
219
 
80
220
  asked = arguments.intersect?(%w[--color --colour])
81
221
  return report(findings) unless asked || $stdout.tty?
82
222
 
83
- TerminalReport.call(findings, colour: asked || ENV['NO_COLOR'].to_s.empty?)
223
+ TerminalReport.call(findings, colour: asked || ENV['NO_COLOR'].to_s.empty?, all: arguments.include?('--all'))
84
224
  end
85
225
 
86
- # Sets a coding agent up in the current directory: slopbouncer install claude-code
226
+ # Sets the commit hook up in the current repository, for a coding agent if
227
+ # one is named: slopbouncer install claude-code. With --turn-end, sets up
228
+ # that agent's turn-end hook instead.
87
229
  def self.install(words, arguments)
88
- raise Error, "Say which agent to set up: #{Agents::ALL.keys.join(', ')}" if words.empty?
230
+ local = arguments.include?('--local')
231
+ if arguments.include?('--turn-end')
232
+ raise Error, "Say which agent's turn-end hook to set up: #{Agents::ALL.keys.join(', ')}" if words.empty?
89
233
 
90
- puts Install.call(words.first, local: arguments.include?('--local'))
234
+ puts Install.call(words.first, local: local)
235
+ else
236
+ puts Install.commit_hook(words.first, local: local)
237
+ end
91
238
  0
92
239
  rescue Error => e
93
240
  warn e.message
94
241
  2
95
242
  end
96
243
 
244
+ # The counts the commit hook has logged, as lines, or as JSON when asked.
245
+ def self.stats(arguments)
246
+ totals = CommitLog.totals
247
+ return said(JSON.pretty_generate(totals)) if arguments.include?('--json')
248
+
249
+ said(<<~STATS)
250
+ Commits checked: #{totals['checked']}
251
+ Stopped: #{totals['stopped']}
252
+ Findings: #{totals['findings']}
253
+ Explained in the commit message: #{totals['explained_in_message']}
254
+ Already explained (repeats not asked about): #{totals['already_explained']}
255
+ Not checked, because Slopbouncer could not run: #{totals['not_checked']}
256
+ STATS
257
+ end
258
+
97
259
  # Run as a coding agent's stop hook. The agent is stopped from finishing
98
- # once and told what it weakened, in the way its own harness expects. A
99
- # mistake in how the hook is set up never stops the agent: it is said on
100
- # standard error and the agent is let finish.
260
+ # once and told what it weakened, in the way its own harness expects, and
261
+ # only about what it has not been told already. A mistake in how the hook
262
+ # is set up never stops the agent: it is said on standard error and the
263
+ # agent is let finish.
101
264
  def self.stop_hook(target, input, agent_name)
102
265
  agent = Agents.named(agent_name)
103
266
  told = hook_input(input)
104
267
  return 0 if agent.stopped_before?(told)
105
268
 
106
- findings = Dir.chdir(working_directory(told)) { check(target) }
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)
107
272
  return 0 if findings.empty?
108
273
 
109
- output, error, status = agent.block(to_the_agent(findings))
274
+ output, error, status = agent.block(to_the_agent(findings, target))
110
275
  print output
111
276
  warn error unless error.empty?
112
277
  status
@@ -131,12 +296,16 @@ module Slopbouncer
131
296
  {}
132
297
  end
133
298
 
134
- def self.to_the_agent(findings)
299
+ # A coding agent is told about the first few findings, since a mechanical
300
+ # rewrite can make hundreds, and how to list the rest.
301
+ def self.to_the_agent(findings, target)
302
+ more = findings.size - TOLD_TO_AN_AGENT
303
+ rest = "\nand #{more} more. Run `slopbouncer #{target}` to list them all.\n" if more.positive?
135
304
  <<~MESSAGE
136
- Slopbouncer: this change weakens #{findings.size} existing #{findings.size == 1 ? 'test' : 'tests'}.
137
-
138
- #{report(findings)}
305
+ Slopbouncer: this change looks to weaken #{findings.size} existing #{findings.size == 1 ? 'test' : 'tests'}.
139
306
 
307
+ #{report(findings.first(TOLD_TO_AN_AGENT))}
308
+ #{rest}
140
309
  For each one, do one of two things before you finish:
141
310
  1. Put the test back as it was, and make the code pass it.
142
311
  2. Leave the change, and tell the person which behaviour changed and why the test had to change with it.
@@ -149,5 +318,5 @@ module Slopbouncer
149
318
  "#{finding.file}: #{finding.test}: #{finding.rule}: #{what}"
150
319
  }.join("\n")
151
320
  end
152
- private_class_method :deleted, :settings, :printed, :install, :stop_hook, :working_directory, :hook_input, :to_the_agent
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
153
322
  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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kipple Pty Ltd
@@ -29,20 +29,27 @@ dependencies:
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
31
  version: '3'
32
- description: A static, diff-aware check for code written by coding agents. It compares
33
- each test before and after a change and flags the ones that got weaker, and can
34
- tell the agent that made the change before it finishes.
32
+ description: When a commit weakens an existing test, Slopbouncer stops it until the
33
+ commit message says which behaviour changed and why. The same check runs as a plain
34
+ command in a pipeline, and as an optional turn-end hook for coding agents, comparing
35
+ each test before and after a change without running any code.
35
36
  executables:
36
37
  - slopbouncer
37
38
  extensions: []
38
39
  extra_rdoc_files: []
39
40
  files:
41
+ - CHANGELOG.md
40
42
  - LICENSE
41
43
  - README.md
42
44
  - bin/slopbouncer
43
45
  - lib/slopbouncer.rb
44
46
  - lib/slopbouncer/agents.rb
47
+ - lib/slopbouncer/commit_hook.rb
48
+ - lib/slopbouncer/commit_log.rb
49
+ - lib/slopbouncer/commit_memory.rb
45
50
  - lib/slopbouncer/compare.rb
51
+ - lib/slopbouncer/explained.rb
52
+ - lib/slopbouncer/form.rb
46
53
  - lib/slopbouncer/git.rb
47
54
  - lib/slopbouncer/install.rb
48
55
  - lib/slopbouncer/ruby_tests.rb
@@ -53,6 +60,10 @@ licenses:
53
60
  - MIT
54
61
  metadata:
55
62
  rubygems_mfa_required: 'true'
63
+ homepage_uri: https://slopbouncer.com
64
+ source_code_uri: https://github.com/mainline-dev/slopbouncer
65
+ bug_tracker_uri: https://github.com/mainline-dev/slopbouncer/issues
66
+ changelog_uri: https://github.com/mainline-dev/slopbouncer/blob/master/CHANGELOG.md
56
67
  rdoc_options: []
57
68
  require_paths:
58
69
  - lib
@@ -60,7 +71,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
71
  requirements:
61
72
  - - ">="
62
73
  - !ruby/object:Gem::Version
63
- version: '3.4'
74
+ version: '3.2'
64
75
  required_rubygems_version: !ruby/object:Gem::Requirement
65
76
  requirements:
66
77
  - - ">="
@@ -69,5 +80,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
80
  requirements: []
70
81
  rubygems_version: 3.6.9
71
82
  specification_version: 4
72
- summary: Flags tests that got weaker in a change
83
+ summary: A commit hook that catches weakened tests
73
84
  test_files: []