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 | |
parent | 87d4123053cb10254f3352c1f5fd8eef508b1e9d (diff) | |
download | chef-fdf28372bd7f89e9a3d991f42e761dc3df838b78.tar.gz |
add back diffing against no file for why-run mode
-rw-r--r-- | lib/chef/util/diff.rb | 27 | ||||
-rw-r--r-- | spec/unit/util/diff_spec.rb | 8 |
2 files changed, 25 insertions, 10 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 diff --git a/spec/unit/util/diff_spec.rb b/spec/unit/util/diff_spec.rb index a856902c9b..4cc72c0006 100644 --- a/spec/unit/util/diff_spec.rb +++ b/spec/unit/util/diff_spec.rb @@ -44,16 +44,16 @@ describe Chef::Util::Diff, :uses_diff => true do expect(differ).to be_a_kind_of(Chef::Util::Diff) end - it "should raise an exception if the old_file does not exist" do + it "produces a diff even if the old_file does not exist" do old_tempfile.close old_tempfile.unlink - expect { differ.diff(old_file, new_file) }.to raise_error + expect(differ.for_output).to eql(["(no diff)"]) end - it "should raise an exception if the new_file does not exist" do + it "produces a diff even if the new_file does not exist" do new_tempfile.close new_tempfile.unlink - expect { differ.diff(old_file, new_file) }.to raise_error + expect(differ.for_output).to eql(["(no diff)"]) end describe "when the two files exist with no content" do |