chef-config 12.4.0.rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/README.md +4 -0
- data/Rakefile +55 -0
- data/lib/chef-config.rb +20 -0
- data/lib/chef-config/config.rb +744 -0
- data/lib/chef-config/exceptions.rb +26 -0
- data/lib/chef-config/logger.rb +62 -0
- data/lib/chef-config/path_helper.rb +233 -0
- data/lib/chef-config/version.rb +25 -0
- data/lib/chef-config/windows.rb +29 -0
- data/spec/spec_helper.rb +75 -0
- data/spec/unit/config_spec.rb +581 -0
- data/spec/unit/path_helper_spec.rb +291 -0
- metadata +143 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright (c) 2015 Chef Software, Inc.
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
require 'chef-config/windows'
|
19
|
+
require 'chef-config/logger'
|
20
|
+
|
21
|
+
module ChefConfig
|
22
|
+
|
23
|
+
class InvalidPath < StandardError
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright (c) 2015 Chef Software, Inc.
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
|
19
|
+
module ChefConfig
|
20
|
+
|
21
|
+
# Implements enough of Logger's API that we can use it in place of a real
|
22
|
+
# logger for `ChefConfig.logger`
|
23
|
+
class NullLogger
|
24
|
+
|
25
|
+
def <<(_msg)
|
26
|
+
end
|
27
|
+
|
28
|
+
def add(_severity, _message = nil, _progname = nil)
|
29
|
+
end
|
30
|
+
|
31
|
+
def debug(_progname = nil, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def info(_progname = nil, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def warn(_progname = nil, &block)
|
38
|
+
end
|
39
|
+
|
40
|
+
def deprecation(_progname = nil, &block)
|
41
|
+
end
|
42
|
+
|
43
|
+
def error(_progname = nil, &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def fatal(_progname = nil, &block)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
@logger = NullLogger.new
|
52
|
+
|
53
|
+
def self.logger=(new_logger)
|
54
|
+
@logger = new_logger
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.logger
|
58
|
+
@logger
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
@@ -0,0 +1,233 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Bryan McLellan <btm@loftninjas.org>
|
3
|
+
# Copyright:: Copyright (c) 2014 Chef Software, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'chef-config/windows'
|
20
|
+
require 'chef-config/logger'
|
21
|
+
require 'chef-config/exceptions'
|
22
|
+
|
23
|
+
module ChefConfig
|
24
|
+
class PathHelper
|
25
|
+
# Maximum characters in a standard Windows path (260 including drive letter and NUL)
|
26
|
+
WIN_MAX_PATH = 259
|
27
|
+
|
28
|
+
def self.dirname(path)
|
29
|
+
if ChefConfig.windows?
|
30
|
+
# Find the first slash, not counting trailing slashes
|
31
|
+
end_slash = path.size
|
32
|
+
loop do
|
33
|
+
slash = path.rindex(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]/, end_slash - 1)
|
34
|
+
if !slash
|
35
|
+
return end_slash == path.size ? '.' : path_separator
|
36
|
+
elsif slash == end_slash - 1
|
37
|
+
end_slash = slash
|
38
|
+
else
|
39
|
+
return path[0..slash-1]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
else
|
43
|
+
::File.dirname(path)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
BACKSLASH = '\\'.freeze
|
48
|
+
|
49
|
+
def self.path_separator
|
50
|
+
if ChefConfig.windows?
|
51
|
+
File::ALT_SEPARATOR || BACKSLASH
|
52
|
+
else
|
53
|
+
File::SEPARATOR
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.join(*args)
|
58
|
+
path_separator_regex = Regexp.escape(File::SEPARATOR)
|
59
|
+
unless path_separator == File::SEPARATOR
|
60
|
+
path_separator_regex << Regexp.escape(path_separator)
|
61
|
+
end
|
62
|
+
|
63
|
+
trailing_slashes = /[#{path_separator_regex}]+$/
|
64
|
+
leading_slashes = /^[#{path_separator_regex}]+/
|
65
|
+
|
66
|
+
args.flatten.inject() do |joined_path, component|
|
67
|
+
joined_path = joined_path.sub(trailing_slashes, '')
|
68
|
+
component = component.sub(leading_slashes, '')
|
69
|
+
joined_path += "#{path_separator}#{component}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.validate_path(path)
|
74
|
+
if ChefConfig.windows?
|
75
|
+
unless printable?(path)
|
76
|
+
msg = "Path '#{path}' contains non-printable characters. Check that backslashes are escaped with another backslash (e.g. C:\\\\Windows) in double-quoted strings."
|
77
|
+
ChefConfig.logger.error(msg)
|
78
|
+
raise ChefConfig::InvalidPath, msg
|
79
|
+
end
|
80
|
+
|
81
|
+
if windows_max_length_exceeded?(path)
|
82
|
+
ChefConfig.logger.debug("Path '#{path}' is longer than #{WIN_MAX_PATH}, prefixing with'\\\\?\\'")
|
83
|
+
path.insert(0, "\\\\?\\")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
path
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.windows_max_length_exceeded?(path)
|
91
|
+
# Check to see if paths without the \\?\ prefix are over the maximum allowed length for the Windows API
|
92
|
+
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
|
93
|
+
unless path =~ /^\\\\?\\/
|
94
|
+
if path.length > WIN_MAX_PATH
|
95
|
+
return true
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
false
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.printable?(string)
|
103
|
+
# returns true if string is free of non-printable characters (escape sequences)
|
104
|
+
# this returns false for whitespace escape sequences as well, e.g. \n\t
|
105
|
+
if string =~ /[^[:print:]]/
|
106
|
+
false
|
107
|
+
else
|
108
|
+
true
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Produces a comparable path.
|
113
|
+
def self.canonical_path(path, add_prefix=true)
|
114
|
+
# First remove extra separators and resolve any relative paths
|
115
|
+
abs_path = File.absolute_path(path)
|
116
|
+
|
117
|
+
if ChefConfig.windows?
|
118
|
+
# Add the \\?\ API prefix on Windows unless add_prefix is false
|
119
|
+
# Downcase on Windows where paths are still case-insensitive
|
120
|
+
abs_path.gsub!(::File::SEPARATOR, path_separator)
|
121
|
+
if add_prefix && abs_path !~ /^\\\\?\\/
|
122
|
+
abs_path.insert(0, "\\\\?\\")
|
123
|
+
end
|
124
|
+
|
125
|
+
abs_path.downcase!
|
126
|
+
end
|
127
|
+
|
128
|
+
abs_path
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.cleanpath(path)
|
132
|
+
path = Pathname.new(path).cleanpath.to_s
|
133
|
+
# ensure all forward slashes are backslashes
|
134
|
+
if ChefConfig.windows?
|
135
|
+
path = path.gsub(File::SEPARATOR, path_separator)
|
136
|
+
end
|
137
|
+
path
|
138
|
+
end
|
139
|
+
|
140
|
+
def self.paths_eql?(path1, path2)
|
141
|
+
canonical_path(path1) == canonical_path(path2)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Paths which may contain glob-reserved characters need
|
145
|
+
# to be escaped before globbing can be done.
|
146
|
+
# http://stackoverflow.com/questions/14127343
|
147
|
+
def self.escape_glob(*parts)
|
148
|
+
path = cleanpath(join(*parts))
|
149
|
+
path.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\"+x }
|
150
|
+
end
|
151
|
+
|
152
|
+
def self.relative_path_from(from, to)
|
153
|
+
Pathname.new(cleanpath(to)).relative_path_from(Pathname.new(cleanpath(from)))
|
154
|
+
end
|
155
|
+
|
156
|
+
# Retrieves the "home directory" of the current user while trying to ascertain the existence
|
157
|
+
# of said directory. The path returned uses / for all separators (the ruby standard format).
|
158
|
+
# If the home directory doesn't exist or an error is otherwise encountered, nil is returned.
|
159
|
+
#
|
160
|
+
# If a set of path elements is provided, they are appended as-is to the home path if the
|
161
|
+
# homepath exists.
|
162
|
+
#
|
163
|
+
# If an optional block is provided, the joined path is passed to that block if the home path is
|
164
|
+
# valid and the result of the block is returned instead.
|
165
|
+
#
|
166
|
+
# Home-path discovery is performed once. If a path is discovered, that value is memoized so
|
167
|
+
# that subsequent calls to home_dir don't bounce around.
|
168
|
+
#
|
169
|
+
# See self.all_homes.
|
170
|
+
def self.home(*args)
|
171
|
+
@@home_dir ||= self.all_homes { |p| break p }
|
172
|
+
if @@home_dir
|
173
|
+
path = File.join(@@home_dir, *args)
|
174
|
+
block_given? ? (yield path) : path
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# See self.home. This method performs a similar operation except that it yields all the different
|
179
|
+
# possible values of 'HOME' that one could have on this platform. Hence, on windows, if
|
180
|
+
# HOMEDRIVE\HOMEPATH and USERPROFILE are different, the provided block will be called twice.
|
181
|
+
# This method goes out and checks the existence of each location at the time of the call.
|
182
|
+
#
|
183
|
+
# The return is a list of all the returned values from each block invocation or a list of paths
|
184
|
+
# if no block is provided.
|
185
|
+
def self.all_homes(*args)
|
186
|
+
paths = []
|
187
|
+
if ChefConfig.windows?
|
188
|
+
# By default, Ruby uses the the following environment variables to determine Dir.home:
|
189
|
+
# HOME
|
190
|
+
# HOMEDRIVE HOMEPATH
|
191
|
+
# USERPROFILE
|
192
|
+
# Ruby only checks to see if the variable is specified - not if the directory actually exists.
|
193
|
+
# On Windows, HOMEDRIVE HOMEPATH can point to a different location (such as an unavailable network mounted drive)
|
194
|
+
# while USERPROFILE points to the location where the user application settings and profile are stored. HOME
|
195
|
+
# is not defined as an environment variable (usually). If the home path actually uses UNC, then the prefix is
|
196
|
+
# HOMESHARE instead of HOMEDRIVE.
|
197
|
+
#
|
198
|
+
# We instead walk down the following and only include paths that actually exist.
|
199
|
+
# HOME
|
200
|
+
# HOMEDRIVE HOMEPATH
|
201
|
+
# HOMESHARE HOMEPATH
|
202
|
+
# USERPROFILE
|
203
|
+
|
204
|
+
paths << ENV['HOME']
|
205
|
+
paths << ENV['HOMEDRIVE'] + ENV['HOMEPATH'] if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
|
206
|
+
paths << ENV['HOMESHARE'] + ENV['HOMEPATH'] if ENV['HOMESHARE'] && ENV['HOMEPATH']
|
207
|
+
paths << ENV['USERPROFILE']
|
208
|
+
end
|
209
|
+
paths << Dir.home if ENV['HOME']
|
210
|
+
|
211
|
+
# Depending on what environment variables we're using, the slashes can go in any which way.
|
212
|
+
# Just change them all to / to keep things consistent.
|
213
|
+
# Note: Maybe this is a bad idea on some unixy systems where \ might be a valid character depending on
|
214
|
+
# the particular brand of kool-aid you consume. This code assumes that \ and / are both
|
215
|
+
# path separators on any system being used.
|
216
|
+
paths = paths.map { |home_path| home_path.gsub(path_separator, ::File::SEPARATOR) if home_path }
|
217
|
+
|
218
|
+
# Filter out duplicate paths and paths that don't exist.
|
219
|
+
valid_paths = paths.select { |home_path| home_path && Dir.exists?(home_path) }
|
220
|
+
valid_paths = valid_paths.uniq
|
221
|
+
|
222
|
+
# Join all optional path elements at the end.
|
223
|
+
# If a block is provided, invoke it - otherwise just return what we've got.
|
224
|
+
joined_paths = valid_paths.map { |home_path| File.join(home_path, *args) }
|
225
|
+
if block_given?
|
226
|
+
joined_paths.each { |p| yield p }
|
227
|
+
else
|
228
|
+
joined_paths
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Copyright:: Copyright (c) 2010-2015 Chef Software, Inc.
|
2
|
+
# License:: Apache License, Version 2.0
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
16
|
+
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
17
|
+
# NOTE: This file is generated by running `rake version` in the top level of
|
18
|
+
# this repo. Do not edit this manually. Edit the VERSION file and run the rake
|
19
|
+
# task instead.
|
20
|
+
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
21
|
+
|
22
|
+
module ChefConfig
|
23
|
+
VERSION = '12.4.0.rc.0'
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#
|
2
|
+
# Copyright:: Copyright (c) 2015 Chef Software, Inc.
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
module ChefConfig
|
19
|
+
|
20
|
+
def self.windows?
|
21
|
+
if RUBY_PLATFORM =~ /mswin|mingw|windows/
|
22
|
+
true
|
23
|
+
else
|
24
|
+
false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'chef-config/windows'
|
2
|
+
|
3
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
4
|
+
RSpec.configure do |config|
|
5
|
+
# rspec-expectations config goes here. You can use an alternate
|
6
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
7
|
+
# assertions if you prefer.
|
8
|
+
config.expect_with :rspec do |expectations|
|
9
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
10
|
+
# and `failure_message` of custom matchers include text for helper methods
|
11
|
+
# defined using `chain`, e.g.:
|
12
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
13
|
+
# # => "be bigger than 2 and smaller than 4"
|
14
|
+
# ...rather than:
|
15
|
+
# # => "be bigger than 2"
|
16
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
17
|
+
end
|
18
|
+
|
19
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
20
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
21
|
+
config.mock_with :rspec do |mocks|
|
22
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
23
|
+
# a real object. This is generally recommended, and will default to
|
24
|
+
# `true` in RSpec 4.
|
25
|
+
mocks.verify_partial_doubles = true
|
26
|
+
end
|
27
|
+
|
28
|
+
# These two settings work together to allow you to limit a spec run
|
29
|
+
# to individual examples or groups you care about by tagging them with
|
30
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
31
|
+
# get run.
|
32
|
+
config.filter_run :focus
|
33
|
+
config.run_all_when_everything_filtered = true
|
34
|
+
|
35
|
+
config.filter_run_excluding :windows_only => true unless ChefConfig.windows?
|
36
|
+
config.filter_run_excluding :unix_only => true if ChefConfig.windows?
|
37
|
+
|
38
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
39
|
+
# recommended. For more details, see:
|
40
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
41
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
42
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
43
|
+
config.disable_monkey_patching!
|
44
|
+
|
45
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
46
|
+
# be too noisy due to issues in dependencies.
|
47
|
+
config.warnings = true
|
48
|
+
|
49
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
50
|
+
# file, and it's useful to allow more verbose output when running an
|
51
|
+
# individual spec file.
|
52
|
+
if config.files_to_run.one?
|
53
|
+
# Use the documentation formatter for detailed output,
|
54
|
+
# unless a formatter has already been configured
|
55
|
+
# (e.g. via a command-line flag).
|
56
|
+
config.default_formatter = 'doc'
|
57
|
+
end
|
58
|
+
|
59
|
+
# Print the 10 slowest examples and example groups at the
|
60
|
+
# end of the spec run, to help surface which specs are running
|
61
|
+
# particularly slow.
|
62
|
+
# config.profile_examples = 10
|
63
|
+
|
64
|
+
# Run specs in random order to surface order dependencies. If you find an
|
65
|
+
# order dependency and want to debug it, you can fix the order by providing
|
66
|
+
# the seed, which is printed after each run.
|
67
|
+
# --seed 1234
|
68
|
+
config.order = :random
|
69
|
+
|
70
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
71
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
72
|
+
# test failures related to randomization by passing the same `--seed` value
|
73
|
+
# as the one that triggered the failure.
|
74
|
+
Kernel.srand config.seed
|
75
|
+
end
|