-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fabios
committed
Dec 9, 2014
1 parent
fbab777
commit 3cd4e7a
Showing
5 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Hiera | ||
class Filenocache | ||
|
||
def read(path, expected_type = Object, default=nil, &block) | ||
read_file(path, expected_type, &block) | ||
rescue TypeError => detail | ||
Hiera.debug("#{detail.message}, setting defaults") | ||
default | ||
rescue => detail | ||
error = "Reading data from #{path} failed: #{detail.class}: #{detail}" | ||
if default.nil? | ||
raise detail | ||
else | ||
Hiera.debug(error) | ||
default | ||
end | ||
end | ||
|
||
def read_file(path, expected_type = Object) | ||
data = File.read(path) | ||
result = block_given? ? yield(data) : data | ||
|
||
if !result.is_a?(expected_type) | ||
raise TypeError, "Data retrieved from #{path} is #{data.class} not #{expected_type}" | ||
end | ||
|
||
result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
require 'hiera/filenocache' | ||
require 'spec_helper' | ||
require 'tmpdir' | ||
|
||
class Hiera | ||
describe Filenocache do | ||
before do | ||
@cache = Filenocache.new | ||
end | ||
|
||
def write_file(file, contents) | ||
File.open(file, 'w') do |f| | ||
f.write(contents) | ||
end | ||
end | ||
|
||
describe "#read" do | ||
it "reads data from a file" do | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
|
||
@cache.read(file).should == "my data" | ||
end | ||
end | ||
|
||
it "rereads data" do | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
@cache.read(file).should == "my data" | ||
|
||
write_file(file, "changed data") | ||
@cache.read(file).should == "changed data" | ||
end | ||
end | ||
|
||
it "uses the provided default when the type does not match the expected type" do | ||
Hiera.expects(:debug).with(regexp_matches(/String.*not.*Hash, setting defaults/)) | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
data = @cache.read(file, Hash, { :testing => "hash" }) do |data| | ||
"a string" | ||
end | ||
|
||
data.should == { :testing => "hash" } | ||
end | ||
end | ||
|
||
it "traps any errors from the block and uses the default value" do | ||
Hiera.expects(:debug).with(regexp_matches(/Reading data.*failed:.*testing error/)) | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
data = @cache.read(file, Hash, { :testing => "hash" }) do |data| | ||
raise ArgumentError, "testing error" | ||
end | ||
|
||
data.should == { :testing => "hash" } | ||
end | ||
end | ||
|
||
it "raises an error when there is no default given and there is a problem" do | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
|
||
expect do | ||
@cache.read(file, Hash) do |data| | ||
raise ArgumentError, "testing error" | ||
end | ||
end.to raise_error(ArgumentError, "testing error") | ||
end | ||
end | ||
end | ||
|
||
describe "#read_file" do | ||
it "reads data from a file" do | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
|
||
@cache.read_file(file).should == "my data" | ||
end | ||
end | ||
|
||
it "rereads data" do | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
@cache.read_file(file).should == "my data" | ||
|
||
write_file(file, "changed data") | ||
@cache.read_file(file).should == "changed data" | ||
end | ||
end | ||
|
||
it "errors when the type does not match the expected type" do | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
|
||
expect do | ||
@cache.read_file(file, Hash) do |data| | ||
"a string" | ||
end | ||
end.to raise_error(TypeError) | ||
end | ||
end | ||
|
||
it "converts the read data using the block" do | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
|
||
@cache.read_file(file, Hash) do |data| | ||
{ :data => data } | ||
end.should == { :data => "my data" } | ||
end | ||
end | ||
|
||
it "errors when the file does not exist" do | ||
expect do | ||
@cache.read_file("/notexist") | ||
end.to raise_error(Errno::ENOENT) | ||
end | ||
|
||
it "propogates any errors from the block" do | ||
Dir.mktmpdir do |dir| | ||
file = File.join(dir, "testing") | ||
write_file(file, "my data") | ||
|
||
expect do | ||
@cache.read_file(file) do |data| | ||
raise ArgumentError, "testing error" | ||
end | ||
end.to raise_error(ArgumentError, "testing error") | ||
end | ||
end | ||
end | ||
end | ||
end |