summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@getchef.com>2015-05-13 14:14:40 -0700
committerdanielsdeleo <dan@getchef.com>2015-05-20 15:13:56 -0700
commitdf152741e880aafc186cf2285ca81540b4fb5992 (patch)
tree2af5593dd28d48bcece811df690e9627d925122c
parente6062caaefe82ff351690af15e882f02efc0f91b (diff)
downloadchef-df152741e880aafc186cf2285ca81540b4fb5992.tar.gz
Use PathHelper from chef-config
-rw-r--r--lib/chef/util/path_helper.rb207
-rw-r--r--spec/unit/util/path_helper_spec.rb255
2 files changed, 3 insertions, 459 deletions
diff --git a/lib/chef/util/path_helper.rb b/lib/chef/util/path_helper.rb
index 66c2e3f19f..10527f8906 100644
--- a/lib/chef/util/path_helper.rb
+++ b/lib/chef/util/path_helper.rb
@@ -16,212 +16,11 @@
# limitations under the License.
#
+require 'chef-config/path_helper'
+
class Chef
class Util
- class PathHelper
- # Maximum characters in a standard Windows path (260 including drive letter and NUL)
- WIN_MAX_PATH = 259
-
- def self.dirname(path)
- if Chef::Platform.windows?
- # Find the first slash, not counting trailing slashes
- end_slash = path.size
- loop do
- slash = path.rindex(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]/, end_slash - 1)
- if !slash
- return end_slash == path.size ? '.' : path_separator
- elsif slash == end_slash - 1
- end_slash = slash
- else
- return path[0..slash-1]
- end
- end
- else
- ::File.dirname(path)
- end
- end
-
- BACKSLASH = '\\'.freeze
-
- def self.path_separator
- if Chef::Platform.windows?
- File::ALT_SEPARATOR || BACKSLASH
- else
- File::SEPARATOR
- end
- end
-
- def self.join(*args)
- args.flatten.inject do |joined_path, component|
- # Joined path ends with /
- joined_path = joined_path.sub(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+$/, '')
- component = component.sub(/^[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+/, '')
- joined_path += "#{path_separator}#{component}"
- end
- end
-
- def self.validate_path(path)
- if Chef::Platform.windows?
- unless printable?(path)
- msg = "Path '#{path}' contains non-printable characters. Check that backslashes are escaped with another backslash (e.g. C:\\\\Windows) in double-quoted strings."
- Chef::Log.error(msg)
- raise Chef::Exceptions::ValidationFailed, msg
- end
-
- if windows_max_length_exceeded?(path)
- Chef::Log.debug("Path '#{path}' is longer than #{WIN_MAX_PATH}, prefixing with'\\\\?\\'")
- path.insert(0, "\\\\?\\")
- end
- end
-
- path
- end
-
- def self.windows_max_length_exceeded?(path)
- # Check to see if paths without the \\?\ prefix are over the maximum allowed length for the Windows API
- # http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
- unless path =~ /^\\\\?\\/
- if path.length > WIN_MAX_PATH
- return true
- end
- end
-
- false
- end
-
- def self.printable?(string)
- # returns true if string is free of non-printable characters (escape sequences)
- # this returns false for whitespace escape sequences as well, e.g. \n\t
- if string =~ /[^[:print:]]/
- false
- else
- true
- end
- end
-
- # Produces a comparable path.
- def self.canonical_path(path, add_prefix=true)
- # First remove extra separators and resolve any relative paths
- abs_path = File.absolute_path(path)
-
- if Chef::Platform.windows?
- # Add the \\?\ API prefix on Windows unless add_prefix is false
- # Downcase on Windows where paths are still case-insensitive
- abs_path.gsub!(::File::SEPARATOR, path_separator)
- if add_prefix && abs_path !~ /^\\\\?\\/
- abs_path.insert(0, "\\\\?\\")
- end
-
- abs_path.downcase!
- end
-
- abs_path
- end
-
- def self.cleanpath(path)
- path = Pathname.new(path).cleanpath.to_s
- # ensure all forward slashes are backslashes
- if Chef::Platform.windows?
- path = path.gsub(File::SEPARATOR, path_separator)
- end
- path
- end
-
- def self.paths_eql?(path1, path2)
- canonical_path(path1) == canonical_path(path2)
- end
-
- # Paths which may contain glob-reserved characters need
- # to be escaped before globbing can be done.
- # http://stackoverflow.com/questions/14127343
- def self.escape_glob(*parts)
- path = cleanpath(join(*parts))
- path.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\"+x }
- end
-
- def self.relative_path_from(from, to)
- pathname = Pathname.new(Chef::Util::PathHelper.cleanpath(to)).relative_path_from(Pathname.new(Chef::Util::PathHelper.cleanpath(from)))
- end
-
- # Retrieves the "home directory" of the current user while trying to ascertain the existence
- # of said directory. The path returned uses / for all separators (the ruby standard format).
- # If the home directory doesn't exist or an error is otherwise encountered, nil is returned.
- #
- # If a set of path elements is provided, they are appended as-is to the home path if the
- # homepath exists.
- #
- # If an optional block is provided, the joined path is passed to that block if the home path is
- # valid and the result of the block is returned instead.
- #
- # Home-path discovery is performed once. If a path is discovered, that value is memoized so
- # that subsequent calls to home_dir don't bounce around.
- #
- # See self.all_homes.
- def self.home(*args)
- @@home_dir ||= self.all_homes { |p| break p }
- if @@home_dir
- path = File.join(@@home_dir, *args)
- block_given? ? (yield path) : path
- end
- end
-
- # See self.home. This method performs a similar operation except that it yields all the different
- # possible values of 'HOME' that one could have on this platform. Hence, on windows, if
- # HOMEDRIVE\HOMEPATH and USERPROFILE are different, the provided block will be called twice.
- # This method goes out and checks the existence of each location at the time of the call.
- #
- # The return is a list of all the returned values from each block invocation or a list of paths
- # if no block is provided.
- def self.all_homes(*args)
- paths = []
- if Chef::Platform.windows?
- # By default, Ruby uses the the following environment variables to determine Dir.home:
- # HOME
- # HOMEDRIVE HOMEPATH
- # USERPROFILE
- # Ruby only checks to see if the variable is specified - not if the directory actually exists.
- # On Windows, HOMEDRIVE HOMEPATH can point to a different location (such as an unavailable network mounted drive)
- # while USERPROFILE points to the location where the user application settings and profile are stored. HOME
- # is not defined as an environment variable (usually). If the home path actually uses UNC, then the prefix is
- # HOMESHARE instead of HOMEDRIVE.
- #
- # We instead walk down the following and only include paths that actually exist.
- # HOME
- # HOMEDRIVE HOMEPATH
- # HOMESHARE HOMEPATH
- # USERPROFILE
-
- paths << ENV['HOME']
- paths << ENV['HOMEDRIVE'] + ENV['HOMEPATH'] if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
- paths << ENV['HOMESHARE'] + ENV['HOMEPATH'] if ENV['HOMESHARE'] && ENV['HOMEPATH']
- paths << ENV['USERPROFILE']
- end
- paths << Dir.home if ENV['HOME']
-
- # Depending on what environment variables we're using, the slashes can go in any which way.
- # Just change them all to / to keep things consistent.
- # Note: Maybe this is a bad idea on some unixy systems where \ might be a valid character depending on
- # the particular brand of kool-aid you consume. This code assumes that \ and / are both
- # path separators on any system being used.
- paths = paths.map { |home_path| home_path.gsub(path_separator, ::File::SEPARATOR) if home_path }
-
- # Filter out duplicate paths and paths that don't exist.
- valid_paths = paths.select { |home_path| home_path && Dir.exists?(home_path) }
- valid_paths = valid_paths.uniq
-
- # Join all optional path elements at the end.
- # If a block is provided, invoke it - otherwise just return what we've got.
- joined_paths = valid_paths.map { |home_path| File.join(home_path, *args) }
- if block_given?
- joined_paths.each { |p| yield p }
- else
- joined_paths
- end
- end
- end
+ PathHelper = ChefConfig::PathHelper
end
end
-# Break a require loop when require chef/util/path_helper
-require 'chef/platform'
-require 'chef/exceptions'
diff --git a/spec/unit/util/path_helper_spec.rb b/spec/unit/util/path_helper_spec.rb
deleted file mode 100644
index 23db9587a6..0000000000
--- a/spec/unit/util/path_helper_spec.rb
+++ /dev/null
@@ -1,255 +0,0 @@
-#
-# Author:: Bryan McLellan <btm@loftninjas.org>
-# Copyright:: Copyright (c) 2014 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/util/path_helper'
-require 'spec_helper'
-
-describe Chef::Util::PathHelper do
- PathHelper = Chef::Util::PathHelper
-
- [ false, true ].each do |is_windows|
- context "on #{is_windows ? "windows" : "unix"}" do
- before(:each) do
- allow(Chef::Platform).to receive(:windows?).and_return(is_windows)
- end
-
- describe "join" do
- it "joins components when some end with separators" do
- expected = PathHelper.cleanpath("/foo/bar/baz")
- expected = "C:#{expected}" if is_windows
- expect(PathHelper.join(is_windows ? 'C:\\foo\\' : "/foo/", "bar", "baz")).to eq(expected)
- end
-
- it "joins components when some end and start with separators" do
- expected = PathHelper.cleanpath("/foo/bar/baz")
- expected = "C:#{expected}" if is_windows
- expect(PathHelper.join(is_windows ? 'C:\\foo\\' : "/foo/", "bar/", "/baz")).to eq(expected)
- end
-
- it "joins components that don't end in separators" do
- expected = PathHelper.cleanpath("/foo/bar/baz")
- expected = "C:#{expected}" if is_windows
- expect(PathHelper.join(is_windows ? 'C:\\foo' : "/foo", "bar", "baz")).to eq(expected)
- end
-
- it "joins starting with '' resolve to absolute paths" do
- expect(PathHelper.join('', 'a', 'b')).to eq("#{PathHelper.path_separator}a#{PathHelper.path_separator}b")
- end
-
- it "joins ending with '' add a / to the end" do
- expect(PathHelper.join('a', 'b', '')).to eq("a#{PathHelper.path_separator}b#{PathHelper.path_separator}")
- end
-
- if is_windows
- it "joins components on Windows when some end with unix separators" do
- expect(PathHelper.join('C:\\foo/', "bar", "baz")).to eq('C:\\foo\\bar\\baz')
- end
- end
- end
-
- if is_windows
- it "path_separator is \\" do
- expect(PathHelper.path_separator).to eq('\\')
- end
- else
- it "path_separator is /" do
- expect(PathHelper.path_separator).to eq('/')
- end
- end
-
- if is_windows
- it "cleanpath changes slashes into backslashes and leaves backslashes alone" do
- expect(PathHelper.cleanpath('/a/b\\c/d/')).to eq('\\a\\b\\c\\d')
- end
- it "cleanpath does not remove leading double backslash" do
- expect(PathHelper.cleanpath('\\\\a/b\\c/d/')).to eq('\\\\a\\b\\c\\d')
- end
- else
- it "cleanpath removes extra slashes alone" do
- expect(PathHelper.cleanpath('/a///b/c/d/')).to eq('/a/b/c/d')
- end
- end
-
- describe "dirname" do
- it "dirname('abc') is '.'" do
- expect(PathHelper.dirname('abc')).to eq('.')
- end
- it "dirname('/') is '/'" do
- expect(PathHelper.dirname(PathHelper.path_separator)).to eq(PathHelper.path_separator)
- end
- it "dirname('a/b/c') is 'a/b'" do
- expect(PathHelper.dirname(PathHelper.join('a', 'b', 'c'))).to eq(PathHelper.join('a', 'b'))
- end
- it "dirname('a/b/c/') is 'a/b'" do
- expect(PathHelper.dirname(PathHelper.join('a', 'b', 'c', ''))).to eq(PathHelper.join('a', 'b'))
- end
- it "dirname('/a/b/c') is '/a/b'" do
- expect(PathHelper.dirname(PathHelper.join('', 'a', 'b', 'c'))).to eq(PathHelper.join('', 'a', 'b'))
- end
- end
- end
- end
-
- describe "validate_path" do
- context "on windows" do
- before(:each) do
- # pass by default
- allow(Chef::Platform).to receive(:windows?).and_return(true)
- allow(PathHelper).to receive(:printable?).and_return(true)
- allow(PathHelper).to receive(:windows_max_length_exceeded?).and_return(false)
- end
-
- it "returns the path if the path passes the tests" do
- expect(PathHelper.validate_path("C:\\ThisIsRigged")).to eql("C:\\ThisIsRigged")
- end
-
- it "does not raise an error if everything looks great" do
- expect { PathHelper.validate_path("C:\\cool path\\dude.exe") }.not_to raise_error
- end
-
- it "raises an error if the path has invalid characters" do
- allow(PathHelper).to receive(:printable?).and_return(false)
- expect { PathHelper.validate_path("Newline!\n") }.to raise_error(Chef::Exceptions::ValidationFailed)
- end
-
- it "Adds the \\\\?\\ prefix if the path exceeds MAX_LENGTH and does not have it" do
- long_path = "C:\\" + "a" * 250 + "\\" + "b" * 250
- prefixed_long_path = "\\\\?\\" + long_path
- allow(PathHelper).to receive(:windows_max_length_exceeded?).and_return(true)
- expect(PathHelper.validate_path(long_path)).to eql(prefixed_long_path)
- end
- end
- end
-
- describe "windows_max_length_exceeded?" do
- it "returns true if the path is too long (259 + NUL) for the API" do
- expect(PathHelper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 6)).to be_truthy
- end
-
- it "returns false if the path is not too long (259 + NUL) for the standard API" do
- expect(PathHelper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 5)).to be_falsey
- end
-
- it "returns false if the path is over 259 characters but uses the \\\\?\\ prefix" do
- expect(PathHelper.windows_max_length_exceeded?("\\\\?\\C:\\" + "a" * 250 + "\\" + "b" * 250)).to be_falsey
- end
- end
-
- describe "printable?" do
- it "returns true if the string contains no non-printable characters" do
- expect(PathHelper.printable?("C:\\Program Files (x86)\\Microsoft Office\\Files.lst")).to be_truthy
- end
-
- it "returns true when given 'abc' in unicode" do
- expect(PathHelper.printable?("\u0061\u0062\u0063")).to be_truthy
- end
-
- it "returns true when given japanese unicode" do
- expect(PathHelper.printable?("\uff86\uff87\uff88")).to be_truthy
- end
-
- it "returns false if the string contains a non-printable character" do
- expect(PathHelper.printable?("\my files\work\notes.txt")).to be_falsey
- end
-
- # This isn't necessarily a requirement, but here to be explicit about functionality.
- it "returns false if the string contains a newline or tab" do
- expect(PathHelper.printable?("\tThere's no way,\n\t *no* way,\n\t that you came from my loins.\n")).to be_falsey
- end
- end
-
- describe "canonical_path" do
- context "on windows", :windows_only do
- it "returns an absolute path with backslashes instead of slashes" do
- expect(PathHelper.canonical_path("\\\\?\\C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
- end
-
- it "adds the \\\\?\\ prefix if it is missing" do
- expect(PathHelper.canonical_path("C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
- end
-
- it "returns a lowercase path" do
- expect(PathHelper.canonical_path("\\\\?\\C:\\CASE\\INSENSITIVE")).to eq("\\\\?\\c:\\case\\insensitive")
- end
- end
-
- context "not on windows", :unix_only do
- it "returns a canonical path" do
- expect(PathHelper.canonical_path("/etc//apache.d/sites-enabled/../sites-available/default")).to eq("/etc/apache.d/sites-available/default")
- end
- end
- end
-
- describe "paths_eql?" do
- it "returns true if the paths are the same" do
- allow(PathHelper).to receive(:canonical_path).with("bandit").and_return("c:/bandit/bandit")
- allow(PathHelper).to receive(:canonical_path).with("../bandit/bandit").and_return("c:/bandit/bandit")
- expect(PathHelper.paths_eql?("bandit", "../bandit/bandit")).to be_truthy
- end
-
- it "returns false if the paths are different" do
- allow(PathHelper).to receive(:canonical_path).with("bandit").and_return("c:/Bo/Bandit")
- allow(PathHelper).to receive(:canonical_path).with("../bandit/bandit").and_return("c:/bandit/bandit")
- expect(PathHelper.paths_eql?("bandit", "../bandit/bandit")).to be_falsey
- end
- end
-
- describe "escape_glob" do
- it "escapes characters reserved by glob" do
- path = "C:\\this\\*path\\[needs]\\escaping?"
- escaped_path = "C:\\\\this\\\\\\*path\\\\\\[needs\\]\\\\escaping\\?"
- expect(PathHelper.escape_glob(path)).to eq(escaped_path)
- end
-
- context "when given more than one argument" do
- it "joins, cleanpaths, and escapes characters reserved by glob" do
- args = ["this/*path", "[needs]", "escaping?"]
- escaped_path = if windows?
- "this\\\\\\*path\\\\\\[needs\\]\\\\escaping\\?"
- else
- "this/\\*path/\\[needs\\]/escaping\\?"
- end
- expect(PathHelper).to receive(:join).with(*args).and_call_original
- expect(PathHelper).to receive(:cleanpath).and_call_original
- expect(PathHelper.escape_glob(*args)).to eq(escaped_path)
- end
- end
- end
-
- describe "all_homes" do
- before do
- stub_const('ENV', env)
- allow(Chef::Platform).to receive(:windows?).and_return(is_windows)
- end
-
- context "on windows" do
- let (:is_windows) { true }
- end
-
- context "on unix" do
- let (:is_windows) { false }
-
- context "when HOME is not set" do
- let (:env) { {} }
- it "returns an empty array" do
- expect(PathHelper.all_homes).to eq([])
- end
- end
- end
- end
-end