Skip to content

Commit b3fb2cf

Browse files
committed
make sure specs clean their tempfiles
1 parent 566fe34 commit b3fb2cf

2 files changed

Lines changed: 17 additions & 22 deletions

File tree

‎chef/spec/unit/provider/cookbook_file_spec.rb‎

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@
6565

6666
describe "when the file doesn't yet exist" do
6767
before do
68+
@install_to = Dir.tmpdir + '/apache2_modconf.pl'
69+
6870
@current_resource = @new_resource.dup
6971
@provider.current_resource = @current_resource
7072
end
7173

74+
after { ::File.exist?(@install_to) && FileUtils.rm(@install_to) }
75+
7276
it "loads the current file state" do
7377
@provider.load_current_resource
7478
@provider.current_resource.checksum.should be_nil
@@ -105,33 +109,20 @@
105109
end
106110

107111
it "installs the file from the cookbook cache" do
108-
tempfile = nil
109-
begin
110-
tempfile = Tempfile.open('rspec-action-create-test')
111-
install_path = ::File.dirname(tempfile.path) + '/apache2_modconf.pl'
112-
@new_resource.path(install_path)
113-
@provider.should_receive(:set_all_access_controls)
114-
@provider.action_create
115-
actual = IO.read(install_path)
116-
actual.should == @file_content
117-
ensure
118-
tempfile && tempfile.close!
119-
end
112+
@new_resource.path(@install_to)
113+
@provider.should_receive(:backup_new_resource)
114+
@provider.should_receive(:set_all_access_controls)
115+
@provider.action_create
116+
actual = IO.read(@install_to)
117+
actual.should == @file_content
120118
end
121119

122120
it "installs the file for create_if_missing" do
123-
install_path = nil
124-
Tempfile.open('rspec-action-create-test') do |tempfile|
125-
install_path = ::File.dirname(tempfile.path) + '/apache2_modconf.pl'
126-
FileUtils.rm(install_path)
127-
end
128-
129-
::File.exist?(install_path).should be_false
130-
131-
@new_resource.path(install_path)
121+
@new_resource.path(@install_to)
132122
@provider.should_receive(:set_all_access_controls)
123+
@provider.should_receive(:backup_new_resource)
133124
@provider.action_create_if_missing
134-
actual = IO.read(install_path)
125+
actual = IO.read(@install_to)
135126
actual.should == @file_content
136127
end
137128
end
@@ -148,6 +139,7 @@
148139

149140
it "overwrites it when the create action is called" do
150141
@provider.should_receive(:set_all_access_controls)
142+
@provider.should_receive(:backup_new_resource)
151143
@provider.action_create
152144
actual = IO.read(@target_file)
153145
actual.should == @file_content
@@ -173,6 +165,8 @@
173165
@provider.current_resource = @current_resource
174166
end
175167

168+
after { @tempfile && @tempfile.unlink}
169+
176170
it "it checks access control but does not alter content when action is create" do
177171
@provider.load_current_resource
178172
@provider.should_receive(:set_all_access_controls)

‎chef/spec/unit/provider/template_spec.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
@resource.owner("adam")
101101
@resource.group("wheel")
102102
@resource.mode(00644)
103+
@provider.should_receive(:backup)
103104
@provider.should_receive(:set_all_access_controls).with(an_instance_of(String))
104105
@provider.action_create
105106
end

0 commit comments

Comments
 (0)