rubocop-bottrall 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: e0a08ed9eac30511ec7903e3d93b3cab636a0eff14f68382bc25803f6f915949
4
- data.tar.gz: 947e300fe8c054f27a9ff33cd04a2ee08e5c97440f9a6ad3e877cd7f57d04e3c
3
+ metadata.gz: a031408aadc40ecacb9d3baded630b9dd6967b2889eca4043aa7244a6ef49608
4
+ data.tar.gz: 4387f1f373fc494965168df370680abd47bf6f2cb1594ae7bafdfa9f421c29c3
5
5
  SHA512:
6
- metadata.gz: c2a290b46ed43cb27c02fe83abf86cd2ccfa53c5d3b1c7074084f6f0acfce31a9857c298633662f0f71ec34fe77bebc47bce860d77287c26ecb05f3cc4c62876
7
- data.tar.gz: cc0549fd0868c0ba96fe076d95a78a2c82e97b4409b41f780aa6d93d6a259288bfa491b75be309c2ce01c217d691850ca78ab1cefe3a754542acc18945b17b56
6
+ metadata.gz: dff27aa2b81653f5f1d14feec3e54d25ec0704e4b9ac474b3177950451e9beb8fd51d8d6429d1102358db38d802fb8aa7b8f6e5a719d32d5fea425bfe52ad175
7
+ data.tar.gz: a2cade32ae3ea9ff47b21b0f97c5bb6a20b78a24d6ba1daf7ac9e3b57a0bad98ffab553a340c5bf00a284e88ff740deea5fefac39f66cfe32ecd7e79cf247771
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.2.0](https://github.com/bottrall/blueprint/compare/rubocop-bottrall/v0.1.0...rubocop-bottrall/v0.2.0) (2026-11-15)
4
+
5
+
6
+ ### Features
7
+
8
+ * add Bottrall/MultipleAssertions, which also checks spec-style tests ([#23](https://github.com/bottrall/blueprint/issues/23)) ([1b3e2b1](https://github.com/bottrall/blueprint/commit/1b3e2b14872a4a7f367dc920a53403e392552487))
9
+
3
10
  ## 0.1.0 (2026-11-14)
4
11
 
5
12
 
data/README.md CHANGED
@@ -4,7 +4,8 @@ The shared Ruby style for bottrall projects, as a [RuboCop](https://rubocop.org)
4
4
 
5
5
  - `config/base.yml`: the shared style (new cops enabled, one-argument-per-line layout, `Metrics` off except `Metrics/BlockNesting`, compact class nesting, `extend_self` modules, one assertion per Minitest test), with [rubocop-minitest](https://github.com/rubocop/rubocop-minitest), [rubocop-performance](https://github.com/rubocop/rubocop-performance) and [rubocop-rake](https://github.com/rubocop/rubocop-rake) as dependencies and plugins.
6
6
  - `config/rbs-inline.yml`: lets `Layout/LeadingCommentSpace` and `Layout/LineLength` accept [rbs-inline](https://github.com/soutaro/rbs-inline) annotations.
7
- - Three cops in the `Bottrall/` department:
7
+ - Four cops in the `Bottrall/` department:
8
+ - `Bottrall/MultipleAssertions`: a test makes at most `Max` assertions (default 1). It checks `it`, `test` and `specify` blocks wherever they are, as well as `def test_*` methods, so specs written with a top-level `describe` are covered; `Minitest/MultipleAssertions` only looks inside `*Test` classes, so `config/base.yml` turns it off. Only checks test files.
8
9
  - `Bottrall/NoInheritance`: compose instead of inheriting. A superclass is allowed only when it's listed in `AllowedParents`, its name ends in `Error`, or it's a core class that is `Exception` or a `StandardError` descendant.
9
10
  - `Bottrall/NoMocking`: no `stub`, no `Minitest::Mock`, and (for code under `OwnedNamespaces`) no methods patched in with `define_singleton_method` / `define_method`. Inject a real lightweight fake instead. Only checks test files (`**/test/**/*`, `**/*_test.rb`).
10
11
  - `Bottrall/RequireDisableReason`: every `rubocop:disable` / `rubocop:todo` ends with `-- <reason>`.
data/config/base.yml CHANGED
@@ -47,8 +47,11 @@ Metrics:
47
47
  Metrics/BlockNesting:
48
48
  Enabled: true
49
49
 
50
+ # Bottrall/MultipleAssertions replaces it: this one only looks inside a class
51
+ # whose name ends in `Test`, so it misses specs written with a top-level
52
+ # `describe`.
50
53
  Minitest/MultipleAssertions:
51
- Max: 1
54
+ Enabled: false
52
55
 
53
56
  Style/ClassAndModuleChildren:
54
57
  EnforcedStyle: compact
data/config/default.yml CHANGED
@@ -1,5 +1,16 @@
1
1
  # Defaults for the Bottrall/ cops, loaded by RuboCop::Bottrall::Plugin.
2
2
 
3
+ Bottrall/MultipleAssertions:
4
+ Description: >-
5
+ A test makes at most Max assertions, counting `it`, `test` and `specify`
6
+ blocks anywhere as well as `def test_*` methods.
7
+ Enabled: true
8
+ VersionAdded: '0.2'
9
+ Max: 1
10
+ Include:
11
+ - '**/test/**/*'
12
+ - '**/*_test.rb'
13
+
3
14
  Bottrall/NoInheritance:
4
15
  Description: >-
5
16
  Compose instead of inheriting: a superclass is allowed only when it's an
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Bottrall
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A test makes at most `Max` assertions (default 1), so a failure names exactly
4
+ # one broken behaviour. Minitest/MultipleAssertions only looks inside a class
5
+ # whose name ends in `Test`, so it never sees a spec written as a top-level
6
+ # `describe`; this cop checks every `it`, `test` and `specify` block wherever it
7
+ # is, plus `def test_*` methods. It counts `assert*`, `refute*` and `flunk`
8
+ # calls and `must_*` / `wont_*` expectations; in an `if` or `case` only the
9
+ # branch with the most assertions counts, as in Minitest/MultipleAssertions.
10
+ class RuboCop::Cop::Bottrall::MultipleAssertions < RuboCop::Cop::Base
11
+ MSG = 'Test case has too many assertions [%<total>d/%<max>d].'
12
+
13
+ TEST_BLOCKS = %i[it test specify].freeze
14
+ ASSERTION = /\A(?:assert|refute)(?:_|\z)|\Aflunk\z/
15
+ EXPECTATION = /\A(?:must|wont)_/
16
+
17
+ def on_block(node)
18
+ return unless test_block?(node)
19
+
20
+ check(node)
21
+ end
22
+ alias on_numblock on_block
23
+ alias on_itblock on_block
24
+
25
+ def on_def(node)
26
+ return unless node.method_name.to_s.start_with?('test_') && !node.arguments?
27
+
28
+ check(node)
29
+ end
30
+
31
+ private
32
+
33
+ def check(node)
34
+ total = count(node.body)
35
+ return if total <= max
36
+
37
+ add_offense(node, message: format(MSG, total: total, max: max))
38
+ end
39
+
40
+ def max
41
+ cop_config.fetch('Max', 1)
42
+ end
43
+
44
+ def test_block?(node)
45
+ node.send_node.receiver.nil? && TEST_BLOCKS.include?(node.method_name)
46
+ end
47
+
48
+ def count(node)
49
+ return 0 unless node.is_a?(RuboCop::AST::Node)
50
+
51
+ case node.type
52
+ when :if, :case, :case_match
53
+ most(node.branches)
54
+ when :rescue
55
+ count(node.body) + most(node.branches)
56
+ when :block, :numblock, :itblock
57
+ test_block?(node) ? 0 : count(node.send_node) + count(node.body)
58
+ else
59
+ own(node) + node.each_child_node.sum { |child| count(child) }
60
+ end
61
+ end
62
+
63
+ def most(branches)
64
+ branches.map { |branch| count(branch) }.max || 0
65
+ end
66
+
67
+ def own(node)
68
+ return 0 unless node.send_type?
69
+
70
+ name = node.method_name.to_s
71
+ assertion = node.receiver.nil? && ASSERTION.match?(name)
72
+ assertion || EXPECTATION.match?(name) ? 1 : 0
73
+ end
74
+ end
@@ -4,6 +4,7 @@
4
4
  module RuboCop::Cop::Bottrall
5
5
  end
6
6
 
7
+ require_relative 'bottrall/multiple_assertions'
7
8
  require_relative 'bottrall/no_inheritance'
8
9
  require_relative 'bottrall/no_mocking'
9
10
  require_relative 'bottrall/require_disable_reason'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-bottrall
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
  - Jake Bottrall
@@ -105,6 +105,7 @@ files:
105
105
  - lib/rubocop/bottrall/plugin.rb
106
106
  - lib/rubocop/bottrall/version.rb
107
107
  - lib/rubocop/cop/bottrall.rb
108
+ - lib/rubocop/cop/bottrall/multiple_assertions.rb
108
109
  - lib/rubocop/cop/bottrall/no_inheritance.rb
109
110
  - lib/rubocop/cop/bottrall/no_mocking.rb
110
111
  - lib/rubocop/cop/bottrall/require_disable_reason.rb