temporaries 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,170 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe Temporaries::Directory do
4
+ before do
5
+ @context = Object.new
6
+ @context.extend Temporaries::Directory
7
+ FileUtils.rm_rf TMP
8
+ FileUtils.mkdir_p TMP
9
+ end
10
+
11
+ before do
12
+ FileUtils.rm_rf TMP
13
+ end
14
+
15
+ describe "#push_temporary_directory and #pop_temporary_directory" do
16
+ it "should create the given directory and make it the #tmp until it is popped" do
17
+ @context.tmp.should be_nil
18
+ File.should_not exist("#{TMP}/dir1")
19
+
20
+ @context.push_temporary_directory "#{TMP}/dir1"
21
+
22
+ @context.tmp.should == "#{TMP}/dir1"
23
+ File.should be_directory("#{TMP}/dir1")
24
+
25
+ @context.pop_temporary_directory
26
+
27
+ @context.tmp.should be_nil
28
+ File.should_not exist("#{TMP}/dir1")
29
+ end
30
+
31
+ it "should be nestable" do
32
+ @context.tmp.should be_nil
33
+ File.should_not exist("#{TMP}/dir1")
34
+
35
+ @context.push_temporary_directory "#{TMP}/dir1"
36
+
37
+ @context.tmp.should == "#{TMP}/dir1"
38
+ File.should be_directory("#{TMP}/dir1")
39
+ File.should_not be_directory("#{TMP}/dir2")
40
+
41
+ @context.push_temporary_directory "#{TMP}/dir2"
42
+
43
+ @context.tmp.should == "#{TMP}/dir2"
44
+ File.should be_directory("#{TMP}/dir2")
45
+
46
+ @context.pop_temporary_directory
47
+
48
+ @context.tmp.should == "#{TMP}/dir1"
49
+ File.should_not exist("#{TMP}/dir2")
50
+ File.should be_directory("#{TMP}/dir1")
51
+
52
+ @context.pop_temporary_directory
53
+
54
+ @context.tmp.should be_nil
55
+ File.should_not exist("#{TMP}/dir1")
56
+ end
57
+
58
+ it "should not destroy the directory on pop if the directory already existed on push" do
59
+ @context.tmp.should be_nil
60
+ File.should_not exist("#{TMP}/dir")
61
+
62
+ @context.push_temporary_directory "#{TMP}/dir"
63
+
64
+ @context.tmp.should == "#{TMP}/dir"
65
+ File.should be_directory("#{TMP}/dir")
66
+
67
+ @context.push_temporary_directory "#{TMP}/dir"
68
+
69
+ @context.tmp.should == "#{TMP}/dir"
70
+ File.should be_directory("#{TMP}/dir")
71
+
72
+ @context.pop_temporary_directory
73
+
74
+ @context.tmp.should == "#{TMP}/dir"
75
+ File.should be_directory("#{TMP}/dir")
76
+
77
+ @context.pop_temporary_directory
78
+
79
+ @context.tmp.should be_nil
80
+ File.should_not exist("#{TMP}/dir")
81
+ end
82
+ end
83
+
84
+ describe "#with_temporary_directory" do
85
+ it "should make the given directory and make it the #tmp only during the given block" do
86
+ @context.tmp.should be_nil
87
+ File.should_not exist("#{TMP}/dir1")
88
+
89
+ block_run = false
90
+ @context.with_temporary_directory "#{TMP}/dir1" do
91
+ block_run = true
92
+ @context.tmp.should == "#{TMP}/dir1"
93
+ File.should be_directory("#{TMP}/dir1")
94
+ end
95
+ block_run.should be_true
96
+
97
+ @context.tmp.should be_nil
98
+ File.should_not exist("#{TMP}/dir1")
99
+ end
100
+
101
+ it "should be nestable" do
102
+ @context.tmp.should be_nil
103
+ File.should_not exist("#{TMP}/dir1")
104
+
105
+ blocks_run = []
106
+ @context.with_temporary_directory "#{TMP}/dir1" do
107
+ blocks_run << 1
108
+ @context.tmp.should == "#{TMP}/dir1"
109
+ File.should be_directory("#{TMP}/dir1")
110
+ File.should_not be_directory("#{TMP}/dir2")
111
+
112
+ @context.with_temporary_directory "#{TMP}/dir2" do
113
+ blocks_run << 2
114
+ @context.tmp.should == "#{TMP}/dir2"
115
+ File.should be_directory("#{TMP}/dir2")
116
+ end
117
+
118
+ @context.tmp.should == "#{TMP}/dir1"
119
+ File.should_not exist("#{TMP}/dir2")
120
+ File.should be_directory("#{TMP}/dir1")
121
+ end
122
+
123
+ blocks_run.should == [1, 2]
124
+
125
+ @context.tmp.should be_nil
126
+ File.should_not exist("#{TMP}/dir1")
127
+ end
128
+
129
+ it "should handle nonlocal exits" do
130
+ exception_class = Class.new(Exception)
131
+ @context.tmp.should be_nil
132
+ begin
133
+ @context.with_temporary_directory "#{TMP}/dir" do
134
+ @context.tmp.should == "#{TMP}/dir"
135
+ raise exception_class, 'boom'
136
+ end
137
+ rescue exception_class => e
138
+ end
139
+ @context.tmp.should be_nil
140
+ end
141
+ end
142
+ end
143
+
144
+ describe Temporaries::Directory do
145
+ describe ".use_temporary_directory" do
146
+ before do
147
+ @context_class = Class.new(TestContext) do
148
+ include Temporaries::Directory
149
+ use_temporary_directory "#{TMP}/dir"
150
+ end
151
+ @context = @context_class.new
152
+ end
153
+
154
+ it "should create the given directory and make it the #tmp for the duration of the run" do
155
+ @context.tmp.should be_nil
156
+ File.should_not exist("#{TMP}/dir")
157
+
158
+ block_run = false
159
+ @context.run do
160
+ block_run = true
161
+ @context.tmp.should == "#{TMP}/dir"
162
+ File.should be_directory("#{TMP}/dir")
163
+ end
164
+ block_run.should be_true
165
+
166
+ @context.tmp.should be_nil
167
+ File.should_not exist("#{TMP}/dir")
168
+ end
169
+ end
170
+ end