Support for `--config [file]` option #561

Closed
wants to merge 3 commits into
from
View
@@ -50,6 +50,10 @@ opts = OptionParser.new do |opts|
end
+ opts.on("--config [FILE]", "Configuration file to use") do |config_file|
+ options['config_file'] = config_file
+ end
+
opts.on("--[no-]safe", "Safe mode (default unsafe)") do |safe|
options['safe'] = safe
end
View
@@ -60,6 +60,8 @@ module Jekyll
'destination' => File.join(Dir.pwd, '_site'),
'plugins' => File.join(Dir.pwd, '_plugins'),
+ 'config_file' => '_config.yml',
+
'future' => true,
'lsi' => false,
'pygments' => false,
@@ -119,8 +121,10 @@ def self.configuration(override)
# then, we need to know where to look for _config.yml
source = override['source'] || Jekyll::DEFAULTS['source']
- # Get configuration from <source>/_config.yml
- config_file = File.join(source, '_config.yml')
+ config_filename = override['config_file'] || Jekyll::DEFAULTS['config_file']
+
+ # Get configuration from <source>/<config_filename>
+ config_file = File.join(source, config_filename)
begin
config = YAML.load_file(config_file)
raise "Invalid configuration - #{config_file}" if !config.is_a?(Hash)
@@ -129,6 +133,7 @@ def self.configuration(override)
$stderr.puts "WARNING: Could not read configuration. " +
"Using defaults (and options)."
$stderr.puts "\t" + err.to_s
+ override.delete 'config_file' # bad config file, using default one
config = {}
end
View
@@ -1,6 +1,7 @@
require 'helper'
class TestConfiguration < Test::Unit::TestCase
+
context "loading configuration" do
setup do
@path = File.join(Dir.pwd, '_config.yml')
@@ -26,4 +27,43 @@ class TestConfiguration < Test::Unit::TestCase
assert_equal Jekyll::DEFAULTS, Jekyll.configuration({})
end
end
+
+ context "loading configuration from a non-existent given file" do
+ setup do
+ @path = File.join(Dir.pwd, 'bad.yml')
+ end
+
+ should "fire warning if given file does not exist" do
+ mock(YAML).load_file(@path) { raise "No such file or directory - #{@path}" }
+ mock($stderr).puts("WARNING: Could not read configuration. Using defaults (and options).")
+ mock($stderr).puts("\tNo such file or directory - #{@path}")
+ assert_equal Jekyll::DEFAULTS, Jekyll.configuration({'config_file'=>'bad.yml'})
+ end
+ end
+
+ context "loading configuration from an existent given file" do
+ setup do
+ @path = File.join(Dir.pwd, '_myconfig.yml')
+ end
+
+ should "load configuration as hash" do
+ mock(YAML).load_file(@path) { { 'foo' => 'bar' } }
+ mock($stdout).puts("Configuration from #{@path}")
+ assert_equal Jekyll::DEFAULTS.merge({'config_file'=>'_myconfig.yml', 'foo'=>'bar'}),
+ Jekyll.configuration({'config_file'=>'_myconfig.yml'})
+ end
+ end
+
+ context "loading configuration from a bad given file" do
+ setup do
+ @path = File.join(Dir.pwd, '_array_config.yml')
+ end
+
+ should "fire warning with bad config" do
+ mock(YAML).load_file(@path) { ['foo', 'bar'] }
+ mock($stderr).puts("WARNING: Could not read configuration. Using defaults (and options).")
+ mock($stderr).puts("\tInvalid configuration - #{@path}")
+ assert_equal Jekyll::DEFAULTS, Jekyll.configuration({'config_file'=>'_array_config.yml'})
+ end
+ end
end