Skip to content

Commit 63824bd

Browse files
committed
Add spec for Tempfile.create
1 parent a351e1a commit 63824bd

2 files changed

Lines changed: 114 additions & 6 deletions

File tree

library/tempfile/callback_spec.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.

library/tempfile/create_spec.rb

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
require_relative '../../spec_helper'
2+
require 'tempfile'
3+
4+
describe "Tempfile.create" do
5+
after :each do
6+
if @tempfile
7+
@tempfile.close
8+
File.unlink(@tempfile.path) if File.file?(@tempfile.path)
9+
end
10+
end
11+
12+
it "returns a new, open File instance placed in tmpdir" do
13+
@tempfile = Tempfile.create
14+
# Unlike Tempfile.open this returns a true File,
15+
# but `.should be_an_instance_of(File)` would be true either way.
16+
@tempfile.instance_of?(File).should be_true
17+
18+
@tempfile.should_not.closed?
19+
File.file?(@tempfile.path).should be_true
20+
21+
@tempfile.path.should.start_with?(Dir.tmpdir)
22+
@tempfile.path.should_not == "#{Dir.tmpdir}/"
23+
end
24+
25+
it "returns a writable, readable file" do
26+
@tempfile = Tempfile.create
27+
File.writable?(@tempfile.path).should be_true
28+
@tempfile << "Test!\nMore test!"
29+
@tempfile.seek(0)
30+
@tempfile.gets.should == "Test!\n"
31+
end
32+
33+
it "returns a private file" do
34+
@tempfile = Tempfile.create
35+
@tempfile.stat.mode.to_s(8).should.end_with?("600")
36+
end
37+
38+
context "when called with a block" do
39+
it "returns the value of the block" do
40+
value = Tempfile.create do |tempfile|
41+
tempfile << "Test!"
42+
@tempfile = tempfile
43+
"return"
44+
end
45+
value.should == "return"
46+
end
47+
48+
it "closes and unlinks file after block execution" do
49+
Tempfile.create do |tempfile|
50+
@tempfile = tempfile
51+
@tempfile.should_not.closed?
52+
File.file?(@tempfile.path).should be_true
53+
end
54+
55+
@tempfile.should.closed?
56+
File.file?(@tempfile.path).should be_false
57+
end
58+
end
59+
60+
context "when called with a single positional argument" do
61+
it "uses a String as a prefix for the filename" do
62+
@tempfile = Tempfile.create("create_spec")
63+
@tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec")
64+
@tempfile.path.should_not == "#{Dir.tmpdir}/create_spec"
65+
end
66+
67+
it "uses an array of two Strings as a prefix and suffix for the filename" do
68+
@tempfile = Tempfile.create(["create_spec", ".temp"])
69+
@tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec")
70+
@tempfile.path.should.end_with?(".temp")
71+
end
72+
73+
it "ignores more than two elements in the array" do
74+
@tempfile = Tempfile.create(["create_spec", ".temp", :".txt"])
75+
@tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec")
76+
@tempfile.path.should.end_with?(".temp")
77+
end
78+
79+
it "raises ArgumentError if passed something else than a String or an array of Strings" do
80+
-> { Tempfile.create(:temp) }.should raise_error(ArgumentError, "unexpected prefix: :temp")
81+
-> { Tempfile.create(["create_spec", :temp]) }.should raise_error(ArgumentError, "unexpected suffix: :temp")
82+
end
83+
end
84+
85+
context "when called with a second positional argument" do
86+
it "uses it as a directory for the tempfile" do
87+
@tempfile = Tempfile.create("create_spec", "./")
88+
@tempfile.path.should.start_with?("./create_spec")
89+
end
90+
91+
it "raises TypeError if argument can not be converted to a String" do
92+
-> { Tempfile.create("create_spec", :temp) }.should raise_error(TypeError, "no implicit conversion of Symbol into String")
93+
end
94+
end
95+
96+
context "when called with a mode option" do
97+
it "ORs it with the default mode, forcing it to be writable" do
98+
@tempfile = Tempfile.create(mode: File::RDONLY)
99+
File.writable?(@tempfile.path).should be_true
100+
end
101+
102+
it "raises NoMethodError if passed something else than an Integer" do
103+
-> { Tempfile.create(mode: "wb") }.should raise_error(NoMethodError, "undefined method '|' for an instance of String")
104+
end
105+
end
106+
107+
context "when called with other options" do
108+
it "passes them along to File.open" do
109+
@tempfile = Tempfile.create(encoding: "IBM037:IBM037", binmode: true)
110+
@tempfile.external_encoding.should == Encoding.find("IBM037")
111+
@tempfile.binmode?.should be_true
112+
end
113+
end
114+
end

0 commit comments

Comments
 (0)