Skip to content
This repository
Browse code

fixture file will validate fixture format

  • Loading branch information...
commit 081b36c6ce799f2e4c755bdaf2fa978c9e494567 1 parent 5278af3
Aaron Patterson tenderlove authored
9 activerecord/lib/active_record/fixtures/file.rb
@@ -34,12 +34,19 @@ def rows
34 34 return @rows if @rows
35 35
36 36 data = YAML.load(render(IO.read(@file)))
37   - @rows = data ? data.to_a : []
  37 + @rows = data ? validate(data).to_a : []
38 38 end
39 39
40 40 def render(content)
41 41 ERB.new(content).result
42 42 end
  43 +
  44 + # Validate our unmarshalled data.
  45 + def validate(data)
  46 + raise Fixture::FormatError, 'fixture is not a hash' unless Hash === data
  47 + raise Fixture::FormatError unless data.all? { |name, row| Hash === row }
  48 + data
  49 + end
43 50 end
44 51 end
45 52 end
18 activerecord/test/cases/fixtures/file_test.rb
@@ -48,6 +48,24 @@ def test_empty_file
48 48 end
49 49 end
50 50
  51 + # A valid YAML file is not necessarily a value Fixture file. Make sure
  52 + # an exception is raised if the format is not valid Fixture format.
  53 + def test_wrong_fixture_format_string
  54 + tmp_yaml ['empty', 'yml'], 'qwerty' do |t|
  55 + assert_raises(ActiveRecord::Fixture::FormatError) do
  56 + File.open(t.path) { |fh| fh.to_a }
  57 + end
  58 + end
  59 + end
  60 +
  61 + def test_wrong_fixture_format_nested
  62 + tmp_yaml ['empty', 'yml'], 'one: two' do |t|
  63 + assert_raises(ActiveRecord::Fixture::FormatError) do
  64 + File.open(t.path) { |fh| fh.to_a }
  65 + end
  66 + end
  67 + end
  68 +
51 69 private
52 70 def tmp_yaml(name, contents)
53 71 t = Tempfile.new name

0 comments on commit 081b36c

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