core: Machine#with_ui

This commit is contained in:
Mitchell Hashimoto 2014-02-08 14:03:31 -08:00
parent 8c85e57db1
commit efc1bf50dd
2 changed files with 31 additions and 0 deletions

View File

@ -1,3 +1,5 @@
require "thread"
require "log4r"
module Vagrant
@ -103,6 +105,7 @@ module Vagrant
@provider_name = provider_name
@provider_options = provider_options
@ui = Vagrant::UI::Prefixed.new(@env.ui, @name)
@ui_mutex = Mutex.new
# Read the ID, which is usually in local storage
@id = nil
@ -337,5 +340,19 @@ module Vagrant
raise Errors::MachineStateInvalid if !result.is_a?(MachineState)
result
end
# Temporarily changes the machine UI. This is useful if you want
# to execute an {#action} with a different UI.
def with_ui(ui)
@ui_mutex.synchronize do
begin
old_ui = @ui
@ui = ui
yield
ensure
@ui = old_ui
end
end
end
end
end

View File

@ -445,4 +445,18 @@ describe Vagrant::Machine do
to raise_error(Vagrant::Errors::MachineStateInvalid)
end
end
describe "#with_ui" do
it "temporarily changes the UI" do
ui = Object.new
changed_ui = nil
subject.with_ui(ui) do
changed_ui = subject.ui
end
expect(changed_ui).to equal(ui)
expect(subject.ui).to_not equal(ui)
end
end
end