slopbouncer 0.2.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.
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slopbouncer
4
+ # The findings laid out for a person at a terminal: each file once, the
5
+ # tests flagged in it underneath, each rule with its detail under its test,
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.
10
+ module TerminalReport
11
+ LIMIT = 30
12
+ BOLD = 1
13
+ RED = 31
14
+ GREEN = 32
15
+ YELLOW = 33
16
+ # Red for a check that has gone, yellow for one that was changed or switched off.
17
+ COLOURS = { 'test_deleted' => RED, 'assertion_removed' => RED, 'no_assertions' => RED,
18
+ 'expected_value_changed' => YELLOW, 'skip_added' => YELLOW }.freeze
19
+
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)
29
+ 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?
31
+
32
+ files = findings.group_by(&:file)
33
+ tests = files.sum { |_file, found| found.map(&:test).uniq.size }
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))
46
+ end
47
+
48
+ def self.file(file, found, paint)
49
+ tests = found.group_by(&:test).flat_map do |test, about|
50
+ [" #{test}", *about.map { |finding| " #{rule(finding.rule, paint)} #{detail(finding)}" }]
51
+ end
52
+ [paint.call(file, BOLD), *tests].join("\n")
53
+ end
54
+
55
+ def self.detail(finding)
56
+ finding.before ? "#{finding.before} → #{finding.after}" : finding.detail
57
+ end
58
+
59
+ def self.count(tests, files)
60
+ "#{tests} #{tests == 1 ? 'test looks' : 'tests look'} weaker in #{files} #{files == 1 ? 'file' : 'files'}"
61
+ end
62
+ private_class_method :listed, :counted, :rule, :file, :detail, :count
63
+ end
64
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slopbouncer
4
- VERSION = '0.2.0'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/slopbouncer.rb CHANGED
@@ -6,8 +6,11 @@ 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'
13
+ require_relative 'slopbouncer/terminal_report'
11
14
 
12
15
  # A static check that flags tests which got weaker in a change, so a person
13
16
  # can decide whether that was intended.
@@ -16,9 +19,14 @@ module Slopbouncer
16
19
 
17
20
  # A project's own settings, in the directory the command is run from:
18
21
  # assertions: the names of its helper methods that count as assertions.
22
+ # equivalents: groups of assertion names that make the same check.
19
23
  CONFIG = '.slopbouncer.yml'
20
24
 
21
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
22
30
 
23
31
  USAGE = 'Give a range of commits, such as main..HEAD, or one commit to compare the working tree with, such as HEAD'
24
32
 
@@ -28,15 +36,113 @@ module Slopbouncer
28
36
  base, head = target.split('..', 2)
29
37
  raise Error, USAGE if base.to_s.empty? || (target.include?('..') && head.to_s.empty?)
30
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)
31
49
  helpers = settings.fetch('assertions', [])
32
- 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|
33
53
  next unless [changed.before_path, changed.after_path].compact.all? { |path| path.match?(TEST_FILE) }
34
54
 
35
55
  before = changed.before_path ? RubyTests.in(Git.show(base, changed.before_path), helpers: helpers) : []
36
56
  after = changed.after_path ? RubyTests.in(Git.show(head, changed.after_path), helpers: helpers) : []
37
- [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.')
38
89
  end
39
- results.flat_map { |_file, result| result.findings } + deleted(results)
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
+ []
102
+ end
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}"
40
146
  end
41
147
 
42
148
  # A test that has gone from one file is deleted unless the same test, body
@@ -55,45 +161,117 @@ module Slopbouncer
55
161
  end
56
162
 
57
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
+
58
167
  words = arguments.reject { |argument| argument.start_with?('--') }
59
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'
60
171
 
61
172
  target = words.first
62
173
  agent = arguments.find { |argument| argument == '--agent' || argument.start_with?('--agent=') }
63
174
  return stop_hook(target.to_s, input, agent.split('=', 2)[1] || Agents::DEFAULT) if agent
64
175
 
65
176
  findings = check(target.to_s)
66
- puts arguments.include?('--json') ? JSON.pretty_generate(findings.map { |finding| finding.to_h.compact }) : report(findings)
177
+ puts printed(findings, arguments)
67
178
  arguments.include?('--strict') && findings.any? ? 1 : 0
68
179
  rescue Error => e
69
180
  warn e.message
70
181
  2
71
182
  end
72
183
 
73
- # Sets a coding agent up in the current directory: slopbouncer install claude-code
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
+
213
+ # JSON when asked for it. Otherwise laid out for a person when the output is
214
+ # a terminal or colour is asked for, and one plain line for each finding
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.
217
+ def self.printed(findings, arguments)
218
+ return JSON.pretty_generate(findings.map { |finding| finding.to_h.compact }) if arguments.include?('--json')
219
+
220
+ asked = arguments.intersect?(%w[--color --colour])
221
+ return report(findings) unless asked || $stdout.tty?
222
+
223
+ TerminalReport.call(findings, colour: asked || ENV['NO_COLOR'].to_s.empty?, all: arguments.include?('--all'))
224
+ end
225
+
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.
74
229
  def self.install(words, arguments)
75
- 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?
76
233
 
77
- 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
78
238
  0
79
239
  rescue Error => e
80
240
  warn e.message
81
241
  2
82
242
  end
83
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
+
84
259
  # Run as a coding agent's stop hook. The agent is stopped from finishing
85
- # once and told what it weakened, in the way its own harness expects. A
86
- # mistake in how the hook is set up never stops the agent: it is said on
87
- # 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.
88
264
  def self.stop_hook(target, input, agent_name)
89
265
  agent = Agents.named(agent_name)
90
266
  told = hook_input(input)
91
267
  return 0 if agent.stopped_before?(told)
92
268
 
93
- 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)
94
272
  return 0 if findings.empty?
95
273
 
96
- output, error, status = agent.block(to_the_agent(findings))
274
+ output, error, status = agent.block(to_the_agent(findings, target))
97
275
  print output
98
276
  warn error unless error.empty?
99
277
  status
@@ -118,12 +296,16 @@ module Slopbouncer
118
296
  {}
119
297
  end
120
298
 
121
- 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?
122
304
  <<~MESSAGE
123
- Slopbouncer: this change weakens #{findings.size} existing #{findings.size == 1 ? 'test' : 'tests'}.
124
-
125
- #{report(findings)}
305
+ Slopbouncer: this change looks to weaken #{findings.size} existing #{findings.size == 1 ? 'test' : 'tests'}.
126
306
 
307
+ #{report(findings.first(TOLD_TO_AN_AGENT))}
308
+ #{rest}
127
309
  For each one, do one of two things before you finish:
128
310
  1. Put the test back as it was, and make the code pass it.
129
311
  2. Leave the change, and tell the person which behaviour changed and why the test had to change with it.
@@ -136,5 +318,5 @@ module Slopbouncer
136
318
  "#{finding.file}: #{finding.test}: #{finding.rule}: #{what}"
137
319
  }.join("\n")
138
320
  end
139
- private_class_method :deleted, :settings, :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
140
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.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kipple Pty Ltd
@@ -29,29 +29,41 @@ 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
56
+ - lib/slopbouncer/terminal_report.rb
49
57
  - lib/slopbouncer/version.rb
50
58
  homepage: https://github.com/mainline-dev/slopbouncer
51
59
  licenses:
52
60
  - MIT
53
61
  metadata:
54
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
55
67
  rdoc_options: []
56
68
  require_paths:
57
69
  - lib
@@ -59,7 +71,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
71
  requirements:
60
72
  - - ">="
61
73
  - !ruby/object:Gem::Version
62
- version: '3.4'
74
+ version: '3.2'
63
75
  required_rubygems_version: !ruby/object:Gem::Requirement
64
76
  requirements:
65
77
  - - ">="
@@ -68,5 +80,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
80
  requirements: []
69
81
  rubygems_version: 3.6.9
70
82
  specification_version: 4
71
- summary: Flags tests that got weaker in a change
83
+ summary: A commit hook that catches weakened tests
72
84
  test_files: []