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) def refute_exist(filename, msg = nil)
msg = message(msg) { "Expected '#{filename}' not to exist" } msg = message(msg) { "Expected '#{filename}' not to exist" }
refute File.exist?(filename), msg 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
end end
class JekyllUnitTest < Minitest::Test class JekyllUnitTest < Minitest::Test
include ::RSpec::Mocks::ExampleMethods 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) def mocks_expect(*args)
RSpec::Mocks::ExampleMethods::ExpectHost.instance_method(:expect)\ RSpec::Mocks::ExampleMethods::ExpectHost.instance_method(:expect)\
@ -106,23 +127,11 @@ class JekyllUnitTest < Minitest::Test
add_default_collections add_default_collections
end end
def dest_dir(*subdirs)
test_dir("dest", *subdirs)
end
def source_dir(*subdirs)
test_dir("source", *subdirs)
end
def clear_dest def clear_dest
FileUtils.rm_rf(dest_dir) FileUtils.rm_rf(dest_dir)
FileUtils.rm_rf(source_dir(".jekyll-metadata")) FileUtils.rm_rf(source_dir(".jekyll-metadata"))
end end
def test_dir(*subdirs)
File.join(File.dirname(__FILE__), *subdirs)
end
def directory_with_contents(path) def directory_with_contents(path)
FileUtils.rm_rf(path) FileUtils.rm_rf(path)
FileUtils.mkdir(path) FileUtils.mkdir(path)

View File

@ -1,7 +1,10 @@
require 'helper' require 'helper'
class TestConfiguration < JekyllUnitTest 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 context "#stringify_keys" do
setup do setup do
@ -154,27 +157,27 @@ class TestConfiguration < JekyllUnitTest
end end
context "loading configuration" do context "loading configuration" do
setup 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") @user_config = File.join(Dir.pwd, "my_config_file.yml")
end end
should "fire warning with no _config.yml" do 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(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) allow($stderr).to receive(:puts).with("Configuration file: none".yellow)
assert_equal @@defaults, Jekyll.configuration({}) assert_equal site_configuration, Jekyll.configuration(@@test_config)
end end
should "load configuration as hash" do should "load configuration as hash" do
allow(SafeYAML).to receive(:load_file).with(@path).and_return(Hash.new) allow(SafeYAML).to receive(:load_file).with(@path).and_return(Hash.new)
allow($stdout).to receive(:puts).with("Configuration file: #{@path}") allow($stdout).to receive(:puts).with("Configuration file: #{@path}")
assert_equal @@defaults, Jekyll.configuration({}) assert_equal site_configuration, Jekyll.configuration(@@test_config)
end end
should "fire warning with bad config" do should "fire warning with bad config" do
allow(SafeYAML).to receive(:load_file).with(@path).and_return(Array.new) 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(("WARNING: ".rjust(20) + "Error reading configuration. Using defaults (and options).").yellow)
allow($stderr).to receive(:puts).and_return("Configuration file: (INVALID) #{@path}".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 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
@ -193,8 +196,8 @@ class TestConfiguration < JekyllUnitTest
context "loading config from external file" do context "loading config from external file" do
setup do setup do
@paths = { @paths = {
:default => File.join(Dir.pwd, '_config.yml'), :default => source_dir('_config.yml'),
:other => File.join(Dir.pwd, '_config.live.yml'), :other => source_dir('_config.live.yml'),
:toml => source_dir('_config.dev.toml'), :toml => source_dir('_config.dev.toml'),
:empty => "" :empty => ""
} }
@ -203,24 +206,31 @@ class TestConfiguration < JekyllUnitTest
should "load default plus posts config if no config_file is set" do 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(SafeYAML).to receive(:load_file).with(@paths[:default]).and_return({})
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}") allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}")
assert_equal @@defaults, Jekyll.configuration({}) assert_equal site_configuration, Jekyll.configuration(@@test_config)
end end
should "load different config if specified" do should "load different config if specified" do
allow(SafeYAML).to receive(:load_file).with(@paths[:other]).and_return({"baseurl" => "http://wahoo.dev"}) 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]}") 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 end
should "load default config if path passed is empty" do should "load default config if path passed is empty" do
allow(SafeYAML).to receive(:load_file).with(@paths[:default]).and_return({}) allow(SafeYAML).to receive(:load_file).with(@paths[:default]).and_return({})
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}") 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 end
should "successfully load a TOML file" do should "successfully load a TOML file" do
Jekyll.logger.log_level = :warn 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 Jekyll.logger.log_level = :info
end 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[:default]}")
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:other]}") allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:other]}")
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:toml]}") 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 end
should "load multiple config files and last config should win" do 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(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[:default]}")
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:other]}") 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 end
end end

View File

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

View File

@ -4,8 +4,6 @@ class TestGeneratedSite < JekyllUnitTest
context "generated sites" do context "generated sites" do
setup do setup do
clear_dest clear_dest
config = Jekyll::Configuration::DEFAULTS.merge({ "source" => source_dir,
"destination" => dest_dir })
@site = fixture_site(config) @site = fixture_site(config)
@site.process @site.process
@ -80,24 +78,15 @@ OUTPUT
should "ensure limit posts is 0 or more" do should "ensure limit posts is 0 or more" do
assert_raises ArgumentError do assert_raises ArgumentError do
clear_dest clear_dest
config = Jekyll::Configuration::DEFAULTS.merge({
"source" => source_dir, @site = fixture_site("limit_posts" => -1)
"destination" => dest_dir,
"limit_posts" => -1
})
@site = fixture_site(config)
end end
end end
should "acceptable limit post is 0" do should "acceptable limit post is 0" do
clear_dest 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 end
end end

View File

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