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.
@@ -0,0 +1,291 @@
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/path_helper'
20
+ require 'spec_helper'
21
+
22
+ RSpec.describe ChefConfig::PathHelper do
23
+
24
+ let(:path_helper) { described_class }
25
+
26
+ shared_examples_for "common_functionality" do
27
+ describe "join" do
28
+
29
+ it "joins starting with '' resolve to absolute paths" do
30
+ expect(path_helper.join('', 'a', 'b')).to eq("#{path_helper.path_separator}a#{path_helper.path_separator}b")
31
+ end
32
+
33
+ it "joins ending with '' add a / to the end" do
34
+ expect(path_helper.join('a', 'b', '')).to eq("a#{path_helper.path_separator}b#{path_helper.path_separator}")
35
+ end
36
+
37
+ end
38
+
39
+ describe "dirname" do
40
+ it "dirname('abc') is '.'" do
41
+ expect(path_helper.dirname('abc')).to eq('.')
42
+ end
43
+ it "dirname('/') is '/'" do
44
+ expect(path_helper.dirname(path_helper.path_separator)).to eq(path_helper.path_separator)
45
+ end
46
+ it "dirname('a/b/c') is 'a/b'" do
47
+ expect(path_helper.dirname(path_helper.join('a', 'b', 'c'))).to eq(path_helper.join('a', 'b'))
48
+ end
49
+ it "dirname('a/b/c/') is 'a/b'" do
50
+ expect(path_helper.dirname(path_helper.join('a', 'b', 'c', ''))).to eq(path_helper.join('a', 'b'))
51
+ end
52
+ it "dirname('/a/b/c') is '/a/b'" do
53
+ expect(path_helper.dirname(path_helper.join('', 'a', 'b', 'c'))).to eq(path_helper.join('', 'a', 'b'))
54
+ end
55
+ end
56
+ end
57
+
58
+ context "on windows" do
59
+
60
+ before(:each) do
61
+ allow(ChefConfig).to receive(:windows?).and_return(true)
62
+ end
63
+
64
+ include_examples("common_functionality")
65
+
66
+ it "path_separator is \\" do
67
+ expect(path_helper.path_separator).to eq('\\')
68
+ end
69
+
70
+ describe "platform-specific #join behavior" do
71
+
72
+ it "joins components on Windows when some end with unix separators" do
73
+ expect(path_helper.join('C:\\foo/', "bar", "baz")).to eq('C:\\foo\\bar\\baz')
74
+ end
75
+
76
+ it "joins components when some end with separators" do
77
+ expected = path_helper.cleanpath("/foo/bar/baz")
78
+ expected = "C:#{expected}"
79
+ expect(path_helper.join('C:\\foo\\', "bar", "baz")).to eq(expected)
80
+ end
81
+
82
+ it "joins components when some end and start with separators" do
83
+ expected = path_helper.cleanpath("/foo/bar/baz")
84
+ expected = "C:#{expected}"
85
+ expect(path_helper.join('C:\\foo\\', "bar/", "/baz")).to eq(expected)
86
+ end
87
+
88
+ it "joins components that don't end in separators" do
89
+ expected = path_helper.cleanpath("/foo/bar/baz")
90
+ expected = "C:#{expected}"
91
+ expect(path_helper.join('C:\\foo', "bar", "baz")).to eq(expected)
92
+ end
93
+
94
+ end
95
+
96
+
97
+ it "cleanpath changes slashes into backslashes and leaves backslashes alone" do
98
+ expect(path_helper.cleanpath('/a/b\\c/d/')).to eq('\\a\\b\\c\\d')
99
+ end
100
+
101
+ it "cleanpath does not remove leading double backslash" do
102
+ expect(path_helper.cleanpath('\\\\a/b\\c/d/')).to eq('\\\\a\\b\\c\\d')
103
+ end
104
+
105
+ end
106
+
107
+ context "on unix" do
108
+
109
+ before(:each) do
110
+ allow(ChefConfig).to receive(:windows?).and_return(false)
111
+ end
112
+
113
+ include_examples("common_functionality")
114
+
115
+ it "path_separator is /" do
116
+ expect(path_helper.path_separator).to eq('/')
117
+ end
118
+
119
+ it "cleanpath removes extra slashes alone" do
120
+ expect(path_helper.cleanpath('/a///b/c/d/')).to eq('/a/b/c/d')
121
+ end
122
+
123
+ describe "platform-specific #join behavior" do
124
+
125
+ it "joins components when some end with separators" do
126
+ expected = path_helper.cleanpath("/foo/bar/baz")
127
+ expect(path_helper.join("/foo/", "bar", "baz")).to eq(expected)
128
+ end
129
+
130
+ it "joins components when some end and start with separators" do
131
+ expected = path_helper.cleanpath("/foo/bar/baz")
132
+ expect(path_helper.join("/foo/", "bar/", "/baz")).to eq(expected)
133
+ end
134
+
135
+ it "joins components that don't end in separators" do
136
+ expected = path_helper.cleanpath("/foo/bar/baz")
137
+ expect(path_helper.join("/foo", "bar", "baz")).to eq(expected)
138
+ end
139
+
140
+ end
141
+
142
+ end
143
+
144
+ describe "validate_path" do
145
+ context "on windows" do
146
+ before(:each) do
147
+ # pass by default
148
+ allow(ChefConfig).to receive(:windows?).and_return(true)
149
+ allow(path_helper).to receive(:printable?).and_return(true)
150
+ allow(path_helper).to receive(:windows_max_length_exceeded?).and_return(false)
151
+ end
152
+
153
+ it "returns the path if the path passes the tests" do
154
+ expect(path_helper.validate_path("C:\\ThisIsRigged")).to eql("C:\\ThisIsRigged")
155
+ end
156
+
157
+ it "does not raise an error if everything looks great" do
158
+ expect { path_helper.validate_path("C:\\cool path\\dude.exe") }.not_to raise_error
159
+ end
160
+
161
+ it "raises an error if the path has invalid characters" do
162
+ allow(path_helper).to receive(:printable?).and_return(false)
163
+ expect { path_helper.validate_path("Newline!\n") }.to raise_error(ChefConfig::InvalidPath)
164
+ end
165
+
166
+ it "Adds the \\\\?\\ prefix if the path exceeds MAX_LENGTH and does not have it" do
167
+ long_path = "C:\\" + "a" * 250 + "\\" + "b" * 250
168
+ prefixed_long_path = "\\\\?\\" + long_path
169
+ allow(path_helper).to receive(:windows_max_length_exceeded?).and_return(true)
170
+ expect(path_helper.validate_path(long_path)).to eql(prefixed_long_path)
171
+ end
172
+ end
173
+ end
174
+
175
+ describe "windows_max_length_exceeded?" do
176
+ it "returns true if the path is too long (259 + NUL) for the API" do
177
+ expect(path_helper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 6)).to be_truthy
178
+ end
179
+
180
+ it "returns false if the path is not too long (259 + NUL) for the standard API" do
181
+ expect(path_helper.windows_max_length_exceeded?("C:\\" + "a" * 250 + "\\" + "b" * 5)).to be_falsey
182
+ end
183
+
184
+ it "returns false if the path is over 259 characters but uses the \\\\?\\ prefix" do
185
+ expect(path_helper.windows_max_length_exceeded?("\\\\?\\C:\\" + "a" * 250 + "\\" + "b" * 250)).to be_falsey
186
+ end
187
+ end
188
+
189
+ describe "printable?" do
190
+ it "returns true if the string contains no non-printable characters" do
191
+ expect(path_helper.printable?("C:\\Program Files (x86)\\Microsoft Office\\Files.lst")).to be_truthy
192
+ end
193
+
194
+ it "returns true when given 'abc' in unicode" do
195
+ expect(path_helper.printable?("\u0061\u0062\u0063")).to be_truthy
196
+ end
197
+
198
+ it "returns true when given japanese unicode" do
199
+ expect(path_helper.printable?("\uff86\uff87\uff88")).to be_truthy
200
+ end
201
+
202
+ it "returns false if the string contains a non-printable character" do
203
+ expect(path_helper.printable?("\my files\work\notes.txt")).to be_falsey
204
+ end
205
+
206
+ # This isn't necessarily a requirement, but here to be explicit about functionality.
207
+ it "returns false if the string contains a newline or tab" do
208
+ expect(path_helper.printable?("\tThere's no way,\n\t *no* way,\n\t that you came from my loins.\n")).to be_falsey
209
+ end
210
+ end
211
+
212
+ describe "canonical_path" do
213
+ context "on windows", :windows_only do
214
+ it "returns an absolute path with backslashes instead of slashes" do
215
+ expect(path_helper.canonical_path("\\\\?\\C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
216
+ end
217
+
218
+ it "adds the \\\\?\\ prefix if it is missing" do
219
+ expect(path_helper.canonical_path("C:/windows/win.ini")).to eq("\\\\?\\c:\\windows\\win.ini")
220
+ end
221
+
222
+ it "returns a lowercase path" do
223
+ expect(path_helper.canonical_path("\\\\?\\C:\\CASE\\INSENSITIVE")).to eq("\\\\?\\c:\\case\\insensitive")
224
+ end
225
+ end
226
+
227
+ context "not on windows", :unix_only do
228
+ it "returns a canonical path" do
229
+ expect(path_helper.canonical_path("/etc//apache.d/sites-enabled/../sites-available/default")).to eq("/etc/apache.d/sites-available/default")
230
+ end
231
+ end
232
+ end
233
+
234
+ describe "paths_eql?" do
235
+ it "returns true if the paths are the same" do
236
+ allow(path_helper).to receive(:canonical_path).with("bandit").and_return("c:/bandit/bandit")
237
+ allow(path_helper).to receive(:canonical_path).with("../bandit/bandit").and_return("c:/bandit/bandit")
238
+ expect(path_helper.paths_eql?("bandit", "../bandit/bandit")).to be_truthy
239
+ end
240
+
241
+ it "returns false if the paths are different" do
242
+ allow(path_helper).to receive(:canonical_path).with("bandit").and_return("c:/Bo/Bandit")
243
+ allow(path_helper).to receive(:canonical_path).with("../bandit/bandit").and_return("c:/bandit/bandit")
244
+ expect(path_helper.paths_eql?("bandit", "../bandit/bandit")).to be_falsey
245
+ end
246
+ end
247
+
248
+ describe "escape_glob" do
249
+ it "escapes characters reserved by glob" do
250
+ path = "C:\\this\\*path\\[needs]\\escaping?"
251
+ escaped_path = "C:\\\\this\\\\\\*path\\\\\\[needs\\]\\\\escaping\\?"
252
+ expect(path_helper.escape_glob(path)).to eq(escaped_path)
253
+ end
254
+
255
+ context "when given more than one argument" do
256
+ it "joins, cleanpaths, and escapes characters reserved by glob" do
257
+ args = ["this/*path", "[needs]", "escaping?"]
258
+ escaped_path = if ChefConfig.windows?
259
+ "this\\\\\\*path\\\\\\[needs\\]\\\\escaping\\?"
260
+ else
261
+ "this/\\*path/\\[needs\\]/escaping\\?"
262
+ end
263
+ expect(path_helper).to receive(:join).with(*args).and_call_original
264
+ expect(path_helper).to receive(:cleanpath).and_call_original
265
+ expect(path_helper.escape_glob(*args)).to eq(escaped_path)
266
+ end
267
+ end
268
+ end
269
+
270
+ describe "all_homes" do
271
+ before do
272
+ stub_const('ENV', env)
273
+ allow(ChefConfig).to receive(:windows?).and_return(is_windows)
274
+ end
275
+
276
+ context "on windows" do
277
+ let (:is_windows) { true }
278
+ end
279
+
280
+ context "on unix" do
281
+ let (:is_windows) { false }
282
+
283
+ context "when HOME is not set" do
284
+ let (:env) { {} }
285
+ it "returns an empty array" do
286
+ expect(path_helper.all_homes).to eq([])
287
+ end
288
+ end
289
+ end
290
+ end
291
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-config
3
+ version: !ruby/object:Gem::Version
4
+ version: 12.4.0.rc.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Jacob
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mixlib-shellout
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mixlib-config
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-core
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-expectations
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-mocks
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.2'
97
+ description:
98
+ email:
99
+ - adam@chef.io
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files:
103
+ - README.md
104
+ - LICENSE
105
+ files:
106
+ - LICENSE
107
+ - README.md
108
+ - Rakefile
109
+ - lib/chef-config.rb
110
+ - lib/chef-config/config.rb
111
+ - lib/chef-config/exceptions.rb
112
+ - lib/chef-config/logger.rb
113
+ - lib/chef-config/path_helper.rb
114
+ - lib/chef-config/version.rb
115
+ - lib/chef-config/windows.rb
116
+ - spec/spec_helper.rb
117
+ - spec/unit/config_spec.rb
118
+ - spec/unit/path_helper_spec.rb
119
+ homepage: http://www.chef.io
120
+ licenses:
121
+ - Apache-2.0
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">"
135
+ - !ruby/object:Gem::Version
136
+ version: 1.3.1
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.4.5
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Chef's default configuration and config loading
143
+ test_files: []