summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornimisha <nimisha.sharad@clogeny.com>2017-08-09 18:06:30 +0530
committernimisha <nimisha.sharad@clogeny.com>2017-08-16 14:49:20 +0530
commit561c06d451427d444217f6d901a16f30144a6903 (patch)
tree50c0a2356ae58aef9a57c82b2a1f60b6a59da941
parent300fd3e5fdfc88fec26f8ff955b4a1ba96c9c0be (diff)
downloadchef-561c06d451427d444217f6d901a16f30144a6903.tar.gz
Using FFI instead of Win32api
Signed-off-by: nimisha <nimisha.sharad@clogeny.com>
-rw-r--r--lib/chef/provider/windows_path.rb39
-rw-r--r--spec/unit/provider/windows_path_spec.rb24
2 files changed, 19 insertions, 44 deletions
diff --git a/lib/chef/provider/windows_path.rb b/lib/chef/provider/windows_path.rb
index 7158ab2df8..b31789b4b9 100644
--- a/lib/chef/provider/windows_path.rb
+++ b/lib/chef/provider/windows_path.rb
@@ -16,13 +16,15 @@
# limitations under the License.
#
-require "Win32API" if Chef::Platform.windows?
+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
- ExpandEnvironmentStrings = Win32API.new("kernel32", "ExpandEnvironmentStrings", %w{ P P L }, "L") if Chef::Platform.windows? && !defined?(ExpandEnvironmentStrings)
+
+ include Chef::Mixin::WindowsEnvHelper if Chef::Platform.windows?
def load_current_resource
@current_resource = Chef::Resource::WindowsPath.new(new_resource.name)
@@ -31,32 +33,29 @@ class Chef
end
action :add do
- declare_resource(:env, "path") do
- action :modify
- delim ::File::PATH_SEPARATOR
- value new_resource.path.tr("/", '\\')
+ # 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
- # Expands environment-variable strings and replaces them with the values defined for the current user
- ENV["PATH"] = expand_env_vars(ENV["PATH"])
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 new_resource.path.tr("/", '\\')
- end
- end
-
- # Expands the environment variables
- def expand_env_vars(path)
- # We pick 32k because that is the largest it could be:
- # http://msdn.microsoft.com/en-us/library/windows/desktop/ms724265%28v=vs.85%29.aspx
- buf = 0.chr * 32 * 1024 # 32k
- if Chef::Provider::WindowsPath::ExpandEnvironmentStrings.call(path.dup, buf, buf.length) == 0
- raise Chef::Exceptions::Win32APIError, "Failed calling ExpandEnvironmentStrings with error code #{FFI.errno}"
+ value path.tr("/", '\\')
end
- buf.strip
end
end
end
diff --git a/spec/unit/provider/windows_path_spec.rb b/spec/unit/provider/windows_path_spec.rb
index 1ff7e98f8c..7eca3f3113 100644
--- a/spec/unit/provider/windows_path_spec.rb
+++ b/spec/unit/provider/windows_path_spec.rb
@@ -62,28 +62,4 @@ describe Chef::Provider::WindowsPath, :windows_only do
provider.run_action(:remove)
end
end
-
- describe "#expand_env_vars" do
- context "when a simple path is given" do
- it "doesn't expand the environment variable" do
- expanded_var = provider.expand_env_vars("some_path")
- expect(expanded_var).to eq("some_path")
- end
- end
-
- context "when an environment variable string is provided" do
- it "expands the environment variable" do
- expanded_var = provider.expand_env_vars("%WINDIR%")
- expect(expanded_var).to match(/C:\\Windows/i)
- end
- end
-
- context "when ExpandEnvironmentStrings fails" do
- it "raises error" do
- allow(Chef::Provider::WindowsPath::ExpandEnvironmentStrings).to receive(:call).and_return(0)
- allow(FFI).to receive(:errno).and_return(10)
- expect { provider.expand_env_vars("some_path") }.to raise_error(/Failed calling ExpandEnvironmentStrings with error code 10/)
- end
- end
- end
end