Merge pull request #1982 from dtao/safe-yaml-no-clobber
This commit is contained in:
commit
5edb4c6bfd
|
@ -18,7 +18,7 @@ require 'rubygems'
|
||||||
# stdlib
|
# stdlib
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
require 'time'
|
require 'time'
|
||||||
require 'safe_yaml'
|
require 'safe_yaml/load'
|
||||||
require 'English'
|
require 'English'
|
||||||
require 'pathname'
|
require 'pathname'
|
||||||
|
|
||||||
|
|
|
@ -105,7 +105,7 @@ module Jekyll
|
||||||
when '.toml'
|
when '.toml'
|
||||||
TOML.load_file(filename)
|
TOML.load_file(filename)
|
||||||
when /\.y(a)?ml/
|
when /\.y(a)?ml/
|
||||||
YAML.safe_load_file(filename)
|
SafeYAML.load_file(filename)
|
||||||
else
|
else
|
||||||
raise ArgumentError, "No parser for '#{filename}' is available. Use a .toml or .y(a)ml file instead."
|
raise ArgumentError, "No parser for '#{filename}' is available. Use a .toml or .y(a)ml file instead."
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,7 +45,7 @@ module Jekyll
|
||||||
merged_file_read_opts(opts))
|
merged_file_read_opts(opts))
|
||||||
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.safe_load($1)
|
self.data = SafeYAML.load($1)
|
||||||
end
|
end
|
||||||
rescue SyntaxError => e
|
rescue SyntaxError => e
|
||||||
puts "YAML Exception reading #{File.join(base, name)}: #{e.message}"
|
puts "YAML Exception reading #{File.join(base, name)}: #{e.message}"
|
||||||
|
|
|
@ -233,7 +233,7 @@ module Jekyll
|
||||||
next if File.symlink?(path) && self.safe
|
next if File.symlink?(path) && self.safe
|
||||||
|
|
||||||
key = sanitize_filename(File.basename(entry, '.*'))
|
key = sanitize_filename(File.basename(entry, '.*'))
|
||||||
self.data[key] = YAML.safe_load_file(path)
|
self.data[key] = SafeYAML.load_file(path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -106,31 +106,36 @@ class TestConfiguration < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
should "fire warning with no _config.yml" do
|
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)
|
mock($stderr).puts("Configuration file: none".yellow)
|
||||||
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
|
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
|
||||||
end
|
end
|
||||||
|
|
||||||
should "load configuration as hash" do
|
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}")
|
mock($stdout).puts("Configuration file: #{@path}")
|
||||||
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
|
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
|
||||||
end
|
end
|
||||||
|
|
||||||
should "fire warning with bad config" do
|
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(("WARNING: ".rjust(20) + "Error reading configuration. Using defaults (and options).").yellow)
|
||||||
mock($stderr).puts("Configuration file: (INVALID) #{@path}".yellow)
|
mock($stderr).puts("Configuration file: (INVALID) #{@path}".yellow)
|
||||||
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
|
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
|
||||||
end
|
end
|
||||||
|
|
||||||
should "fire warning when user-specified config file isn't there" do
|
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)
|
mock($stderr).puts(("Fatal: ".rjust(20) + "The configuration file '#{@user_config}' could not be found.").red)
|
||||||
assert_raises LoadError do
|
assert_raises LoadError do
|
||||||
Jekyll.configuration({'config' => [@user_config]})
|
Jekyll.configuration({'config' => [@user_config]})
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
context "loading config from external file" do
|
context "loading config from external file" do
|
||||||
setup do
|
setup do
|
||||||
|
@ -143,19 +148,19 @@ class TestConfiguration < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
should "load default config if no config_file is set" do
|
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]}")
|
mock($stdout).puts("Configuration file: #{@paths[:default]}")
|
||||||
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
|
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({})
|
||||||
end
|
end
|
||||||
|
|
||||||
should "load different config if specified" do
|
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]}")
|
mock($stdout).puts("Configuration file: #{@paths[:other]}")
|
||||||
assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => @paths[:other] })
|
assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => @paths[:other] })
|
||||||
end
|
end
|
||||||
|
|
||||||
should "load default config if path passed is empty" do
|
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]}")
|
mock($stdout).puts("Configuration file: #{@paths[:default]}")
|
||||||
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => @paths[:empty] })
|
assert_equal Jekyll::Configuration::DEFAULTS, Jekyll.configuration({ "config" => @paths[:empty] })
|
||||||
end
|
end
|
||||||
|
@ -167,8 +172,8 @@ class TestConfiguration < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
should "load multiple config files" do
|
should "load multiple config files" do
|
||||||
mock(YAML).safe_load_file(@paths[:default]) { Hash.new }
|
mock(SafeYAML).load_file(@paths[:default]) { Hash.new }
|
||||||
mock(YAML).safe_load_file(@paths[:other]) { Hash.new }
|
mock(SafeYAML).load_file(@paths[:other]) { Hash.new }
|
||||||
mock(TOML).load_file(@paths[:toml]) { Hash.new }
|
mock(TOML).load_file(@paths[:toml]) { Hash.new }
|
||||||
mock($stdout).puts("Configuration file: #{@paths[:default]}")
|
mock($stdout).puts("Configuration file: #{@paths[:default]}")
|
||||||
mock($stdout).puts("Configuration file: #{@paths[:other]}")
|
mock($stdout).puts("Configuration file: #{@paths[:other]}")
|
||||||
|
@ -177,8 +182,8 @@ class TestConfiguration < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
should "load multiple config files and last config should win" do
|
should "load multiple config files and last config should win" do
|
||||||
mock(YAML).safe_load_file(@paths[:default]) { {"baseurl" => "http://example.dev"} }
|
mock(SafeYAML).load_file(@paths[:default]) { {"baseurl" => "http://example.dev"} }
|
||||||
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[:default]}")
|
mock($stdout).puts("Configuration file: #{@paths[:default]}")
|
||||||
mock($stdout).puts("Configuration file: #{@paths[:other]}")
|
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]] })
|
assert_equal Jekyll::Configuration::DEFAULTS.deep_merge({ "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] })
|
||||||
|
|
|
@ -330,7 +330,7 @@ class TestSite < Test::Unit::TestCase
|
||||||
site = Site.new(Jekyll.configuration)
|
site = Site.new(Jekyll.configuration)
|
||||||
site.process
|
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.data['members'], file_content
|
||||||
assert_equal site.site_payload['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 = Site.new(Jekyll.configuration)
|
||||||
site.process
|
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.data['languages'], file_content
|
||||||
assert_equal site.site_payload['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 = Site.new(Jekyll.configuration.merge({'safe' => false}))
|
||||||
site.process
|
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.data['products'], file_content
|
||||||
assert_equal site.site_payload['site']['data']['products'], file_content
|
assert_equal site.site_payload['site']['data']['products'], file_content
|
||||||
|
|
Loading…
Reference in New Issue