diff options
Diffstat (limited to 'chef-utils')
-rw-r--r-- | chef-utils/lib/chef-utils.rb | 3 | ||||
-rw-r--r-- | chef-utils/lib/chef-utils/dsl/default_paths.rb | 59 | ||||
-rw-r--r-- | chef-utils/lib/chef-utils/dsl/path_sanity.rb | 29 | ||||
-rw-r--r-- | chef-utils/spec/spec_helper.rb | 2 | ||||
-rw-r--r-- | chef-utils/spec/unit/dsl/path_sanity_spec.rb | 28 |
5 files changed, 80 insertions, 41 deletions
diff --git a/chef-utils/lib/chef-utils.rb b/chef-utils/lib/chef-utils.rb index 43058f6fe3..c1b06ed3fb 100644 --- a/chef-utils/lib/chef-utils.rb +++ b/chef-utils/lib/chef-utils.rb @@ -19,6 +19,7 @@ require_relative "chef-utils/dsl/architecture" require_relative "chef-utils/dsl/cloud" require_relative "chef-utils/dsl/introspection" require_relative "chef-utils/dsl/os" +require_relative "chef-utils/dsl/default_paths" require_relative "chef-utils/dsl/path_sanity" require_relative "chef-utils/dsl/platform" require_relative "chef-utils/dsl/platform_family" @@ -34,9 +35,9 @@ require_relative "chef-utils/mash" module ChefUtils include ChefUtils::DSL::Architecture include ChefUtils::DSL::Cloud + include ChefUtils::DSL::DefaultPaths include ChefUtils::DSL::Introspection include ChefUtils::DSL::OS - include ChefUtils::DSL::PathSanity include ChefUtils::DSL::Platform include ChefUtils::DSL::PlatformFamily include ChefUtils::DSL::PlatformVersion diff --git a/chef-utils/lib/chef-utils/dsl/default_paths.rb b/chef-utils/lib/chef-utils/dsl/default_paths.rb new file mode 100644 index 0000000000..f18bd93157 --- /dev/null +++ b/chef-utils/lib/chef-utils/dsl/default_paths.rb @@ -0,0 +1,59 @@ +# +# Copyright:: Copyright (c) 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_relative "../internal" +require_relative "platform_family" + +module ChefUtils + module DSL + module DefaultPaths + include Internal + + # @since 15.5 + def default_paths(env = nil) + env_path = env ? env["PATH"] : __env_path + env_path = "" if env_path.nil? + path_separator = ChefUtils.windows? ? ";" : ":" + # ensure the Ruby and Gem bindirs are included for omnibus chef installs + new_paths = env_path.split(path_separator) + [ __ruby_bindir, __gem_bindir ].compact.each do |path| + new_paths = [ path ] + new_paths unless new_paths.include?(path) + end + __default_paths.each do |path| + new_paths << path unless new_paths.include?(path) + end + new_paths.join(path_separator).encode("utf-8", invalid: :replace, undef: :replace) + end + + private + + def __default_paths + ChefUtils.windows? ? %w{} : %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin} + end + + def __ruby_bindir + RbConfig::CONFIG["bindir"] + end + + def __gem_bindir + Gem.bindir + end + + extend self + end + end +end diff --git a/chef-utils/lib/chef-utils/dsl/path_sanity.rb b/chef-utils/lib/chef-utils/dsl/path_sanity.rb index 921c666124..1fbfbdccf3 100644 --- a/chef-utils/lib/chef-utils/dsl/path_sanity.rb +++ b/chef-utils/lib/chef-utils/dsl/path_sanity.rb @@ -15,42 +15,21 @@ # limitations under the License. # -require_relative "../internal" -require_relative "platform_family" +require_relative "default_paths" module ChefUtils module DSL module PathSanity - include Internal + include ChefUtils::DSL::DefaultPaths - # @since 15.5 def sanitized_path(env = nil) - env_path = env ? env["PATH"] : __env_path - env_path = "" if env_path.nil? - path_separator = ChefUtils.windows? ? ";" : ":" - # ensure the Ruby and Gem bindirs are included for omnibus chef installs - new_paths = env_path.split(path_separator) - [ __ruby_bindir, __gem_bindir ].compact.each do |path| - new_paths = [ path ] + new_paths unless new_paths.include?(path) - end - __sane_paths.each do |path| - new_paths << path unless new_paths.include?(path) - end - new_paths.join(path_separator).encode("utf-8", invalid: :replace, undef: :replace) + default_paths(env) end private def __sane_paths - ChefUtils.windows? ? %w{} : %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin} - end - - def __ruby_bindir - RbConfig::CONFIG["bindir"] - end - - def __gem_bindir - Gem.bindir + __default_paths end extend self diff --git a/chef-utils/spec/spec_helper.rb b/chef-utils/spec/spec_helper.rb index a44377336e..0b193e2d9a 100644 --- a/chef-utils/spec/spec_helper.rb +++ b/chef-utils/spec/spec_helper.rb @@ -6,7 +6,7 @@ HELPER_MODULES = [ ChefUtils::DSL::Cloud, ChefUtils::DSL::Introspection, ChefUtils::DSL::OS, - ChefUtils::DSL::PathSanity, + ChefUtils::DSL::DefaultPaths, ChefUtils::DSL::Platform, ChefUtils::DSL::PlatformFamily, ChefUtils::DSL::Service, diff --git a/chef-utils/spec/unit/dsl/path_sanity_spec.rb b/chef-utils/spec/unit/dsl/path_sanity_spec.rb index 5f8e03aeb9..c75b99a751 100644 --- a/chef-utils/spec/unit/dsl/path_sanity_spec.rb +++ b/chef-utils/spec/unit/dsl/path_sanity_spec.rb @@ -17,9 +17,9 @@ require "spec_helper" -RSpec.describe ChefUtils::DSL::PathSanity do - class PathSanityTestClass - include ChefUtils::DSL::PathSanity +RSpec.describe ChefUtils::DSL::DefaultPaths do + class DefaultPathsTestClass + include ChefUtils::DSL::DefaultPaths end before do @@ -32,26 +32,26 @@ RSpec.describe ChefUtils::DSL::PathSanity do allow(ChefUtils).to receive(:windows?).and_return(false) end - let(:test_instance) { PathSanityTestClass.new } + let(:test_instance) { DefaultPathsTestClass.new } it "works with no path" do env = {} - expect(test_instance.sanitized_path(env)).to eql("#{Gem.bindir}:#{RbConfig::CONFIG["bindir"]}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") + expect(test_instance.default_paths(env)).to eql("#{Gem.bindir}:#{RbConfig::CONFIG["bindir"]}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") end it "works with nil path" do env = { "PATH" => nil } - expect(test_instance.sanitized_path(env)).to eql("#{Gem.bindir}:#{RbConfig::CONFIG["bindir"]}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") + expect(test_instance.default_paths(env)).to eql("#{Gem.bindir}:#{RbConfig::CONFIG["bindir"]}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") end it "works with empty path" do env = { "PATH" => "" } - expect(test_instance.sanitized_path(env)).to eql("#{Gem.bindir}:#{RbConfig::CONFIG["bindir"]}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") + expect(test_instance.default_paths(env)).to eql("#{Gem.bindir}:#{RbConfig::CONFIG["bindir"]}:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin") end - it "appends the sane_paths to the end of the path, preserving any that already exist, in the same order" do + it "appends the default_paths to the end of the path, preserving any that already exist, in the same order" do env = { "PATH" => "/bin:/opt/app/bin:/sbin" } - expect(test_instance.sanitized_path(env)).to eql("#{Gem.bindir}:#{RbConfig::CONFIG["bindir"]}:/bin:/opt/app/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin") + expect(test_instance.default_paths(env)).to eql("#{Gem.bindir}:#{RbConfig::CONFIG["bindir"]}:/bin:/opt/app/bin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin") end end @@ -60,26 +60,26 @@ RSpec.describe ChefUtils::DSL::PathSanity do allow(ChefUtils).to receive(:windows?).and_return(true) end - let(:test_instance) { PathSanityTestClass.new } + let(:test_instance) { DefaultPathsTestClass.new } it "works with no path" do env = {} - expect(test_instance.sanitized_path(env)).to eql("#{Gem.bindir};#{RbConfig::CONFIG["bindir"]}") + expect(test_instance.default_paths(env)).to eql("#{Gem.bindir};#{RbConfig::CONFIG["bindir"]}") end it "works with nil path" do env = { "PATH" => nil } - expect(test_instance.sanitized_path(env)).to eql("#{Gem.bindir};#{RbConfig::CONFIG["bindir"]}") + expect(test_instance.default_paths(env)).to eql("#{Gem.bindir};#{RbConfig::CONFIG["bindir"]}") end it "works with empty path" do env = { "PATH" => "" } - expect(test_instance.sanitized_path(env)).to eql("#{Gem.bindir};#{RbConfig::CONFIG["bindir"]}") + expect(test_instance.default_paths(env)).to eql("#{Gem.bindir};#{RbConfig::CONFIG["bindir"]}") end it "prepends to an existing path" do env = { "PATH" => '%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\\' } - expect(test_instance.sanitized_path(env)).to eql("#{Gem.bindir};#{RbConfig::CONFIG["bindir"]};%SystemRoot%\\system32;%SystemRoot%;%SystemRoot%\\System32\\Wbem;%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\") + expect(test_instance.default_paths(env)).to eql("#{Gem.bindir};#{RbConfig::CONFIG["bindir"]};%SystemRoot%\\system32;%SystemRoot%;%SystemRoot%\\System32\\Wbem;%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\") end end end |