slopbouncer 0.1.0 → 0.2.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: ba4f63d3b0f813fcd42c469d4d4051ca2b0c93a3c12d8f34c4ebc8683831b166
4
- data.tar.gz: 4504c98ddfbd09547290ab538c0b8a75d963a0b43d442788eec05c8349daf62e
3
+ metadata.gz: 2ca84305189f394bc4bb5fdf378975319cffcb60a12c8f7daa24bd502e6b8a0e
4
+ data.tar.gz: 871c5423d4062d56b649a2deed05f8ad74d5104d680c78e35b9d602bafb4fd1d
5
5
  SHA512:
6
- metadata.gz: a6a0e5a1f3bb6db696c32ecdf23807fcaadaa62b39d83d91341f9fbca6358cd70990c8d72ecd0aff1934a26d75da4aaa2c1925c13749603c7f7d4b42865c1856
7
- data.tar.gz: '0838bd507b4124151b1d0aed78bafe574fc3c96eba3d306dbde56ff4a8cdb6896200663fcaf339b5a3ac3fc8153cd2502222ed996d01b22da13b274006603acc'
6
+ metadata.gz: 720017c454cf95a4e4a5816e422adfa969cf73192bd76ccfea41564ab1bedeb790f66adf08020ea6dd4668af363d1c13c38ab19e9e4c782b77b9fd7f0e589044
7
+ data.tar.gz: aebfec5161e5b55e0a2f5b6d2bddc6cc374bb6454233b7f777fc6cd4917e24dbcb527fccd7bd137c8cfab92f938a807a79c6f1f0e9caf2d5a7953d9ea306b579
data/README.md CHANGED
@@ -4,7 +4,7 @@ A static check that flags tests which got weaker in a change.
4
4
 
5
5
  A coding agent's job is to make the checks pass. One way to do that is to weaken the check. Slopbouncer compares each test before and after a change and reports the ones that lost something, so a person can decide whether that was intended. It reads code and never runs it, so it takes seconds.
6
6
 
7
- Status: in development. It reads Ruby tests today (Minitest, as `it` blocks or `test_` methods).
7
+ Status: in development. It reads Ruby tests today: Minitest, as `it` blocks or `test_` methods, and RSpec.
8
8
 
9
9
  ## What it flags
10
10
 
@@ -25,13 +25,10 @@ Status: in development. It reads Ruby tests today (Minitest, as `it` blocks or `
25
25
 
26
26
  ## Running it
27
27
 
28
- It needs Ruby 3.4 and git. It is not on RubyGems yet, so build the gem from a clone and install that:
28
+ It needs Ruby 3.4 and git.
29
29
 
30
30
  ```shell
31
- git clone https://github.com/mainline-dev/slopbouncer.git
32
- cd slopbouncer
33
- gem build slopbouncer.gemspec
34
- gem install slopbouncer-0.1.0.gem
31
+ gem install slopbouncer
35
32
  ```
36
33
 
37
34
  Then run it from the root of the repository you want checked:
@@ -85,6 +82,15 @@ Claude Code is the one this has been run under. The Cursor, Gemini CLI and Codex
85
82
 
86
83
  An agent set on a passing build can still get round a static check, so keep running the plain command where a person will see it.
87
84
 
85
+ ## RSpec
86
+
87
+ An expectation is read whole: its subject, its direction and its matcher. For a plain matcher such as `eq(5)` an edited value shows as `5 → 6`. For a matcher built from several calls, such as `change { total }.by(1)`, the whole matcher is shown before and after.
88
+
89
+ - A spec with no name, such as `it { is_expected.to be_valid }`, is known by its body.
90
+ - `:skip`, `:pending`, `skip: 'reason'` and `pending: 'reason'` beside a spec's or a group's name count as skipping it.
91
+ - A `before`, `after` or `around` hook that makes an expectation is read as a test of its own, named after its group. The specs in that group are not reported as asserting nothing.
92
+ - `should` and `should_not` are read as expectations too.
93
+
88
94
  ## Your own assertion helpers
89
95
 
90
96
  Slopbouncer counts calls such as `assert_equal`, `refute_nil`, `must_equal` and `expect` as assertions. If your tests assert through helpers of your own, name them in `.slopbouncer.yml` at the root of your repository:
@@ -101,6 +107,8 @@ A test that only calls helpers Slopbouncer has not been told about looks like a
101
107
 
102
108
  - It catches tests that are visibly weaker. A test that looks thorough and checks nothing needs mutation testing, which is slower and runs the code. Use both: Slopbouncer on every change, mutation testing behind it.
103
109
  - It cannot tell a stronger assertion from a weaker one. An assertion swapped for another kind is reported as removed.
110
+ - It compares assertions as they are written. A value rewritten in another style, such as `{ a: 1 }` to `a: 1`, is flagged as edited.
111
+ - It reads one file at a time. A check that lives elsewhere, such as an RSpec shared example pulled in with `it_behaves_like`, is not followed, so a test that relies on one can look as if it asserts nothing.
104
112
  - It cannot see intent. A test changed on purpose, because the behaviour changed, is flagged the same way as one weakened to get a build to pass.
105
113
 
106
114
  On its first run, over the 706 commits in [Mainline](https://mainline.dev)'s history that touch a test, it flagged 210 of them. In a sample of those, every finding was a real change to a test and every one looked intended. So read a finding as "this test changed in a way worth a look".
@@ -114,6 +122,16 @@ bundle exec rake
114
122
 
115
123
  The tests are scenarios. Each one builds a throwaway git repository, makes a change in it, and runs the real command over that change. `PROJECT.md` holds the plan and the rules still to come.
116
124
 
125
+ ## Releasing
126
+
127
+ Move the version on with `rake bump:patch`, `rake bump:minor` or `rake bump:major`, and commit. Then tag that commit with the same version and push the tag, as the command tells you:
128
+
129
+ ```shell
130
+ git tag v0.2.0 && git push origin v0.2.0
131
+ ```
132
+
133
+ GitHub runs the scenarios and publishes the gem to RubyGems. A tag that does not match the version is refused before anything is published.
134
+
117
135
  ## Licence
118
136
 
119
137
  MIT. See `LICENSE`.
@@ -34,13 +34,22 @@ module Slopbouncer
34
34
  left = fresh.dup
35
35
  gone.filter_map do |was|
36
36
  match = left.max_by { |test| likeness(was, test) }
37
- next unless match && likeness(was, match) >= SAME_TEST
37
+ match = left.find { |test| one_edit?(was, test) } unless match && likeness(was, match) >= SAME_TEST
38
+ next unless match
38
39
 
39
40
  left.delete(match)
40
41
  [was, match]
41
42
  end
42
43
  end
43
44
 
45
+ # A one-line test whose only assertion had one value edited is the same
46
+ # test. This is how a test with no name of its own is followed:
47
+ # it { is_expected.to eq(5) } edited to expect 6.
48
+ def self.one_edit?(was, now)
49
+ [was, now].all? { |test| test.body.size == 1 && test.assertions.size == 1 } &&
50
+ !edited_value(was.assertions.first, now.assertions.first).nil?
51
+ end
52
+
44
53
  def self.likeness(was, now)
45
54
  shared = (was.body & now.body).size
46
55
  total = [was.body.size, now.body.size].max
@@ -60,10 +69,16 @@ module Slopbouncer
60
69
  finding.call('expected_value_changed', detail: edit.source, before: before, after: after)
61
70
  end
62
71
  found << finding.call('skip_added', detail: 'marked to be skipped') if now.skipped && !was.skipped
63
- found << finding.call('no_assertions', detail: 'asserts nothing') if now.assertions.empty? && was.body != now.body
72
+ found << finding.call('no_assertions', detail: 'asserts nothing') if asserts_nothing?(now) && was.body != now.body
64
73
  found
65
74
  end
66
75
 
76
+ # A test with no assertions of its own still asserts when a hook in its
77
+ # group does it for it.
78
+ def self.asserts_nothing?(test)
79
+ test.assertions.empty? && !test.hooked
80
+ end
81
+
67
82
  # The one argument that differs between two calls of the same assertion,
68
83
  # as [before, after], when everything else about them is the same.
69
84
  def self.edited_value(was, now)
@@ -72,6 +87,6 @@ module Slopbouncer
72
87
  differing = was.arguments.zip(now.arguments).reject { |before, after| before == after }
73
88
  differing.first if differing.size == 1
74
89
  end
75
- private_class_method :reworded, :likeness, :weaker, :edited_value
90
+ private_class_method :reworded, :one_edit?, :likeness, :weaker, :asserts_nothing?, :edited_value
76
91
  end
77
92
  end
@@ -7,15 +7,26 @@ module Slopbouncer
7
7
  # the assertions it makes, whether it is marked to be skipped, and its body.
8
8
  # It reads the code and never runs it.
9
9
  module RubyTests
10
- Test = Struct.new(:name, :assertions, :skipped, :body, keyword_init: true)
10
+ # hooked: a hook in one of the test's groups makes an assertion for it.
11
+ Test = Struct.new(:name, :assertions, :skipped, :body, :hooked, keyword_init: true)
11
12
  Assertion = Struct.new(:method, :arguments, :source, keyword_init: true)
12
13
 
13
- GROUPS = %i[describe context feature].freeze
14
+ GROUPS = %i[describe context feature shared_examples shared_examples_for shared_context].freeze
14
15
  SKIPPED_GROUPS = %i[xdescribe xcontext xfeature].freeze
15
16
  TESTS = %i[it test specify scenario example].freeze
16
17
  SKIPPED_TESTS = %i[xit xtest xspecify xscenario xexample].freeze
18
+ # RSpec lets a test go without a name: it { is_expected.to be_valid }
19
+ UNNAMED = %i[it specify].freeze
17
20
  SKIPS = %i[skip pending].freeze
21
+ # What a group runs around each of its tests. A hook can hold the
22
+ # assertion for all of them: after { expect(request).to have_been_made }
23
+ HOOKS = %i[before after around].freeze
18
24
  ASSERTION = /\A(assert|refute|must|wont)(_|\z)|\A(expect|flunk)\z/
25
+ # An RSpec expectation: expect(total).to eq(5), is_expected.not_to be_nil,
26
+ # and the older total.should eq(5).
27
+ SUBJECTS = %i[expect is_expected].freeze
28
+ DIRECTIONS = %i[to not_to to_not].freeze
29
+ SHOULDS = %i[should should_not].freeze
19
30
 
20
31
  # helpers: the project's own method names that count as assertions.
21
32
  def self.in(source, helpers: [])
@@ -29,6 +40,7 @@ module Slopbouncer
29
40
  @tests = []
30
41
  @groups = []
31
42
  @skipping = 0
43
+ @hooked = 0
32
44
  end
33
45
 
34
46
  def read(node)
@@ -51,39 +63,97 @@ module Slopbouncer
51
63
 
52
64
  def call(node)
53
65
  label = label(node)
54
- if !(node.block && label) then node.compact_child_nodes.each { |child| visit(child) }
55
- elsif GROUPS.include?(node.name) then inside(label) { visit(node.block) }
56
- elsif SKIPPED_GROUPS.include?(node.name) then inside(label, skipped: true) { visit(node.block) }
57
- elsif TESTS.include?(node.name) then test(label, node.block)
66
+ if unnamed_test?(node) then test(body_of(node.block).join(' '), node.block)
67
+ elsif hook?(node) then hook(node)
68
+ elsif !(node.block && label) then node.compact_child_nodes.each { |child| visit(child) }
69
+ elsif GROUPS.include?(node.name) then inside(label, node.block, skipped: marked_skipped?(node)) { visit(node.block) }
70
+ elsif SKIPPED_GROUPS.include?(node.name) then inside(label, node.block, skipped: true) { visit(node.block) }
71
+ elsif TESTS.include?(node.name) then test(label, node.block, skipped: marked_skipped?(node))
58
72
  elsif SKIPPED_TESTS.include?(node.name) then test(label, node.block, skipped: true)
59
73
  else node.compact_child_nodes.each { |child| visit(child) }
60
74
  end
61
75
  end
62
76
 
63
- # What a group or test is called: its first argument, as written.
77
+ # What a group or test is called: its first argument, as written, and
78
+ # any descriptions straight after it, as in describe Basket, '#total'.
64
79
  def label(node)
65
- first = node.arguments&.arguments&.first
80
+ first, *rest = node.arguments&.arguments.to_a
66
81
  return unless first
67
82
 
68
- first.respond_to?(:unescaped) ? first.unescaped : first.slice
83
+ more = rest.take_while { |argument| argument.is_a?(Prism::StringNode) }
84
+ [first, *more].map { |argument| argument.respond_to?(:unescaped) ? argument.unescaped : argument.slice }.join(' ')
69
85
  end
70
86
 
71
- def inside(name, skipped: false)
87
+ # A test in a group with a body and no name. It is known by its body.
88
+ def unnamed_test?(node)
89
+ UNNAMED.include?(node.name) && node.receiver.nil? && node.arguments.nil? &&
90
+ node.block.is_a?(Prism::BlockNode) && @groups.any?
91
+ end
92
+
93
+ # RSpec marks a group or test to be skipped beside its name:
94
+ # it 'adds up', :skip do, or it 'adds up', pending: 'later' do.
95
+ def marked_skipped?(node)
96
+ node.arguments&.arguments.to_a.drop(1).any? do |argument|
97
+ case argument
98
+ when Prism::SymbolNode then skip_mark?(argument)
99
+ when Prism::KeywordHashNode, Prism::HashNode then argument.elements.any? { |pair| skip_pair?(pair) }
100
+ end
101
+ end
102
+ end
103
+
104
+ def skip_mark?(node)
105
+ node.is_a?(Prism::SymbolNode) && SKIPS.include?(node.unescaped.to_sym)
106
+ end
107
+
108
+ def skip_pair?(pair)
109
+ pair.is_a?(Prism::AssocNode) && skip_mark?(pair.key) &&
110
+ !pair.value.is_a?(Prism::FalseNode) && !pair.value.is_a?(Prism::NilNode)
111
+ end
112
+
113
+ def inside(name, block = nil, skipped: false)
114
+ hooked = asserting_hook?(block)
72
115
  @groups.push(name)
73
116
  @skipping += 1 if skipped
117
+ @hooked += 1 if hooked
74
118
  yield
75
119
  ensure
120
+ @hooked -= 1 if hooked
76
121
  @skipping -= 1 if skipped
77
122
  @groups.pop
78
123
  end
79
124
 
125
+ def hook?(node)
126
+ hook_shape?(node) && @groups.any?
127
+ end
128
+
129
+ # A hook that makes assertions is read as a test of its own, so an
130
+ # assertion it loses is flagged once and not for every test it covers.
131
+ def hook(node)
132
+ assertions = assertions_in(calls_in(node.block))
133
+ return if assertions.empty?
134
+
135
+ @tests << Test.new(name: [*@groups, "#{node.name} hook"].join(' '), assertions: assertions,
136
+ skipped: @skipping.positive?, body: body_of(node.block), hooked: false)
137
+ end
138
+
139
+ # Whether a group has, among its own lines, a hook that asserts.
140
+ def asserting_hook?(block)
141
+ lines = block.is_a?(Prism::BlockNode) && block.body.is_a?(Prism::StatementsNode) ? block.body.body : []
142
+ lines.any? { |line| line.is_a?(Prism::CallNode) && hook_shape?(line) && assertions_in(calls_in(line.block)).any? }
143
+ end
144
+
145
+ def hook_shape?(node)
146
+ HOOKS.include?(node.name) && node.receiver.nil? && node.block.is_a?(Prism::BlockNode)
147
+ end
148
+
80
149
  def test(name, body, skipped: false)
81
150
  calls = calls_in(body)
82
151
  @tests << Test.new(
83
152
  name: [*@groups, name].join(' '),
84
- assertions: calls.select { |call| assertion?(call) }.map { |call| assertion(call) },
153
+ assertions: assertions_in(calls),
85
154
  skipped: skipped || @skipping.positive? || calls.any? { |call| call.receiver.nil? && SKIPS.include?(call.name) },
86
- body: body_of(body)
155
+ body: body_of(body),
156
+ hooked: @hooked.positive?
87
157
  )
88
158
  end
89
159
 
@@ -94,13 +164,64 @@ module Slopbouncer
94
164
  own + node.compact_child_nodes.flat_map { |child| calls_in(child) }
95
165
  end
96
166
 
167
+ # The assertions among a test's calls, in the order they are written.
168
+ # An RSpec expectation is read whole, so the expect inside it is not
169
+ # counted a second time.
170
+ def assertions_in(calls)
171
+ expectations = calls.select { |call| expectation?(call) }
172
+ inside = expectations.map(&:receiver)
173
+ plain = calls.select { |call| assertion?(call) && inside.none? { |subject| subject.equal?(call) } }
174
+ (expectations + plain).sort_by { |call| call.location.start_offset }
175
+ .map { |call| expectation?(call) ? expectation(call) : assertion(call) }
176
+ end
177
+
97
178
  def assertion?(call)
98
179
  call.name.match?(ASSERTION) || @helpers.include?(call.name)
99
180
  end
100
181
 
101
182
  def assertion(node)
102
- arguments = node.arguments&.arguments.to_a.map { |argument| argument.slice.split.join(' ') }
103
- Assertion.new(method: node.name.to_s, arguments: arguments, source: node.slice.split.join(' '))
183
+ Assertion.new(method: node.name.to_s, arguments: node.arguments&.arguments.to_a.map { |argument| tidy(argument) },
184
+ source: tidy(node))
185
+ end
186
+
187
+ def expectation?(call)
188
+ return !call.arguments.nil? if SHOULDS.include?(call.name)
189
+
190
+ subject = call.receiver
191
+ DIRECTIONS.include?(call.name) && subject.is_a?(Prism::CallNode) &&
192
+ SUBJECTS.include?(subject.name) && subject.receiver.nil?
193
+ end
194
+
195
+ # An expectation as an assertion: what it is about, then what the
196
+ # matcher expects. A plain matcher such as eq(5) gives its own
197
+ # arguments, so an edited value shows as 5 → 6. A matcher built from
198
+ # several calls, such as change { total }.by(1), is taken whole.
199
+ def expectation(node)
200
+ matcher = node.arguments&.arguments&.first
201
+ plain = matcher.is_a?(Prism::CallNode) && matcher.receiver.nil? && matcher.block.nil?
202
+ expected = plain ? matcher.arguments&.arguments.to_a : [matcher].compact
203
+ Assertion.new(
204
+ method: [(node.receiver.name if about_a_subject?(node)), node.name, (matcher.name if plain)].compact.join(' '),
205
+ arguments: (subject_of(node) + expected).map { |part| tidy(part) },
206
+ source: tidy(node)
207
+ )
208
+ end
209
+
210
+ def about_a_subject?(node)
211
+ DIRECTIONS.include?(node.name)
212
+ end
213
+
214
+ # What an expectation is about: expect's arguments or block, or what
215
+ # should was called on.
216
+ def subject_of(node)
217
+ subject = node.receiver
218
+ return [subject].compact unless about_a_subject?(node)
219
+
220
+ [*subject.arguments&.arguments.to_a, subject.block].compact
221
+ end
222
+
223
+ def tidy(node)
224
+ node.slice.split.join(' ')
104
225
  end
105
226
 
106
227
  # The lines inside a test, without its opening and closing lines.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slopbouncer
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kipple Pty Ltd