Skip to content

Commit c35a9e2

Browse files
authored
Merge pull request rails#42490 from Shopify/fixture-row-strictness
Validate keys in the `_fixture` field
2 parents e581678 + fccbcd6 commit c35a9e2

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Fixture configurations (`_fixture`) are now strictly validated.
2+
3+
If an error will be raised if that entry contains unknown keys while previously it
4+
would silently have no effects.
5+
6+
*Jean Boussier*
7+
18
* Add `ActiveRecord::Base.update!` that works like `ActiveRecord::Base.update` but raises exceptions.
29

310
This allows for the same behavior as the instance method `#update!` at a class level.

activerecord/lib/active_record/fixture_set/file.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def config_row
4141
@config_row ||= begin
4242
row = raw_rows.find { |fixture_name, _| fixture_name == "_fixture" }
4343
if row
44-
row.last
44+
validate_config_row(row.last)
4545
else
4646
{ 'model_class': nil, 'ignore': nil }
4747
end
@@ -58,6 +58,20 @@ def raw_rows
5858
end
5959
end
6060

61+
def validate_config_row(data)
62+
unless Hash === data
63+
raise Fixture::FormatError, "Invalid `_fixture` section: `_fixture` must be a hash: #{@file}"
64+
end
65+
66+
begin
67+
data.assert_valid_keys("model_class", "ignore")
68+
rescue ArgumentError => error
69+
raise Fixture::FormatError, "Invalid `_fixture` section: #{error.message}: #{@file}"
70+
end
71+
72+
data
73+
end
74+
6175
# Validate our unmarshalled data.
6276
def validate(data)
6377
unless Hash === data || YAML::Omap === data

activerecord/test/cases/fixture_set/file_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ def test_wrong_fixture_format_nested
7070
end
7171
end
7272

73+
def test_wrong_config_row
74+
tmp_yaml ["empty", "yml"], { "_fixture" => { "class_name" => "Foo" } }.to_yaml do |t|
75+
error = assert_raises(ActiveRecord::Fixture::FormatError) do
76+
File.open(t.path) { |fh| fh.model_class }
77+
end
78+
assert_includes error.message, "Invalid `_fixture` section"
79+
end
80+
end
81+
7382
def test_render_context_helper
7483
ActiveRecord::FixtureSet.context_class.class_eval do
7584
def fixture_helper

0 commit comments

Comments
 (0)