Skip to content

Commit 47eebba

Browse files
authored
Don't suppress regular ArgumentError exceptions (#366)
* Don't suppress regular ArgumentError exceptions Account specifically for the following scenario: expose :foo, &:bar Note that `:bar.to_proc.parameters` always returns `[[:req], [:rest]]`. We should not swallow ArgumentError exceptions in any other instance. * Update rubocop todos * Test Ruby 3.1 on CI * Skip code coverage on Ruby >= 3.1 See simplecov-ruby/simplecov#1003
1 parent aac8e77 commit 47eebba

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

.github/workflows/ruby.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ubuntu-latest
1414
strategy:
1515
matrix:
16-
ruby-version: ['2.6', '2.7', '3.0', head, jruby, truffleruby]
16+
ruby-version: ['2.6', '2.7', '3.0', '3.1', head, jruby, truffleruby]
1717

1818
steps:
1919
- uses: actions/checkout@v2

.rubocop_todo.yml

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-11-07 00:01:40 UTC using RuboCop version 1.2.0.
3+
# on 2022-07-26 21:29:59 UTC using RuboCop version 1.32.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9+
# Offense count: 1
10+
# This cop supports safe autocorrection (--autocorrect).
11+
# Configuration parameters: Include.
12+
# Include: **/*.gemspec
13+
Gemspec/DeprecatedAttributeAssignment:
14+
Exclude:
15+
- 'grape-entity.gemspec'
16+
17+
# Offense count: 1
18+
# This cop supports safe autocorrection (--autocorrect).
19+
# Configuration parameters: Include.
20+
# Include: **/*.gemspec
21+
Gemspec/RequireMFA:
22+
Exclude:
23+
- 'grape-entity.gemspec'
24+
925
# Offense count: 1
1026
# Configuration parameters: Include.
1127
# Include: **/*.gemspec
@@ -14,14 +30,20 @@ Gemspec/RequiredRubyVersion:
1430
- 'grape-entity.gemspec'
1531

1632
# Offense count: 6
17-
# Cop supports --auto-correct.
33+
# This cop supports unsafe autocorrection (--autocorrect-all).
1834
Lint/BooleanSymbol:
1935
Exclude:
2036
- 'spec/grape_entity/exposure_spec.rb'
2137

22-
# Offense count: 1
23-
# Cop supports --auto-correct.
24-
# Configuration parameters: IgnoredMethods.
38+
# Offense count: 15
39+
Style/OpenStructUse:
40+
Exclude:
41+
- 'lib/grape_entity/delegator.rb'
42+
- 'spec/grape_entity/entity_spec.rb'
43+
44+
# Offense count: 2
45+
# This cop supports unsafe autocorrection (--autocorrect-all).
46+
# Configuration parameters: AllowMethodsWithArguments, IgnoredMethods, AllowComments.
2547
# IgnoredMethods: respond_to, define_method
2648
Style/SymbolProc:
2749
Exclude:

lib/grape_entity/entity.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,10 @@ def exec_with_object(options, &block)
522522
end
523523
rescue StandardError => e
524524
# it handles: https://github.com/ruby/ruby/blob/v3_0_0_preview1/NEWS.md#language-changes point 3, Proc
525-
raise Grape::Entity::Deprecated.new e.message, 'in ruby 3.0' if e.is_a?(ArgumentError)
525+
# accounting for expose :foo, &:bar
526+
if e.is_a?(ArgumentError) && block.parameters == [[:req], [:rest]]
527+
raise Grape::Entity::Deprecated.new e.message, 'in ruby 3.0'
528+
end
526529

527530
raise e
528531
end

spec/grape_entity/entity_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ class SomeObject
389389
def method_without_args
390390
'result'
391391
end
392+
393+
def raises_argument_error
394+
raise ArgumentError, 'something different'
395+
end
392396
end
393397

394398
describe 'with block passed in' do
@@ -402,6 +406,17 @@ def method_without_args
402406
value = subject.represent(object).value_for(:that_method_without_args)
403407
expect(value).to eq('result')
404408
end
409+
410+
it 'does not suppress ArgumentError' do
411+
subject.expose :raises_argument_error do |object|
412+
object.raises_argument_error
413+
end
414+
415+
object = SomeObject.new
416+
expect do
417+
subject.represent(object).value_for(:raises_argument_error)
418+
end.to raise_error(ArgumentError, 'something different')
419+
end
405420
end
406421

407422
context 'with block passed in via &' do

spec/spec_helper.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
ActiveSupport::VERSION::MAJOR &&
1010
ActiveSupport::VERSION::MAJOR < 4
1111

12-
SimpleCov.start do
13-
add_filter 'spec/'
12+
# Skip code covarge on Ruby >= 3.1
13+
# See https://github.com/simplecov-ruby/simplecov/issues/1003
14+
unless RUBY_VERSION >= '3.1'
15+
SimpleCov.start do
16+
add_filter 'spec/'
17+
end
18+
19+
Coveralls.wear! unless RUBY_PLATFORM.eql? 'java'
1420
end
1521

16-
Coveralls.wear! unless RUBY_PLATFORM.eql? 'java'
17-
1822
$LOAD_PATH.unshift(File.dirname(__FILE__))
1923
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2024
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'support'))

0 commit comments

Comments
 (0)