Skip to content

Commit d00a850

Browse files
committed
(maint) Truncate file before overwriting it
Previously, we did not truncate the script when updating a variable. Since the module only support setting variables to "YES" and "NO", the only consequence was that when switching from "YES" to "NO" the last character of the file was kept and we ended up with a blank line: sshd_enable="YES"\n -> sshd_enable="NO"\n\n With the introduction of the support of more variables for the freebsd provider, this result however in more breakage so we have to fix it.
1 parent 3aecb85 commit d00a850

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) { |f| f << s }
77+
File.open(filename, File::WRONLY | File::TRUNC) { |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).and_yield(fh)
85+
allow(File).to receive(:open).with('/etc/rc.conf', File::WRONLY | File::TRUNC).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)