Skip to content

Commit b4705cc

Browse files
committed
Merge remote-tracking branch 'upstream/stable'
* upstream/stable: (Maint) Stop asserting nothingness (Maint) Don't keep Tempfile open while specs are running (Maint) TestHelper already clears global state
2 parents 72f2fa8 + cc3341e commit b4705cc

File tree

1 file changed

+21
-39
lines changed

1 file changed

+21
-39
lines changed

spec/unit/util/storage_spec.rb

+21-39
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22
require 'spec_helper'
33

44
require 'yaml'
5+
require 'fileutils'
56
require 'puppet/util/storage'
67

78
describe Puppet::Util::Storage do
89
include PuppetSpec::Files
910

10-
before(:all) do
11-
@basepath = make_absolute("/somepath")
12-
Puppet[:statedir] = tmpdir("statedir")
13-
end
14-
15-
after(:all) do
16-
Puppet.settings.clear
17-
end
18-
1911
before(:each) do
20-
Puppet::Util::Storage.clear
12+
@basepath = File.expand_path("/somepath")
2113
end
2214

2315
describe "when caching a symbol" do
@@ -40,7 +32,7 @@
4032
end
4133

4234
describe "when caching a Puppet::Type" do
43-
before(:all) do
35+
before(:each) do
4436
@file_test = Puppet::Type.type(:file).new(:name => @basepath+"/yayness", :audit => %w{checksum type})
4537
@exec_test = Puppet::Type.type(:exec).new(:name => @basepath+"/bin/ls /yayness")
4638
end
@@ -81,17 +73,15 @@
8173

8274
describe "when the state file/directory does not exist" do
8375
before(:each) do
84-
transient = Tempfile.new('storage_test')
85-
@path = transient.path()
86-
transient.close!()
76+
@path = tmpfile('storage_test')
8777
end
8878

89-
it "should not fail to load()" do
79+
it "should not fail to load" do
9080
FileTest.exists?(@path).should be_false
9181
Puppet[:statedir] = @path
92-
expect { Puppet::Util::Storage.load }.to_not raise_error
82+
Puppet::Util::Storage.load
9383
Puppet[:statefile] = @path
94-
expect { Puppet::Util::Storage.load }.to_not raise_error
84+
Puppet::Util::Storage.load
9585
end
9686

9787
it "should not lose its internal state when load() is called" do
@@ -109,9 +99,13 @@
10999

110100
describe "when the state file/directory exists" do
111101
before(:each) do
112-
@state_file = Tempfile.new('storage_test')
113-
@saved_statefile = Puppet[:statefile]
114-
Puppet[:statefile] = @state_file.path
102+
@state_file = tmpfile('storage_test')
103+
FileUtils.touch(@state_file)
104+
Puppet[:statefile] = @state_file
105+
end
106+
107+
def write_state_file(contents)
108+
File.open(@state_file, 'w') { |f| f.write(contents) }
115109
end
116110

117111
it "should overwrite its internal state if load() is called" do
@@ -126,62 +120,50 @@
126120

127121
it "should restore its internal state if the state file contains valid YAML" do
128122
test_yaml = {'File["/yayness"]'=>{"name"=>{:a=>:b,:c=>:d}}}
129-
@state_file.write(test_yaml.to_yaml)
130-
@state_file.flush
123+
write_state_file(test_yaml.to_yaml)
131124

132125
Puppet::Util::Storage.load
133126

134127
Puppet::Util::Storage.state.should == test_yaml
135128
end
136129

137130
it "should initialize with a clear internal state if the state file does not contain valid YAML" do
138-
@state_file.write('{ invalid')
139-
@state_file.flush
131+
write_state_file('{ invalid')
140132

141133
Puppet::Util::Storage.load
142134

143135
Puppet::Util::Storage.state.should == {}
144136
end
145137

146138
it "should initialize with a clear internal state if the state file does not contain a hash of data" do
147-
@state_file.write("not_a_hash")
148-
@state_file.flush
139+
write_state_file("not_a_hash")
149140

150141
Puppet::Util::Storage.load
151142

152143
Puppet::Util::Storage.state.should == {}
153144
end
154145

155146
it "should raise an error if the state file does not contain valid YAML and cannot be renamed" do
156-
@state_file.write('{ invalid')
157-
@state_file.flush
147+
write_state_file('{ invalid')
158148

159149
File.expects(:rename).raises(SystemCallError)
160150

161151
expect { Puppet::Util::Storage.load }.to raise_error(Puppet::Error, /Could not rename/)
162152
end
163153

164154
it "should attempt to rename the state file if the file is corrupted" do
165-
@state_file.write('{ invalid')
166-
@state_file.flush
155+
write_state_file('{ invalid')
167156

168157
File.expects(:rename).at_least_once
169158

170159
Puppet::Util::Storage.load
171160
end
172161

173162
it "should fail gracefully on load() if the state file is not a regular file" do
174-
@state_file.close!()
175-
Dir.mkdir(Puppet[:statefile])
163+
FileUtils.rm_f(@state_file)
164+
Dir.mkdir(@state_file)
176165

177166
Puppet::Util::Storage.load
178-
179-
Dir.rmdir(Puppet[:statefile])
180-
end
181-
182-
after(:each) do
183-
@state_file.close!()
184-
Puppet[:statefile] = @saved_statefile
185167
end
186168
end
187169
end

0 commit comments

Comments
 (0)