diff options
author | Tim Smith <tsmith@chef.io> | 2017-08-17 09:20:57 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-17 09:20:57 -0700 |
commit | 80d0ffbe4f52ad7f01907f839b0f929d5ae9cca8 (patch) | |
tree | 89b6ee6b9ff2cd2543bd8ca13a94531871c279de | |
parent | d09aa4556a539db269ff5a6f46c830e302877bbb (diff) | |
parent | 561c06d451427d444217f6d901a16f30144a6903 (diff) | |
download | chef-80d0ffbe4f52ad7f01907f839b0f929d5ae9cca8.tar.gz |
Merge pull request #6295 from MsysTechnologiesllc/nim/windows_path
Add windows_path resource from the Windows cookbook
-rw-r--r-- | lib/chef/provider/windows_path.rb | 62 | ||||
-rw-r--r-- | lib/chef/providers.rb | 1 | ||||
-rw-r--r-- | lib/chef/resource/windows_path.rb | 41 | ||||
-rw-r--r-- | lib/chef/resources.rb | 1 | ||||
-rw-r--r-- | spec/functional/resource/windows_path_spec.rb | 64 | ||||
-rw-r--r-- | spec/unit/provider/windows_path_spec.rb | 65 | ||||
-rw-r--r-- | spec/unit/resource/windows_path_spec.rb | 38 |
7 files changed, 272 insertions, 0 deletions
diff --git a/lib/chef/provider/windows_path.rb b/lib/chef/provider/windows_path.rb new file mode 100644 index 0000000000..b31789b4b9 --- /dev/null +++ b/lib/chef/provider/windows_path.rb @@ -0,0 +1,62 @@ +# +# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>) +# Copyright:: Copyright 2008-2017, Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "chef/mixin/windows_env_helper" if Chef::Platform.windows? +require "chef/mixin/wide_string" +require "chef/exceptions" + +class Chef + class Provider + class WindowsPath < Chef::Provider + + include Chef::Mixin::WindowsEnvHelper if Chef::Platform.windows? + + def load_current_resource + @current_resource = Chef::Resource::WindowsPath.new(new_resource.name) + @current_resource.path(new_resource.path) + @current_resource + end + + action :add do + # The windows Env provider does not correctly expand variables in + # the PATH environment variable. Ruby expects these to be expanded. + # + path = expand_path(new_resource.path) + converge_by "Adding #{new_resource.path} to path environment variable" do + declare_resource(:env, "path") do + action :modify + delim ::File::PATH_SEPARATOR + value path.tr("/", '\\') + end + end + end + + action :remove do + # The windows Env provider does not correctly expand variables in + # the PATH environment variable. Ruby expects these to be expanded. + # + path = expand_path(new_resource.path) + declare_resource(:env, "path") do + action :delete + delim ::File::PATH_SEPARATOR + value path.tr("/", '\\') + end + end + end + end +end diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb index e5b39c4e3c..a3332477e7 100644 --- a/lib/chef/providers.rb +++ b/lib/chef/providers.rb @@ -61,6 +61,7 @@ require "chef/provider/whyrun_safe_ruby_block" require "chef/provider/yum_repository" require "chef/provider/windows_task" require "chef/provider/zypper_repository" +require "chef/provider/windows_path" require "chef/provider/env/windows" diff --git a/lib/chef/resource/windows_path.rb b/lib/chef/resource/windows_path.rb new file mode 100644 index 0000000000..5472a7e4fd --- /dev/null +++ b/lib/chef/resource/windows_path.rb @@ -0,0 +1,41 @@ +# +# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>) +# Copyright:: Copyright 2008-2017, Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "chef/resource" + +class Chef + class Resource + class WindowsPath < Chef::Resource + + provides :windows_path, os: "windows" + + allowed_actions :add, :remove + default_action :add + + def initialize(name, run_context = nil) + super + @resource_name = :windows_path + @path = name + @provider = Chef::Provider::WindowsPath + @action = :add + end + + property :path, String, name_property: true + end + end +end diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index a1f4f8ac0b..54d21fd53c 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -101,3 +101,4 @@ require "chef/resource/cab_package" require "chef/resource/powershell_package" require "chef/resource/msu_package" require "chef/resource/windows_task" +require "chef/resource/windows_path" diff --git a/spec/functional/resource/windows_path_spec.rb b/spec/functional/resource/windows_path_spec.rb new file mode 100644 index 0000000000..912abe6b24 --- /dev/null +++ b/spec/functional/resource/windows_path_spec.rb @@ -0,0 +1,64 @@ +# +# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>) +# Copyright:: Copyright (c) 2017 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" + +describe Chef::Resource::WindowsPath, :windows_only do + let(:path) { "test_path" } + + before(:all) do + @old_path = ENV["PATH"].dup + end + + after(:all) do + ENV["PATH"] = @old_path + end + + subject do + new_resource = Chef::Resource::WindowsPath.new(path, run_context) + new_resource + end + + describe "adding path" do + after { remove_path } + + it "appends the user given path in the Environment variable Path" do + subject.run_action(:add) + expect(ENV["PATH"]).to include(path) + end + end + + describe "removing path" do + before { add_path } + + it "removes the user given path from the Environment variable Path" do + subject.run_action(:remove) + expect(ENV["PATH"]).not_to include(path) + end + end + + def remove_path + new_resource = Chef::Resource::WindowsPath.new(path, run_context) + new_resource.run_action(:remove) + end + + def add_path + new_resource = Chef::Resource::WindowsPath.new(path, run_context) + new_resource.run_action(:add) + end +end diff --git a/spec/unit/provider/windows_path_spec.rb b/spec/unit/provider/windows_path_spec.rb new file mode 100644 index 0000000000..7eca3f3113 --- /dev/null +++ b/spec/unit/provider/windows_path_spec.rb @@ -0,0 +1,65 @@ +# +# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>) +# Copyright:: Copyright 2008-2017, Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" + +describe Chef::Provider::WindowsPath, :windows_only do + before(:all) do + @old_path = ENV["PATH"].dup + end + + after(:all) do + ENV["PATH"] = @old_path + end + + let(:new_resource) { Chef::Resource::WindowsPath.new("some_path") } + + let(:provider) do + node = Chef::Node.new + events = Chef::EventDispatch::Dispatcher.new + run_context = Chef::RunContext.new(node, {}, events) + Chef::Provider::WindowsPath.new(new_resource, run_context) + end + + describe "#load_current_resource" do + it "returns a current_resource" do + expect(provider.load_current_resource).to be_kind_of(Chef::Resource::WindowsPath) + end + + it "sets the path of current resource as the path of new resource" do + current_resource = provider.load_current_resource + expect(current_resource.path).to eq("some_path") + end + end + + describe "#action_add" do + it "uses env resource to add 'path' environment variable" do + allow(provider).to receive(:expand_env_vars) + expect(provider).to receive(:declare_resource).with(:env, "path") + provider.run_action(:add) + end + end + + describe "#action_remove" do + it "uses env resource to remove 'path' environment variable" do + allow(provider).to receive(:expand_env_vars) + expect(provider).to receive(:declare_resource).with(:env, "path") + provider.run_action(:remove) + end + end +end diff --git a/spec/unit/resource/windows_path_spec.rb b/spec/unit/resource/windows_path_spec.rb new file mode 100644 index 0000000000..aecea8ee6b --- /dev/null +++ b/spec/unit/resource/windows_path_spec.rb @@ -0,0 +1,38 @@ +# +# Author:: Nimisha Sharad (<nimisha.sharad@msystechnologies.com>) +# Copyright:: Copyright 2008-2017, Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "spec_helper" + +describe Chef::Resource::WindowsPath do + subject { Chef::Resource::WindowsPath.new("some_path") } + + it { is_expected.to be_a_kind_of(Chef::Resource) } + it { is_expected.to be_a_instance_of(Chef::Resource::WindowsPath) } + + it "sets resource name as :windows_path" do + expect(subject.resource_name).to eql(:windows_path) + end + + it "sets the path as it's name" do + expect(subject.path).to eql("some_path") + end + + it "sets the default action as :add" do + expect(subject.action).to eql(:add) + end +end |