summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorharikesh-kolekar <harikesh.kolekar@msystechnologies.com>2017-12-08 19:05:23 +0530
committerharikesh-kolekar <harikesh.kolekar@msystechnologies.com>2018-01-30 07:31:48 +0000
commit5809c04e0921f213cc59055d60c97453ae5a2dde (patch)
treedca6cafdbacd54bfc724029524f4f07d03aea8e7
parente88d467c7db7f72bbbe3aae1de4eb19379e45e86 (diff)
downloadchef-5809c04e0921f213cc59055d60c97453ae5a2dde.tar.gz
delete env variable if present in current process
Signed-off-by: harikesh-kolekar <harikesh.kolekar@msystechnologies.com>
-rw-r--r--lib/chef/provider/env.rb2
-rwxr-xr-xspec/functional/resource/env_spec.rb22
2 files changed, 23 insertions, 1 deletions
diff --git a/lib/chef/provider/env.rb b/lib/chef/provider/env.rb
index 490fa31146..b8b9d99846 100644
--- a/lib/chef/provider/env.rb
+++ b/lib/chef/provider/env.rb
@@ -123,7 +123,7 @@ class Chef
end
def action_delete
- if @key_exists && !delete_element
+ if ( ENV[new_resource.key_name] || @key_exists ) && !delete_element
delete_env
Chef::Log.info("#{new_resource} deleted")
new_resource.updated_by_last_action(true)
diff --git a/spec/functional/resource/env_spec.rb b/spec/functional/resource/env_spec.rb
index ad7370255b..db1c40d506 100755
--- a/spec/functional/resource/env_spec.rb
+++ b/spec/functional/resource/env_spec.rb
@@ -22,9 +22,13 @@ describe Chef::Resource::Env, :windows_only do
context "when running on Windows" do
let(:chef_env_test_lower_case) { "chefenvtest" }
let(:chef_env_test_mixed_case) { "chefENVtest" }
+ let(:chef_env_with_delim) { "chef_env_with_delim" }
+ let(:chef_env_delim) { ";" }
+ let(:chef_env_test_delim) { "#{value1};#{value2}" }
let(:env_dne_key) { "env_dne_key" }
let(:env_value1) { "value1" }
let(:env_value2) { "value2" }
+ let(:delim_value) { "#{env_value1};#{env_value2}" }
let(:env_user) { ENV["USERNAME"].upcase }
let(:default_env_user) { "<SYSTEM>" }
@@ -250,6 +254,17 @@ describe Chef::Resource::Env, :windows_only do
expect(@env_obj).not_to be_nil
end
+ it "should not delete variable when a delim present" do
+ test_resource.key_name(chef_env_with_delim)
+ test_resource.delim(chef_env_delim)
+ test_resource.value(delim_value)
+ test_resource.run_action(:create)
+ expect(ENV[chef_env_with_delim]).to eq(delim_value)
+ test_resource.value(env_value1)
+ test_resource.run_action(:delete)
+ expect(ENV[chef_env_with_delim]).to eq(env_value2)
+ end
+
it "should not raise an exception when a non-existent environment variable is deleted" do
expect(ENV[chef_env_test_lower_case]).to eq(nil)
test_resource.key_name(chef_env_test_lower_case)
@@ -268,6 +283,13 @@ describe Chef::Resource::Env, :windows_only do
expect(ENV[chef_env_test_lower_case]).to eq(nil)
expect(ENV[chef_env_test_mixed_case]).to eq(nil)
end
+ it "should delete a value from the current process even if it is not in the registry" do
+ expect(ENV[env_dne_key]).to eq(nil)
+ ENV[env_dne_key] = env_value1
+ test_resource.key_name(env_dne_key)
+ test_resource.run_action(:delete)
+ expect(ENV[env_dne_key]).to eq(nil)
+ end
end
end