diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index bc5df8be8..e66629f0f 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -158,12 +158,23 @@ module Vagrant :provider => @provider.to_s end + action_raw(name, callable, extra_env) + end + + # This calls a raw callable in the proper context of the machine using + # the middleware stack. + # + # @param [Symbol] name Name of the action + # @param [Proc] callable + # @param [Hash] extra_env Extra env for the action env. + # @return [Hash] The resulting env + def action_raw(name, callable, extra_env=nil) # Run the action with the action runner on the environment env = { - :action_name => "machine_action_#{name}".to_sym, - :machine => self, - :machine_action => name, - :ui => @ui + action_name: "machine_action_#{name}".to_sym, + machine: self, + machine_action: name, + ui: @ui, }.merge(extra_env || {}) @env.action_runner.run(callable, env) end diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index ded42ffc7..aa7431b54 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -261,6 +261,39 @@ describe Vagrant::Machine do end end + describe "#action_raw" do + let(:callable) {lambda { |e| + e[:called] = true + @env = e + }} + + before do + @env = {} + end + + it "should run the callable with the proper env" do + subject.action_raw(:foo, callable) + + expect(@env[:called]).to be_true + expect(@env[:action_name]).to eq(:machine_action_foo) + expect(@env[:machine]).to equal(subject) + expect(@env[:machine_action]).to eq(:foo) + expect(@env[:ui]).to equal(subject.ui) + end + + it "should return the environment as a result" do + result = subject.action_raw(:foo, callable) + expect(result).to equal(@env) + end + + it "should merge in any extra env" do + subject.action_raw(:bar, callable, foo: :bar) + + expect(@env[:called]).to be_true + expect(@env[:foo]).to eq(:bar) + end + end + describe "#communicate" do it "should return the SSH communicator by default" do expect(subject.communicate).