Skip to content

Commit 2efdf11

Browse files
authored
Merge pull request #14 from ydah/add-new4
feat: add Committee/UnusedSchemaCoverage cop to warn when schema coverage is set up without a report call
2 parents bfd5cb3 + 6c6d87c commit 2efdf11

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- [#14](https://github.com/ydah/rubocop-committee/pull/14) Add `Committee/UnusedSchemaCoverage` cop to warn when schema coverage is set up without any report call. ([@ydah])

config/default.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ Committee/UnspecifiedExpectedStatus:
3232
where the expected response status code is required.
3333
Enabled: pending
3434
VersionAdded: "0.1"
35+
36+
Committee/UnusedSchemaCoverage:
37+
Description: Check for schema coverage setup without any report call.
38+
Enabled: pending
39+
VersionAdded: "<<next>>"

docs/modules/ROOT/pages/cops.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
* xref:cops_committee.adoc#committeemultipleschemaconform[Committee/MultipleSchemaConform]
88
* xref:cops_committee.adoc#committeeredundantresponsestatusassertions[Committee/RedundantResponseStatusAssertions]
99
* xref:cops_committee.adoc#committeeunspecifiedexpectedstatus[Committee/UnspecifiedExpectedStatus]
10+
* xref:cops_committee.adoc#committeeunusedschemacoverage[Committee/UnusedSchemaCoverage]
1011

1112
// END_COP_LIST

docs/modules/ROOT/pages/cops_committee.adoc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,40 @@ it 'does something' do
170170
assert_schema_conform(200)
171171
end
172172
----
173+
174+
[#committeeunusedschemacoverage]
175+
== Committee/UnusedSchemaCoverage
176+
177+
|===
178+
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
179+
180+
| Enabled
181+
| Yes
182+
| No
183+
| -
184+
| -
185+
|===
186+
187+
Check for unused schema coverage setup without any report call.
188+
189+
[#examples-committeeunusedschemacoverage]
190+
=== Examples
191+
192+
[source,ruby]
193+
----
194+
# bad
195+
before do
196+
@schema_coverage = Committee::Test::SchemaCoverage.new(schema)
197+
@committee_options[:schema_coverage] = @schema_coverage
198+
end
199+
200+
# good
201+
before do
202+
@schema_coverage = Committee::Test::SchemaCoverage.new(schema)
203+
@committee_options[:schema_coverage] = @schema_coverage
204+
end
205+
206+
after(:all) do
207+
@schema_coverage.report
208+
end
209+
----

lib/rubocop-committee.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
require_relative "rubocop/cop/committee/deprecated_old_assert_behavior"
1010
require_relative "rubocop/cop/committee/multiple_schema_conform"
1111
require_relative "rubocop/cop/committee/redundant_response_status_assertions"
12+
require_relative "rubocop/cop/committee/unused_schema_coverage"
1213
require_relative "rubocop/cop/committee/unspecified_expected_status"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Committee
6+
# Check for unused schema coverage setup without any report call.
7+
#
8+
# @example
9+
# # bad
10+
# before do
11+
# @schema_coverage = Committee::Test::SchemaCoverage.new(schema)
12+
# @committee_options[:schema_coverage] = @schema_coverage
13+
# end
14+
#
15+
# # good
16+
# before do
17+
# @schema_coverage = Committee::Test::SchemaCoverage.new(schema)
18+
# @committee_options[:schema_coverage] = @schema_coverage
19+
# end
20+
#
21+
# after(:all) do
22+
# @schema_coverage.report
23+
# end
24+
#
25+
class UnusedSchemaCoverage < Base
26+
MSG = "Call `report` or `report_flatten` when schema coverage is set up."
27+
28+
EXAMPLE_GROUP_METHODS = %i[describe context feature].freeze
29+
30+
# @!method schema_coverage_assignment(node)
31+
def_node_search :schema_coverage_assignment, <<~PATTERN
32+
(ivasgn :@schema_coverage (send (const (const (const nil? :Committee) :Test) :SchemaCoverage) :new ...))
33+
PATTERN
34+
35+
# @!method schema_coverage_report?(node)
36+
def_node_search :schema_coverage_report?, <<~PATTERN
37+
(send (ivar :@schema_coverage) {:report :report_flatten})
38+
PATTERN
39+
40+
def on_block(node)
41+
return unless example_group?(node)
42+
43+
assignment = schema_coverage_assignment(node).first
44+
return unless assignment
45+
return if schema_coverage_report?(node)
46+
47+
add_offense(assignment)
48+
end
49+
alias on_numblock on_block
50+
51+
private
52+
53+
def example_group?(node)
54+
send_node = node.send_node
55+
return false unless send_node&.send_type?
56+
57+
EXAMPLE_GROUP_METHODS.include?(send_node.method_name)
58+
end
59+
end
60+
end
61+
end
62+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::Cop::Committee::UnusedSchemaCoverage, :config do
4+
it "registers an offense when schema coverage is set up but not reported" do
5+
expect_offense(<<~RUBY)
6+
RSpec.describe 'Users API' do
7+
before do
8+
@schema_coverage = Committee::Test::SchemaCoverage.new(schema)
9+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Call `report` or `report_flatten` when schema coverage is set up.
10+
@committee_options[:schema_coverage] = @schema_coverage
11+
end
12+
13+
it 'does something' do
14+
get '/users'
15+
assert_schema_conform(200)
16+
end
17+
end
18+
RUBY
19+
end
20+
21+
it "does not register an offense when report is called" do
22+
expect_no_offenses(<<~RUBY)
23+
RSpec.describe 'Users API' do
24+
before do
25+
@schema_coverage = Committee::Test::SchemaCoverage.new(schema)
26+
@committee_options[:schema_coverage] = @schema_coverage
27+
end
28+
29+
after(:all) do
30+
@schema_coverage.report
31+
end
32+
end
33+
RUBY
34+
end
35+
36+
it "does not register an offense when report_flatten is called" do
37+
expect_no_offenses(<<~RUBY)
38+
RSpec.describe 'Users API' do
39+
before do
40+
@schema_coverage = Committee::Test::SchemaCoverage.new(schema)
41+
@committee_options[:schema_coverage] = @schema_coverage
42+
end
43+
44+
after(:all) do
45+
@schema_coverage.report_flatten
46+
end
47+
end
48+
RUBY
49+
end
50+
51+
it "does not register an offense when schema coverage is not used" do
52+
expect_no_offenses(<<~RUBY)
53+
RSpec.describe 'Users API' do
54+
before do
55+
@committee_options[:schema_path] = 'schema.yaml'
56+
end
57+
end
58+
RUBY
59+
end
60+
end

0 commit comments

Comments
 (0)