Function to persist UUID
This commit is contained in:
parent
273cb132ba
commit
e948ce9663
|
@ -17,6 +17,7 @@ module Hobo
|
|||
class << self
|
||||
def persisted_uuid; @@persisted_uuid; end
|
||||
def root_path; @@root_path; end
|
||||
def dotfile_path; File.join(root_path, Hobo.config[:dotfile_name]); end
|
||||
|
||||
def load!
|
||||
load_root_path!
|
||||
|
@ -46,13 +47,19 @@ module Hobo
|
|||
end
|
||||
|
||||
def load_uuid!
|
||||
File.open(File.join(root_path, Hobo.config[:dotfile_name])) do |f|
|
||||
File.open(dotfile_path) do |f|
|
||||
@@persisted_uuid = f.read
|
||||
end
|
||||
rescue Errno::ENOENT
|
||||
@@persisted_uuid = nil
|
||||
end
|
||||
|
||||
def persist_uuid(uuid)
|
||||
File.open(dotfile_path, 'w+') do |f|
|
||||
f.write(uuid)
|
||||
end
|
||||
end
|
||||
|
||||
def load_root_path!(path=Pathname.new(Dir.pwd))
|
||||
if path.to_s == '/'
|
||||
error_and_exit(<<-msg)
|
||||
|
|
|
@ -79,13 +79,30 @@ class EnvTest < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
context "loading the UUID out from the persisted file" do
|
||||
test "loading of the uuid from the dotfile" do
|
||||
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
|
||||
|
||||
context "loading the UUID out from the persisted file" do
|
||||
setup do
|
||||
Hobo.config! hobo_mock_config
|
||||
end
|
||||
|
||||
test "loading of the uuid from the dotfile" do
|
||||
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)
|
||||
File.expects(:open).with(Hobo::Env.dotfile_path).once.yields(filemock)
|
||||
Hobo::Env.load_uuid!
|
||||
assert_equal Hobo::Env.persisted_uuid, 'foo'
|
||||
end
|
||||
|
@ -95,6 +112,10 @@ class EnvTest < Test::Unit::TestCase
|
|||
Hobo::Env.load_uuid!
|
||||
assert_nil Hobo::Env.persisted_uuid
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
context "loading the root path" do
|
||||
|
|
Loading…
Reference in New Issue