1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
require 'spec_helper'
require 'tmpdir'
class Hiera
describe Filecache do
before do
@cache = Filecache.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")
expect(@cache.read(file)).to eq("my data")
end
end
it "rereads data when the file changes" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")
expect(@cache.read(file)).to eq("my data")
write_file(file, "changed data")
expect(@cache.read(file)).to eq("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
expect(data).to eq({ :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
expect(data).to eq({ :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")
expect(@cache.read_file(file)).to eq("my data")
end
end
it "sets the encoding to UTF-8 when reading a file" do
begin
original_encoding = Encoding.default_external
Encoding.default_external = Encoding::ISO_8859_1
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")
expect(@cache.read_file(file).encoding).to eq(Encoding::UTF_8)
end
ensure
Encoding.default_external = original_encoding
end
end
it "reads a file with unicode characters" do
begin
original_encoding = Encoding.default_external
Encoding.default_external = Encoding::ISO_8859_1
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "\u2603")
expect(@cache.read_file(file)).to eq("\u2603")
end
ensure
Encoding.default_external = original_encoding
end
end
it "rereads data when the file changes" do
Dir.mktmpdir do |dir|
file = File.join(dir, "testing")
write_file(file, "my data")
expect(@cache.read_file(file)).to eq("my data")
write_file(file, "changed data")
expect(@cache.read_file(file)).to eq("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")
expect(@cache.read_file(file, Hash) do |data|
{ :data => data }
end).to eq({ :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
|