summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2012-11-16 16:28:49 -0800
committerBryan McLellan <btm@opscode.com>2012-11-27 12:08:01 -0800
commit30dfde19219a58ab94f4f76963a78a792bce7ef2 (patch)
tree8035a0963525701afc17838ad011e8d97b391781
parent40eb061dac38dad114054b77e235c84b3606cfef (diff)
downloadchef-30dfde19219a58ab94f4f76963a78a792bce7ef2.tar.gz
fixes binmode issues on windows
-rw-r--r--chef/spec/functional/resource/cookbook_file_spec.rb8
-rw-r--r--chef/spec/functional/resource/remote_file_spec.rb12
-rw-r--r--chef/spec/spec_helper.rb1
-rw-r--r--chef/spec/support/chef_helpers.rb13
-rw-r--r--chef/spec/support/shared/functional/file_resource.rb15
-rw-r--r--chef/spec/support/shared/functional/securable_resource.rb4
-rw-r--r--chef/spec/unit/provider/file_spec.rb6
7 files changed, 50 insertions, 9 deletions
diff --git a/chef/spec/functional/resource/cookbook_file_spec.rb b/chef/spec/functional/resource/cookbook_file_spec.rb
index adc1f7eef8..684dd85a12 100644
--- a/chef/spec/functional/resource/cookbook_file_spec.rb
+++ b/chef/spec/functional/resource/cookbook_file_spec.rb
@@ -24,7 +24,13 @@ describe Chef::Resource::CookbookFile do
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
diff --git a/chef/spec/functional/resource/remote_file_spec.rb b/chef/spec/functional/resource/remote_file_spec.rb
index 2ebeecd3d1..998255e720 100644
--- a/chef/spec/functional/resource/remote_file_spec.rb
+++ b/chef/spec/functional/resource/remote_file_spec.rb
@@ -24,7 +24,13 @@ describe Chef::Resource::RemoteFile do
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
@@ -45,7 +51,9 @@ describe Chef::Resource::RemoteFile do
@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
diff --git a/chef/spec/spec_helper.rb b/chef/spec/spec_helper.rb
index 32bddeb415..3611297aa6 100644
--- a/chef/spec/spec_helper.rb
+++ b/chef/spec/spec_helper.rb
@@ -71,6 +71,7 @@ RSpec.configure do |config|
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
diff --git a/chef/spec/support/chef_helpers.rb b/chef/spec/support/chef_helpers.rb
index 77f5fc7669..77cbe5b5cb 100644
--- a/chef/spec/support/chef_helpers.rb
+++ b/chef/spec/support/chef_helpers.rb
@@ -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
diff --git a/chef/spec/support/shared/functional/file_resource.rb b/chef/spec/support/shared/functional/file_resource.rb
index 631a5ed742..c85512b5c6 100644
--- a/chef/spec/support/shared/functional/file_resource.rb
+++ b/chef/spec/support/shared/functional/file_resource.rb
@@ -78,6 +78,13 @@ shared_examples_for "a file resource" do
# 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
it "creates the file when the :create action is run" do
resource.run_action(:create)
@@ -86,12 +93,12 @@ shared_examples_for "a file resource" do
it "creates the file with the correct content when the :create action is run" do
resource.run_action(:create)
- IO.read(path).should == expected_content
+ binread(path).should == expected_content
end
it "creates the file with the correct content when the :create_if_missing action is run" do
resource.run_action(:create_if_missing)
- IO.read(path).should == expected_content
+ binread(path).should == expected_content
end
it "deletes the file when the :delete action is run" do
@@ -112,7 +119,7 @@ shared_examples_for "a file resource" do
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!!!" }
@expected_mtime = File.stat(path).mtime
@expected_checksum = sha256_checksum(path)
end
@@ -136,7 +143,7 @@ shared_examples_for "a file resource" do
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 }
@expected_mtime = File.stat(path).mtime
@expected_atime = File.stat(path).atime
@expected_checksum = sha256_checksum(path)
diff --git a/chef/spec/support/shared/functional/securable_resource.rb b/chef/spec/support/shared/functional/securable_resource.rb
index 2eeb16c784..2e5797bc8f 100644
--- a/chef/spec/support/shared/functional/securable_resource.rb
+++ b/chef/spec/support/shared/functional/securable_resource.rb
@@ -29,12 +29,16 @@ shared_context "setup correct permissions" do
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
diff --git a/chef/spec/unit/provider/file_spec.rb b/chef/spec/unit/provider/file_spec.rb
index 9f5ad3a8f8..23352a5124 100644
--- a/chef/spec/unit/provider/file_spec.rb
+++ b/chef/spec/unit/provider/file_spec.rb
@@ -342,10 +342,12 @@ describe Chef::Provider::File do
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)
@@ -355,7 +357,7 @@ describe Chef::Provider::File do
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