From a177bcf4b7a8d2a2234efbc7ab8b63a05d68a246 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 7 May 2018 11:33:55 -0700 Subject: [PATCH] Ensure internal trigger fire does not get called if plugin installed This commit wraps up the internal machine action level trigger calls if the community vagrant-trigger plugin is installed. --- lib/vagrant/machine.rb | 10 ++++++++-- test/unit/vagrant/machine_test.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 5a3f4a3d0..88ef368bd 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -160,7 +160,11 @@ module Vagrant # as extra data set on the environment hash for the middleware # runner. def action(name, opts=nil) - @triggers.fire_triggers(name, :before, @name.to_s) + plugins = Vagrant::Plugin::Manager.instance.installed_plugins + if !plugins.keys.include?("vagrant-triggers") + @triggers.fire_triggers(name, :before, @name.to_s) + end + @logger.info("Calling action: #{name} on provider #{@provider}") opts ||= {} @@ -206,7 +210,9 @@ module Vagrant action_result end - @triggers.fire_triggers(name, :after, @name.to_s) + if !plugins.keys.include?("vagrant-triggers") + @triggers.fire_triggers(name, :after, @name.to_s) + end # preserve returning environment after machine action runs return return_env rescue Errors::EnvironmentLockedError diff --git a/test/unit/vagrant/machine_test.rb b/test/unit/vagrant/machine_test.rb index feeb8176d..0e22b8a06 100644 --- a/test/unit/vagrant/machine_test.rb +++ b/test/unit/vagrant/machine_test.rb @@ -265,6 +265,10 @@ describe Vagrant::Machine do end describe "#action" do + before do + allow(Vagrant::Plugin::Manager.instance).to receive(:installed_plugins).and_return({}) + end + it "should be able to run an action that exists" do action_name = :up called = false @@ -387,6 +391,32 @@ describe Vagrant::Machine do expect(subject.ui).to_not have_received(:warn) end end + + context "with the vagrant-triggers community plugin" do + it "should not call the internal trigger functions if installed" do + action_name = :up + callable = lambda { |_env| } + + allow(provider).to receive(:action).with(action_name).and_return(callable) + allow(Vagrant::Plugin::Manager.instance).to receive(:installed_plugins) + .and_return({"vagrant-triggers"=>"stuff"}) + + expect(instance.instance_variable_get(:@triggers)).not_to receive(:fire_triggers) + instance.action(action_name) + end + + it "should call the internal trigger functions if not installed" do + action_name = :up + callable = lambda { |_env| } + + allow(provider).to receive(:action).with(action_name).and_return(callable) + allow(Vagrant::Plugin::Manager.instance).to receive(:installed_plugins) + .and_return({}) + + expect(instance.instance_variable_get(:@triggers)).to receive(:fire_triggers).twice + instance.action(action_name) + end + end end describe "#action_raw" do