slopbouncer 0.1.0 → 0.3.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: 75fbc0d28692eb3df2fcd5cde3e0d0b2f3c55cab7f531b274d58fc90da63c8f4
4
+ data.tar.gz: 4364064bac7d9c3f5b92e7f1df5c215b2c4a4da9fc8ef8ce69fdf24dfab60d8d
5
5
  SHA512:
6
- metadata.gz: a6a0e5a1f3bb6db696c32ecdf23807fcaadaa62b39d83d91341f9fbca6358cd70990c8d72ecd0aff1934a26d75da4aaa2c1925c13749603c7f7d4b42865c1856
7
- data.tar.gz: '0838bd507b4124151b1d0aed78bafe574fc3c96eba3d306dbde56ff4a8cdb6896200663fcaf339b5a3ac3fc8153cd2502222ed996d01b22da13b274006603acc'
6
+ metadata.gz: 2028a31966406e8b1e9eaa5057ee69fde76e25da026eabbb125153dc92bbe6117384b6866dea6e3d337861296c5791c439c965f4aa3cf8963cb1f74794d1a90f
7
+ data.tar.gz: 37d95d69e84d3410ecef7a5123f56f4340308f0531c8732ccd40fd734cee4ed41964d3adb71d9b292289a117d01928b9f51e026e2a417a55933e0fb976ad439c
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:
@@ -40,7 +37,25 @@ Then run it from the root of the repository you want checked:
40
37
  slopbouncer main..HEAD
41
38
  ```
42
39
 
43
- The argument is a range of commits. Give one commit instead, such as `HEAD` or `origin/main`, and it checks everything since then, committed or not. It prints one line for each finding:
40
+ The argument is a range of commits. Give one commit instead, such as `HEAD` or `origin/main`, and it checks everything since then, committed or not. At a terminal it lays the findings out by file, with each rule in colour:
41
+
42
+ ```
43
+ test/basket_test.rb
44
+ Basket applies a voucher
45
+ skip_added marked to be skipped
46
+
47
+ test/checkout_test.rb
48
+ Checkout lets a guest pay
49
+ assertion_removed assert_equal "paid", order.status
50
+
51
+ test/refund_test.rb
52
+ Refund refunds part of an order
53
+ expected_value_changed 100 → 90
54
+
55
+ 3 tests got weaker in 3 files
56
+ ```
57
+
58
+ When its output goes to a file or another program, it prints one plain line for each finding:
44
59
 
45
60
  ```
46
61
  test/basket_test.rb: Basket applies a voucher: skip_added: marked to be skipped
@@ -52,9 +67,10 @@ test/refund_test.rb: Refund refunds part of an order: expected_value_changed: 10
52
67
  |---|---|
53
68
  | `--json` | Prints the findings as JSON |
54
69
  | `--strict` | Exits with 1 when anything is flagged |
70
+ | `--color` | Uses the terminal layout, in colour, wherever the output goes. For a script whose own output a person reads |
55
71
  | `--agent=NAME` | Runs as that coding agent's hook (see below) |
56
72
 
57
- Without `--strict` it exits with 0 whatever it finds: it flags for review and does not block. It exits with 2 when it is given nothing to compare.
73
+ Setting `NO_COLOR` keeps the terminal layout and leaves the colour out. Without `--strict` it exits with 0 whatever it finds: it flags for review and does not block. It exits with 2 when it is given nothing to compare.
58
74
 
59
75
  ## Feeding it back to a coding agent
60
76
 
@@ -85,6 +101,15 @@ Claude Code is the one this has been run under. The Cursor, Gemini CLI and Codex
85
101
 
86
102
  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
103
 
104
+ ## RSpec
105
+
106
+ 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.
107
+
108
+ - A spec with no name, such as `it { is_expected.to be_valid }`, is known by its body.
109
+ - `:skip`, `:pending`, `skip: 'reason'` and `pending: 'reason'` beside a spec's or a group's name count as skipping it.
110
+ - 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.
111
+ - `should` and `should_not` are read as expectations too.
112
+
88
113
  ## Your own assertion helpers
89
114
 
90
115
  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 +126,8 @@ A test that only calls helpers Slopbouncer has not been told about looks like a
101
126
 
102
127
  - 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
128
  - It cannot tell a stronger assertion from a weaker one. An assertion swapped for another kind is reported as removed.
129
+ - It compares assertions as they are written. A value rewritten in another style, such as `{ a: 1 }` to `a: 1`, is flagged as edited.
130
+ - 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
131
  - 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
132
 
106
133
  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 +141,16 @@ bundle exec rake
114
141
 
115
142
  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
143
 
144
+ ## Releasing
145
+
146
+ 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:
147
+
148
+ ```shell
149
+ git tag v0.2.0 && git push origin v0.2.0
150
+ ```
151
+
152
+ GitHub runs the scenarios and publishes the gem to RubyGems. A tag that does not match the version is refused before anything is published.
153
+
117
154
  ## Licence
118
155
 
119
156
  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.
@@ -0,0 +1,41 @@
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
+ module TerminalReport
8
+ BOLD = 1
9
+ RED = 31
10
+ GREEN = 32
11
+ YELLOW = 33
12
+ # Red for a check that has gone, yellow for one that was changed or switched off.
13
+ COLOURS = { 'test_deleted' => RED, 'assertion_removed' => RED, 'no_assertions' => RED,
14
+ 'expected_value_changed' => YELLOW, 'skip_added' => YELLOW }.freeze
15
+
16
+ def self.call(findings, colour:)
17
+ paint = ->(text, code) { colour ? "\e[#{code}m#{text}\e[0m" : text }
18
+ return paint.call('No tests got weaker.', GREEN) if findings.empty?
19
+
20
+ files = findings.group_by(&:file)
21
+ 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")
23
+ end
24
+
25
+ def self.file(file, found, paint)
26
+ 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)}" }]
28
+ end
29
+ [paint.call(file, BOLD), *tests].join("\n")
30
+ end
31
+
32
+ def self.detail(finding)
33
+ finding.before ? "#{finding.before} → #{finding.after}" : finding.detail
34
+ end
35
+
36
+ def self.count(tests, files)
37
+ "#{tests} #{tests == 1 ? 'test' : 'tests'} got weaker in #{files} #{files == 1 ? 'file' : 'files'}"
38
+ end
39
+ private_class_method :file, :detail, :count
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slopbouncer
4
- VERSION = '0.1.0'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/slopbouncer.rb CHANGED
@@ -8,6 +8,7 @@ require_relative 'slopbouncer/ruby_tests'
8
8
  require_relative 'slopbouncer/compare'
9
9
  require_relative 'slopbouncer/agents'
10
10
  require_relative 'slopbouncer/install'
11
+ require_relative 'slopbouncer/terminal_report'
11
12
 
12
13
  # A static check that flags tests which got weaker in a change, so a person
13
14
  # can decide whether that was intended.
@@ -63,13 +64,25 @@ module Slopbouncer
63
64
  return stop_hook(target.to_s, input, agent.split('=', 2)[1] || Agents::DEFAULT) if agent
64
65
 
65
66
  findings = check(target.to_s)
66
- puts arguments.include?('--json') ? JSON.pretty_generate(findings.map { |finding| finding.to_h.compact }) : report(findings)
67
+ puts printed(findings, arguments)
67
68
  arguments.include?('--strict') && findings.any? ? 1 : 0
68
69
  rescue Error => e
69
70
  warn e.message
70
71
  2
71
72
  end
72
73
 
74
+ # JSON when asked for it. Otherwise laid out for a person when the output is
75
+ # a terminal or colour is asked for, and one plain line for each finding
76
+ # when it goes to another program.
77
+ def self.printed(findings, arguments)
78
+ return JSON.pretty_generate(findings.map { |finding| finding.to_h.compact }) if arguments.include?('--json')
79
+
80
+ asked = arguments.intersect?(%w[--color --colour])
81
+ return report(findings) unless asked || $stdout.tty?
82
+
83
+ TerminalReport.call(findings, colour: asked || ENV['NO_COLOR'].to_s.empty?)
84
+ end
85
+
73
86
  # Sets a coding agent up in the current directory: slopbouncer install claude-code
74
87
  def self.install(words, arguments)
75
88
  raise Error, "Say which agent to set up: #{Agents::ALL.keys.join(', ')}" if words.empty?
@@ -136,5 +149,5 @@ module Slopbouncer
136
149
  "#{finding.file}: #{finding.test}: #{finding.rule}: #{what}"
137
150
  }.join("\n")
138
151
  end
139
- private_class_method :deleted, :settings, :install, :stop_hook, :working_directory, :hook_input, :to_the_agent
152
+ private_class_method :deleted, :settings, :printed, :install, :stop_hook, :working_directory, :hook_input, :to_the_agent
140
153
  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.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kipple Pty Ltd
@@ -46,6 +46,7 @@ files:
46
46
  - lib/slopbouncer/git.rb
47
47
  - lib/slopbouncer/install.rb
48
48
  - lib/slopbouncer/ruby_tests.rb
49
+ - lib/slopbouncer/terminal_report.rb
49
50
  - lib/slopbouncer/version.rb
50
51
  homepage: https://github.com/mainline-dev/slopbouncer
51
52
  licenses: