Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions bin/prism
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Prism
bin/prism console
bin/prism dot [source]
bin/prism encoding [encoding]
bin/prism error [name] [source]
bin/prism error [source]
bin/prism lex [source]
bin/prism lex_compat [source]
bin/prism locals [source]
Expand Down Expand Up @@ -161,36 +161,17 @@ module Prism
unicode_lists(found) if found == Encoding::UTF_8 || found == Encoding::UTF8_MAC
end

# bin/prism error [name] [source]
# bin/prism error [source]
def error(argv)
name = argv.shift

source = nil
filepath = File.expand_path("../test/prism/errors/#{name}.txt", __dir__)
source, filepath = read_source(argv)
result = Prism.parse(source)

if argv.empty?
raise "Expected #{filepath} to exist" unless File.file?(filepath)

source = File.read(filepath, binmode: true, external_encoding: Encoding::UTF_8)
source = source.lines.grep_v(/^\s*\^/).join.gsub(/\n*\z/, "")
if result.errors.any?
puts result.errors_format
else
if File.file?(filepath)
counter = 1

begin
current = "#{File.dirname(filepath)}/#{File.basename(filepath, ".txt")}_#{counter += 1}.txt"
end while File.file?(current)

filepath = current
end

source, _ = read_source(argv)
puts "No syntax errors for #{filepath}"
end

result = Prism.parse(source)
raise "Expected #{source.inspect} to have errors" if result.success?

File.write(filepath, result.errors_format)
end

# bin/prism lex [source]
Expand Down
6 changes: 5 additions & 1 deletion test/prism/errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Prism
class ErrorsTest < TestCase
base = File.expand_path("errors", __dir__)
filepaths = Dir["**/*.txt", base: base]
filepaths = Dir[ENV.fetch("FOCUS", "**/*.txt"), base: base]

filepaths.each do |filepath|
ruby_versions_for(filepath).each do |version|
Expand Down Expand Up @@ -100,6 +100,10 @@ def assert_errors(filepath, version)
refute_empty errors, "Expected errors in #{filepath}"

actual = result.errors_format
if expected != actual && ENV["UPDATE_SNAPSHOTS"]
File.write(filepath, actual)
end

assert_equal expected, actual, "Expected errors to match for #{filepath}"
end
end
Expand Down