Skip to content

Commit 105cd57

Browse files
committed
Improve tests after review
1 parent 6efeebe commit 105cd57

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

library/tempfile/create_spec.rb

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,47 @@
2222
@tempfile.path.should_not == "#{Dir.tmpdir}/"
2323
end
2424

25-
it "returns a writable, readable file" do
25+
it "returns file in w+ mode" do
2626
@tempfile = Tempfile.create
27-
File.writable?(@tempfile.path).should be_true
2827
@tempfile << "Test!\nMore test!"
29-
@tempfile.seek(0)
30-
@tempfile.gets.should == "Test!\n"
28+
@tempfile.rewind
29+
@tempfile.read.should == "Test!\nMore test!"
30+
31+
# Not "a+" mode, which would write at the end of the file.
32+
@tempfile.rewind
33+
@tempfile.print "Trust"
34+
@tempfile.rewind
35+
@tempfile.read.should == "Trust\nMore test!"
3136
end
3237

3338
platform_is_not :windows do
34-
it "returns a private file" do
39+
it "returns a private, readable and writable file" do
3540
@tempfile = Tempfile.create
36-
@tempfile.stat.mode.to_s(8).should.end_with?("600")
41+
stat = @tempfile.stat
42+
stat.should.readable?
43+
stat.should.writable?
44+
stat.should_not.executable?
45+
stat.should_not.world_readable?
46+
stat.should_not.world_writable?
3747
end
3848
end
3949

4050
platform_is :windows do
41-
it "returns a public file" do
51+
it "returns a public, readable and writable file" do
4252
@tempfile = Tempfile.create
43-
@tempfile.stat.mode.to_s(8).should.end_with?("666")
53+
stat = @tempfile.stat
54+
stat.should.readable?
55+
stat.should.writable?
56+
stat.should_not.executable?
57+
stat.should.world_readable?
58+
stat.should.world_writable?
4459
end
4560
end
4661

4762
context "when called with a block" do
4863
it "returns the value of the block" do
4964
value = Tempfile.create do |tempfile|
5065
tempfile << "Test!"
51-
@tempfile = tempfile
5266
"return"
5367
end
5468
value.should == "return"
@@ -58,11 +72,11 @@
5872
Tempfile.create do |tempfile|
5973
@tempfile = tempfile
6074
@tempfile.should_not.closed?
61-
File.file?(@tempfile.path).should be_true
75+
File.exist?(@tempfile.path).should be_true
6276
end
6377

6478
@tempfile.should.closed?
65-
File.file?(@tempfile.path).should be_false
79+
File.exist?(@tempfile.path).should be_false
6680
end
6781
end
6882

@@ -73,21 +87,29 @@
7387
@tempfile.path.should_not == "#{Dir.tmpdir}/create_spec"
7488
end
7589

90+
it "uses an array of one String as a prefix for the filename" do
91+
@tempfile = Tempfile.create(["create_spec"])
92+
@tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec")
93+
@tempfile.path.should_not == "#{Dir.tmpdir}/create_spec"
94+
end
95+
7696
it "uses an array of two Strings as a prefix and suffix for the filename" do
7797
@tempfile = Tempfile.create(["create_spec", ".temp"])
7898
@tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec")
7999
@tempfile.path.should.end_with?(".temp")
80100
end
81101

82-
it "ignores more than two elements in the array" do
102+
it "ignores excessive array elements after the first two" do
83103
@tempfile = Tempfile.create(["create_spec", ".temp", :".txt"])
84104
@tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec")
85105
@tempfile.path.should.end_with?(".temp")
86106
end
87107

88108
it "raises ArgumentError if passed something else than a String or an array of Strings" do
89-
-> { Tempfile.create(:temp) }.should raise_error(ArgumentError, "unexpected prefix: :temp")
109+
-> { Tempfile.create(:create_spec) }.should raise_error(ArgumentError, "unexpected prefix: :create_spec")
110+
-> { Tempfile.create([:create_spec]) }.should raise_error(ArgumentError, "unexpected prefix: :create_spec")
90111
-> { Tempfile.create(["create_spec", :temp]) }.should raise_error(ArgumentError, "unexpected suffix: :temp")
112+
91113
end
92114
end
93115

0 commit comments

Comments
 (0)