Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for invalid parser_engine value #286

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/rubocop/ast/processed_source.rb
Expand Up @@ -27,9 +27,10 @@ def self.from_file(path, ruby_version, parser_engine: :parser_whitequark)
end

def initialize(source, ruby_version, path = nil, parser_engine: :parser_whitequark)
parser_engine = parser_engine.to_sym
unless PARSER_ENGINES.include?(parser_engine)
raise ArgumentError, 'The keyword argument `parser_engine` accepts ' \
"`parser` or `parser_prism`, but `#{parser_engine}` was passed."
raise ArgumentError, 'The keyword argument `parser_engine` accepts `parser_whitequark` ' \
"or `parser_prism`, but `#{parser_engine}` was passed."
end

# Defaults source encoding to UTF-8, regardless of the encoding it has
Expand Down
23 changes: 17 additions & 6 deletions spec/rubocop/ast/processed_source_spec.rb
Expand Up @@ -28,16 +28,27 @@ def some_method
end
end

context 'when using invalid `parser_engine` argument' do
let(:parser_engine) { :unknown_parser_engine }

it 'raises a Errno::ENOENT when the file does not exist' do
shared_examples 'invalid parser_engine' do
it 'raises ArgumentError' do
expect { processed_source }.to raise_error(ArgumentError) do |e|
expect(e.message).to eq 'The keyword argument `parser_engine` accepts `parser` or ' \
'`parser_prism`, but `unknown_parser_engine` was passed.'
expected = 'The keyword argument `parser_engine` accepts `parser_whitequark` ' \
"or `parser_prism`, but `#{parser_engine}` was passed."
expect(e.message).to eq(expected)
end
end
end

context 'when using an invalid `parser_engine` symbol argument' do
let(:parser_engine) { :unknown_parser_engine }

it_behaves_like 'invalid parser_engine'
end

context 'when using an invalid `parser_engine` string argument' do
let(:parser_engine) { 'unknown_parser_engine' }

it_behaves_like 'invalid parser_engine'
end
end

describe '.from_file' do
Expand Down