Skip to content
Browse files

Starting to test recipe code.

  • Loading branch information...
1 parent 04f186e commit 4bc5cc15869b09cf4c556254f8a4f3b0bd4f6388 @svenaas committed Aug 11, 2010
Showing with 66 additions and 2 deletions.
  1. +13 −2 lib/audrey2.rb
  2. +53 −0 test/test_recipes.rb
View
15 lib/audrey2.rb
@@ -220,13 +220,24 @@ def parse_feed(feed, entry_sources)
def load_recipe(recipe)
recipefile = File.join(@recipes_folder, recipe)
if ! File.exist? recipefile
- $stderr.puts "ERROR: Recipe #{recipefile} does not exist"
+ $stderr.puts "ERROR: Recipe file #{recipefile} does not exist"
exit(1)
elsif ! File.readable? recipefile
$stderr.puts "ERROR: Recipe file #{recipefile} is not readable"
exit(1)
end
- YAML::load_file(recipefile).symbolize_keys
+
+ recipe = {}
+
+ begin
+ recipe = YAML::load_file(recipefile).symbolize_keys
+ rescue Exception => e
+ $stderr.puts "ERROR: Problem parsing recipe file #{recipefile}"
+ $stderr.puts e
+ exit(1)
+ end
+
+ recipe
end
def init_config(configfile)
View
53 test/test_recipes.rb
@@ -0,0 +1,53 @@
+require 'helper'
+
+class TestRecipes < Test::Unit::TestCase
+ context "With an initialized Aggregator" do
+ setup do
+ @config
+ File.stubs(:exist?).with('configfile').returns(true)
+ File.stubs(:readable?).with('configfile').returns(true)
+ YAML.stubs(:load_file).with('configfile').returns({
+ 'recipes_folder' => 'recipes_folder',
+ 'themes_folder' => 'themes_folder',
+ 'user_agent' => 'user_agent',
+ 'sort' => 'sort'
+ })
+ File.stubs(:exist?).with('recipes_folder').returns(true)
+ File.stubs(:readable?).with('recipes_folder').returns(true)
+ File.stubs(:exist?).with('themes_folder').returns(true)
+ File.stubs(:readable?).with('themes_folder').returns(true)
+ @aggregator = Audrey2::Aggregator.new('configfile')
+ end
+
+ context "and a nonexistent recipe file" do
+ should 'report error and exit' do
+ recipefile = File.join('recipes_folder', 'recipe')
+ File.expects(:exist?).with(recipefile).returns(false)
+ err = capture_stderr { assert_raise(SystemExit) { @aggregator.feed_me('recipe') } }
+ assert_match /ERROR: Recipe file #{recipefile} does not exist/, err.string
+ end
+ end
+
+ context "and an unreadable recipe file" do
+ should 'report error and exit' do
+ recipefile = File.join('recipes_folder', 'recipe')
+ File.stubs(:exist?).with(recipefile).returns(true)
+ File.expects(:readable?).with(recipefile).returns(false)
+ err = capture_stderr { assert_raise(SystemExit) { @aggregator.feed_me('recipe') } }
+ assert_match /ERROR: Recipe file #{recipefile} is not readable/, err.string
+ end
+ end
+
+ context "with an unparsable recipe file" do
+ should 'report error and exit' do
+ recipefile = File.join('recipes_folder', 'recipe')
+ File.stubs(:exist?).with(recipefile).returns(true)
+ File.expects(:readable?).with(recipefile).returns(true)
+ YAML.expects(:load_file).with(recipefile).raises(Exception)
+ err = capture_stderr { assert_raise(SystemExit) { @aggregator.feed_me('recipe') } }
+ assert_match /ERROR: Problem parsing recipe file #{recipefile}/, err.string
+ end
+ end
+
+ end
+end

0 comments on commit 4bc5cc1

Please sign in to comment.
Something went wrong with that request. Please try again.