Merge branch 'message-for-yaml-error' of https://github.com/stereobooster/jekyll into stereobooster-message-for-yaml-error

This commit is contained in:
Tom Preston-Werner 2012-12-31 13:25:05 -08:00
commit 2c0b3b33d9
5 changed files with 43 additions and 2 deletions

View File

@ -25,14 +25,16 @@ module Jekyll
# #
# Returns nothing. # Returns nothing.
def read_yaml(base, name) def read_yaml(base, name)
self.content = File.read(File.join(base, name))
begin begin
self.content = File.read(File.join(base, name))
if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.content = $POSTMATCH self.content = $POSTMATCH
self.data = YAML.load($1) self.data = YAML.load($1)
end end
rescue => e rescue => e
puts "Error reading file #{name}: #{e.message}"
rescue SyntaxError => e
puts "YAML Exception reading #{name}: #{e.message}" puts "YAML Exception reading #{name}: #{e.message}"
end end

View File

@ -0,0 +1,4 @@
---
bad yaml: [
---
Real content starts here

View File

@ -0,0 +1,7 @@
---
test: good
---
Real content starts here
Ðóññêèé òåêñò

View File

@ -31,4 +31,14 @@ class Test::Unit::TestCase
def clear_dest def clear_dest
FileUtils.rm_rf(dest_dir) FileUtils.rm_rf(dest_dir)
end end
def capture_stdout
$old_stdout = $stdout
$stdout = StringIO.new
yield
$stdout.rewind
return $stdout.string
ensure
$stdout = $old_stdout
end
end end

View File

@ -18,5 +18,23 @@ class TestConvertible < Test::Unit::TestCase
ret = @convertible.read_yaml(@base, 'broken_front_matter1.erb') ret = @convertible.read_yaml(@base, 'broken_front_matter1.erb')
assert_equal({}, ret) assert_equal({}, ret)
end end
should "not parse if there is syntax error in front-matter" do
out = capture_stdout do
ret = @convertible.read_yaml(@base, 'broken_front_matter2.erb')
assert_equal({}, ret)
end
assert_match(/YAML Exception|syntax error/, out)
end
if RUBY_VERSION >= '1.9.2'
should "not parse if there is encoding error in file" do
out = capture_stdout do
ret = @convertible.read_yaml(@base, 'broken_front_matter3.erb')
assert_equal({}, ret)
end
assert_match(/invalid byte sequence in UTF-8/, out)
end
end
end end
end end