Skip to content

Commit

Permalink
fixes binmode issues on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeleo committed Nov 19, 2012
1 parent 9b39d2c commit d068ae1
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 10 deletions.
8 changes: 7 additions & 1 deletion spec/functional/resource/cookbook_file_spec.rb
Expand Up @@ -24,7 +24,13 @@
let(:file_base) { 'cookbook_file_spec' }
let(:source) { 'java.response' }
let(:cookbook_name) { 'java' }
let(:expected_content) { IO.read(File.join(CHEF_SPEC_DATA, 'cookbooks', 'java', 'files', 'default', 'java.response')) }
let(:expected_content) do
content = File.open(File.join(CHEF_SPEC_DATA, 'cookbooks', 'java', 'files', 'default', 'java.response'), "rb") do |f|
f.read
end
content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding)
content
end

def create_resource
# set up cookbook collection for this run to use, based on our
Expand Down
12 changes: 10 additions & 2 deletions spec/functional/resource/remote_file_spec.rb
Expand Up @@ -24,7 +24,13 @@

let(:file_base) { "remote_file_spec" }
let(:source) { 'http://localhost:9000/nyan_cat.png' }
let(:expected_content) { IO.read(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png')) }
let(:expected_content) do
content = File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png'), "rb") do |f|
f.read
end
content.force_encoding(Encoding::BINARY) if content.respond_to?(:force_encoding)
content
end

def create_resource
node = Chef::Node.new
Expand All @@ -45,7 +51,9 @@ def create_resource
@api = TinyServer::API.instance
@api.clear
@api.get("/nyan_cat.png", 200) {
IO.read(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png'))
File.open(File.join(CHEF_SPEC_DATA, 'remote_file', 'nyan_cat.png'), "rb") do |f|
f.read
end
}
end

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -72,6 +72,7 @@ module Shell
config.filter_run_excluding :ruby_19_only => true unless ruby_19?
config.filter_run_excluding :requires_root => true unless ENV['USER'] == 'root'
config.filter_run_excluding :requires_unprivileged_user => true if ENV['USER'] == 'root'
config.filter_run_excluding :uses_diff => true unless has_diff?

config.run_all_when_everything_filtered = true
config.treat_symbols_as_metadata_keys_with_true_values = true
Expand Down
13 changes: 13 additions & 0 deletions spec/support/chef_helpers.rb
Expand Up @@ -50,3 +50,16 @@ def make_tmpname(prefix_suffix, n)
path << "-#{n}" if n
path << suffix
end

# NOTE:
# This is a temporary fix to get tests passing on systems that have no `diff`
# until we can replace shelling out to `diff` with ruby diff-lcs
def has_diff?
begin
diff_cmd = Mixlib::ShellOut.new("diff -v")
diff_cmd.run_command
true
rescue Errno::ENOENT
false
end
end
15 changes: 11 additions & 4 deletions spec/support/shared/functional/file_resource.rb
Expand Up @@ -146,6 +146,13 @@
# note the stripping of the drive letter from the tmpdir on windows
let(:backup_glob) { File.join(CHEF_SPEC_BACKUP_PATH, Dir.tmpdir.sub(/^([A-Za-z]:)/, ""), "#{file_base}*") }

def binread(file)
content = File.open(file, "rb") do |f|
f.read
end
content.force_encoding(Encoding::BINARY) if "".respond_to?(:force_encoding)
end

context "when the target file does not exist" do
before do
# Assert starting state is expected
Expand All @@ -162,7 +169,7 @@
end

it "creates the file with the correct content when the :create action is run" do
IO.read(path).should == expected_content
binread(path).should == expected_content
end

it "is marked as updated by last action" do
Expand All @@ -176,7 +183,7 @@
end

it "creates the file with the correct content" do
IO.read(path).should == expected_content
binread(path).should == expected_content
end

it "is marked as updated by last action" do
Expand Down Expand Up @@ -211,7 +218,7 @@ def denied_acl(sid, expected_perms)

context "when the target file has the wrong content" do
before(:each) do
File.open(path, "w") { |f| f.print "This is so wrong!!!" }
File.open(path, "wb") { |f| f.print "This is so wrong!!!" }
now = Time.now.to_i
File.utime(now - 9000, now - 9000, path)

Expand All @@ -238,7 +245,7 @@ def denied_acl(sid, expected_perms)

context "when the target file has the correct content" do
before(:each) do
File.open(path, "w") { |f| f.print expected_content }
File.open(path, "wb") { |f| f.print expected_content }
now = Time.now.to_i
File.utime(now - 9000, now - 9000, path)

Expand Down
4 changes: 4 additions & 0 deletions spec/support/shared/functional/securable_resource.rb
Expand Up @@ -29,12 +29,16 @@
before :each do
File.chown(Etc.getpwnam('nobody').uid, 1337, path)
File.chmod(0776, path)
now = Time.now.to_i
File.utime(now - 9000, now - 9000, path)
end
end

context "without root", :requires_unprivileged_user do
before :each do
File.chmod(0776, path)
now = Time.now.to_i
File.utime(now - 9000, now - 9000, path)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/chef_fs/diff_spec.rb
Expand Up @@ -28,7 +28,7 @@ def remove_date(diff)
diff.gsub(/([+-]{3}.*)\t.*/, '\1 DATE')
end

describe 'diff' do
describe 'diff', :uses_diff => true do
include FileSystemSupport

context 'with two filesystems with all types of difference' do
Expand Down
6 changes: 4 additions & 2 deletions spec/unit/provider/file_spec.rb
Expand Up @@ -342,10 +342,12 @@
end

it "should call action create if the does not file exist" do
@resource.path("/tmp/non_existant_file")
@resource.path("/tmp/example-dir/non_existant_file")
@provider = Chef::Provider::File.new(@resource, @run_context)
@provider.should_receive(:diff_current_from_content).and_return("")
::File.stub!(:exists?).with(@resource.path).and_return(false)
::File.stub!(:directory?).with("/tmp/example-dir/non_existant_file").and_return(false)
::File.stub!(:directory?).with("/tmp/example-dir").and_return(true)
@provider.stub!(:update_new_file_state)
io = StringIO.new
File.should_receive(:open).with(@provider.new_resource.path, "w+").and_yield(io)
Expand All @@ -355,7 +357,7 @@
end
end

describe "when a diff is requested" do
describe "when a diff is requested", :uses_diff => true do

before(:each) do
@original_config = Chef::Config.hash_dup
Expand Down

0 comments on commit d068ae1

Please sign in to comment.