Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PUP-9001) Show diff for new files #8890

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions lib/puppet/type/file/data_sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,28 @@ def checksum_insync?(param, is, has_contents, &block)
is_insync = yield(is)

if show_diff?(!is_insync)
if param.sensitive
send resource[:loglevel], "[diff redacted]"
else
write_temporarily(param) do |path|
diff_output = diff(resource[:path], path)
if diff_output.encoding == Encoding::BINARY || !diff_output.valid_encoding?
diff_output = "Binary files #{resource[:path]} and #{path} differ"
end
send resource[:loglevel], "\n" + diff_output
show_diff(param, is)
end
is_insync
end

def show_diff(param, is)
if param.sensitive
send resource[:loglevel], "[diff redacted]"
else
write_temporarily(param) do |path|
resource_path = if is == :absent
Puppet::Util::Platform.windows? ? 'NUL' : '/dev/null'
else
resource[:path]
end
diff_output = diff(resource_path, path)
if diff_output.encoding == Encoding::BINARY || !diff_output.valid_encoding?
diff_output = "Binary files #{resource_path} and #{path} differ"
end
send resource[:loglevel], "\n" + diff_output
end
end
is_insync
end

def show_diff?(has_changes)
Expand Down
15 changes: 12 additions & 3 deletions lib/puppet/type/file/ensure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Puppet
Puppet::Type.type(:file).ensurable do
require 'etc'
require_relative '../../../puppet/util/symbolic_file_mode'
require_relative '../../../puppet/type/file/data_sync.rb'
include Puppet::Util::SymbolicFileMode
include Puppet::DataSync

desc <<-EOT
Whether the file should exist, and if so what kind of file it should be.
Expand Down Expand Up @@ -160,11 +162,18 @@ def insync?(currentvalue)
return true
end

if self.should == :present
return !(currentvalue.nil? or currentvalue == :absent)
is_insync = if self.should == :present
!(currentvalue.nil? or currentvalue == :absent)
else
return super(currentvalue)
super(currentvalue)
end

if self.should == :file && show_diff?(!is_insync)
contents_prop = resource.parameter(:source) || resource.parameter(:content)
show_diff(contents_prop, currentvalue)
end

return is_insync
end

def retrieve
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/type/file/ensure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@
end

describe "when retrieving the current state" do
let(:resource_with_content) { Puppet::Type.type(:file).new(:ensure => 'file', :path => path, :replace => true, :content => 'butter' ) }
let(:property_with_content) { resource_with_content.property(:ensure) }

it "should return :absent if the file does not exist" do
expect(resource).to receive(:stat).and_return(nil)

expect(property.retrieve).to eq(:absent)
end

it "prints the content or source diff, if the file is absent" do
null_file = Puppet::Util::Platform.windows? ? 'NUL' : '/dev/null'
expect(property_with_content).to receive(:show_diff?).and_return(true)
resource_with_content[:loglevel] = "debug"
expect(property_with_content).to receive(:diff).with(null_file, any_args).and_return("my diff")
expect(property_with_content).to receive(:debug).with("\nmy diff")
expect(property_with_content).not_to be_safe_insync(:absent)
end

it "should return the current file type if the file exists" do
stat = double('stat', :ftype => "directory")
expect(resource).to receive(:stat).and_return(stat)
Expand Down