Refactor some tests to prevent manipulation of Jekyll::Config::DEFAULTS

This commit is contained in:
Parker Moore 2016-04-04 13:12:30 -07:00 committed by Pat Hawks
parent f2263a11b7
commit dbcbf809ff
5 changed files with 87 additions and 87 deletions

View File

@ -58,11 +58,32 @@ module Minitest::Assertions
def refute_exist(filename, msg = nil)
msg = message(msg) { "Expected '#{filename}' not to exist" }
refute File.exist?(filename), msg
end
module DirectoryHelpers
def dest_dir(*subdirs)
test_dir('dest', *subdirs)
end
def source_dir(*subdirs)
test_dir('source', *subdirs)
end
def test_dir(*subdirs)
File.join(File.dirname(__FILE__), *subdirs)
end
end
class JekyllUnitTest < Minitest::Test
include ::RSpec::Mocks::ExampleMethods
include DirectoryHelpers
extend DirectoryHelpers
def mu_pp obj
s = obj.is_a?(Hash) ? JSON.pretty_generate(obj) : obj.inspect
s = s.encode Encoding.default_external if defined? Encoding
s
end
def mocks_expect(*args)
RSpec::Mocks::ExampleMethods::ExpectHost.instance_method(:expect)\
@ -106,23 +127,11 @@ class JekyllUnitTest < Minitest::Test
add_default_collections
end
def dest_dir(*subdirs)
test_dir("dest", *subdirs)
end
def source_dir(*subdirs)
test_dir("source", *subdirs)
end
def clear_dest
FileUtils.rm_rf(dest_dir)
FileUtils.rm_rf(source_dir(".jekyll-metadata"))
end
def test_dir(*subdirs)
File.join(File.dirname(__FILE__), *subdirs)
end
def directory_with_contents(path)
FileUtils.rm_rf(path)
FileUtils.mkdir(path)

View File

@ -1,7 +1,10 @@
require 'helper'
class TestConfiguration < JekyllUnitTest
@@defaults = Jekyll::Configuration::DEFAULTS.add_default_collections.freeze
@@test_config = {
"source" => new(nil).source_dir,
"destination" => dest_dir
}
context "#stringify_keys" do
setup do
@ -154,27 +157,27 @@ class TestConfiguration < JekyllUnitTest
end
context "loading configuration" do
setup do
@path = File.join(Dir.pwd, '_config.yml')
@path = source_dir('_config.yml')
@user_config = File.join(Dir.pwd, "my_config_file.yml")
end
should "fire warning with no _config.yml" do
allow(SafeYAML).to receive(:load_file).with(@path) { raise SystemCallError, "No such file or directory - #{@path}" }
allow($stderr).to receive(:puts).with("Configuration file: none".yellow)
assert_equal @@defaults, Jekyll.configuration({})
assert_equal site_configuration, Jekyll.configuration(@@test_config)
end
should "load configuration as hash" do
allow(SafeYAML).to receive(:load_file).with(@path).and_return(Hash.new)
allow($stdout).to receive(:puts).with("Configuration file: #{@path}")
assert_equal @@defaults, Jekyll.configuration({})
assert_equal site_configuration, Jekyll.configuration(@@test_config)
end
should "fire warning with bad config" do
allow(SafeYAML).to receive(:load_file).with(@path).and_return(Array.new)
allow($stderr).to receive(:puts).and_return(("WARNING: ".rjust(20) + "Error reading configuration. Using defaults (and options).").yellow)
allow($stderr).to receive(:puts).and_return("Configuration file: (INVALID) #{@path}".yellow)
assert_equal @@defaults, Jekyll.configuration({})
assert_equal site_configuration, Jekyll.configuration(@@test_config)
end
should "fire warning when user-specified config file isn't there" do
@ -193,8 +196,8 @@ class TestConfiguration < JekyllUnitTest
context "loading config from external file" do
setup do
@paths = {
:default => File.join(Dir.pwd, '_config.yml'),
:other => File.join(Dir.pwd, '_config.live.yml'),
:default => source_dir('_config.yml'),
:other => source_dir('_config.live.yml'),
:toml => source_dir('_config.dev.toml'),
:empty => ""
}
@ -203,24 +206,31 @@ class TestConfiguration < JekyllUnitTest
should "load default plus posts config if no config_file is set" do
allow(SafeYAML).to receive(:load_file).with(@paths[:default]).and_return({})
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}")
assert_equal @@defaults, Jekyll.configuration({})
assert_equal site_configuration, Jekyll.configuration(@@test_config)
end
should "load different config if specified" do
allow(SafeYAML).to receive(:load_file).with(@paths[:other]).and_return({"baseurl" => "http://wahoo.dev"})
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:other]}")
assert_equal Utils.deep_merge_hashes(@@defaults, { "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => @paths[:other] })
Jekyll.configuration({ "config" => @paths[:other] })
assert_equal \
site_configuration({ "baseurl" => "http://wahoo.dev" }),
Jekyll.configuration(@@test_config.merge({ "config" => @paths[:other] }))
end
should "load default config if path passed is empty" do
allow(SafeYAML).to receive(:load_file).with(@paths[:default]).and_return({})
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}")
assert_equal @@defaults, Jekyll.configuration({ "config" => @paths[:empty] })
assert_equal \
site_configuration,
Jekyll.configuration(@@test_config.merge({ "config" => [@paths[:empty]] }))
end
should "successfully load a TOML file" do
Jekyll.logger.log_level = :warn
assert_equal @@defaults.clone.merge({ "baseurl" => "/you-beautiful-blog-you", "title" => "My magnificent site, wut" }), Jekyll.configuration({ "config" => [@paths[:toml]] })
assert_equal \
site_configuration({ "baseurl" => "/you-beautiful-blog-you", "title" => "My magnificent site, wut" }),
Jekyll.configuration(@@test_config.merge({ "config" => [@paths[:toml]] }))
Jekyll.logger.log_level = :info
end
@ -233,7 +243,9 @@ class TestConfiguration < JekyllUnitTest
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}")
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:other]}")
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:toml]}")
assert_equal @@defaults, Jekyll.configuration({ "config" => [@paths[:default], @paths[:other], @paths[:toml]] })
assert_equal \
site_configuration,
Jekyll.configuration(@@test_config.merge({ "config" => [@paths[:default], @paths[:other], @paths[:toml]] }))
end
should "load multiple config files and last config should win" do
@ -241,7 +253,9 @@ class TestConfiguration < JekyllUnitTest
allow(SafeYAML).to receive(:load_file).with(@paths[:other]).and_return({"baseurl" => "http://wahoo.dev"})
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}")
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:other]}")
assert_equal Utils.deep_merge_hashes(@@defaults, { "baseurl" => "http://wahoo.dev" }), Jekyll.configuration({ "config" => [@paths[:default], @paths[:other]] })
assert_equal \
site_configuration({ "baseurl" => "http://wahoo.dev" }),
Jekyll.configuration(@@test_config.merge({ "config" => [@paths[:default], @paths[:other]] }))
end
end
end

View File

@ -3,11 +3,9 @@ require "helper"
class TestFrontMatterDefaults < JekyllUnitTest
context "A site with full front matter defaults" do
setup do
@site = Site.new(Jekyll.configuration({
"source" => source_dir,
"destination" => dest_dir,
"defaults" => [{
"scope" => {
@site = fixture_site({
"defaults" => [{
"scope" => {
"path" => "contacts",
"type" => "page"
},
@ -15,7 +13,7 @@ class TestFrontMatterDefaults < JekyllUnitTest
"key" => "val"
}
}]
}))
})
@site.process
@affected = @site.pages.find { |page| page.relative_path == "/contacts/bar.html" }
@not_affected = @site.pages.find { |page| page.relative_path == "about.html" }
@ -29,18 +27,16 @@ class TestFrontMatterDefaults < JekyllUnitTest
context "A site with front matter type pages and an extension" do
setup do
@site = Site.new(Jekyll.configuration({
"source" => source_dir,
"destination" => dest_dir,
"defaults" => [{
"scope" => {
@site = fixture_site({
"defaults" => [{
"scope" => {
"path" => "index.html"
},
"values" => {
"key" => "val"
}
}]
}))
})
@site.process
@affected = @site.pages.find { |page| page.relative_path == "index.html" }
@ -55,18 +51,17 @@ class TestFrontMatterDefaults < JekyllUnitTest
context "A site with front matter defaults with no type" do
setup do
@site = Site.new(Jekyll.configuration({
"source" => source_dir,
"destination" => dest_dir,
"defaults" => [{
"scope" => {
@site = fixture_site({
"defaults" => [{
"scope" => {
"path" => "win"
},
"values" => {
"key" => "val"
}
}]
}))
})
@site.process
@affected = @site.posts.docs.find { |page| page.relative_path =~ %r!win\/! }
@not_affected = @site.pages.find { |page| page.relative_path == "about.html" }
@ -80,18 +75,17 @@ class TestFrontMatterDefaults < JekyllUnitTest
context "A site with front matter defaults with no path and a deprecated type" do
setup do
@site = Site.new(Jekyll.configuration({
"source" => source_dir,
"destination" => dest_dir,
"defaults" => [{
"scope" => {
@site = fixture_site({
"defaults" => [{
"scope" => {
"type" => "page"
},
"values" => {
"key" => "val"
}
}]
}))
})
@site.process
@affected = @site.pages
@not_affected = @site.posts.docs
@ -106,18 +100,16 @@ class TestFrontMatterDefaults < JekyllUnitTest
context "A site with front matter defaults with no path" do
setup do
@site = Site.new(Jekyll.configuration({
"source" => source_dir,
"destination" => dest_dir,
"defaults" => [{
"scope" => {
@site = fixture_site({
"defaults" => [{
"scope" => {
"type" => "pages"
},
"values" => {
"key" => "val"
}
}]
}))
})
@site.process
@affected = @site.pages
@not_affected = @site.posts.docs
@ -132,17 +124,15 @@ class TestFrontMatterDefaults < JekyllUnitTest
context "A site with front matter defaults with no path or type" do
setup do
@site = Site.new(Jekyll.configuration({
"source" => source_dir,
"destination" => dest_dir,
"defaults" => [{
"scope" => {
@site = fixture_site({
"defaults" => [{
"scope" => {
},
"values" => {
"key" => "val"
}
}]
}))
})
@site.process
@affected = @site.pages
@not_affected = @site.posts
@ -156,15 +146,13 @@ class TestFrontMatterDefaults < JekyllUnitTest
context "A site with front matter defaults with no scope" do
setup do
@site = Site.new(Jekyll.configuration({
"source" => source_dir,
"destination" => dest_dir,
"defaults" => [{
@site = fixture_site({
"defaults" => [{
"values" => {
"key" => "val"
}
}]
}))
})
@site.process
@affected = @site.pages
@not_affected = @site.posts

View File

@ -4,8 +4,6 @@ class TestGeneratedSite < JekyllUnitTest
context "generated sites" do
setup do
clear_dest
config = Jekyll::Configuration::DEFAULTS.merge({ "source" => source_dir,
"destination" => dest_dir })
@site = fixture_site(config)
@site.process
@ -80,24 +78,15 @@ OUTPUT
should "ensure limit posts is 0 or more" do
assert_raises ArgumentError do
clear_dest
config = Jekyll::Configuration::DEFAULTS.merge({
"source" => source_dir,
"destination" => dest_dir,
"limit_posts" => -1
})
@site = fixture_site(config)
@site = fixture_site("limit_posts" => -1)
end
end
should "acceptable limit post is 0" do
clear_dest
config = Jekyll::Configuration::DEFAULTS.merge({
"source" => source_dir,
"destination" => dest_dir,
"limit_posts" => 0
})
assert Site.new(config), "Couldn't create a site with the given limit_posts."
assert fixture_site("limit_posts" => 0), "Couldn't create a site with limit_posts=0."
end
end
end

View File

@ -3,7 +3,7 @@ require 'helper'
class TestSite < JekyllUnitTest
context "configuring sites" do
should "have an array for plugins by default" do
site = Site.new(Jekyll::Configuration::DEFAULTS)
site = Site.new default_configuration
assert_equal [File.join(Dir.pwd, '_plugins')], site.plugins
end
@ -13,32 +13,32 @@ class TestSite < JekyllUnitTest
end
should "have an array for plugins if passed as a string" do
site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'plugins_dir' => '/tmp/plugins'}))
site = Site.new(build_configs({ 'plugins_dir' => '/tmp/plugins' }))
assert_equal ['/tmp/plugins'], site.plugins
end
should "have an array for plugins if passed as an array" do
site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'plugins_dir' => ['/tmp/plugins', '/tmp/otherplugins']}))
site = Site.new(build_configs({ 'plugins_dir' => ['/tmp/plugins', '/tmp/otherplugins'] }))
assert_equal ['/tmp/plugins', '/tmp/otherplugins'], site.plugins
end
should "have an empty array for plugins if nothing is passed" do
site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'plugins_dir' => []}))
site = Site.new(build_configs({ 'plugins_dir' => [] }))
assert_equal [], site.plugins
end
should "have an empty array for plugins if nil is passed" do
site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'plugins_dir' => nil}))
site = Site.new(build_configs({ 'plugins_dir' => nil }))
assert_equal [], site.plugins
end
should "expose default baseurl" do
site = Site.new(Jekyll::Configuration::DEFAULTS)
site = Site.new(default_configuration)
assert_equal Jekyll::Configuration::DEFAULTS['baseurl'], site.baseurl
end
should "expose baseurl passed in from config" do
site = Site.new(Jekyll::Configuration::DEFAULTS.merge({'baseurl' => '/blog'}))
site = Site.new(build_configs({ 'baseurl' => '/blog' }))
assert_equal '/blog', site.baseurl
end
end