diff --git a/lib/vagrant/action/runner.rb b/lib/vagrant/action/runner.rb index d9b791a66..dd36588e6 100644 --- a/lib/vagrant/action/runner.rb +++ b/lib/vagrant/action/runner.rb @@ -40,11 +40,12 @@ module Vagrant # hash above. env = environment[:env] machine = environment[:machine] - machine_name = machine.name if machine + + environment[:triggers] = machine.triggers if machine if env ui = Vagrant::UI::Prefixed.new(env.ui, "vagrant") - environment[:triggers] = Vagrant::Plugin::V2::Trigger. + environment[:triggers] ||= Vagrant::Plugin::V2::Trigger. new(env, env.vagrantfile.config.trigger, machine, ui) end diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index a593b0fff..f782a60f9 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -62,6 +62,11 @@ module Vagrant # @return [Hash] attr_reader :provider_options + # The triggers with machine specific configuration applied + # + # @return [Vagrant::Plugin::V2::Trigger] + attr_reader :triggers + # The UI for outputting in the scope of this machine. # # @return [UI] diff --git a/lib/vagrant/plugin/v2/trigger.rb b/lib/vagrant/plugin/v2/trigger.rb index abee675df..0a6919d2c 100644 --- a/lib/vagrant/plugin/v2/trigger.rb +++ b/lib/vagrant/plugin/v2/trigger.rb @@ -36,7 +36,7 @@ module Vagrant # @param [Symbol] name Name of `type` thing to fire trigger on # @param [Symbol] stage :before or :after # @param [String] guest The guest that invoked firing the triggers - # @param [Symbol] type Type of trigger to fire + # @param [Symbol] type Type of trigger to fire (:action, :hook, :command) def fire(name, stage, guest, type) if community_plugin_detected? @logger.warn("Community plugin `vagrant-triggers detected, so core triggers will not fire") diff --git a/plugins/kernel_v2/config/vm_trigger.rb b/plugins/kernel_v2/config/vm_trigger.rb index 3bd2b0f19..182ea3d8a 100644 --- a/plugins/kernel_v2/config/vm_trigger.rb +++ b/plugins/kernel_v2/config/vm_trigger.rb @@ -214,10 +214,7 @@ module VagrantPlugins end if @type == :command || !@type - commands = [] - Vagrant.plugin("2").manager.commands.each do |key,data| - commands.push(key) - end + commands = Vagrant.plugin("2").manager.commands.keys.map(&:to_s) if !commands.include?(@command) && @command != :all machine.ui.warn(I18n.t("vagrant.config.triggers.bad_command_warning", diff --git a/test/unit/vagrant/action/runner_test.rb b/test/unit/vagrant/action/runner_test.rb index 7ffc1d14e..44db5536f 100644 --- a/test/unit/vagrant/action/runner_test.rb +++ b/test/unit/vagrant/action/runner_test.rb @@ -96,4 +96,49 @@ describe Vagrant::Action::Runner do instance.run(callable) expect(result).to eq("bar") end + + describe "triggers" do + let(:environment) { double("environment", ui: nil) } + let(:machine) { double("machine", triggers: machine_triggers, name: "") } + let(:env_triggers) { double("env_triggers", find: []) } + let(:machine_triggers) { double("machine_triggers", find: []) } + + before do + allow(environment).to receive_message_chain(:vagrantfile, :config, :trigger) + allow(Vagrant::Plugin::V2::Trigger).to receive(:new). + and_return(env_triggers) + end + + context "when only environment is provided" do + let(:instance) { described_class.new(action_name: "test", env: environment) } + + it "should use environment to build new trigger" do + expect(environment).to receive_message_chain(:vagrantfile, :config, :trigger) + instance.run(lambda{|_|}) + end + + it "should pass environment based triggers to callable" do + callable = lambda { |env| expect(env[:triggers]).to eq(env_triggers) } + instance.run(callable) + end + end + + context "when only machine is provided" do + let(:instance) { described_class.new(action_name: "test", machine: machine) } + + it "should pass machine based triggers to callable" do + callable = lambda { |env| expect(env[:triggers]).to eq(machine_triggers) } + instance.run(callable) + end + end + + context "when both environment and machine is provided" do + let(:instance) { described_class.new(action_name: "test", machine: machine, env: environment) } + + it "should pass machine based triggers to callable" do + callable = lambda { |env| expect(env[:triggers]).to eq(machine_triggers) } + instance.run(callable) + end + end + end end