slopbouncer 0.3.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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slopbouncer
4
- VERSION = '0.3.0'
4
+ VERSION = '0.5.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,96 @@ 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)
49
+ settings = settings(head)
32
50
  helpers = settings.fetch('assertions', [])
33
- results = Git.changed(base, head).filter_map do |changed|
51
+ equivalents = settings.fetch('equivalents', [])
52
+ changes = Git.changed(base, head)
53
+ results = changes.filter_map do |changed|
34
54
  next unless [changed.before_path, changed.after_path].compact.all? { |path| path.match?(TEST_FILE) }
35
55
 
36
56
  before = changed.before_path ? RubyTests.in(Git.show(base, changed.before_path), helpers: helpers) : []
37
57
  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)]
58
+ file = changed.after_path || changed.before_path
59
+ [file, Compare.call(file, before, after, equivalents: equivalents)]
60
+ end
61
+ results.flat_map { |_file, result| result.findings } + deleted(results) +
62
+ equivalents_changed(changes, base, head) + helpers_changed(changes, base, head, helpers)
63
+ end
64
+
65
+ # A method the tests define whose body changed, and that tests assert
66
+ # through, is reported once with how many tests depend on it. Slopbouncer
67
+ # cannot judge such a change, so it asks for it to be looked at.
68
+ def self.helpers_changed(changes, base, head, helpers)
69
+ 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) }
71
+ end
72
+
73
+ def self.changed_helpers(changed, base, head, helpers)
74
+ was = RubyTests.methods_in(Git.show(base, changed.before_path))
75
+ source = Git.show(head, changed.after_path)
76
+ now = RubyTests.methods_in(source)
77
+ altered = now.keys.select { |name| was.key?(name) && was[name] != now[name] }
78
+ return [] if altered.empty?
79
+
80
+ tests = dependants_of(source, head, helpers)
81
+ altered.filter_map do |name|
82
+ count = tests.count { |test| test.assertions.any? { |assertion| assertion.names.include?(name) } }
83
+ next if count.zero?
84
+
85
+ Compare::Finding.new(file: changed.after_path, test: name, rule: 'helper_changed',
86
+ detail: "#{name} changed, and #{count} #{count == 1 ? 'test asserts' : 'tests assert'} through it. " \
87
+ 'Slopbouncer cannot judge a change to a helper, so look at this one.')
88
+ end
89
+ end
90
+
91
+ # 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
+ def self.dependants_of(source, head, helpers)
94
+ own = RubyTests.in(source, helpers: helpers)
95
+ return own if own.any?
96
+
97
+ Git.files(head).select { |file| file.match?(TEST_FILE) }.flat_map do |file|
98
+ RubyTests.in(Git.show(head, file), helpers: helpers)
99
+ rescue Errno::ENOENT
100
+ []
39
101
  end
40
- results.flat_map { |_file, result| result.findings } + deleted(results)
102
+ end
103
+
104
+ def self.in_tests?(path)
105
+ path.match?(TEST_FILE) || path.match?(TEST_DIRECTORY)
106
+ end
107
+
108
+ # Declaring assertions equivalent quiets Slopbouncer about them, so a
109
+ # change to what is declared is a finding of its own, naming each group of
110
+ # names added and removed.
111
+ def self.equivalents_changed(changes, base, head)
112
+ changed = changes.find { |file| [file.before_path, file.after_path].include?(CONFIG) }
113
+ return [] unless changed
114
+
115
+ was = declared(changed.before_path && Git.show(base, changed.before_path))
116
+ now = declared(changed.after_path && Git.show(head, changed.after_path))
117
+ said = (now - was).map { |group| "added #{group.join(' = ')}" } + (was - now).map { |group| "removed #{group.join(' = ')}" }
118
+ return [] if said.empty?
119
+
120
+ [Compare::Finding.new(file: CONFIG, test: 'equivalent assertions', rule: 'equivalents_changed', detail: said.join('; '))]
121
+ end
122
+
123
+ # The groups of equivalent names a settings file declares, each in order.
124
+ def self.declared(text)
125
+ groups = text ? (YAML.safe_load(text) || {}).fetch('equivalents', []) : []
126
+ groups.map { |group| Array(group).map(&:to_s).sort }
127
+ rescue Psych::Exception => e
128
+ raise Error, "#{CONFIG} could not be read: #{e.message}"
41
129
  end
42
130
 
43
131
  # A test that has gone from one file is deleted unless the same test, body
@@ -49,15 +137,26 @@ module Slopbouncer
49
137
  end
50
138
  end
51
139
 
52
- def self.settings
53
- 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) || {} : {}
54
148
  rescue Psych::Exception => e
55
149
  raise Error, "#{CONFIG} could not be read: #{e.message}"
56
150
  end
57
151
 
58
152
  def self.run(arguments, input: $stdin)
153
+ return said(help) if arguments.intersect?(%w[--help -h])
154
+ return said("slopbouncer #{VERSION}") if arguments.intersect?(%w[--version -v])
155
+
59
156
  words = arguments.reject { |argument| argument.start_with?('--') }
60
157
  return install(words.drop(1), arguments) if words.first == 'install'
158
+ return CommitHook.call(words[1].to_s) if words.first == 'commit-msg'
159
+ return stats(arguments) if words.first == 'stats'
61
160
 
62
161
  target = words.first
63
162
  agent = arguments.find { |argument| argument == '--agent' || argument.start_with?('--agent=') }
@@ -71,42 +170,95 @@ module Slopbouncer
71
170
  2
72
171
  end
73
172
 
173
+ def self.said(text)
174
+ puts text
175
+ 0
176
+ end
177
+
178
+ def self.help
179
+ <<~HELP
180
+ Slopbouncer flags tests that got weaker in a change. It reads the code and never runs it.
181
+
182
+ Usage:
183
+ slopbouncer main..HEAD the commits in a range
184
+ slopbouncer HEAD everything since one commit, committed or not
185
+ slopbouncer install set up the commit hook in this repository
186
+ slopbouncer install AGENT the commit hook, and for claude-code a rule denying --no-verify
187
+ slopbouncer stats how often the commit hook has stopped a commit
188
+
189
+ Options:
190
+ --json print the findings as JSON
191
+ --strict exit with 1 when anything is flagged
192
+ --all at a terminal, list every finding however many there are
193
+ --color use the terminal layout, in colour, wherever the output goes
194
+ --agent=NAME run as that coding agent's turn-end hook
195
+ --turn-end with install AGENT, set up its turn-end hook instead: #{Agents::ALL.keys.join(', ')}
196
+ --local with install claude-code, keep what it adds out of the shared settings
197
+ -h, --help print this
198
+ -v, --version print the version
199
+ HELP
200
+ end
201
+
74
202
  # JSON when asked for it. Otherwise laid out for a person when the output is
75
203
  # a terminal or colour is asked for, and one plain line for each finding
76
- # when it goes to another program.
204
+ # when it goes to another program. What a program reads is always complete;
205
+ # what a person reads is counted up when it is long, unless they ask for all.
77
206
  def self.printed(findings, arguments)
78
207
  return JSON.pretty_generate(findings.map { |finding| finding.to_h.compact }) if arguments.include?('--json')
79
208
 
80
209
  asked = arguments.intersect?(%w[--color --colour])
81
210
  return report(findings) unless asked || $stdout.tty?
82
211
 
83
- TerminalReport.call(findings, colour: asked || ENV['NO_COLOR'].to_s.empty?)
212
+ TerminalReport.call(findings, colour: asked || ENV['NO_COLOR'].to_s.empty?, all: arguments.include?('--all'))
84
213
  end
85
214
 
86
- # Sets a coding agent up in the current directory: slopbouncer install claude-code
215
+ # Sets the commit hook up in the current repository, for a coding agent if
216
+ # one is named: slopbouncer install claude-code. With --turn-end, sets up
217
+ # that agent's turn-end hook instead.
87
218
  def self.install(words, arguments)
88
- raise Error, "Say which agent to set up: #{Agents::ALL.keys.join(', ')}" if words.empty?
219
+ local = arguments.include?('--local')
220
+ if arguments.include?('--turn-end')
221
+ raise Error, "Say which agent's turn-end hook to set up: #{Agents::ALL.keys.join(', ')}" if words.empty?
89
222
 
90
- puts Install.call(words.first, local: arguments.include?('--local'))
223
+ puts Install.call(words.first, local: local)
224
+ else
225
+ puts Install.commit_hook(words.first, local: local)
226
+ end
91
227
  0
92
228
  rescue Error => e
93
229
  warn e.message
94
230
  2
95
231
  end
96
232
 
233
+ # The counts the commit hook has logged, as lines, or as JSON when asked.
234
+ def self.stats(arguments)
235
+ totals = CommitLog.totals
236
+ return said(JSON.pretty_generate(totals)) if arguments.include?('--json')
237
+
238
+ said(<<~STATS)
239
+ Commits checked: #{totals['checked']}
240
+ Stopped: #{totals['stopped']}
241
+ Findings: #{totals['findings']}
242
+ Explained in the commit message: #{totals['explained_in_message']}
243
+ Already explained (repeats not asked about): #{totals['already_explained']}
244
+ Not checked, because Slopbouncer could not run: #{totals['not_checked']}
245
+ STATS
246
+ end
247
+
97
248
  # 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.
249
+ # once and told what it weakened, in the way its own harness expects, and
250
+ # only about what it has not been told already. A mistake in how the hook
251
+ # is set up never stops the agent: it is said on standard error and the
252
+ # agent is let finish.
101
253
  def self.stop_hook(target, input, agent_name)
102
254
  agent = Agents.named(agent_name)
103
255
  told = hook_input(input)
104
256
  return 0 if agent.stopped_before?(told)
105
257
 
106
- findings = Dir.chdir(working_directory(told)) { check(target) }
258
+ findings = Dir.chdir(working_directory(told)) { Explained.unseen(check(target)) }
107
259
  return 0 if findings.empty?
108
260
 
109
- output, error, status = agent.block(to_the_agent(findings))
261
+ output, error, status = agent.block(to_the_agent(findings, target))
110
262
  print output
111
263
  warn error unless error.empty?
112
264
  status
@@ -131,12 +283,16 @@ module Slopbouncer
131
283
  {}
132
284
  end
133
285
 
134
- def self.to_the_agent(findings)
286
+ # A coding agent is told about the first few findings, since a mechanical
287
+ # rewrite can make hundreds, and how to list the rest.
288
+ def self.to_the_agent(findings, target)
289
+ more = findings.size - TOLD_TO_AN_AGENT
290
+ rest = "\nand #{more} more. Run `slopbouncer #{target}` to list them all.\n" if more.positive?
135
291
  <<~MESSAGE
136
- Slopbouncer: this change weakens #{findings.size} existing #{findings.size == 1 ? 'test' : 'tests'}.
137
-
138
- #{report(findings)}
292
+ Slopbouncer: this change looks to weaken #{findings.size} existing #{findings.size == 1 ? 'test' : 'tests'}.
139
293
 
294
+ #{report(findings.first(TOLD_TO_AN_AGENT))}
295
+ #{rest}
140
296
  For each one, do one of two things before you finish:
141
297
  1. Put the test back as it was, and make the code pass it.
142
298
  2. Leave the change, and tell the person which behaviour changed and why the test had to change with it.
@@ -149,5 +305,5 @@ module Slopbouncer
149
305
  "#{finding.file}: #{finding.test}: #{finding.rule}: #{what}"
150
306
  }.join("\n")
151
307
  end
152
- private_class_method :deleted, :settings, :printed, :install, :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
153
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.3.0
4
+ version: 0.5.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: []