Permalink
Browse files

* Psych errors with poor yaml formatting are proxied. Fixes #2645, #2731

  • Loading branch information...
1 parent b550ecc commit f2aa46b17c579d2b343b2cbbe99e8f6ca12cd0ac @tenderlove tenderlove committed Sep 1, 2011
Showing with 30 additions and 3 deletions.
  1. +6 −1 activerecord/CHANGELOG
  2. +8 −2 activerecord/lib/active_record/fixtures.rb
  3. +16 −0 activerecord/test/cases/fixtures_test.rb
View
@@ -1,4 +1,9 @@
-* Rails 3.0.10 (unreleased)
+* Rails 3.0.11 (unreleased)
+
+* Psych errors with poor yaml formatting are proxied. Fixes GH #2645 and
+ GH #2731
+
+* Rails 3.0.10
* Magic encoding comment added to schema.rb files
@@ -759,10 +759,16 @@ def yaml_fixtures_key(path)
File.basename(@fixture_path).split(".").first
end
+ RESCUE_ERRORS = [ ArgumentError ]
+
+ if defined?(Psych) && defined?(Psych::SyntaxError)
+ RESCUE_ERRORS << Psych::SyntaxError
+ end
+
def parse_yaml_string(fixture_content)
YAML::load(erb_render(fixture_content))
- rescue => error
- raise Fixture::FormatError, "a YAML error occurred parsing #{yaml_file_path}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}"
+ rescue *RESCUE_ERRORS => error
+ raise Fixture::FormatError, "a YAML error occurred parsing #{yaml_file_path}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace
end
def erb_render(fixture_content)
@@ -20,6 +20,7 @@
require 'models/admin'
require 'models/admin/account'
require 'models/admin/user'
+require 'tempfile'
class FixturesTest < ActiveRecord::TestCase
self.use_instantiated_fixtures = true
@@ -45,6 +46,21 @@ def test_clean_fixtures
end
end
+ def test_broken_yaml_exception
+ badyaml = Tempfile.new ['foo', '.yml']
+ badyaml.write 'a: !ruby.yaml.org,2002:str |\nfoo'
+ badyaml.flush
+
+ dir = File.dirname badyaml.path
+ name = File.basename badyaml.path, '.yml'
+ assert_raises(Fixture::FormatError) do
+ ::Fixtures.create_fixtures(dir, name)
+ end
+ ensure
+ badyaml.close
+ badyaml.unlink
+ end
+
def test_multiple_clean_fixtures
fixtures_array = nil
assert_nothing_raised { fixtures_array = create_fixtures(*FIXTURES) }

0 comments on commit f2aa46b

Please sign in to comment.