From 092e73d87fdd80fb873c521527cb648cca9e93a1 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 4 May 2018 09:37:20 -0700 Subject: [PATCH] (#9775) Move triggers object init in machine Prior to this commit, the hyper-v provider called an action on a machine that hadn't fully finished initializing. This commit fixes that by moving up the initialization of the triggers object next to the rest of the instance variables of the machine object. --- lib/vagrant/machine.rb | 3 +-- test/unit/vagrant/machine_test.rb | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index fefbbc376..5a3f4a3d0 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -110,6 +110,7 @@ module Vagrant @ui = Vagrant::UI::Prefixed.new(@env.ui, @name) @ui_mutex = Mutex.new @state_mutex = Mutex.new + @triggers = Vagrant::Plugin::V2::Trigger.new(@env, @config.trigger, self) # Read the ID, which is usually in local storage @id = nil @@ -149,8 +150,6 @@ module Vagrant # Output a bunch of information about this machine in # machine-readable format in case someone is listening. @ui.machine("metadata", "provider", provider_name) - - @triggers = Vagrant::Plugin::V2::Trigger.new(@env, @config.trigger, self) end # This calls an action on the provider. The provider may or may not diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index d6411bd16..feeb8176d 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -50,7 +50,8 @@ describe Vagrant::Machine do def new_provider_mock double("provider").tap do |obj| - allow(obj).to receive(:_initialize).and_return(nil) + allow(obj).to receive(:_initialize) + .with(provider_name, anything).and_return(nil) allow(obj).to receive(:machine_id_changed).and_return(nil) allow(obj).to receive(:state).and_return(Vagrant::MachineState.new( :created, "", "")) @@ -76,6 +77,24 @@ describe Vagrant::Machine do expect(subject.id).to be_nil end + context "setting up triggers" do + before do + expect(provider).to receive(:_initialize) do |*args| + machine = args.last + @trigger_instance = machine.instance_variable_get(:@triggers) + true + end + end + + it "should initialize the trigger object" do + subject = new_instance + expect(subject.instance_variable_get(:@triggers)) + .to be_a(Vagrant::Plugin::V2::Trigger) + expect(subject.instance_variable_get(:@triggers)) + .to eq(@trigger_instance) + end + end + describe "as a base" do let(:base) { true}