Skip to content

Commit

Permalink
Validating the configuration loads properly and is a hash, based on j…
Browse files Browse the repository at this point in the history
…rk's implementation. Closes jekyll#22.
  • Loading branch information
qrush committed Jun 1, 2009
1 parent 86b1450 commit 486ae25
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion features/support/env.rb
Expand Up @@ -11,6 +11,6 @@

def run_jekyll(opts = {})
command = JEKYLL_PATH
command << " >> /dev/null" if opts[:debug].nil?
command << " >> /dev/null 2>&1" if opts[:debug].nil?
system command
end
9 changes: 5 additions & 4 deletions lib/jekyll.rb
Expand Up @@ -63,14 +63,15 @@ def self.configuration(override)
source = override['source'] || Jekyll::DEFAULTS['source']

# Get configuration from <source>/_config.yml
config = {}
config_file = File.join(source, '_config.yml')
begin
config = YAML.load_file(config_file)
puts "Configuration from #{config_file}"
raise "Invalid configuration - #{config_file}" if !config.is_a?(Hash)
STDOUT.puts "Configuration from #{config_file}"
rescue => err
puts "WARNING: Could not read configuration. Using defaults (and options)."
puts "\t" + err
STDERR.puts "WARNING: Could not read configuration. Using defaults (and options)."
STDERR.puts "\t" + err
config = {}
end

# Merge DEFAULTS < _config.yml < override
Expand Down
29 changes: 29 additions & 0 deletions test/test_configuration.rb
@@ -0,0 +1,29 @@
require File.dirname(__FILE__) + '/helper'

class TestConfiguration < Test::Unit::TestCase
context "loading configuration" do
setup do
@path = './_config.yml'
end

should "fire warning with no _config.yml" 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({})
end

should "load configuration as hash" do
mock(YAML).load_file(@path) { Hash.new }
mock(STDOUT).puts("Configuration from #{@path}")
assert_equal Jekyll::DEFAULTS, Jekyll.configuration({})
end

should "fire warning with bad config" do
mock(YAML).load_file(@path) { Array.new }
mock(STDERR).puts("WARNING: Could not read configuration. Using defaults (and options).")
mock(STDERR).puts("\tInvalid configuration - #{@path}")
assert_equal Jekyll::DEFAULTS, Jekyll.configuration({})
end
end
end

0 comments on commit 486ae25

Please sign in to comment.