Merge pull request #1982 from dtao/safe-yaml-no-clobber

This commit is contained in:
Matt Rogers 2014-01-31 06:48:16 -08:00
commit 5edb4c6bfd
6 changed files with 23 additions and 18 deletions

View File

@ -18,7 +18,7 @@ require 'rubygems'
# stdlib
require 'fileutils'
require 'time'
require 'safe_yaml'
require 'safe_yaml/load'
require 'English'
require 'pathname'

View File

@ -105,7 +105,7 @@ module Jekyll
when '.toml'
TOML.load_file(filename)
when /\.y(a)?ml/
YAML.safe_load_file(filename)
SafeYAML.load_file(filename)
else
raise ArgumentError, "No parser for '#{filename}' is available. Use a .toml or .y(a)ml file instead."
end

View File

@ -45,7 +45,7 @@ module Jekyll
merged_file_read_opts(opts))
if self.content =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.content = $POSTMATCH
self.data = YAML.safe_load($1)
self.data = SafeYAML.load($1)
end
rescue SyntaxError => e
puts "YAML Exception reading #{File.join(base, name)}: #{e.message}"

View File

@ -233,7 +233,7 @@ module Jekyll
next if File.symlink?(path) && self.safe
key = sanitize_filename(File.basename(entry, '.*'))
self.data[key] = YAML.safe_load_file(path)
self.data[key] = SafeYAML.load_file(path)
end
end

View File

@ -106,31 +106,36 @@ class TestConfiguration < Test::Unit::TestCase
end
should "fire warning with no _config.yml" do
mock(YAML).safe_load_file(@path) { raise SystemCallError, "No such file or directory - #{@path}" }
mock(SafeYAML).load_file(@path) { raise SystemCallError, "No such file or directory - #{@path}" }
mock($stderr).puts("Configuration file: none".yellow)
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
end
should "load configuration as hash" do
mock(YAML).safe_load_file(@path) { Hash.new }
mock(SafeYAML).load_file(@path) { Hash.new }
mock($stdout).puts("Configuration file: #{@path}")
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
end
should "fire warning with bad config" do
mock(YAML).safe_load_file(@path) { Array.new }
mock(SafeYAML).load_file(@path) { Array.new }
mock($stderr).puts(("WARNING: ".rjust(20) + "Error reading configuration. Using defaults (and options).").yellow)
mock($stderr).puts("Configuration file: (INVALID) #{@path}".yellow)
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
end
should "fire warning when user-specified config file isn't there" do
mock(YAML).safe_load_file(@user_config) { raise SystemCallError, "No such file or directory - #{@user_config}" }
mock(SafeYAML).load_file(@user_config) { raise SystemCallError, "No such file or directory - #{@user_config}" }
mock($stderr).puts(("Fatal: ".rjust(20) + "The configuration file '#{@user_config}' could not be found.").red)
assert_raises LoadError do
Jekyll.configuration({'config' => [@user_config]})
end
end
should "not clobber YAML.load to the dismay of other libraries" do
assert_equal :foo, YAML.load(':foo')
# as opposed to: assert_equal ':foo', SafeYAML.load(':foo')
end
end
context "loading config from external file" do
setup do
@ -143,19 +148,19 @@ class TestConfiguration < Test::Unit::TestCase
end
should "load default config if no config_file is set" do
mock(YAML).safe_load_file(@paths[:default]) { Hash.new }
mock(SafeYAML).load_file(@paths[:default]) { Hash.new }
mock($stdout).puts("Configuration file: #{@paths[:default]}")
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
end
should "load different config if specified" do
mock(YAML).safe_load_file(@paths[:other]) { {"baseurl" => "http://wahoo.dev"} }
mock(SafeYAML).load_file(@paths[:other]) { {"baseurl" => "http://wahoo.dev"} }
mock($stdout).puts("Configuration file: #{@paths[:other]}")
assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => @paths[:other] })
end
should "load default config if path passed is empty" do
mock(YAML).safe_load_file(@paths[:default]) { Hash.new }
mock(SafeYAML).load_file(@paths[:default]) { Hash.new }
mock($stdout).puts("Configuration file: #{@paths[:default]}")
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => @paths[:empty] })
end
@ -167,8 +172,8 @@ class TestConfiguration < Test::Unit::TestCase
end
should "load multiple config files" do
mock(YAML).safe_load_file(@paths[:default]) { Hash.new }
mock(YAML).safe_load_file(@paths[:other]) { Hash.new }
mock(SafeYAML).load_file(@paths[:default]) { Hash.new }
mock(SafeYAML).load_file(@paths[:other]) { Hash.new }
mock(TOML).load_file(@paths[:toml]) { Hash.new }
mock($stdout).puts("Configuration file: #{@paths[:default]}")
mock($stdout).puts("Configuration file: #{@paths[:other]}")
@ -177,8 +182,8 @@ class TestConfiguration < Test::Unit::TestCase
end
should "load multiple config files and last config should win" do
mock(YAML).safe_load_file(@paths[:default]) { {"baseurl" => "http://example.dev"} }
mock(YAML).safe_load_file(@paths[:other]) { {"baseurl" => "http://wahoo.dev"} }
mock(SafeYAML).load_file(@paths[:default]) { {"baseurl" => "http://example.dev"} }
mock(SafeYAML).load_file(@paths[:other]) { {"baseurl" => "http://wahoo.dev"} }
mock($stdout).puts("Configuration file: #{@paths[:default]}")
mock($stdout).puts("Configuration file: #{@paths[:other]}")
assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] })

View File

@ -330,7 +330,7 @@ class TestSite < Test::Unit::TestCase
site = Site.new(Jekyll.configuration)
site.process
file_content = YAML.safe_load_file(File.join(source_dir, '_data', 'members.yaml'))
file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'members.yaml'))
assert_equal site.data['members'], file_content
assert_equal site.site_payload['site']['data']['members'], file_content
@ -340,7 +340,7 @@ class TestSite < Test::Unit::TestCase
site = Site.new(Jekyll.configuration)
site.process
file_content = YAML.safe_load_file(File.join(source_dir, '_data', 'languages.yml'))
file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'languages.yml'))
assert_equal site.data['languages'], file_content
assert_equal site.site_payload['site']['data']['languages'], file_content
@ -350,7 +350,7 @@ class TestSite < Test::Unit::TestCase
site = Site.new(Jekyll.configuration.merge({'safe' => false}))
site.process
file_content = YAML.safe_load_file(File.join(source_dir, '_data', 'products.yml'))
file_content = SafeYAML.load_file(File.join(source_dir, '_data', 'products.yml'))
assert_equal site.data['products'], file_content
assert_equal site.site_payload['site']['data']['products'], file_content