Skip to content

Commit b64ca97

Browse files
committed
Merge branch 'CHEF-1157'
2 parents 2a5f39b + b1917f9 commit b64ca97

5 files changed

Lines changed: 59 additions & 54 deletions

File tree

chef/lib/chef.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Chef
3434
end
3535

3636
# Adds a Dir.glob to Ruby 1.8.5, for compat
37-
if RUBY_VERSION < "1.8.6"
37+
if RUBY_VERSION < "1.8.6" || RUBY_PLATFORM =~ /mswin|mingw32|windows/
3838
class Dir
3939
class << self
4040
alias_method :glob_, :glob
@@ -44,6 +44,7 @@ def glob(pattern, flags=0)
4444
pattern.is_a? Array and !pattern.empty?
4545
) or pattern.is_a? String
4646
)
47+
pattern.gsub!(/\\/, "/") if RUBY_PLATFORM =~ /mswin|mingw32|windows/
4748
[pattern].flatten.inject([]) { |r, p| r + glob_(p, flags) }
4849
end
4950
alias_method :[], :glob

chef/lib/chef/application.rb

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
9-
#
9+
#
1010
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
11+
#
1212
# Unless required by applicable law or agreed to in writing, software
1313
# distributed under the License is distributed on an "AS IS" BASIS,
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,38 +22,40 @@
2222

2323
class Chef::Application
2424
include Mixlib::CLI
25-
26-
def initialize
25+
26+
def initialize
2727
super
28-
28+
2929
trap("TERM") do
3030
Chef::Application.fatal!("SIGTERM received, stopping", 1)
3131
end
3232

3333
trap("INT") do
3434
Chef::Application.fatal!("SIGINT received, stopping", 2)
3535
end
36-
37-
trap("HUP") do
38-
Chef::Log.info("SIGHUP received, reconfiguring")
39-
reconfigure
36+
37+
unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
38+
trap("HUP") do
39+
Chef::Log.info("SIGHUP received, reconfiguring")
40+
reconfigure
41+
end
4042
end
41-
43+
4244
at_exit do
43-
# tear down the logger
45+
# tear down the logger
4446
end
4547

4648
# Always switch to a readable directory. Keeps subsequent Dir.chdir() {}
4749
# from failing due to permissions when launched as a less privileged user.
4850
Dir.chdir("/")
4951
end
50-
52+
5153
# Reconfigure the application. You'll want to override and super this method.
5254
def reconfigure
5355
configure_chef
5456
configure_logging
5557
end
56-
58+
5759
# Get this party started
5860
def run
5961
reconfigure
@@ -64,25 +66,25 @@ def run
6466
# Parse the configuration file
6567
def configure_chef
6668
parse_options
67-
69+
6870
Chef::Config.from_file(config[:config_file]) if !config[:config_file].nil? && File.exists?(config[:config_file]) && File.readable?(config[:config_file])
6971
Chef::Config.merge!(config)
7072
end
71-
73+
7274
# Initialize and configure the logger
7375
def configure_logging
7476
Chef::Log.init(Chef::Config[:log_location])
7577
Chef::Log.level = Chef::Config[:log_level]
7678
end
77-
79+
7880
# Called prior to starting the application, by the run method
7981
def setup_application
80-
raise Chef::Exceptions::Application, "#{self.to_s}: you must override setup_application"
82+
raise Chef::Exceptions::Application, "#{self.to_s}: you must override setup_application"
8183
end
82-
84+
8385
# Actually run the application
8486
def run_application
85-
raise Chef::Exceptions::Application, "#{self.to_s}: you must override run_application"
87+
raise Chef::Exceptions::Application, "#{self.to_s}: you must override run_application"
8688
end
8789

8890
class << self

chef/lib/chef/file_cache.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
9-
#
9+
#
1010
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
11+
#
1212
# Unless required by applicable law or agreed to in writing, software
1313
# distributed under the License is distributed on an "AS IS" BASIS,
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -26,7 +26,7 @@ class FileCache
2626
class << self
2727
include Chef::Mixin::ParamsValidate
2828
include Chef::Mixin::CreatePath
29-
29+
3030
# Write a file to the File Cache.
3131
#
3232
# === Parameters
@@ -47,7 +47,7 @@ def store(path, contents)
4747
:contents => { :kind_of => String },
4848
}
4949
)
50-
50+
5151
file_path_array = File.split(path)
5252
file_name = file_path_array.pop
5353
cache_path = create_cache_path(File.join(file_path_array))
@@ -56,7 +56,7 @@ def store(path, contents)
5656
io.close
5757
true
5858
end
59-
59+
6060
# Move a file in to the cache. Useful with the REST raw file output.
6161
#
6262
# === Parameteres
@@ -73,19 +73,19 @@ def move_to(file, path)
7373
:path => { :kind_of => String },
7474
}
7575
)
76-
76+
7777
file_path_array = File.split(path)
7878
file_name = file_path_array.pop
7979
if File.exists?(file) && File.writable?(file)
8080
FileUtils.mv(
81-
file,
81+
file,
8282
File.join(create_cache_path(File.join(file_path_array), true), file_name)
8383
)
8484
else
8585
raise RuntimeError, "Cannot move #{file} to #{path}!"
8686
end
8787
end
88-
88+
8989
# Read a file from the File Cache
9090
#
9191
# === Parameters
@@ -116,7 +116,7 @@ def load(path, read=true)
116116
cache_path
117117
end
118118
end
119-
119+
120120
# Delete a file from the File Cache
121121
#
122122
# === Parameters
@@ -140,7 +140,7 @@ def delete(path)
140140
end
141141
true
142142
end
143-
143+
144144
# List all the files in the Cache
145145
#
146146
# === Returns
@@ -149,19 +149,19 @@ def list()
149149
keys = Array.new
150150
Dir[File.join(Chef::Config[:file_cache_path], '**', '*')].each do |f|
151151
if File.file?(f)
152-
path = f.match("^#{Chef::Config[:file_cache_path]}\/(.+)")[1]
152+
path = f.match("^#{Dir[Chef::Config[:file_cache_path]].first}\/(.+)")[1]
153153
keys << path
154154
end
155155
end
156156
keys
157157
end
158-
158+
159159
# Whether or not this file exists in the Cache
160160
#
161161
# === Parameters
162-
# path:: The path to the file you want to check - is relative
162+
# path:: The path to the file you want to check - is relative
163163
# to Chef::Config[:file_cache_path]
164-
#
164+
#
165165
# === Returns
166166
# True:: If the file exists
167167
# False:: If it does not
@@ -181,25 +181,25 @@ def has_key?(path)
181181
false
182182
end
183183
end
184-
184+
185185
# Create a full path to a given file in the cache. By default,
186186
# also creates the path if it does not exist.
187187
#
188188
# === Parameters
189189
# path:: The path to create, relative to Chef::Config[:file_cache_path]
190190
# create_if_missing:: True by default - whether to create the path if it does not exist
191-
#
191+
#
192192
# === Returns
193193
# String:: The fully expanded path
194194
def create_cache_path(path, create_if_missing=true)
195195
cache_dir = File.expand_path(File.join(Chef::Config[:file_cache_path], path))
196196
if create_if_missing
197-
create_path(cache_dir)
197+
create_path(cache_dir)
198198
else
199199
cache_dir
200200
end
201201
end
202-
202+
203203
end
204204
end
205205
end

chef/lib/chef/provider/file.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def octal_mode(mode)
4646

4747
def load_current_resource
4848
@current_resource = Chef::Resource::File.new(@new_resource.name)
49+
@new_resource.path.gsub!(/\\/, "/") # for Windows
4950
@current_resource.path(@new_resource.path)
5051
if ::File.exist?(@current_resource.path) && ::File.readable?(@current_resource.path)
5152
cstats = ::File.stat(@current_resource.path)

chef/spec/unit/file_cache_spec.rb

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
88
# You may obtain a copy of the License at
9-
#
9+
#
1010
# http://www.apache.org/licenses/LICENSE-2.0
11-
#
11+
#
1212
# Unless required by applicable law or agreed to in writing, software
1313
# distributed under the License is distributed on an "AS IS" BASIS,
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -26,7 +26,7 @@
2626
@io = mock("IO", { :print => true, :close => true })
2727
File.stub!(:open).and_return(@io)
2828
end
29-
29+
3030
it "should create the directories leading up to bang" do
3131
File.stub!(:directory?).and_return(false)
3232
Dir.should_receive(:mkdir).with("/tmp").and_return(true)
@@ -35,17 +35,17 @@
3535
Dir.should_not_receive(:mkdir).with("/tmp/foo/whiz/bang").and_return(true)
3636
Chef::FileCache.store("whiz/bang", "I found a poop")
3737
end
38-
38+
3939
it "should create a file at /tmp/foo/whiz/bang" do
4040
File.should_receive(:open).with("/tmp/foo/whiz/bang", "w").and_return(@io)
4141
Chef::FileCache.store("whiz/bang", "I found a poop")
4242
end
43-
43+
4444
it "should print the contents to the file" do
4545
@io.should_receive(:print).with("I found a poop")
4646
Chef::FileCache.store("whiz/bang", "I found a poop")
4747
end
48-
48+
4949
it "should close the file" do
5050
@io.should_receive(:close)
5151
Chef::FileCache.store("whiz/bang", "I found a poop")
@@ -61,18 +61,18 @@
6161
File.stub!(:exists?).and_return(true)
6262
File.stub!(:read).and_return("I found a poop")
6363
end
64-
64+
6565
it "should find the full path to whiz/bang" do
6666
File.should_receive(:read).with("/tmp/foo/whiz/bang").and_return(true)
6767
Chef::FileCache.load('whiz/bang')
6868
end
69-
69+
7070
it "should raise a Chef::Exceptions::FileNotFound if the file doesn't exist" do
7171
File.stub!(:exists?).and_return(false)
7272
lambda { Chef::FileCache.load('whiz/bang') }.should raise_error(Chef::Exceptions::FileNotFound)
7373
end
7474
end
75-
75+
7676
describe Chef::FileCache, "delete method" do
7777
before(:each) do
7878
Chef::Config[:file_cache_path] = "/tmp/foo"
@@ -81,21 +81,22 @@
8181
File.stub!(:exists?).and_return(true)
8282
File.stub!(:unlink).and_return(true)
8383
end
84-
84+
8585
it "should unlink the full path to whiz/bang" do
8686
File.should_receive(:unlink).with("/tmp/foo/whiz/bang").and_return(true)
8787
Chef::FileCache.delete("whiz/bang")
8888
end
89-
89+
9090
end
9191

9292
describe Chef::FileCache, "list method" do
9393
before(:each) do
9494
Chef::Config[:file_cache_path] = "/tmp/foo"
95-
Dir.stub!(:[]).and_return(["/tmp/foo/whiz/bang", "/tmp/foo/snappy/patter"])
95+
Dir.stub!(:[]).with(File.join(Chef::Config[:file_cache_path], '**', '*')).and_return(["/tmp/foo/whiz/bang", "/tmp/foo/snappy/patter"])
96+
Dir.stub!(:[]).with(Chef::Config[:file_cache_path]).and_return(["/tmp/foo"])
9697
File.stub!(:file?).and_return(true)
9798
end
98-
99+
99100
it "should return the relative paths" do
100101
Chef::FileCache.list.should eql([ "whiz/bang", "snappy/patter" ])
101102
end
@@ -105,17 +106,17 @@
105106
before(:each) do
106107
Chef::Config[:file_cache_path] = "/tmp/foo"
107108
end
108-
109+
109110
it "should check the full path to the file" do
110111
File.should_receive(:exists?).with("/tmp/foo/whiz/bang")
111112
Chef::FileCache.has_key?("whiz/bang")
112113
end
113-
114+
114115
it "should return true if the file exists" do
115116
File.stub!(:exists?).and_return(true)
116117
Chef::FileCache.has_key?("whiz/bang").should eql(true)
117118
end
118-
119+
119120
it "should return false if the file does not exist" do
120121
File.stub!(:exists?).and_return(false)
121122
Chef::FileCache.has_key?("whiz/bang").should eql(false)

0 commit comments

Comments
 (0)