Skip to content

Commit

Permalink
core: config raises NoMethodError on bad calls once finalized
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Feb 6, 2014
1 parent 3348baa commit f72db0c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/vagrant/config/v2/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def finalize!

@keys.each do |_key, instance|
instance.finalize!
instance._finalize!
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/vagrant/plugin/v2/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def merge(other)
# Capture all bad configuration calls and save them for an error
# message later during validation.
def method_missing(name, *args, &block)
return super if @__finalized

name = name.to_s
name = name[0...-1] if name.end_with?("=")

Expand Down Expand Up @@ -130,6 +132,11 @@ def _detected_errors
return [I18n.t("vagrant.config.common.bad_field",
:fields => @__invalid_methods.to_a.sort.join(", "))]
end

# An internal finalize call that no subclass should override.
def _finalize!
@__finalized = true
end
end
end
end
Expand Down
15 changes: 13 additions & 2 deletions test/unit/vagrant/config/v2/root_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
}
end

describe "finalization" do
it "should finalize un-used keys" do
describe "#finalize!" do
it "should call #finalize!" do
foo_class = Class.new do
attr_accessor :foo

Expand All @@ -57,6 +57,17 @@ def finalize!

instance.foo.foo.should == "SET"
end

it "should call #_finalize!" do
klass = Class.new

klass.any_instance.should_receive(:finalize!)
klass.any_instance.should_receive(:_finalize!)

map = { foo: klass }
instance = described_class.new(map)
instance.finalize!
end
end

describe "validation" do
Expand Down
18 changes: 17 additions & 1 deletion test/unit/vagrant/plugin/v2/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

let(:unset_value) { described_class.const_get("UNSET_VALUE") }

describe "merging" do
subject { foo_class.new }

describe "#merge" do
it "should merge by default by simply copying each instance variable" do
one = foo_class.new
one.one = 2
Expand Down Expand Up @@ -57,4 +59,18 @@
result.instance_variable_get(:@__bar).should be_nil
end
end

describe "#method_missing" do
it "returns a DummyConfig object" do
expect(subject.i_should_not_exist).
to be_kind_of(Vagrant::Config::V2::DummyConfig)
end

it "raises an error if finalized (internally)" do
subject._finalize!

expect { subject.i_should_not_exist }.
to raise_error(NoMethodError)
end
end
end

0 comments on commit f72db0c

Please sign in to comment.