Load the path to hobo file and use that path to load UUID

This commit is contained in:
Mitchell Hashimoto 2010-01-30 19:58:07 -08:00
parent 911c3a0f6c
commit a629889ff6
2 changed files with 102 additions and 53 deletions

View File

@ -2,16 +2,24 @@ require 'yaml'
module Hobo module Hobo
class Env class Env
HOBOFILE_NAME = "hobofile"
HOME = File.expand_path('~/.hobo') HOME = File.expand_path('~/.hobo')
CONFIG = { File.join(HOME, 'config.yml') => '/config/default.yml' } CONFIG = { File.join(HOME, 'config.yml') => '/config/default.yml' }
ENSURE = { ENSURE = {
:files => CONFIG.merge({}), #additional files go mhia! :files => CONFIG.merge({}), #additional files go mhia!
:dirs => [HOME] #additional dirs go mhia! :dirs => [HOME] #additional dirs go mhia!
} }
# Initialize class variables used
@@persisted_uuid = nil
@@root_path = nil
class << self class << self
def persisted_uuid; @@persisted_uuid; end
def root_path; @@root_path; end
def load! def load!
load_root_path!
load_config! load_config!
load_uuid! load_uuid!
end end
@ -27,34 +35,42 @@ module Hobo
File.copy(File.join(PROJECT_ROOT, default), target) unless File.exists?(target) File.copy(File.join(PROJECT_ROOT, default), target) unless File.exists?(target)
end end
end end
def load_config! def load_config!
ensure_directories ensure_directories
ensure_files ensure_files
HOBO_LOGGER.info "Loading config from #{CONFIG.keys.first}" HOBO_LOGGER.info "Loading config from #{CONFIG.keys.first}"
parsed = YAML.load_file(CONFIG.keys.first) parsed = YAML.load_file(CONFIG.keys.first)
Hobo.config!(parsed) Hobo.config!(parsed)
end end
def load_uuid! def load_uuid!
@@persisted_uuid = load_dotfile File.open(File.join(root_path, Hobo.config[:dotfile_name])) do |f|
end @@persisted_uuid = f.read
def load_dotfile(path=Pathname.new(Dir.pwd))
return nil if path.to_s == '/'
file = "#{path}/#{Hobo.config[:dotfile_name]}"
if File.exists?(file)
# TODO check multiple lines after the first for information
return File.open(file, 'r').first
end end
rescue Errno::ENOENT
load_dotfile(path.parent) @@persisted_uuid = nil
end end
def persisted_uuid def load_root_path!(path=Pathname.new(Dir.pwd))
@@persisted_uuid if path.to_s == '/'
error_and_exit("UH OH")
return
end
file = "#{path}/#{HOBOFILE_NAME}"
if File.exist?(file)
@@root_path = path.to_s
return
end
load_root_path!(path.parent)
end
def error_and_exit(error)
puts error
exit
end end
end end
end end

View File

@ -1,13 +1,39 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper') require File.join(File.dirname(__FILE__), '..', 'test_helper')
class EnvTest < Test::Unit::TestCase class EnvTest < Test::Unit::TestCase
context "Hobo environment handler" do 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
setup do setup do
@handler = Hobo::Env @handler = Hobo::Env
@ensure = Hobo::Env::ENSURE @ensure = Hobo::Env::ENSURE
Hobo.config! nil Hobo.config! nil
end end
test "should not create any directories if they exist" do test "should not create any directories if they exist" do
File.expects(:exists?).times(@ensure[:dirs].length).returns(true) File.expects(:exists?).times(@ensure[:dirs].length).returns(true)
Dir.expects(:mkdir).never Dir.expects(:mkdir).never
@ -19,80 +45,87 @@ class EnvTest < Test::Unit::TestCase
File.expects(:copy).never File.expects(:copy).never
@handler.ensure_files @handler.ensure_files
end end
test "should create the ensured directories if they don't exist" do test "should create the ensured directories if they don't exist" do
file_seq = sequence("file_seq") file_seq = sequence("file_seq")
@ensure[:dirs].each do |dir| @ensure[:dirs].each do |dir|
File.expects(:exists?).returns(false).in_sequence(file_seq) File.expects(:exists?).returns(false).in_sequence(file_seq)
Dir.expects(:mkdir).with(dir).in_sequence(file_seq) Dir.expects(:mkdir).with(dir).in_sequence(file_seq)
end end
@handler.ensure_directories @handler.ensure_directories
end end
test "should create the ensured files if they don't exist" do test "should create the ensured files if they don't exist" do
file_seq = sequence("file_seq") file_seq = sequence("file_seq")
@ensure[:files].each do |target, default| @ensure[:files].each do |target, default|
File.expects(:exists?).with(target).returns(false).in_sequence(file_seq) 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) File.expects(:copy).with(File.join(PROJECT_ROOT, default), target).in_sequence(file_seq)
end end
@handler.ensure_files @handler.ensure_files
end end
test "should load of the default" do test "should load of the default" do
config_file_expectation config_file_expectation
@handler.load_config! @handler.load_config!
assert_equal Hobo.config[:ssh], hobo_mock_config[:ssh] assert_equal Hobo.config[:ssh], hobo_mock_config[:ssh]
end end
test "Hobo.config should be nil unless loaded" do test "Hobo.config should be nil unless loaded" do
assert_equal Hobo.config, nil assert_equal Hobo.config, nil
end end
end
context "loading the UUID out from the persisted file" do
test "loading of the uuid from the dotfile" do test "loading of the uuid from the dotfile" do
dot_file_expectation
Hobo.config! hobo_mock_config Hobo.config! hobo_mock_config
filemock = mock("filemock")
filemock.expects(:read).returns("foo")
File.expects(:open).with(File.join(Hobo::Env.root_path, hobo_mock_config[:dotfile_name])).once.yields(filemock)
Hobo::Env.load_uuid! Hobo::Env.load_uuid!
assert_equal Hobo::Env.persisted_uuid, 'foo' assert_equal Hobo::Env.persisted_uuid, 'foo'
end end
test "load! should load the config and set the persisted_uid" do test "uuid should be nil if dotfile didn't exist" do
config_file_expectation File.expects(:open).raises(Errno::ENOENT)
dot_file_expectation
Hobo::Env.load!
end
test "when no dotfile exists uuid should be nil" do
Hobo.config! hobo_mock_config.merge(:dotfile_name => 'unpossiblyunpossiblfilename')
Hobo::Env.load_uuid! Hobo::Env.load_uuid!
assert_equal Hobo::Env.persisted_uuid, nil assert_nil Hobo::Env.persisted_uuid
end end
end
test "should walk the parent directories looking for the dotfile" do
Hobo.config! hobo_mock_config
#Expects exists with the current directory and .hobo context "loading the root path" do
File.expects(:exists?).with(dotfile).returns(false) test "should walk the parent directories looking for hobofile" do
File.expects(:exists?).with(dotfile(Dir.pwd.split('/')[0..-2].join('/'))).returns(true) paths = [
File.expects(:open).returns(['foo']) Pathname.new("/foo/bar/baz"),
Hobo::Env.load_uuid! Pathname.new("/foo/bar"),
assert_equal Hobo::Env.persisted_uuid, 'foo' 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 end
def dot_file_expectation test "should print out an error and exit if not found" do
File.expects(:exists?).at_least_once.returns(true) path = Pathname.new("/")
File.expects(:open).with(dotfile, 'r').returns(['foo'])
Hobo::Env.expects(:error_and_exit).once
Hobo::Env.load_root_path!(path)
end end
def config_file_expectation test "should set the path for the Hobofile" do
YAML.expects(:load_file).with(Hobo::Env::CONFIG.keys.first).returns(hobo_mock_config) path = "/foo"
end File.expects(:exist?).with("#{path}/#{Hobo::Env::HOBOFILE_NAME}").returns(true)
Hobo::Env.load_root_path!(Pathname.new(path))
def dotfile(dir=Dir.pwd) assert_equal path, Hobo::Env.root_path
"#{dir}/#{hobo_mock_config[:dotfile_name]}"
end end
end end
end end