Rubocop: test/test_configuration.rb

This commit is contained in:
Pat Hawks 2016-05-25 21:59:23 -05:00
parent 6ef2bcb29a
commit fd75f0f7b1
1 changed files with 195 additions and 102 deletions

View File

@ -1,8 +1,8 @@
require 'helper' require "helper"
class TestConfiguration < JekyllUnitTest class TestConfiguration < JekyllUnitTest
@@test_config = { test_config = {
"source" => new(nil).source_dir, "source" => new(nil).source_dir,
"destination" => dest_dir "destination" => dest_dir
} }
@ -12,48 +12,77 @@ class TestConfiguration < JekyllUnitTest
end end
should "merge input over defaults" do should "merge input over defaults" do
result = Configuration.from({"source" => "blah"}) result = Configuration.from({ "source" => "blah" })
refute_equal result["source"], Configuration::DEFAULTS["source"] refute_equal result["source"], Configuration::DEFAULTS["source"]
assert_equal result["source"], "blah" assert_equal result["source"], "blah"
end end
should "fix common mistakes" do should "fix common mistakes" do
result = Configuration.from({"paginate" => 0}) result = Configuration.from({ "paginate" => 0 })
assert_nil result["paginate"], "Expected 'paginate' to be corrected to 'nil', but was #{result["paginate"].inspect}" assert_nil(
result["paginate"],
"Expected 'paginate' to be corrected to 'nil', " \
"but was #{result["paginate"].inspect}"
)
end end
should "add default collections" do should "add default collections" do
result = Configuration.from({}) result = Configuration.from({})
assert_equal result["collections"], {"posts" => {"output" => true, "permalink" => "/:categories/:year/:month/:day/:title:output_ext"}} assert_equal(
result["collections"],
{
"posts" => {
"output" => true,
"permalink" => "/:categories/:year/:month/:day/:title:output_ext"
}
}
)
end end
should "NOT backwards-compatibilize" do should "NOT backwards-compatibilize" do
assert Configuration.from("watch" => true)["watch"], "Expected the 'watch' key to not be removed." assert(
Configuration.from("watch" => true)["watch"],
"Expected the 'watch' key to not be removed."
)
end end
end end
context "#add_default_collections" do context "#add_default_collections" do
should "no-op if collections is nil" do should "no-op if collections is nil" do
result = Configuration[{"collections" => nil}].add_default_collections result = Configuration[{ "collections" => nil }].add_default_collections
assert_nil result["collections"] assert_nil result["collections"]
end end
should "turn an array into a hash" do should "turn an array into a hash" do
result = Configuration[{"collections" => %w{methods}}].add_default_collections result = Configuration[{ "collections" => %w(methods) }].add_default_collections
assert_instance_of Hash, result["collections"] assert_instance_of Hash, result["collections"]
assert_equal result["collections"], {"posts" => {"output" => true}, "methods" => {}} assert_equal(
result["collections"],
{ "posts" => { "output" => true }, "methods" => {} }
)
end end
should "only assign collections.posts.permalink if a permalink is specified" do should "only assign collections.posts.permalink if a permalink is specified" do
result = Configuration[{"permalink" => "pretty", "collections" => {}}].add_default_collections result = Configuration[{ "permalink" => "pretty", "collections" => {} }]
assert_equal result["collections"], {"posts" => {"output" => true, "permalink" => "/:categories/:year/:month/:day/:title/"}} .add_default_collections
assert_equal(
result["collections"],
{
"posts" => {
"output" => true,
"permalink" => "/:categories/:year/:month/:day/:title/"
}
}
)
result = Configuration[{"permalink" => nil, "collections" => {}}].add_default_collections result = Configuration[{ "permalink" => nil, "collections" => {} }]
assert_equal result["collections"], {"posts" => {"output" => true}} .add_default_collections
assert_equal result["collections"], { "posts" => { "output" => true } }
end end
should "forces posts to output" do should "forces posts to output" do
result = Configuration[{"collections" => {"posts" => {"output" => false}}}].add_default_collections result = Configuration[{ "collections" => { "posts" => { "output" => false } } }]
.add_default_collections
assert_equal result["collections"]["posts"]["output"], true assert_equal result["collections"]["posts"]["output"], true
end end
end end
@ -61,18 +90,18 @@ class TestConfiguration < JekyllUnitTest
context "#stringify_keys" do context "#stringify_keys" do
setup do setup do
@mixed_keys = Configuration[{ @mixed_keys = Configuration[{
'markdown' => 'kramdown', "markdown" => "kramdown",
:permalink => 'date', :permalink => "date",
'baseurl' => '/', "baseurl" => "/",
:include => ['.htaccess'], :include => [".htaccess"],
:source => './' :source => "./"
}] }]
@string_keys = Configuration[{ @string_keys = Configuration[{
'markdown' => 'kramdown', "markdown" => "kramdown",
'permalink' => 'date', "permalink" => "date",
'baseurl' => '/', "baseurl" => "/",
'include' => ['.htaccess'], "include" => [".htaccess"],
'source' => './' "source" => "./"
}] }]
end end
should "stringify symbol keys" do should "stringify symbol keys" do
@ -84,10 +113,12 @@ class TestConfiguration < JekyllUnitTest
end end
context "#config_files" do context "#config_files" do
setup do setup do
@config = Configuration[{"source" => source_dir}] @config = Configuration[{ "source" => source_dir }]
@no_override = {} @no_override = {}
@one_config_file = {"config" => "config.yml"} @one_config_file = { "config" => "config.yml" }
@multiple_files = {"config" => %w[config/site.yml config/deploy.toml configuration.yml]} @multiple_files = {
"config" => %w(config/site.yml config/deploy.toml configuration.yml)
}
end end
should "always return an array" do should "always return an array" do
@ -108,52 +139,58 @@ class TestConfiguration < JekyllUnitTest
assert_equal [source_dir("_config.yml")], @config.config_files(@no_override) assert_equal [source_dir("_config.yml")], @config.config_files(@no_override)
end end
should "return the config if given one config file" do should "return the config if given one config file" do
assert_equal %w[config.yml], @config.config_files(@one_config_file) assert_equal %w(config.yml), @config.config_files(@one_config_file)
end end
should "return an array of the config files if given many config files" do should "return an array of the config files if given many config files" do
assert_equal %w[config/site.yml config/deploy.toml configuration.yml], @config.config_files(@multiple_files) assert_equal(
%w(config/site.yml config/deploy.toml configuration.yml),
@config.config_files(@multiple_files)
)
end end
end end
context "#read_config_file" do context "#read_config_file" do
setup do setup do
@config = Configuration[{"source" => source_dir('empty.yml')}] @config = Configuration[{ "source" => source_dir("empty.yml") }]
end end
should "not raise an error on empty files" do should "not raise an error on empty files" do
allow(SafeYAML).to receive(:load_file).with('empty.yml').and_return(false) allow(SafeYAML).to receive(:load_file).with("empty.yml").and_return(false)
Jekyll.logger.log_level = :warn Jekyll.logger.log_level = :warn
@config.read_config_file('empty.yml') @config.read_config_file("empty.yml")
Jekyll.logger.log_level = :info Jekyll.logger.log_level = :info
end end
end end
context "#read_config_files" do context "#read_config_files" do
setup do setup do
@config = Configuration[{"source" => source_dir}] @config = Configuration[{ "source" => source_dir }]
end end
should "continue to read config files if one is empty" do should "continue to read config files if one is empty" do
allow(SafeYAML).to receive(:load_file).with('empty.yml').and_return(false) allow(SafeYAML).to receive(:load_file).with("empty.yml").and_return(false)
allow(SafeYAML).to receive(:load_file).with('not_empty.yml').and_return({'foo' => 'bar', 'include' => '', 'exclude' => ''}) allow(SafeYAML)
.to receive(:load_file)
.with("not_empty.yml")
.and_return({ "foo" => "bar", "include" => "", "exclude" => "" })
Jekyll.logger.log_level = :warn Jekyll.logger.log_level = :warn
read_config = @config.read_config_files(['empty.yml', 'not_empty.yml']) read_config = @config.read_config_files(["empty.yml", "not_empty.yml"])
Jekyll.logger.log_level = :info Jekyll.logger.log_level = :info
assert_equal 'bar', read_config['foo'] assert_equal "bar", read_config["foo"]
end end
end end
context "#backwards_compatibilize" do context "#backwards_compatibilize" do
setup do setup do
@config = Configuration[{ @config = Configuration[{
"auto" => true, "auto" => true,
"watch" => true, "watch" => true,
"server" => true, "server" => true,
"exclude" => "READ-ME.md, Gemfile,CONTRIBUTING.hello.markdown", "exclude" => "READ-ME.md, Gemfile,CONTRIBUTING.hello.markdown",
"include" => "STOP_THE_PRESSES.txt,.heloses, .git", "include" => "STOP_THE_PRESSES.txt,.heloses, .git",
"pygments" => true, "pygments" => true,
"plugins" => true, "plugins" => true,
"layouts" => true, "layouts" => true,
"data_source" => true, "data_source" => true
}] }]
end end
should "unset 'auto' and 'watch'" do should "unset 'auto' and 'watch'" do
@ -169,12 +206,18 @@ class TestConfiguration < JekyllUnitTest
should "transform string exclude into an array" do should "transform string exclude into an array" do
assert @config.key?("exclude") assert @config.key?("exclude")
assert @config.backwards_compatibilize.key?("exclude") assert @config.backwards_compatibilize.key?("exclude")
assert_equal @config.backwards_compatibilize["exclude"], %w[READ-ME.md Gemfile CONTRIBUTING.hello.markdown] assert_equal(
@config.backwards_compatibilize["exclude"],
%w(READ-ME.md Gemfile CONTRIBUTING.hello.markdown)
)
end end
should "transform string include into an array" do should "transform string include into an array" do
assert @config.key?("include") assert @config.key?("include")
assert @config.backwards_compatibilize.key?("include") assert @config.backwards_compatibilize.key?("include")
assert_equal @config.backwards_compatibilize["include"], %w[STOP_THE_PRESSES.txt .heloses .git] assert_equal(
@config.backwards_compatibilize["include"],
%w(STOP_THE_PRESSES.txt .heloses .git)
)
end end
should "set highlighter to pygments" do should "set highlighter to pygments" do
assert @config.key?("pygments") assert @config.key?("pygments")
@ -195,62 +238,79 @@ class TestConfiguration < JekyllUnitTest
end end
context "#fix_common_issues" do context "#fix_common_issues" do
setup do setup do
@config = Proc.new do |val| @config = proc do |val|
Configuration[{ Configuration[{
'paginate' => val "paginate" => val
}] }]
end end
end end
should "sets an invalid 'paginate' value to nil" do should "sets an invalid 'paginate' value to nil" do
assert_nil @config.call(0).fix_common_issues['paginate'] assert_nil @config.call(0).fix_common_issues["paginate"]
assert_nil @config.call(-1).fix_common_issues['paginate'] assert_nil @config.call(-1).fix_common_issues["paginate"]
assert_nil @config.call(true).fix_common_issues['paginate'] assert_nil @config.call(true).fix_common_issues["paginate"]
end end
end end
context "loading configuration" do context "loading configuration" do
setup do setup do
@path = source_dir('_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) do
raise SystemCallError, "No such file or directory - #{@path}"
end
allow($stderr).to receive(:puts).with("Configuration file: none".yellow) allow($stderr).to receive(:puts).with("Configuration file: none".yellow)
assert_equal site_configuration, Jekyll.configuration(@@test_config) 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({})
allow($stdout).to receive(:puts).with("Configuration file: #{@path}") allow($stdout).to receive(:puts).with("Configuration file: #{@path}")
assert_equal site_configuration, Jekyll.configuration(@@test_config) 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([])
allow($stderr).to receive(:puts).and_return(("WARNING: ".rjust(20) + "Error reading configuration. Using defaults (and options).").yellow) allow($stderr)
allow($stderr).to receive(:puts).and_return("Configuration file: (INVALID) #{@path}".yellow) .to receive(:puts)
assert_equal site_configuration, Jekyll.configuration(@@test_config) .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 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
allow(SafeYAML).to receive(:load_file).with(@user_config) { raise SystemCallError, "No such file or directory - #{@user_config}" } allow(SafeYAML).to receive(:load_file).with(@user_config) do
allow($stderr).to receive(:puts).with(("Fatal: ".rjust(20) + "The configuration file '#{@user_config}' could not be found.").red) raise SystemCallError, "No such file or directory - #{@user_config}"
end
allow($stderr)
.to receive(:puts)
.with((
"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 should "not clobber YAML.load to the dismay of other libraries" do
assert_equal :foo, YAML.load(':foo') assert_equal :foo, YAML.load(":foo")
# as opposed to: assert_equal ':foo', SafeYAML.load(':foo') # as opposed to: assert_equal ':foo', SafeYAML.load(':foo')
end end
end end
context "loading config from external file" do context "loading config from external file" do
setup do setup do
@paths = { @paths = {
:default => source_dir('_config.yml'), :default => source_dir("_config.yml"),
:other => source_dir('_config.live.yml'), :other => source_dir("_config.live.yml"),
:toml => source_dir('_config.dev.toml'), :toml => source_dir("_config.dev.toml"),
:empty => "" :empty => ""
} }
end end
@ -258,16 +318,19 @@ 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 site_configuration, Jekyll.configuration(@@test_config) 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]}")
Jekyll.configuration({ "config" => @paths[:other] }) Jekyll.configuration({ "config" => @paths[:other] })
assert_equal \ assert_equal \
site_configuration({ "baseurl" => "http://wahoo.dev" }), site_configuration({ "baseurl" => "http://wahoo.dev" }),
Jekyll.configuration(@@test_config.merge({ "config" => @paths[:other] })) 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
@ -275,68 +338,94 @@ class TestConfiguration < JekyllUnitTest
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}") allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}")
assert_equal \ assert_equal \
site_configuration, site_configuration,
Jekyll.configuration(@@test_config.merge({ "config" => [@paths[:empty]] })) 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 \ assert_equal \
site_configuration({ "baseurl" => "/you-beautiful-blog-you", "title" => "My magnificent site, wut" }), site_configuration({ "baseurl" => "/you-beautiful-blog-you",
Jekyll.configuration(@@test_config.merge({ "config" => [@paths[:toml]] })) "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
should "load multiple config files" do should "load multiple config files" do
External.require_with_graceful_fail('toml') External.require_with_graceful_fail("toml")
allow(SafeYAML).to receive(:load_file).with(@paths[:default]).and_return(Hash.new) allow(SafeYAML).to receive(:load_file).with(@paths[:default]).and_return({})
allow(SafeYAML).to receive(:load_file).with(@paths[:other]).and_return(Hash.new) allow(SafeYAML).to receive(:load_file).with(@paths[:other]).and_return({})
allow(TOML).to receive(:load_file).with(@paths[:toml]).and_return(Hash.new) allow(TOML).to receive(:load_file).with(@paths[:toml]).and_return({})
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 \ assert_equal(
site_configuration, site_configuration,
Jekyll.configuration(@@test_config.merge({ "config" => [@paths[:default], @paths[:other], @paths[:toml]] })) 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
allow(SafeYAML).to receive(:load_file).with(@paths[:default]).and_return({"baseurl" => "http://example.dev"}) allow(SafeYAML)
allow(SafeYAML).to receive(:load_file).with(@paths[:other]).and_return({"baseurl" => "http://wahoo.dev"}) .to receive(:load_file)
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:default]}") .with(@paths[:default])
allow($stdout).to receive(:puts).with("Configuration file: #{@paths[:other]}") .and_return({ "baseurl" => "http://example.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[:other]}")
assert_equal \ assert_equal \
site_configuration({ "baseurl" => "http://wahoo.dev" }), site_configuration({ "baseurl" => "http://wahoo.dev" }),
Jekyll.configuration(@@test_config.merge({ "config" => [@paths[:default], @paths[:other]] })) Jekyll.configuration(
test_config.merge({ "config" => [@paths[:default], @paths[:other]] })
)
end end
end end
context "#add_default_collections" do context "#add_default_collections" do
should "not do anything if collections is nil" do should "not do anything if collections is nil" do
conf = Configuration[default_configuration].tap {|c| c['collections'] = nil } conf = Configuration[default_configuration].tap { |c| c["collections"] = nil }
assert_equal conf.add_default_collections, conf assert_equal conf.add_default_collections, conf
assert_nil conf.add_default_collections['collections'] assert_nil conf.add_default_collections["collections"]
end end
should "converts collections to a hash if an array" do should "converts collections to a hash if an array" do
conf = Configuration[default_configuration].tap {|c| c['collections'] = ['docs'] } conf = Configuration[default_configuration].tap do |c|
c["collections"] = ["docs"]
end
assert_equal conf.add_default_collections, conf.merge({ assert_equal conf.add_default_collections, conf.merge({
"collections" => { "collections" => {
"docs" => {}, "docs" => {},
"posts" => { "posts" => {
"output" => true, "output" => true,
"permalink" => "/:categories/:year/:month/:day/:title:output_ext" "permalink" => "/:categories/:year/:month/:day/:title:output_ext"
}}}) }
}
})
end end
should "force collections.posts.output = true" do should "force collections.posts.output = true" do
conf = Configuration[default_configuration].tap {|c| c['collections'] = {'posts' => {'output' => false}} } conf = Configuration[default_configuration].tap do |c|
c["collections"] = { "posts" => { "output" => false } }
end
assert_equal conf.add_default_collections, conf.merge({ assert_equal conf.add_default_collections, conf.merge({
"collections" => { "collections" => {
"posts" => { "posts" => {
"output" => true, "output" => true,
"permalink" => "/:categories/:year/:month/:day/:title:output_ext" "permalink" => "/:categories/:year/:month/:day/:title:output_ext"
}}}) }
}
})
end end
should "set collections.posts.permalink if it's not set" do should "set collections.posts.permalink if it's not set" do
@ -344,24 +433,28 @@ class TestConfiguration < JekyllUnitTest
assert_equal conf.add_default_collections, conf.merge({ assert_equal conf.add_default_collections, conf.merge({
"collections" => { "collections" => {
"posts" => { "posts" => {
"output" => true, "output" => true,
"permalink" => "/:categories/:year/:month/:day/:title:output_ext" "permalink" => "/:categories/:year/:month/:day/:title:output_ext"
}}}) }
}
})
end end
should "leave collections.posts.permalink alone if it is set" do should "leave collections.posts.permalink alone if it is set" do
posts_permalink = "/:year/:title/" posts_permalink = "/:year/:title/"
conf = Configuration[default_configuration].tap do |c| conf = Configuration[default_configuration].tap do |c|
c['collections'] = { c["collections"] = {
"posts" => { "permalink" => posts_permalink } "posts" => { "permalink" => posts_permalink }
} }
end end
assert_equal conf.add_default_collections, conf.merge({ assert_equal conf.add_default_collections, conf.merge({
"collections" => { "collections" => {
"posts" => { "posts" => {
"output" => true, "output" => true,
"permalink" => posts_permalink "permalink" => posts_permalink
}}}) }
}
})
end end
end end
end end