Added tests for invalid yml file

Stephen McLeod committed Dec 22, 2010
commit a78e33e1d2163d331a9839cdba7897cffec4aa6e
Showing 10 changed files with 51 additions and 6 deletions
app/models/cms_layout.rb +2 -0
@@ @@ -55,6 +55,8 @@ class CmsLayout < ActiveRecord::Base
attributes[:parent] = CmsLayout.load_from_file(site, attributes[:parent])
attributes[:cms_site] = site
new(attributes)
+ rescue
+ raise "Failed to load from #{file_path}"
end
# Wrapper around load_from_file and find_by_slug
app/models/cms_page.rb +2 -0
@@ @@ -63,6 +63,8 @@ class CmsPage < ActiveRecord::Base
attributes[:parent] = CmsPage.load_from_file(site, attributes[:parent])
attributes[:cms_site] = site
new(attributes)
+ rescue
+ raise "Failed to load from #{file_path}"
end
# Wrapper around load_from_file and find_by_full_path
app/models/cms_snippet.rb +2 -0
@@ @@ -33,6 +33,8 @@ class CmsSnippet < ActiveRecord::Base
return nil unless File.exists?(file_path)
attributes = YAML.load_file(file_path).symbolize_keys!
new(attributes)
+ rescue
+ raise "Failed to load from #{file_path}"
end
# Wrapper around load_from_file and find_by_slug
tasks/comfortable_mexican_sofa.rake b/lib/tasks/comfortable_mexican_sofa.rake +18 -6
@@ @@ -32,8 +32,12 @@ namespace :comfortable_mexican_sofa do
puts 'Importing Layouts'
puts '-----------------'
layouts = Dir.glob(File.expand_path('layouts/*.yml', @seed_path)).collect do |layout_file_path|
- attributes = YAML.load_file(layout_file_path).symbolize_keys!
- @site.cms_layouts.load_from_file(@site, attributes[:slug])
+ begin
+ attributes = YAML.load_file(layout_file_path).symbolize_keys!
+ @site.cms_layouts.load_from_file(@site, attributes[:slug])
+ rescue
+ nil
+ end
end.compact
CmsLayout.connection.transaction do
# Fixtures are not ordered in any particular way. Saving order matters,
@@ @@ -71,8 +75,12 @@ namespace :comfortable_mexican_sofa do
puts 'Importing Pages'
puts '---------------'
pages = Dir.glob(File.expand_path('pages/**/*.yml', @seed_path)).collect do |page_file_path|
- attributes = YAML.load_file(page_file_path).symbolize_keys!
- @site.cms_pages.load_from_file(@site, attributes[:full_path])
+ begin
+ attributes = YAML.load_file(page_file_path).symbolize_keys!
+ @site.cms_pages.load_from_file(@site, attributes[:full_path])
+ rescue
+ nil
+ end
end.compact
CmsPage.connection.transaction do
# Fixtures are not ordered in any particular way. Saving order matters,
@@ @@ -120,8 +128,12 @@ namespace :comfortable_mexican_sofa do
puts 'Importing Snippets'
puts '------------------'
snippets = Dir.glob(File.expand_path('snippets/*.yml', @seed_path)).collect do |snippet_file_path|
- attributes = YAML.load_file(snippet_file_path).symbolize_keys!
- @site.cms_snippets.load_from_file(@site, attributes[:slug])
+ begin
+ attributes = YAML.load_file(snippet_file_path).symbolize_keys!
+ @site.cms_snippets.load_from_file(@site, attributes[:slug])
+ rescue
+ nil
+ end
end.compact
CmsSnippet.connection.transaction do
snippets.each do |snippet|
test/cms_seeds/test.host/layouts/broken.yml +1 -0
@@ @@ -0,0 +1 @@
+ broken yml file
\ No newline at end of file
test/cms_seeds/test.host/pages/broken.yml +1 -0
@@ @@ -0,0 +1 @@
+ broken yml file
\ No newline at end of file
test/cms_seeds/test.host/snippets/broken.yml +1 -0
@@ @@ -0,0 +1 @@
+ broken yml file
\ No newline at end of file
test/unit/cms_layout_test.rb +8 -0
@@ @@ -109,6 +109,14 @@ class CmsLayoutTest < ActiveSupport::TestCase
assert_equal '<html><div>{{cms:page:content}}</div></html>', layout.merged_content
end
+ def test_load_from_file_broken
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+ error_message = "Failed to load from #{ComfortableMexicanSofa.configuration.seed_data_path}/test.host/layouts/broken.yml"
+ assert_exception_raised RuntimeError, error_message do
+ CmsLayout.load_from_file(cms_sites(:default), 'broken')
+ end
+ end
+
def test_load_for_slug
assert layout = CmsLayout.load_for_slug!(cms_sites(:default), 'default')
assert !layout.new_record?
test/unit/cms_page_test.rb +8 -0
@@ @@ -173,6 +173,14 @@ class CmsPageTest < ActiveSupport::TestCase
assert_equal '<html><div>Sub Child Page Content Content for Default Snippet</div></html>', page.content
end
+ def test_load_from_file_broken
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+ error_message = "Failed to load from #{ComfortableMexicanSofa.configuration.seed_data_path}/test.host/pages/broken.yml"
+ assert_exception_raised RuntimeError, error_message do
+ CmsPage.load_from_file(cms_sites(:default), '/broken')
+ end
+ end
+
def test_load_for_full_path
assert page = CmsPage.load_for_full_path!(cms_sites(:default), '/')
assert !page.new_record?
test/unit/cms_snippet_test.rb +8 -0
@@ @@ -32,6 +32,14 @@ class CmsSnippetTest < ActiveSupport::TestCase
assert_equal 'Content for Default Snippet', snippet.content
end
+ def test_load_from_file_broken
+ ComfortableMexicanSofa.configuration.seed_data_path = File.expand_path('../cms_seeds', File.dirname(__FILE__))
+ error_message = "Failed to load from #{ComfortableMexicanSofa.configuration.seed_data_path}/test.host/snippets/broken.yml"
+ assert_exception_raised RuntimeError, error_message do
+ CmsSnippet.load_from_file(cms_sites(:default), 'broken')
+ end
+ end
+
def test_load_for_slug
assert snippet = CmsSnippet.load_for_slug!(cms_sites(:default), 'default')
assert !snippet.new_record?