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

Display all syntax error messages when catching SyntaxException #2117

Merged
merged 1 commit into from Mar 21, 2020
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
2 changes: 1 addition & 1 deletion lib/pry/pry_instance.rb
Expand Up @@ -627,7 +627,7 @@ def handle_line(line, options)
begin
complete_expr = Pry::Code.complete_expression?(@eval_string)
rescue SyntaxError => e
output.puts "SyntaxError: #{e.message.sub(/.*syntax error, */m, '')}"
output.puts e.message.gsub(/^.*syntax error, */, "SyntaxError: ")
reset_eval_string
end

Expand Down
28 changes: 23 additions & 5 deletions spec/syntax_checking_spec.rb
Expand Up @@ -23,24 +23,42 @@
end
end

([
[
["end"],
["puts )("],
["1 1"],
["puts :"]
] + [
["puts :"],

# in this case the syntax error is "expecting ')'".
["def", "method(1"],

# in this case the syntax error is "expecting keyword_end".
["o = Object.new.tap{ def o.render;", "'MEH'", "}"]
]).compact.each do |foo|
["o = Object.new.tap{ def o.render;", "'MEH'", "}"],

# multiple syntax errors reported in one SyntaxException
["puts {'key'=>'val'}.to_json"]
].compact.each do |foo|
it "should raise an error on invalid syntax like #{foo.inspect}" do
redirect_pry_io(InputTester.new(*foo), @str_output) do
Pry.start
end

expect(@str_output.string).to match(/SyntaxError/)
end

it "should display correct number of errors on invalid syntax like #{foo.inspect}" do
begin
# rubocop:disable Security/Eval
eval(foo.join("\n"))
# rubocop:enable Security/Eval
rescue SyntaxError => e
error_count = e.message.scan(/syntax error/).count
end
expect(error_count).not_to be_nil

pry_output = mock_pry(*foo)
expect(pry_output.scan(/SyntaxError/).count).to eq(error_count)
end
end

it "should not intefere with syntax errors explicitly raised" do
Expand Down