Use machine specific triggers instance when machine is available in runner

This commit is contained in:
Chris Roberts 2020-03-26 17:20:55 -07:00
parent bcbbc825e0
commit 217f2530db
5 changed files with 55 additions and 7 deletions

View File

@ -40,11 +40,12 @@ module Vagrant
# hash above. # hash above.
env = environment[:env] env = environment[:env]
machine = environment[:machine] machine = environment[:machine]
machine_name = machine.name if machine
environment[:triggers] = machine.triggers if machine
if env if env
ui = Vagrant::UI::Prefixed.new(env.ui, "vagrant") 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) new(env, env.vagrantfile.config.trigger, machine, ui)
end end

View File

@ -62,6 +62,11 @@ module Vagrant
# @return [Hash] # @return [Hash]
attr_reader :provider_options 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. # The UI for outputting in the scope of this machine.
# #
# @return [UI] # @return [UI]

View File

@ -36,7 +36,7 @@ module Vagrant
# @param [Symbol] name Name of `type` thing to fire trigger on # @param [Symbol] name Name of `type` thing to fire trigger on
# @param [Symbol] stage :before or :after # @param [Symbol] stage :before or :after
# @param [String] guest The guest that invoked firing the triggers # @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) def fire(name, stage, guest, type)
if community_plugin_detected? if community_plugin_detected?
@logger.warn("Community plugin `vagrant-triggers detected, so core triggers will not fire") @logger.warn("Community plugin `vagrant-triggers detected, so core triggers will not fire")

View File

@ -214,10 +214,7 @@ module VagrantPlugins
end end
if @type == :command || !@type if @type == :command || !@type
commands = [] commands = Vagrant.plugin("2").manager.commands.keys.map(&:to_s)
Vagrant.plugin("2").manager.commands.each do |key,data|
commands.push(key)
end
if !commands.include?(@command) && @command != :all if !commands.include?(@command) && @command != :all
machine.ui.warn(I18n.t("vagrant.config.triggers.bad_command_warning", machine.ui.warn(I18n.t("vagrant.config.triggers.bad_command_warning",

View File

@ -96,4 +96,49 @@ describe Vagrant::Action::Runner do
instance.run(callable) instance.run(callable)
expect(result).to eq("bar") expect(result).to eq("bar")
end 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 end