Skip to content

Commit bfd5cb3

Browse files
authored
Merge pull request #13 from ydah/add-new3
feat: add Committee/DeprecatedOldAssertBehavior cop to warn against enabling deprecated old_assert_behavior
2 parents 9e18f5f + f347686 commit bfd5cb3

File tree

7 files changed

+121
-0
lines changed

7 files changed

+121
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- [#13](https://github.com/ydah/rubocop-committee/pull/13) Add `Committee/DeprecatedOldAssertBehavior` cop to warn when `committee_options` enables `old_assert_behavior`. ([@ydah])

config/default.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Committee/AssertSchemaConformWithoutRequest:
1111
Enabled: pending
1212
VersionAdded: "<<next>>"
1313

14+
Committee/DeprecatedOldAssertBehavior:
15+
Description: Check if `committee_options` enables deprecated `old_assert_behavior`.
16+
Enabled: pending
17+
VersionAdded: "<<next>>"
18+
1419
Committee/MultipleSchemaConform:
1520
Description: Check for multiple schema conformance assertions within the same request block.
1621
Enabled: pending

docs/modules/ROOT/pages/cops.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
=== Department xref:cops_committee.adoc[Committee]
44

55
* xref:cops_committee.adoc#committeeassertschemaconformwithoutrequest[Committee/AssertSchemaConformWithoutRequest]
6+
* xref:cops_committee.adoc#committeedeprecatedoldassertbehavior[Committee/DeprecatedOldAssertBehavior]
67
* xref:cops_committee.adoc#committeemultipleschemaconform[Committee/MultipleSchemaConform]
78
* xref:cops_committee.adoc#committeeredundantresponsestatusassertions[Committee/RedundantResponseStatusAssertions]
89
* xref:cops_committee.adoc#committeeunspecifiedexpectedstatus[Committee/UnspecifiedExpectedStatus]

docs/modules/ROOT/pages/cops_committee.adoc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,37 @@ it 'conforms to schema' do
3838
end
3939
----
4040
41+
[#committeedeprecatedoldassertbehavior]
42+
== Committee/DeprecatedOldAssertBehavior
43+
44+
|===
45+
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
46+
47+
| Enabled
48+
| Yes
49+
| No
50+
| -
51+
| -
52+
|===
53+
54+
Check if `committee_options` enables deprecated `old_assert_behavior`.
55+
56+
[#examples-committeedeprecatedoldassertbehavior]
57+
=== Examples
58+
59+
[source,ruby]
60+
----
61+
# bad
62+
def committee_options
63+
{ schema_path: "schema.yaml", old_assert_behavior: true }
64+
end
65+
66+
# good
67+
def committee_options
68+
{ schema_path: "schema.yaml", old_assert_behavior: false }
69+
end
70+
----
71+
4172
[#committeemultipleschemaconform]
4273
== Committee/MultipleSchemaConform
4374

lib/rubocop-committee.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_relative "rubocop/committee/plugin"
77

88
require_relative "rubocop/cop/committee/assert_schema_conform_without_request"
9+
require_relative "rubocop/cop/committee/deprecated_old_assert_behavior"
910
require_relative "rubocop/cop/committee/multiple_schema_conform"
1011
require_relative "rubocop/cop/committee/redundant_response_status_assertions"
1112
require_relative "rubocop/cop/committee/unspecified_expected_status"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Committee
6+
# Check if `committee_options` enables deprecated `old_assert_behavior`.
7+
#
8+
# @example
9+
# # bad
10+
# def committee_options
11+
# { schema_path: "schema.yaml", old_assert_behavior: true }
12+
# end
13+
#
14+
# # good
15+
# def committee_options
16+
# { schema_path: "schema.yaml", old_assert_behavior: false }
17+
# end
18+
#
19+
class DeprecatedOldAssertBehavior < Base
20+
MSG = "Do not enable deprecated `old_assert_behavior` in `committee_options`."
21+
22+
def on_def(node)
23+
return unless node.method?(:committee_options)
24+
25+
node.each_descendant(:pair).each do |pair|
26+
key, value = *pair
27+
next unless old_assert_behavior_key?(key)
28+
next unless value&.true_type?
29+
30+
add_offense(pair)
31+
end
32+
end
33+
34+
private
35+
36+
def old_assert_behavior_key?(node)
37+
return node.value == :old_assert_behavior if node.sym_type?
38+
return node.value == "old_assert_behavior" if node.str_type?
39+
40+
false
41+
end
42+
end
43+
end
44+
end
45+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::Committee::DeprecatedOldAssertBehavior, :config do
4+
it "registers an offense when `old_assert_behavior: true` is set" do
5+
expect_offense(<<~RUBY)
6+
def committee_options
7+
{ schema_path: "schema.yaml", old_assert_behavior: true }
8+
^^^^^^^^^^^^^^^^^^^^^^^^^ Do not enable deprecated `old_assert_behavior` in `committee_options`.
9+
end
10+
RUBY
11+
end
12+
13+
it "registers an offense when memoized options enable old behavior" do
14+
expect_offense(<<~RUBY)
15+
def committee_options
16+
@committee_options ||= { schema_path: "schema.yaml", old_assert_behavior: true }
17+
^^^^^^^^^^^^^^^^^^^^^^^^^ Do not enable deprecated `old_assert_behavior` in `committee_options`.
18+
end
19+
RUBY
20+
end
21+
22+
it "does not register an offense when `old_assert_behavior` is false" do
23+
expect_no_offenses(<<~RUBY)
24+
def committee_options
25+
{ schema_path: "schema.yaml", old_assert_behavior: false }
26+
end
27+
RUBY
28+
end
29+
30+
it "does not register an offense when `old_assert_behavior` is not present" do
31+
expect_no_offenses(<<~RUBY)
32+
def committee_options
33+
{ schema_path: "schema.yaml" }
34+
end
35+
RUBY
36+
end
37+
end

0 commit comments

Comments
 (0)