Skip to content

Commit 7d13402

Browse files
committed
(maint) Prefer Puppet::FileSystem.replace_file to File.open
The replace_file function ensure the operation is done atomically.
1 parent d00a850 commit 7d13402

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

lib/puppet/provider/service/freebsd.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def rc_replace(service, rcvar, yesno)
7474
if Puppet::FileSystem.exist?(filename)
7575
s = File.read(filename)
7676
if s.gsub!(/^(#{rcvar}(_enable)?)=\"?(YES|NO)\"?/, "\\1=\"#{yesno}\"")
77-
File.open(filename, File::WRONLY | File::TRUNC) { |f| f << s }
77+
Puppet::FileSystem.replace_file(filename) { |f| f << s }
7878
self.debug("Replaced in #{filename}")
7979
success = true
8080
end

spec/unit/provider/service/freebsd_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
allow(Puppet::FileSystem).to receive(:exist?).with('/etc/rc.conf').and_return(true)
8383
allow(File).to receive(:read).with('/etc/rc.conf').and_return("openntpd_enable=\"NO\"\nntpd_enable=\"NO\"\n")
8484
fh = double('fh')
85-
allow(File).to receive(:open).with('/etc/rc.conf', File::WRONLY | File::TRUNC).and_yield(fh)
85+
allow(Puppet::FileSystem).to receive(:replace_file).with('/etc/rc.conf').and_yield(fh)
8686
expect(fh).to receive(:<<).with("openntpd_enable=\"NO\"\nntpd_enable=\"YES\"\n")
8787
allow(Puppet::FileSystem).to receive(:exist?).with('/etc/rc.conf.local').and_return(false)
8888
allow(Puppet::FileSystem).to receive(:exist?).with('/etc/rc.conf.d/ntpd').and_return(false)

0 commit comments

Comments
 (0)