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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -0
- data/README.md +170 -40
- data/lib/slopbouncer/commit_hook.rb +66 -0
- data/lib/slopbouncer/commit_log.rb +46 -0
- data/lib/slopbouncer/commit_memory.rb +47 -0
- data/lib/slopbouncer/compare.rb +104 -27
- data/lib/slopbouncer/explained.rb +54 -0
- data/lib/slopbouncer/form.rb +45 -0
- data/lib/slopbouncer/git.rb +63 -7
- data/lib/slopbouncer/install.rb +54 -4
- data/lib/slopbouncer/ruby_tests.rb +73 -13
- data/lib/slopbouncer/terminal_report.rb +29 -6
- data/lib/slopbouncer/version.rb +1 -1
- data/lib/slopbouncer.rb +187 -18
- metadata +17 -6
data/lib/slopbouncer/compare.rb
CHANGED
|
@@ -6,42 +6,89 @@ 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
|
-
|
|
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)
|
|
10
12
|
Result = Struct.new(:findings, :gone, :fresh, keyword_init: true)
|
|
11
13
|
|
|
12
14
|
# A reworded test is the same test: one that has gone is taken to be a
|
|
13
15
|
# new one when at least this share of their body lines are the same.
|
|
14
16
|
SAME_TEST = 0.5
|
|
15
17
|
|
|
18
|
+
# What an edited argument is called, by the side of the assertion it is
|
|
19
|
+
# on. An argument on no side the reader knows makes assertion_changed.
|
|
20
|
+
EDITED = { expected: 'expected_value_changed', subject: 'subject_changed' }.freeze
|
|
21
|
+
|
|
16
22
|
# gone: tests no longer in the file. fresh: tests that are new to it.
|
|
17
23
|
# The caller decides whether a gone test turned up in another file.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
# equivalents: groups of assertion names the project says make the same
|
|
25
|
+
# check.
|
|
26
|
+
def self.call(file, before, after, equivalents: [])
|
|
27
|
+
named = same_name(before, after)
|
|
28
|
+
gone = without(before, named.map(&:first))
|
|
29
|
+
fresh = without(after, named.map(&:last))
|
|
22
30
|
reworded = reworded(gone, fresh)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
31
|
+
Result.new(findings: (named + reworded).flat_map { |was, test| weaker(file, was, test, equivalents) },
|
|
32
|
+
gone: without(gone, reworded.map(&:first)), fresh: without(fresh, reworded.map(&:last)))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Pairs each test with the test of the same name after the change. A
|
|
36
|
+
# file can hold several tests with one name: those are paired with the
|
|
37
|
+
# one whose body is unchanged if there is one, and otherwise in the
|
|
38
|
+
# order they appear, first with first.
|
|
39
|
+
def self.same_name(before, after)
|
|
40
|
+
waiting = after.group_by(&:name)
|
|
41
|
+
unchanged = before.filter_map { |was| pair(was, waiting) { |test| test.body == was.body } }
|
|
42
|
+
rest = without(before, unchanged.map(&:first)).filter_map { |was| pair(was, waiting) { true } }
|
|
43
|
+
(unchanged + rest).sort_by { |was, _| before.index { |test| test.equal?(was) } }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Takes the first waiting test of this name that fits, if any.
|
|
47
|
+
def self.pair(was, waiting)
|
|
48
|
+
candidates = waiting.fetch(was.name, [])
|
|
49
|
+
place = candidates.index { |test| yield test }
|
|
50
|
+
[was, candidates.delete_at(place)] if place
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The tests that are not, one for one, among those taken. Two tests can
|
|
54
|
+
# be alike in every way and still be two tests.
|
|
55
|
+
def self.without(tests, taken)
|
|
56
|
+
tests.reject { |test| taken.any? { |other| other.equal?(test) } }
|
|
26
57
|
end
|
|
27
58
|
|
|
28
59
|
def self.deleted(file, test)
|
|
29
60
|
Finding.new(file: file, test: test.name, rule: 'test_deleted', detail: "#{test.assertions.size} assertions")
|
|
30
61
|
end
|
|
31
62
|
|
|
32
|
-
# Pairs each test that has gone with the new test
|
|
63
|
+
# Pairs each test that has gone with the new test it became, surest
|
|
64
|
+
# first: the same body written another way, then the body most like it,
|
|
65
|
+
# then a one-line test with one value edited. Each new test is taken once.
|
|
33
66
|
def self.reworded(gone, fresh)
|
|
34
67
|
left = fresh.dup
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
68
|
+
ways = [->(was) { left.find { |test| rewritten?(was, test) } },
|
|
69
|
+
->(was) { most_alike(was, left) },
|
|
70
|
+
->(was) { left.find { |test| one_edit?(was, test) } }]
|
|
71
|
+
ways.each_with_object([]) do |way, pairs|
|
|
72
|
+
without(gone, pairs.map(&:first)).each do |was|
|
|
73
|
+
match = way.call(was)
|
|
74
|
+
next unless match
|
|
39
75
|
|
|
40
|
-
|
|
41
|
-
|
|
76
|
+
left.delete_at(left.index { |test| test.equal?(match) })
|
|
77
|
+
pairs << [was, match]
|
|
78
|
+
end
|
|
42
79
|
end
|
|
43
80
|
end
|
|
44
81
|
|
|
82
|
+
# The same body written another way, as a formatter leaves it.
|
|
83
|
+
def self.rewritten?(was, now)
|
|
84
|
+
!was.form.nil? && was.form == now.form
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.most_alike(was, tests)
|
|
88
|
+
match = tests.max_by { |test| likeness(was, test) }
|
|
89
|
+
match if match && likeness(was, match) >= SAME_TEST
|
|
90
|
+
end
|
|
91
|
+
|
|
45
92
|
# A one-line test whose only assertion had one value edited is the same
|
|
46
93
|
# test. This is how a test with no name of its own is followed:
|
|
47
94
|
# it { is_expected.to eq(5) } edited to expect 6.
|
|
@@ -56,23 +103,45 @@ module Slopbouncer
|
|
|
56
103
|
total.zero? ? 0.0 : shared.to_f / total
|
|
57
104
|
end
|
|
58
105
|
|
|
59
|
-
def self.weaker(file, was, now)
|
|
106
|
+
def self.weaker(file, was, now, equivalents)
|
|
60
107
|
finding = ->(rule, **rest) { Finding.new(file: file, test: now.name, rule: rule, **rest) }
|
|
61
|
-
lost = was.assertions.reject { |assertion| now.assertions.
|
|
62
|
-
gained = now.assertions.reject { |assertion| was.assertions.
|
|
108
|
+
lost = was.assertions.reject { |assertion| now.assertions.any? { |other| same_check?(assertion, other, equivalents) } }
|
|
109
|
+
gained = now.assertions.reject { |assertion| was.assertions.any? { |other| same_check?(other, assertion, equivalents) } }
|
|
63
110
|
found = lost.map do |assertion|
|
|
64
|
-
edit = gained.find { |other| edited_value(assertion, other) }
|
|
111
|
+
edit = gained.find { |other| edited_value(assertion, other, equivalents) }
|
|
65
112
|
next finding.call('assertion_removed', detail: assertion.source) unless edit
|
|
66
113
|
|
|
67
114
|
gained.delete(edit)
|
|
68
|
-
before, after = edited_value(assertion, edit)
|
|
69
|
-
finding.call(
|
|
115
|
+
before, after = edited_value(assertion, edit, equivalents)
|
|
116
|
+
finding.call(EDITED.fetch(edit.sides[edited_place(assertion, edit, equivalents)], 'assertion_changed'),
|
|
117
|
+
detail: edit.source, before: before, after: after)
|
|
70
118
|
end
|
|
119
|
+
mark_reworded(found, gained, was, now)
|
|
71
120
|
found << finding.call('skip_added', detail: 'marked to be skipped') if now.skipped && !was.skipped
|
|
72
|
-
found << finding.call('no_assertions', detail: 'asserts nothing') if asserts_nothing?(now) && was.
|
|
121
|
+
found << finding.call('no_assertions', detail: 'asserts nothing') if asserts_nothing?(now) && was.form != now.form
|
|
73
122
|
found
|
|
74
123
|
end
|
|
75
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)
|
|
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 }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Two assertions make the same check when they parse the same, or when the
|
|
135
|
+
# project has declared their names equivalent and their arguments parse
|
|
136
|
+
# the same.
|
|
137
|
+
def self.same_check?(was, now, equivalents)
|
|
138
|
+
was.form == now.form || (equivalent?(was.method, now.method, equivalents) && was.forms == now.forms)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def self.equivalent?(one, other, equivalents)
|
|
142
|
+
one == other || equivalents.any? { |group| group.include?(one) && group.include?(other) }
|
|
143
|
+
end
|
|
144
|
+
|
|
76
145
|
# A test with no assertions of its own still asserts when a hook in its
|
|
77
146
|
# group does it for it.
|
|
78
147
|
def self.asserts_nothing?(test)
|
|
@@ -80,13 +149,21 @@ module Slopbouncer
|
|
|
80
149
|
end
|
|
81
150
|
|
|
82
151
|
# The one argument that differs between two calls of the same assertion,
|
|
83
|
-
# as [before, after], when
|
|
84
|
-
|
|
85
|
-
|
|
152
|
+
# or of two the project declared equivalent, as [before, after], when
|
|
153
|
+
# everything else about them is the same.
|
|
154
|
+
def self.edited_value(was, now, equivalents = [])
|
|
155
|
+
place = edited_place(was, now, equivalents)
|
|
156
|
+
[was.arguments[place], now.arguments[place]] if place
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Where that one differing argument is.
|
|
160
|
+
def self.edited_place(was, now, equivalents = [])
|
|
161
|
+
return unless equivalent?(was.method, now.method, equivalents) && was.arguments.size == now.arguments.size
|
|
86
162
|
|
|
87
|
-
differing = was.
|
|
163
|
+
differing = was.forms.each_index.reject { |place| was.forms[place] == now.forms[place] }
|
|
88
164
|
differing.first if differing.size == 1
|
|
89
165
|
end
|
|
90
|
-
private_class_method :reworded, :one_edit?, :likeness, :weaker,
|
|
166
|
+
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
|
|
91
168
|
end
|
|
92
169
|
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require_relative 'git'
|
|
5
|
+
|
|
6
|
+
module Slopbouncer
|
|
7
|
+
# What a coding agent's hook has already shown the agent for the work in
|
|
8
|
+
# hand, so that it is sent back only for what is new. It is kept under the
|
|
9
|
+
# git directory, out of the working tree, and belongs to one HEAD: once
|
|
10
|
+
# HEAD moves the work in hand is new, and nothing of it has been shown.
|
|
11
|
+
module Explained
|
|
12
|
+
FILE = 'slopbouncer-explained.json'
|
|
13
|
+
|
|
14
|
+
# Findings that are shown every time, however often they have been. A
|
|
15
|
+
# change to what a project declares equivalent quiets Slopbouncer, so
|
|
16
|
+
# nothing may quiet that change in turn.
|
|
17
|
+
ALWAYS = %w[equivalents_changed].freeze
|
|
18
|
+
|
|
19
|
+
# The findings the agent has not been shown yet. All of them count as
|
|
20
|
+
# shown from now on, and any that have gone are forgotten, so a finding
|
|
21
|
+
# put right and then brought back is shown again.
|
|
22
|
+
def self.unseen(findings)
|
|
23
|
+
shown = recall
|
|
24
|
+
remember(findings.map { |finding| fingerprint(finding) })
|
|
25
|
+
findings.reject { |finding| !ALWAYS.include?(finding.rule) && shown.include?(fingerprint(finding)) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# A finding is the same finding when its file, test, rule and what it says
|
|
29
|
+
# changed are the same.
|
|
30
|
+
def self.fingerprint(finding)
|
|
31
|
+
JSON.generate([finding.file, finding.test, finding.rule, finding.before || finding.detail, finding.after])
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.recall
|
|
35
|
+
kept = JSON.parse(File.read(path))
|
|
36
|
+
kept['head'] == Git.head ? kept['shown'].to_a : []
|
|
37
|
+
rescue Errno::ENOENT, JSON::ParserError
|
|
38
|
+
[]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Where it cannot be written, nothing is remembered and every finding is
|
|
42
|
+
# shown each time, as before there was a memory.
|
|
43
|
+
def self.remember(fingerprints)
|
|
44
|
+
File.write(path, JSON.generate('head' => Git.head, 'shown' => fingerprints))
|
|
45
|
+
rescue SystemCallError
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.path
|
|
50
|
+
File.join(Git.directory, FILE)
|
|
51
|
+
end
|
|
52
|
+
private_class_method :fingerprint, :recall, :remember, :path
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'prism'
|
|
4
|
+
|
|
5
|
+
module Slopbouncer
|
|
6
|
+
# What a piece of code parses to, with everything about how it was written
|
|
7
|
+
# left out: where it sits, its white space and line breaks, a string's
|
|
8
|
+
# quote style, the parentheses round a call's arguments, whether a block
|
|
9
|
+
# uses braces or do and end. Two pieces of code with the same form are the
|
|
10
|
+
# same code written two ways.
|
|
11
|
+
module Form
|
|
12
|
+
# Flags that say how something was written and not what it means:
|
|
13
|
+
# total against total(), and how a string's bytes were spelled.
|
|
14
|
+
WRITTEN = /\A(variable_call|ignore_visibility|frozen|mutable|forced_\w+_encoding)\?\z/
|
|
15
|
+
|
|
16
|
+
def self.of(node)
|
|
17
|
+
return unless node
|
|
18
|
+
return [:name, node.name] if bare_name?(node)
|
|
19
|
+
|
|
20
|
+
[node.type, *node.class.fields.filter_map { |field| part(node, field) }]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# A name on its own, such as basket. Whether it is a variable or a call
|
|
24
|
+
# with no arguments is decided by the code round it, and so is how many
|
|
25
|
+
# blocks out a variable was set. Neither is part of what was written.
|
|
26
|
+
def self.bare_name?(node)
|
|
27
|
+
return true if node.is_a?(Prism::LocalVariableReadNode)
|
|
28
|
+
|
|
29
|
+
node.is_a?(Prism::CallNode) && node.receiver.nil? && node.arguments.nil? && node.block.nil?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.part(node, field)
|
|
33
|
+
return if field.name == :depth
|
|
34
|
+
|
|
35
|
+
case field
|
|
36
|
+
when Prism::Reflection::LocationField, Prism::Reflection::OptionalLocationField then nil
|
|
37
|
+
when Prism::Reflection::FlagsField then field.flags.reject { |flag| flag.match?(WRITTEN) }.select { |flag| node.public_send(flag) }
|
|
38
|
+
when Prism::Reflection::NodeListField then node.public_send(field.name).map { |child| of(child) }
|
|
39
|
+
when Prism::Reflection::NodeField, Prism::Reflection::OptionalNodeField then of(node.public_send(field.name))
|
|
40
|
+
else node.public_send(field.name)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
private_class_method :bare_name?, :part
|
|
44
|
+
end
|
|
45
|
+
end
|
data/lib/slopbouncer/git.rb
CHANGED
|
@@ -8,11 +8,22 @@ module Slopbouncer
|
|
|
8
8
|
module Git
|
|
9
9
|
Changed = Struct.new(:before_path, :after_path, keyword_init: true)
|
|
10
10
|
|
|
11
|
-
# The files
|
|
12
|
-
|
|
13
|
-
#
|
|
11
|
+
# The files as staged for the next commit, in place of a commit.
|
|
12
|
+
STAGED = :staged
|
|
13
|
+
# What a repository with no commits yet is compared with.
|
|
14
|
+
EMPTY_TREE = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
|
|
15
|
+
|
|
16
|
+
NOT_A_REPOSITORY = 'Not a git repository. Run it from the root of the repository you want checked.'
|
|
17
|
+
|
|
18
|
+
# The files changed between two commits, between a commit and the working
|
|
19
|
+
# tree when no second commit is given, or between a commit and what is
|
|
20
|
+
# staged. A file that was added has no before path, and one that was
|
|
21
|
+
# deleted has no after path.
|
|
14
22
|
def self.changed(base, head = nil)
|
|
15
|
-
|
|
23
|
+
raise Error, NOT_A_REPOSITORY unless repository?
|
|
24
|
+
|
|
25
|
+
range = head == STAGED ? ['--cached', base] : [base, head].compact
|
|
26
|
+
run('diff', '--name-status', '-M', *range).lines.map do |line|
|
|
16
27
|
status, first, second = line.chomp.split("\t")
|
|
17
28
|
case status[0]
|
|
18
29
|
when 'A' then Changed.new(after_path: first)
|
|
@@ -23,21 +34,66 @@ module Slopbouncer
|
|
|
23
34
|
end
|
|
24
35
|
end
|
|
25
36
|
|
|
26
|
-
# What a file holds at a commit, or in the working tree when
|
|
37
|
+
# What a file holds at a commit, as staged, or in the working tree when no
|
|
38
|
+
# commit is given.
|
|
27
39
|
def self.show(revision, path)
|
|
40
|
+
return run('show', ":#{path}") if revision == STAGED
|
|
41
|
+
|
|
28
42
|
revision ? run('show', "#{revision}:#{path}") : File.read(File.join(root, path))
|
|
29
43
|
end
|
|
30
44
|
|
|
45
|
+
# The commit a staged change is compared with: HEAD, or nothing at all
|
|
46
|
+
# before the first commit.
|
|
47
|
+
def self.staged_base
|
|
48
|
+
Open3.capture3('git', 'rev-parse', '--verify', '-q', 'HEAD').last.success? ? 'HEAD' : EMPTY_TREE
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Where git runs a hook from, as the repository is set up: its own hooks
|
|
52
|
+
# directory, or wherever core.hooksPath points.
|
|
53
|
+
def self.hook_path(name)
|
|
54
|
+
raise Error, NOT_A_REPOSITORY unless repository?
|
|
55
|
+
|
|
56
|
+
run('rev-parse', '--git-path', "hooks/#{name}").strip
|
|
57
|
+
end
|
|
58
|
+
|
|
31
59
|
def self.root
|
|
32
60
|
@root ||= run('rev-parse', '--show-toplevel').strip
|
|
33
61
|
end
|
|
34
62
|
|
|
63
|
+
# Where git keeps its own files for this repository, outside the working
|
|
64
|
+
# tree.
|
|
65
|
+
def self.directory
|
|
66
|
+
run('rev-parse', '--git-dir').strip
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def self.head
|
|
70
|
+
run('rev-parse', 'HEAD').strip
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Every file in the repository at a commit, or in the working tree, new
|
|
74
|
+
# files included, when no commit is given.
|
|
75
|
+
def self.files(revision)
|
|
76
|
+
listed = if revision == STAGED then run('ls-files', '--cached', '--full-name')
|
|
77
|
+
elsif revision then run('ls-tree', '-r', '--name-only', '--full-name', revision)
|
|
78
|
+
else run('ls-files', '--cached', '--others', '--exclude-standard', '--full-name')
|
|
79
|
+
end
|
|
80
|
+
listed.lines.map(&:chomp)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Outside a repository git answers a diff with its whole help text, so
|
|
84
|
+
# this is asked first.
|
|
85
|
+
def self.repository?
|
|
86
|
+
Open3.capture3('git', 'rev-parse', '--is-inside-work-tree').last.success?
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# When git fails, its first line of error says why. The rest is advice
|
|
90
|
+
# about git's own command line.
|
|
35
91
|
def self.run(*arguments)
|
|
36
92
|
output, error, status = Open3.capture3('git', *arguments)
|
|
37
|
-
raise Error,
|
|
93
|
+
raise Error, error.lines.first.to_s.strip unless status.success?
|
|
38
94
|
|
|
39
95
|
output
|
|
40
96
|
end
|
|
41
|
-
private_class_method :run, :root
|
|
97
|
+
private_class_method :repository?, :run, :root
|
|
42
98
|
end
|
|
43
99
|
end
|
data/lib/slopbouncer/install.rb
CHANGED
|
@@ -4,10 +4,60 @@ require 'fileutils'
|
|
|
4
4
|
require 'json'
|
|
5
5
|
|
|
6
6
|
module Slopbouncer
|
|
7
|
-
# Sets
|
|
8
|
-
#
|
|
9
|
-
#
|
|
7
|
+
# Sets Slopbouncer up in the current repository: as git's commit-msg hook,
|
|
8
|
+
# which every commit runs whoever makes it, or as a coding agent's turn-end
|
|
9
|
+
# hook. Everything already there is kept, and setting up twice changes
|
|
10
|
+
# nothing.
|
|
10
11
|
module Install
|
|
12
|
+
# The line the commit hook runs. Without the slopbouncer command it says
|
|
13
|
+
# the commit was not checked and lets it through.
|
|
14
|
+
LINE = 'command -v slopbouncer >/dev/null 2>&1 && { slopbouncer commit-msg "$1" || exit 1; } || ' \
|
|
15
|
+
'echo "Slopbouncer could not run: the slopbouncer command was not found. This commit was not checked." >&2'
|
|
16
|
+
# What Claude Code may not run, since --no-verify skips every hook.
|
|
17
|
+
DENY = ['Bash(git commit --no-verify:*)', 'Bash(git commit -n:*)'].freeze
|
|
18
|
+
|
|
19
|
+
# Sets up the commit hook, and for Claude Code the rules that deny it
|
|
20
|
+
# --no-verify. Says what it did, a line for each.
|
|
21
|
+
def self.commit_hook(agent_name = nil, local: false)
|
|
22
|
+
agent = Agents.named(agent_name) if agent_name
|
|
23
|
+
done = [hook]
|
|
24
|
+
done << deny(agent.settings_path(local: local)) if agent == Agents::ClaudeCode
|
|
25
|
+
done.join("\n")
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Writes the commit-msg hook wherever git runs hooks from. A hook the
|
|
29
|
+
# project already has gets one line, after its first, so it runs before
|
|
30
|
+
# anything that might end the hook early.
|
|
31
|
+
def self.hook
|
|
32
|
+
path = Git.hook_path('commit-msg')
|
|
33
|
+
where = File.dirname(path)
|
|
34
|
+
return "The commit-msg hook in #{where} already runs Slopbouncer. Nothing changed." if File.exist?(path) && File.read(path).include?('slopbouncer commit-msg')
|
|
35
|
+
|
|
36
|
+
FileUtils.mkdir_p(where)
|
|
37
|
+
if File.exist?(path)
|
|
38
|
+
lines = File.read(path).lines
|
|
39
|
+
at = lines.first.to_s.start_with?('#!') ? 1 : 0
|
|
40
|
+
File.write(path, lines.insert(at, "#{LINE}\n").join)
|
|
41
|
+
File.chmod(File.stat(path).mode | 0o111, path)
|
|
42
|
+
return "Added Slopbouncer to the commit-msg hook in #{where}. Both run on every commit."
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
File.write(path, "#!/bin/sh\n#{LINE}\n")
|
|
46
|
+
File.chmod(0o755, path)
|
|
47
|
+
"Set up the commit-msg hook in #{where}. Every commit that weakens a test now needs a reason."
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.deny(path)
|
|
51
|
+
settings = read(path)
|
|
52
|
+
denied = Array(settings.dig('permissions', 'deny'))
|
|
53
|
+
missing = DENY - denied
|
|
54
|
+
return "#{path} already denies git commit --no-verify. Nothing changed." if missing.empty?
|
|
55
|
+
|
|
56
|
+
settings['permissions'] = (settings['permissions'] || {}).merge('deny' => denied + missing)
|
|
57
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
58
|
+
File.write(path, "#{JSON.pretty_generate(settings)}\n")
|
|
59
|
+
"Added rules to #{path} that stop Claude Code committing with --no-verify, which skips every hook."
|
|
60
|
+
end
|
|
11
61
|
# Says what it did, in one line.
|
|
12
62
|
def self.call(agent_name, local: false, base: 'HEAD')
|
|
13
63
|
agent = Agents.named(agent_name)
|
|
@@ -31,6 +81,6 @@ module Slopbouncer
|
|
|
31
81
|
rescue JSON::ParserError
|
|
32
82
|
raise Error, "#{path} could not be read as JSON. Nothing changed."
|
|
33
83
|
end
|
|
34
|
-
private_class_method :read
|
|
84
|
+
private_class_method :read, :hook, :deny
|
|
35
85
|
end
|
|
36
86
|
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'prism'
|
|
4
|
+
require_relative 'form'
|
|
4
5
|
|
|
5
6
|
module Slopbouncer
|
|
6
7
|
# Reads a Ruby test file into a table of its tests: each test's full name,
|
|
@@ -8,8 +9,32 @@ module Slopbouncer
|
|
|
8
9
|
# It reads the code and never runs it.
|
|
9
10
|
module RubyTests
|
|
10
11
|
# hooked: a hook in one of the test's groups makes an assertion for it.
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
# form: what the body parses to, however it is written.
|
|
13
|
+
Test = Struct.new(:name, :assertions, :skipped, :body, :hooked, :form, keyword_init: true)
|
|
14
|
+
# arguments and source are as written, for showing. forms and form are
|
|
15
|
+
# what the same things parse to, for telling whether anything changed.
|
|
16
|
+
# sides says which side of the assertion each argument is on, :expected
|
|
17
|
+
# or :subject, where the reader knows the assertion. names are the
|
|
18
|
+
# methods it calls and the symbols it names, as assert_predicate order,
|
|
19
|
+
# :paid? names paid?.
|
|
20
|
+
Assertion = Struct.new(:method, :arguments, :source, :forms, :form, :sides, :names, keyword_init: true)
|
|
21
|
+
|
|
22
|
+
# The assertions whose sides the reader knows, and where each argument
|
|
23
|
+
# sits: what is expected, or the subject, the thing being checked. An
|
|
24
|
+
# argument past the end of its list, such as the message shown when the
|
|
25
|
+
# assertion fails, is on no side it knows.
|
|
26
|
+
SIDES = {
|
|
27
|
+
%i[assert_equal refute_equal assert_same refute_same assert_match refute_match
|
|
28
|
+
assert_instance_of refute_instance_of assert_kind_of refute_kind_of] => %i[expected subject],
|
|
29
|
+
%i[assert_in_delta refute_in_delta assert_in_epsilon refute_in_epsilon] => %i[expected subject expected],
|
|
30
|
+
%i[assert_includes refute_includes assert_predicate refute_predicate
|
|
31
|
+
assert_respond_to refute_respond_to] => %i[subject expected],
|
|
32
|
+
%i[assert_operator refute_operator] => %i[subject expected expected],
|
|
33
|
+
%i[assert refute assert_nil refute_nil assert_empty refute_empty] => %i[subject]
|
|
34
|
+
}.flat_map { |names, sides| names.map { |name| [name, sides] } }.to_h.freeze
|
|
35
|
+
# Assertions whose every argument is what is expected: the subject of
|
|
36
|
+
# _(total).must_equal 5 is what must_equal is called on.
|
|
37
|
+
ALL_EXPECTED = /\A(must|wont)_|\Aassert_raises\z/
|
|
13
38
|
|
|
14
39
|
GROUPS = %i[describe context feature shared_examples shared_examples_for shared_context].freeze
|
|
15
40
|
SKIPPED_GROUPS = %i[xdescribe xcontext xfeature].freeze
|
|
@@ -33,6 +58,18 @@ module Slopbouncer
|
|
|
33
58
|
Reader.new(helpers).read(Prism.parse(source).value)
|
|
34
59
|
end
|
|
35
60
|
|
|
61
|
+
# The methods a file defines, other than tests written as methods, each
|
|
62
|
+
# with what it parses to.
|
|
63
|
+
def self.methods_in(source)
|
|
64
|
+
found = {}
|
|
65
|
+
walk = lambda do |node|
|
|
66
|
+
found[node.name.to_s] = Form.of(node) if node.is_a?(Prism::DefNode) && !node.name.start_with?('test_')
|
|
67
|
+
node.compact_child_nodes.each { |child| walk.call(child) }
|
|
68
|
+
end
|
|
69
|
+
walk.call(Prism.parse(source).value)
|
|
70
|
+
found
|
|
71
|
+
end
|
|
72
|
+
|
|
36
73
|
# Walks the syntax tree, keeping the names of the groups it is inside.
|
|
37
74
|
class Reader
|
|
38
75
|
def initialize(helpers)
|
|
@@ -63,7 +100,7 @@ module Slopbouncer
|
|
|
63
100
|
|
|
64
101
|
def call(node)
|
|
65
102
|
label = label(node)
|
|
66
|
-
if unnamed_test?(node) then test(
|
|
103
|
+
if unnamed_test?(node) then test(tidy(node.block.body || node.block), node.block)
|
|
67
104
|
elsif hook?(node) then hook(node)
|
|
68
105
|
elsif !(node.block && label) then node.compact_child_nodes.each { |child| visit(child) }
|
|
69
106
|
elsif GROUPS.include?(node.name) then inside(label, node.block, skipped: marked_skipped?(node)) { visit(node.block) }
|
|
@@ -133,7 +170,7 @@ module Slopbouncer
|
|
|
133
170
|
return if assertions.empty?
|
|
134
171
|
|
|
135
172
|
@tests << Test.new(name: [*@groups, "#{node.name} hook"].join(' '), assertions: assertions,
|
|
136
|
-
skipped: @skipping.positive?, body: body_of(node.block), hooked: false)
|
|
173
|
+
skipped: @skipping.positive?, body: body_of(node.block), hooked: false, form: Form.of(inner(node.block)))
|
|
137
174
|
end
|
|
138
175
|
|
|
139
176
|
# Whether a group has, among its own lines, a hook that asserts.
|
|
@@ -153,7 +190,8 @@ module Slopbouncer
|
|
|
153
190
|
assertions: assertions_in(calls),
|
|
154
191
|
skipped: skipped || @skipping.positive? || calls.any? { |call| call.receiver.nil? && SKIPS.include?(call.name) },
|
|
155
192
|
body: body_of(body),
|
|
156
|
-
hooked: @hooked.positive
|
|
193
|
+
hooked: @hooked.positive?,
|
|
194
|
+
form: Form.of(inner(body))
|
|
157
195
|
)
|
|
158
196
|
end
|
|
159
197
|
|
|
@@ -180,8 +218,14 @@ module Slopbouncer
|
|
|
180
218
|
end
|
|
181
219
|
|
|
182
220
|
def assertion(node)
|
|
183
|
-
|
|
184
|
-
|
|
221
|
+
parts = node.arguments&.arguments.to_a
|
|
222
|
+
made(node, node.name.to_s, parts, sides_of(node.name, parts.size))
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def sides_of(name, count)
|
|
226
|
+
return Array.new(count, :expected) if name.match?(ALL_EXPECTED)
|
|
227
|
+
|
|
228
|
+
SIDES.fetch(name, [])
|
|
185
229
|
end
|
|
186
230
|
|
|
187
231
|
def expectation?(call)
|
|
@@ -200,11 +244,23 @@ module Slopbouncer
|
|
|
200
244
|
matcher = node.arguments&.arguments&.first
|
|
201
245
|
plain = matcher.is_a?(Prism::CallNode) && matcher.receiver.nil? && matcher.block.nil?
|
|
202
246
|
expected = plain ? matcher.arguments&.arguments.to_a : [matcher].compact
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
247
|
+
subject = subject_of(node)
|
|
248
|
+
made(node, [(node.receiver.name if about_a_subject?(node)), node.name, (matcher.name if plain)].compact.join(' '),
|
|
249
|
+
subject + expected, [:subject] * subject.size + [:expected] * expected.size)
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def made(node, method, parts, sides)
|
|
253
|
+
Assertion.new(method: method, arguments: parts.map { |part| tidy(part) }, source: tidy(node),
|
|
254
|
+
forms: parts.map { |part| Form.of(part) }, form: Form.of(node), sides: sides, names: names_in(node))
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def names_in(node)
|
|
258
|
+
calls_in(node).map { |call| call.name.to_s } + symbols_in(node)
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def symbols_in(node)
|
|
262
|
+
own = node.is_a?(Prism::SymbolNode) ? [node.unescaped] : []
|
|
263
|
+
own + node.compact_child_nodes.flat_map { |child| symbols_in(child) }
|
|
208
264
|
end
|
|
209
265
|
|
|
210
266
|
def about_a_subject?(node)
|
|
@@ -226,9 +282,13 @@ module Slopbouncer
|
|
|
226
282
|
|
|
227
283
|
# The lines inside a test, without its opening and closing lines.
|
|
228
284
|
def body_of(node)
|
|
229
|
-
inner = node
|
|
285
|
+
inner = inner(node)
|
|
230
286
|
inner ? inner.slice.lines.map(&:strip).reject(&:empty?) : []
|
|
231
287
|
end
|
|
288
|
+
|
|
289
|
+
def inner(node)
|
|
290
|
+
node.is_a?(Prism::BlockNode) ? node.body : node
|
|
291
|
+
end
|
|
232
292
|
end
|
|
233
293
|
end
|
|
234
294
|
end
|