summaryrefslogtreecommitdiff
path: root/spec/support
diff options
context:
space:
mode:
authordanielsdeleo <dan@opscode.com>2014-02-03 10:42:43 -0800
committerdanielsdeleo <dan@opscode.com>2014-02-03 11:51:56 -0800
commit4312ebe6e4d651f248d215519acecf93a19e2d8a (patch)
tree4390d78e7aeaeb9b2e7b248307a53ce356cd591c /spec/support
parent3ec8fe22b2ce7a64fead70eb32f0e0ec8d378dd1 (diff)
downloadchef-4312ebe6e4d651f248d215519acecf93a19e2d8a.tar.gz
Use custom tempfile to avoid ruby 1.8-related test failures
Tempfile in ruby 1.8.7 calls `File.exist?()`, which we set a stub on. For reasons that are not clear, this does not consistently fail. In any case, using a custom temporary file implementation resolves the issue.
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/shared/unit/provider/file.rb27
1 files changed, 26 insertions, 1 deletions
diff --git a/spec/support/shared/unit/provider/file.rb b/spec/support/shared/unit/provider/file.rb
index c3d678816a..75c7379e92 100644
--- a/spec/support/shared/unit/provider/file.rb
+++ b/spec/support/shared/unit/provider/file.rb
@@ -106,10 +106,31 @@ def setup_missing_enclosing_directory
File.stub(:directory?).with(enclosing_directory).and_return(false)
end
+# A File subclass that we use as a replacement for Tempfile. Some versions of
+# Tempfile call `File.exist?()` internally which will cause test failures if
+# `File.exist?()` has been stubbed. This implementation doesn't actually handle
+# selecting the tempfile's name, that is left for the caller to decide.
+class BasicTempfile < ::File
+
+ def self.new(path)
+ super(path, File::RDWR|File::CREAT|File::EXCL, 0600)
+ end
+
+ def unlink
+ self.class.unlink(path)
+ end
+
+end
+
shared_examples_for Chef::Provider::File do
+ let(:tempfile_path) do
+ slug = "rspec-shared-file-provider-#{rand(1 << 128)}"
+ File.join(Dir.tmpdir, slug)
+ end
+
let!(:tempfile) do
- Tempfile.new("rspec-shared-file-provider")
+ BasicTempfile.new(tempfile_path)
end
before(:each) do
@@ -118,6 +139,10 @@ shared_examples_for Chef::Provider::File do
File.stub(:exists?).with(tempfile.path).and_call_original
end
+ after do
+ File.unlink(tempfile_path) rescue nil
+ end
+
it "should return a #{described_class}" do
provider.should be_a_kind_of(described_class)
end