2010-01-22 07:38:23 +00:00
|
|
|
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
|
|
|
|
|
|
class EnvTest < Test::Unit::TestCase
|
2010-01-31 03:58:07 +00:00
|
|
|
def dot_file_expectation
|
|
|
|
File.expects(:exists?).at_least_once.returns(true)
|
|
|
|
File.expects(:open).with(dotfile, 'r').returns(['foo'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def config_file_expectation
|
|
|
|
YAML.expects(:load_file).with(Hobo::Env::CONFIG.keys.first).returns(hobo_mock_config)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dotfile(dir=Dir.pwd)
|
|
|
|
"#{dir}/#{hobo_mock_config[:dotfile_name]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
Hobo::Env.stubs(:error_and_exit)
|
|
|
|
end
|
|
|
|
|
|
|
|
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_uuid!).once
|
|
|
|
Hobo::Env.expects(:load_root_path!).once
|
|
|
|
Hobo::Env.load!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "loading config" do
|
2010-01-22 07:38:23 +00:00
|
|
|
setup do
|
2010-01-26 08:01:17 +00:00
|
|
|
@handler = Hobo::Env
|
2010-01-22 08:56:27 +00:00
|
|
|
@ensure = Hobo::Env::ENSURE
|
2010-01-30 06:39:45 +00:00
|
|
|
Hobo.config! nil
|
2010-01-22 07:38:23 +00:00
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-01-22 08:56:27 +00:00
|
|
|
test "should not create any directories if they exist" do
|
|
|
|
File.expects(:exists?).times(@ensure[:dirs].length).returns(true)
|
|
|
|
Dir.expects(:mkdir).never
|
2010-01-22 07:38:23 +00:00
|
|
|
@handler.ensure_directories
|
|
|
|
end
|
|
|
|
|
2010-01-22 08:56:27 +00:00
|
|
|
test "should not copy any files if they exist" do
|
|
|
|
File.expects(:exists?).times(@ensure[:files].length).returns(true)
|
|
|
|
File.expects(:copy).never
|
2010-01-22 07:38:23 +00:00
|
|
|
@handler.ensure_files
|
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-01-22 09:47:28 +00:00
|
|
|
test "should create the ensured directories if they don't exist" do
|
|
|
|
file_seq = sequence("file_seq")
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-01-22 09:47:28 +00:00
|
|
|
@ensure[:dirs].each do |dir|
|
|
|
|
File.expects(:exists?).returns(false).in_sequence(file_seq)
|
|
|
|
Dir.expects(:mkdir).with(dir).in_sequence(file_seq)
|
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-01-22 09:47:28 +00:00
|
|
|
@handler.ensure_directories
|
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-01-22 09:47:28 +00:00
|
|
|
test "should create the ensured files if they don't exist" do
|
|
|
|
file_seq = sequence("file_seq")
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-01-22 09:47:28 +00:00
|
|
|
@ensure[:files].each do |target, default|
|
|
|
|
File.expects(:exists?).with(target).returns(false).in_sequence(file_seq)
|
|
|
|
File.expects(:copy).with(File.join(PROJECT_ROOT, default), target).in_sequence(file_seq)
|
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-01-22 09:47:28 +00:00
|
|
|
@handler.ensure_files
|
|
|
|
end
|
2010-01-22 07:38:23 +00:00
|
|
|
|
2010-01-30 06:39:45 +00:00
|
|
|
test "should load of the default" do
|
2010-01-30 07:22:03 +00:00
|
|
|
config_file_expectation
|
|
|
|
@handler.load_config!
|
2010-01-31 03:58:07 +00:00
|
|
|
assert_equal Hobo.config[:ssh], hobo_mock_config[:ssh]
|
2010-01-22 07:38:23 +00:00
|
|
|
end
|
2010-01-30 06:39:45 +00:00
|
|
|
|
|
|
|
test "Hobo.config should be nil unless loaded" do
|
|
|
|
assert_equal Hobo.config, nil
|
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
end
|
2010-01-30 07:22:03 +00:00
|
|
|
|
2010-01-31 06:18:18 +00:00
|
|
|
context "persisting the UUID into a file" do
|
|
|
|
setup do
|
|
|
|
Hobo.config! hobo_mock_config
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should save it to the dotfile path" do
|
|
|
|
uuid = "foo"
|
|
|
|
|
|
|
|
filemock = mock("filemock")
|
|
|
|
filemock.expects(:write).with(uuid)
|
|
|
|
File.expects(:open).with(Hobo::Env.dotfile_path, 'w+').once.yields(filemock)
|
|
|
|
Hobo::Env.persist_uuid(uuid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
context "loading the UUID out from the persisted file" do
|
2010-01-31 06:18:18 +00:00
|
|
|
setup do
|
2010-01-30 07:22:03 +00:00
|
|
|
Hobo.config! hobo_mock_config
|
2010-01-31 06:18:18 +00:00
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
|
2010-01-31 06:18:18 +00:00
|
|
|
test "loading of the uuid from the dotfile" do
|
2010-01-31 03:58:07 +00:00
|
|
|
filemock = mock("filemock")
|
|
|
|
filemock.expects(:read).returns("foo")
|
2010-01-31 06:18:18 +00:00
|
|
|
File.expects(:open).with(Hobo::Env.dotfile_path).once.yields(filemock)
|
2010-01-30 07:22:03 +00:00
|
|
|
Hobo::Env.load_uuid!
|
|
|
|
assert_equal Hobo::Env.persisted_uuid, 'foo'
|
|
|
|
end
|
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
test "uuid should be nil if dotfile didn't exist" do
|
|
|
|
File.expects(:open).raises(Errno::ENOENT)
|
2010-01-30 07:48:05 +00:00
|
|
|
Hobo::Env.load_uuid!
|
2010-01-31 03:58:07 +00:00
|
|
|
assert_nil Hobo::Env.persisted_uuid
|
2010-01-30 07:48:05 +00:00
|
|
|
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
|
|
|
|
assert_equal File.join(Hobo::Env.root_path, hobo_mock_config[:dotfile_name]), Hobo::Env.dotfile_path
|
|
|
|
end
|
2010-01-31 03:58:07 +00:00
|
|
|
end
|
2010-01-30 09:03:18 +00:00
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
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")
|
|
|
|
]
|
2010-01-30 08:46:56 +00:00
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
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)
|
2010-01-30 07:22:03 +00:00
|
|
|
end
|
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
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)
|
2010-01-30 07:22:03 +00:00
|
|
|
end
|
2010-01-30 08:46:56 +00:00
|
|
|
|
2010-01-31 03:58:07 +00:00
|
|
|
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
|
2010-01-30 08:46:56 +00:00
|
|
|
end
|
2010-01-22 07:38:23 +00:00
|
|
|
end
|
|
|
|
end
|