diff options
author | Lamont Granquist <lamont@opscode.com> | 2013-03-29 14:39:30 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@opscode.com> | 2013-03-29 14:39:30 -0700 |
commit | fdf28372bd7f89e9a3d991f42e761dc3df838b78 (patch) | |
tree | 07b9f3d0bb96195783ecd5bdea1f330753549ee9 /lib/chef/util/diff.rb | |
parent | 87d4123053cb10254f3352c1f5fd8eef508b1e9d (diff) | |
download | chef-fdf28372bd7f89e9a3d991f42e761dc3df838b78.tar.gz |
add back diffing against no file for why-run mode
Diffstat (limited to 'lib/chef/util/diff.rb')
-rw-r--r-- | lib/chef/util/diff.rb | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/chef/util/diff.rb b/lib/chef/util/diff.rb index 72459642de..1151722af8 100644 --- a/lib/chef/util/diff.rb +++ b/lib/chef/util/diff.rb @@ -33,13 +33,27 @@ class Chef @diff.join("\\n") end + def use_tempfile_if_missing(file) + tempfile = nil + unless File.exists?(file) + Chef::Log.debug("file #{file} does not exist to diff against, using empty tempfile") + tempfile = Tempfile.new("chef-diff") + file = tempfile.path + end + yield file + unless tempfile.nil? + tempfile.close + tempfile.unlink + end + end + def diff(old_file, new_file) - # indicates calling code bug: caller is reponsible for making certain both - # files exist - raise "old file #{old_file} does not exist" unless File.exists?(old_file) - raise "new file #{new_file} does not exist" unless File.exists?(new_file) - @error = catch (:nodiff) do - do_diff(old_file, new_file) + use_tempfile_if_missing(old_file) do |old_file| + use_tempfile_if_missing(new_file) do |new_file| + @error = catch (:nodiff) do + do_diff(old_file, new_file) + end + end end end @@ -63,6 +77,7 @@ class Chef begin # -u: Unified diff format + Chef::Log.debug("running: diff -u #{old_file} #{new_file}") result = shell_out("diff -u #{old_file} #{new_file}") rescue Exception => e # Should *not* receive this, but in some circumstances it seems that |