vagrant/test/hobo/env_test.rb

151 lines
4.3 KiB
Ruby
Raw Normal View History

2010-01-22 07:38:23 +00:00
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class EnvTest < Test::Unit::TestCase
def dot_file_expectation
File.expects(:exists?).at_least_once.returns(true)
File.expects(:open).with(dotfile, 'r').returns(['foo'])
end
def dotfile(dir=Dir.pwd)
"#{dir}/#{hobo_mock_config[:dotfile_name]}"
end
2010-01-31 09:27:18 +00:00
def mock_persisted_vm(returnvalue="foovm")
filemock = mock("filemock")
filemock.expects(:read).returns("foo")
Hobo::VM.expects(:find).with("foo").returns(returnvalue)
2010-01-31 09:27:18 +00:00
File.expects(:open).with(Hobo::Env.dotfile_path).once.yields(filemock)
Hobo::Env.load_vm!
end
setup do
Hobo::Env.stubs(:error_and_exit)
2010-02-02 06:14:40 +00:00
hobo_mock_config
end
2010-01-31 09:27:18 +00:00
context "requiring a VM" do
should "error and exit if no persisted VM was found" do
assert_nil Hobo::Env.persisted_vm
Hobo::Env.expects(:error_and_exit).once
Hobo::Env.require_persisted_vm
end
should "return and continue if persisted VM is found" do
mock_persisted_vm
Hobo::Env.expects(:error_and_exit).never
Hobo::Env.require_persisted_vm
end
end
context "loading config" do
2010-01-22 07:38:23 +00:00
setup do
2010-02-02 06:14:40 +00:00
@root_path = "/foo"
Hobo::Env.stubs(:root_path).returns(@root_path)
File.stubs(:exist?).returns(false)
Hobo::Config.stubs(:execute!)
2010-01-22 07:38:23 +00:00
end
2010-02-02 06:14:40 +00:00
should "load from the project root" do
File.expects(:exist?).with(File.join(PROJECT_ROOT, "config", "default.rb")).once
Hobo::Env.load_config!
2010-01-22 07:38:23 +00:00
end
2010-02-02 06:14:40 +00:00
should "load from the root path" do
File.expects(:exist?).with(File.join(@root_path, Hobo::Env::HOBOFILE_NAME)).once
Hobo::Env.load_config!
2010-01-22 07:38:23 +00:00
end
2010-02-02 06:14:40 +00:00
should "load the files only if exist? returns true" do
File.expects(:exist?).once.returns(true)
Hobo::Env.expects(:load).once
Hobo::Env.load_config!
2010-01-22 09:47:28 +00:00
end
2010-02-02 06:14:40 +00:00
should "not load the files if exist? returns false" do
Hobo::Env.expects(:load).never
Hobo::Env.load_config!
2010-01-22 09:47:28 +00:00
end
2010-01-22 07:38:23 +00:00
2010-02-02 06:14:40 +00:00
should "execute after loading" do
File.expects(:exist?).once.returns(true)
Hobo::Env.expects(:load).once
Hobo::Config.expects(:execute!).once
Hobo::Env.load_config!
2010-01-22 07:38:23 +00:00
end
2010-02-02 06:14:40 +00:00
end
2010-01-30 06:39:45 +00:00
2010-02-02 06:14:40 +00:00
context "initial load" do
test "load! should load the config and set the persisted_uid" do
Hobo::Env.expects(:load_config!).once
Hobo::Env.expects(:load_vm!).once
Hobo::Env.expects(:load_root_path!).once
Hobo::Env.load!
2010-01-30 06:39:45 +00:00
end
end
context "persisting the VM into a file" do
2010-01-31 06:18:18 +00:00
setup do
2010-02-02 06:14:40 +00:00
hobo_mock_config
2010-01-31 06:18:18 +00:00
end
test "should save it to the dotfile path" do
vm = mock("vm")
vm.stubs(:uuid).returns("foo")
2010-01-31 06:18:18 +00:00
filemock = mock("filemock")
filemock.expects(:write).with(vm.uuid)
2010-01-31 06:18:18 +00:00
File.expects(:open).with(Hobo::Env.dotfile_path, 'w+').once.yields(filemock)
Hobo::Env.persist_vm(vm)
2010-01-31 06:18:18 +00:00
end
end
context "loading the UUID out from the persisted file" do
2010-01-31 06:18:18 +00:00
test "loading of the uuid from the dotfile" do
2010-01-31 09:27:18 +00:00
mock_persisted_vm
assert_equal 'foovm', Hobo::Env.persisted_vm
end
test "uuid should be nil if dotfile didn't exist" do
File.expects(:open).raises(Errno::ENOENT)
Hobo::Env.load_vm!
assert_nil Hobo::Env.persisted_vm
end
2010-01-31 06:18:18 +00:00
test "should build up the dotfile out of the root path and the dotfile name" do
2010-02-02 06:14:40 +00:00
assert_equal File.join(Hobo::Env.root_path, Hobo.config.dotfile_name), Hobo::Env.dotfile_path
2010-01-31 06:18:18 +00:00
end
end
context "loading the root path" do
test "should walk the parent directories looking for hobofile" do
paths = [
Pathname.new("/foo/bar/baz"),
Pathname.new("/foo/bar"),
Pathname.new("/foo")
]
search_seq = sequence("search_seq")
paths.each do |path|
File.expects(:exist?).with("#{path}/#{Hobo::Env::HOBOFILE_NAME}").returns(false).in_sequence(search_seq)
end
assert_nil Hobo::Env.load_root_path!(paths.first)
end
test "should print out an error and exit if not found" do
path = Pathname.new("/")
Hobo::Env.expects(:error_and_exit).once
Hobo::Env.load_root_path!(path)
end
test "should set the path for the Hobofile" do
path = "/foo"
File.expects(:exist?).with("#{path}/#{Hobo::Env::HOBOFILE_NAME}").returns(true)
Hobo::Env.load_root_path!(Pathname.new(path))
assert_equal path, Hobo::Env.root_path
end
2010-01-22 07:38:23 +00:00
end
end