From 577773bbb838a91fee53d1ad21dddfb5fd9dbd22 Mon Sep 17 00:00:00 2001 From: marguerite Date: Tue, 21 Jul 2015 14:47:12 -0400 Subject: Interpolate `%{path}` in verify command See chef/chef#3232 --- lib/chef/resource/file/verification.rb | 4 +++- spec/unit/resource/file/verification_spec.rb | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/chef/resource/file/verification.rb b/lib/chef/resource/file/verification.rb index f1ca0f1883..6be1f912fd 100644 --- a/lib/chef/resource/file/verification.rb +++ b/lib/chef/resource/file/verification.rb @@ -106,7 +106,9 @@ class Chef # We reuse Chef::GuardInterpreter in order to support # the same set of options that the not_if/only_if blocks do def verify_command(path, opts) - command = @command % {:file => path} + # First implementation interpolated `file`; docs & RFC claim `path` + # is interpolated. Until `file` can be deprecated, interpolate both. + command = @command % {:file => path, :path => path} interpreter = Chef::GuardInterpreter.for_resource(@parent_resource, command, @command_opts) interpreter.evaluate end diff --git a/spec/unit/resource/file/verification_spec.rb b/spec/unit/resource/file/verification_spec.rb index 3609d9d482..0ab4ce28e6 100644 --- a/spec/unit/resource/file/verification_spec.rb +++ b/spec/unit/resource/file/verification_spec.rb @@ -69,12 +69,22 @@ describe Chef::Resource::File::Verification do end context "with a verification command(String)" do + def platform_specific_verify_command(variable_name) + if windows? + "if \"#{temp_path}\" == \"%{#{variable_name}}\" (exit 0) else (exit 1)" + else + "test #{temp_path} = %{#{variable_name}}" + end + end + it "substitutes \%{file} with the path" do - test_command = if windows? - "if \"#{temp_path}\" == \"%{file}\" (exit 0) else (exit 1)" - else - "test #{temp_path} = %{file}" - end + test_command = platform_specific_verify_command('file') + v = Chef::Resource::File::Verification.new(parent_resource, test_command, {}) + expect(v.verify(temp_path)).to eq(true) + end + + it "substitutes \%{path} with the path" do + test_command = platform_specific_verify_command('path') v = Chef::Resource::File::Verification.new(parent_resource, test_command, {}) expect(v.verify(temp_path)).to eq(true) end -- cgit v1.2.1 From e330c30a6d9db16cc65f16164a40cfc96c0f6f7a Mon Sep 17 00:00:00 2001 From: marguerite Date: Tue, 21 Jul 2015 15:36:21 -0400 Subject: Warn about deprecation `%{file}` interpolation in verify command See chef/chef#3232 --- lib/chef/resource/file/verification.rb | 4 ++++ spec/unit/resource/file/verification_spec.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/chef/resource/file/verification.rb b/lib/chef/resource/file/verification.rb index 6be1f912fd..faf4791884 100644 --- a/lib/chef/resource/file/verification.rb +++ b/lib/chef/resource/file/verification.rb @@ -108,6 +108,10 @@ class Chef def verify_command(path, opts) # First implementation interpolated `file`; docs & RFC claim `path` # is interpolated. Until `file` can be deprecated, interpolate both. + Chef::Log.deprecation( + '%{file} is deprecated in verify command and will not be '\ + 'supported in Chef 13. Please use %{path} instead.' + ) if @command.include?('%{file}') command = @command % {:file => path, :path => path} interpreter = Chef::GuardInterpreter.for_resource(@parent_resource, command, @command_opts) interpreter.evaluate diff --git a/spec/unit/resource/file/verification_spec.rb b/spec/unit/resource/file/verification_spec.rb index 0ab4ce28e6..04ae9ad629 100644 --- a/spec/unit/resource/file/verification_spec.rb +++ b/spec/unit/resource/file/verification_spec.rb @@ -69,6 +69,10 @@ describe Chef::Resource::File::Verification do end context "with a verification command(String)" do + before(:each) do + allow(Chef::Log).to receive(:deprecation).and_return(nil) + end + def platform_specific_verify_command(variable_name) if windows? "if \"#{temp_path}\" == \"%{#{variable_name}}\" (exit 0) else (exit 1)" @@ -83,6 +87,20 @@ describe Chef::Resource::File::Verification do expect(v.verify(temp_path)).to eq(true) end + it "warns about deprecation when \%{file} is used" do + expect(Chef::Log).to receive(:deprecation).with(/%{file} is deprecated/) + test_command = platform_specific_verify_command('file') + Chef::Resource::File::Verification.new(parent_resource, test_command, {}) + .verify(temp_path) + end + + it "does not warn about deprecation when \%{file} is not used" do + expect(Chef::Log).to_not receive(:deprecation) + test_command = platform_specific_verify_command('path') + Chef::Resource::File::Verification.new(parent_resource, test_command, {}) + .verify(temp_path) + end + it "substitutes \%{path} with the path" do test_command = platform_specific_verify_command('path') v = Chef::Resource::File::Verification.new(parent_resource, test_command, {}) -- cgit v1.2.1