diff --git a/lib/hobo/env.rb b/lib/hobo/env.rb index d68d13eca..40b0fe7e0 100644 --- a/lib/hobo/env.rb +++ b/lib/hobo/env.rb @@ -48,7 +48,7 @@ module Hobo def load_vm! File.open(dotfile_path) do |f| - @@persisted_vm = VirtualBox::VM.find(f.read) + @@persisted_vm = Hobo::VM.find(f.read) end rescue Errno::ENOENT @@persisted_vm = nil diff --git a/lib/hobo/vm.rb b/lib/hobo/vm.rb index 1b4266282..fc8378cec 100644 --- a/lib/hobo/vm.rb +++ b/lib/hobo/vm.rb @@ -1,5 +1,7 @@ module Hobo class VM + attr_reader :vm + class < true) end + + # Finds a virtual machine by a given UUID and either returns + # a Hobo::VM object or returns nil. + def find(uuid) + vm = VirtualBox::VM.find(uuid) + return nil if vm.nil? + new(vm) + end end def initialize(vm=nil) diff --git a/test/hobo/env_test.rb b/test/hobo/env_test.rb index 7cee5bf30..301408267 100644 --- a/test/hobo/env_test.rb +++ b/test/hobo/env_test.rb @@ -17,7 +17,7 @@ class EnvTest < Test::Unit::TestCase def mock_persisted_vm(returnvalue="foovm") filemock = mock("filemock") filemock.expects(:read).returns("foo") - VirtualBox::VM.expects(:find).with("foo").returns(returnvalue) + Hobo::VM.expects(:find).with("foo").returns(returnvalue) File.expects(:open).with(Hobo::Env.dotfile_path).once.yields(filemock) Hobo::Env.load_vm! end diff --git a/test/hobo/vm_test.rb b/test/hobo/vm_test.rb index 3b200f864..792639ed9 100644 --- a/test/hobo/vm_test.rb +++ b/test/hobo/vm_test.rb @@ -33,6 +33,20 @@ class VMTest < Test::Unit::TestCase end end + context "finding a VM" do + should "return nil if the VM is not found" do + VirtualBox::VM.expects(:find).returns(nil) + assert_nil Hobo::VM.find("foo") + end + + should "return a Hobo::VM object for that VM otherwise" do + VirtualBox::VM.expects(:find).with("foo").returns("bar") + result = Hobo::VM.find("foo") + assert result.is_a?(Hobo::VM) + assert_equal "bar", result.vm + end + end + context "hobo VM instance" do setup do @vm = Hobo::VM.new(@mock_vm)