Skip to content

Commit dc0aa0e

Browse files
jmeridthHkly
andcommitted
fix: tests
Learned we can't get offenses directly off of the cop, it has to be obtained from the commissioner.investigate method. [rubocop sourcecode about not being able to access offenses off the cop base](https://github.com/rubocop/rubocop/blob/9c6e0ef9039f3602cc0b6e267b7be71ed1110d5b/lib/rubocop/cop/base.rb#L314-L318) Learned we have to provide our cop as the forces argument to the commissioner.investigate if we have our own investigate like we do for the overfetch cop. If we don't it never runs. This is not the case with the heredoc cop because it does not have an investigate method. [rubocop commissioner source code where our custom cop invetigate method is actually called](https://github.com/rubocop/rubocop/blob/9c6e0ef9039f3602cc0b6e267b7be71ed1110d5b/lib/rubocop/cop/commissioner.rb#L85) that calls [this function](https://github.com/rubocop/rubocop/blob/9c6e0ef9039f3602cc0b6e267b7be71ed1110d5b/lib/rubocop/cop/commissioner.rb#L163) and you'll notice the second argument is called cops Signed-off-by: jmeridth <[email protected]> Co-authored-by: Hannah Yiu <[email protected]>
1 parent 4ef6ee1 commit dc0aa0e

File tree

4 files changed

+43
-45
lines changed

4 files changed

+43
-45
lines changed

lib/rubocop/cop/graphql/overfetch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def investigate(processed_source)
4242

4343
visitor.fields.each do |field, count|
4444
next if count > 0
45-
add_offense(nil, message: "GraphQL field '#{field}' query but was not used in template.")
45+
add_offense(visitor.ranges[field], message: "GraphQL field '#{field}' query but was not used in template.")
4646
end
4747
end
4848

test/test_client_fetch.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ def self.resolve_type(_type, _obj, _ctx)
7979
refute response.data
8080

8181
refute_empty response.errors
82-
assert_equal "Field 'err' doesn't exist on type 'Query'", response.errors[:data][0]
82+
assert_includes response.errors[:data][0], "Field 'err' doesn't exist on type 'Query'"
8383

8484
refute_empty response.errors.all
85-
assert_equal "Field 'err' doesn't exist on type 'Query'", response.errors[:data][0]
85+
assert_includes response.errors[:data][0], "Field 'err' doesn't exist on type 'Query'"
8686
end
8787

8888
def test_failed_response

test/test_rubocop_heredoc.rb

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,126 +9,126 @@ def setup
99
end
1010

1111
def test_good_graphql_heredoc
12-
investigate(@cop, <<-RUBY)
12+
result = investigate(@cop, <<-RUBY)
1313
Query = Client.parse <<'GRAPHQL'
1414
{ version }
1515
GRAPHQL
1616
RUBY
1717

18-
assert_empty @cop.offenses.map(&:message)
18+
assert_empty result.offenses.map(&:message)
1919
end
2020

2121
def test_good_graphql_dash_heredoc
22-
investigate(@cop, <<-RUBY)
22+
result = investigate(@cop, <<-RUBY)
2323
Query = Client.parse <<-'GRAPHQL'
2424
{ version }
2525
GRAPHQL
2626
RUBY
2727

28-
assert_empty @cop.offenses.map(&:message)
28+
assert_empty result.offenses.map(&:message)
2929
end
3030

3131
def test_good_graphql_squiggly_heredoc
32-
investigate(@cop, <<-RUBY)
32+
result = investigate(@cop, <<-RUBY)
3333
Query = Client.parse <<~'GRAPHQL'
3434
{ version }
3535
GRAPHQL
3636
RUBY
3737

38-
assert_empty @cop.offenses.map(&:message)
38+
assert_empty result.offenses.map(&:message)
3939
end
4040

4141
def test_bad_graphql_heredoc
42-
investigate(@cop, <<-RUBY)
42+
result = investigate(@cop, <<-RUBY)
4343
Query = Client.parse <<GRAPHQL
4444
{ version }
4545
GRAPHQL
4646
RUBY
4747

48-
assert_equal 1, @cop.offenses.count
49-
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses.first.message
48+
assert_equal 1, result.offenses.count
49+
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses.first.message
5050
end
5151

5252
def test_bad_graphql_dash_heredoc
53-
investigate(@cop, <<-RUBY)
53+
result = investigate(@cop, <<-RUBY)
5454
Query = Client.parse <<-GRAPHQL
5555
{ version }
5656
GRAPHQL
5757
RUBY
5858

59-
assert_equal 1, @cop.offenses.count
60-
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses.first.message
59+
assert_equal 1, result.offenses.count
60+
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses.first.message
6161
end
6262

6363
def test_bad_graphql_squiggly_heredoc
6464
skip if RUBY_VERSION < "2.3"
6565

66-
investigate(@cop, <<-RUBY)
66+
result = investigate(@cop, <<-RUBY)
6767
Query = Client.parse <<~GRAPHQL
6868
{ version }
6969
GRAPHQL
7070
RUBY
7171

72-
assert_equal 1, @cop.offenses.count
73-
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses.first.message
72+
assert_equal 1, result.offenses.count
73+
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses.first.message
7474
end
7575

7676
def test_bad_graphql_heredoc_with_interpolation
77-
investigate(@cop, <<-RUBY)
77+
result = investigate(@cop, <<-RUBY)
7878
field = "version"
7979
Query = Client.parse <<-GRAPHQL
8080
{ \#{field} }
8181
GRAPHQL
8282
RUBY
8383

84-
assert_equal 2, @cop.offenses.count
85-
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", @cop.offenses[0].message
86-
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[1].message
84+
assert_equal 2, result.offenses.count
85+
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", result.offenses[0].message
86+
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[1].message
8787
end
8888

8989
def test_bad_graphql_multiline_heredoc
90-
investigate(@cop, <<-RUBY)
90+
result = investigate(@cop, <<-RUBY)
9191
Query = Client.parse <<GRAPHQL
9292
{
9393
version
9494
}
9595
GRAPHQL
9696
RUBY
9797

98-
assert_equal 1, @cop.offenses.count
99-
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[0].message
98+
assert_equal 1, result.offenses.count
99+
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[0].message
100100
end
101101

102102
def test_bad_graphql_multiline_dash_heredoc
103-
investigate(@cop, <<-RUBY)
103+
result = investigate(@cop, <<-RUBY)
104104
Query = Client.parse <<-GRAPHQL
105105
{
106106
version
107107
}
108108
GRAPHQL
109109
RUBY
110110

111-
assert_equal 1, @cop.offenses.count
112-
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[0].message
111+
assert_equal 1, result.offenses.count
112+
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[0].message
113113
end
114114

115115
def test_bad_graphql_multiline_squiggly_heredoc
116116
skip if RUBY_VERSION < "2.3"
117117

118-
investigate(@cop, <<-RUBY)
118+
result = investigate(@cop, <<-RUBY)
119119
Query = Client.parse <<~GRAPHQL
120120
{
121121
version
122122
}
123123
GRAPHQL
124124
RUBY
125125

126-
assert_equal 1, @cop.offenses.count
127-
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[0].message
126+
assert_equal 1, result.offenses.count
127+
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[0].message
128128
end
129129

130130
def test_bad_graphql_multiline_heredoc_with_interpolation
131-
investigate(@cop, <<-RUBY)
131+
result = investigate(@cop, <<-RUBY)
132132
field = "version"
133133
Query = Client.parse <<-GRAPHQL
134134
{
@@ -137,9 +137,9 @@ def test_bad_graphql_multiline_heredoc_with_interpolation
137137
GRAPHQL
138138
RUBY
139139

140-
assert_equal 2, @cop.offenses.count
141-
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", @cop.offenses[0].message
142-
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", @cop.offenses[1].message
140+
assert_equal 2, result.offenses.count
141+
assert_equal "GraphQL/Heredoc: Do not interpolate variables into GraphQL queries, used variables instead.", result.offenses[0].message
142+
assert_equal "GraphQL/Heredoc: GraphQL heredocs should be quoted. <<-'GRAPHQL'", result.offenses[1].message
143143
end
144144

145145
private
@@ -148,6 +148,5 @@ def investigate(cop, src)
148148
processed_source = RuboCop::ProcessedSource.new(src, RUBY_VERSION.to_f)
149149
commissioner = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
150150
commissioner.investigate(processed_source)
151-
commissioner
152151
end
153152
end

test/test_rubocop_overfetch.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,32 @@ def setup
1212
end
1313

1414
def test_all_fields_used
15-
investigate(@cop, "#{Root}/views/users/show.html.erb")
15+
result = investigate(@cop, "#{Root}/views/users/show.html.erb")
1616

17-
assert_empty @cop.offenses.map(&:message)
17+
assert_empty result.offenses.map(&:message)
1818
end
1919

2020
def test_all_fields_used_with_safe_navigation_operator
2121
skip if RUBY_VERSION < "2.3"
2222

23-
investigate(@cop, "#{Root}/views/users/show-2-3.html.erb")
23+
result = investigate(@cop, "#{Root}/views/users/show-2-3.html.erb")
2424

25-
assert_empty @cop.offenses.map(&:message)
25+
assert_empty result.offenses.map(&:message)
2626
end
2727

2828
def test_field_unused
29-
investigate(@cop, "#{Root}/views/users/overfetch.html.erb")
29+
result = investigate(@cop, "#{Root}/views/users/overfetch.html.erb")
3030

31-
assert_equal 1, @cop.offenses.count
32-
assert_equal "GraphQL/Overfetch: GraphQL field 'birthday' query but was not used in template.", @cop.offenses.first.message
31+
assert_equal 1, result.offenses.count
32+
assert_equal "GraphQL/Overfetch: GraphQL field 'birthday' query but was not used in template.", result.offenses.first.message
3333
end
3434

3535
private
3636

3737
def investigate(cop, path)
3838
engine = GraphQL::Client::ERB.new(File.read(path))
3939
processed_source = RuboCop::ProcessedSource.new(engine.src.dup, RUBY_VERSION.to_f, path)
40-
commissioner = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
40+
commissioner = RuboCop::Cop::Commissioner.new([cop], [cop], raise_error: true)
4141
commissioner.investigate(processed_source)
42-
commissioner
4342
end
4443
end

0 commit comments

Comments
 (0)