Skip to content

Commit

Permalink
* Psych errors with poor yaml formatting are proxied. Fixes rails#2645,
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Sep 1, 2011
1 parent 3afa90d commit e46c4f1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG
@@ -1,3 +1,8 @@
*Rails 3.1.1 (unreleased)*

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

*Rails 3.1.0 (August 30, 2011)*

* Add a proxy_association method to association proxies, which can be called by association
Expand Down
8 changes: 7 additions & 1 deletion activerecord/lib/active_record/fixtures.rb
Expand Up @@ -715,9 +715,15 @@ 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
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}"
end

Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/fixtures_test.rb
Expand Up @@ -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
Expand All @@ -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(ActiveRecord::Fixture::FormatError) do
ActiveRecord::Fixtures.create_fixtures(dir, name)
end
ensure
badyaml.close
badyaml.unlink
end

def test_create_fixtures
ActiveRecord::Fixtures.create_fixtures(FIXTURES_ROOT, "parrots")
assert Parrot.find_by_name('Curious George'), 'George is in the database'
Expand Down

0 comments on commit e46c4f1

Please sign in to comment.