test_construct 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +125 -2
- data/Rakefile +9 -0
- data/lib/test_construct/helpers.rb +28 -0
- data/lib/test_construct/pathname_extensions.rb +53 -0
- data/lib/test_construct/version.rb +1 -1
- data/lib/test_construct.rb +19 -1
- data/test/test_construct_test.rb +475 -0
- data/test/test_helper.rb +24 -0
- data/test_construct.gemspec +3 -0
- metadata +57 -3
data/README.md
CHANGED
@@ -1,6 +1,25 @@
|
|
1
1
|
# TestConstruct
|
2
2
|
|
3
|
-
|
3
|
+
> "This is the construct. It's our loading program. We can load anything, from clothing to equipment, weapons, and training simulations, anything we need" -- Morpheus
|
4
|
+
|
5
|
+
TestConstruct is a DSL for creating temporary files and directories during testing.
|
6
|
+
|
7
|
+
## SYNOPSIS
|
8
|
+
|
9
|
+
class ExampleTest < Test::Unit::TestCase
|
10
|
+
include TestConstruct::Helpers
|
11
|
+
|
12
|
+
def test_example
|
13
|
+
within_construct do |c|
|
14
|
+
c.directory 'alice/rabbithole' do |d|
|
15
|
+
d.file 'white_rabbit.txt', "I'm late!"
|
16
|
+
|
17
|
+
assert_equal "I'm late!", File.read('white_rabbit.txt')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
4
23
|
|
5
24
|
## Installation
|
6
25
|
|
@@ -18,7 +37,111 @@ Or install it yourself as:
|
|
18
37
|
|
19
38
|
## Usage
|
20
39
|
|
21
|
-
|
40
|
+
To use TestConstruct, you need to include the TestConstruct module in your class like so:
|
41
|
+
|
42
|
+
include TestConstruct::Helpers
|
43
|
+
|
44
|
+
Using construct is as simple as calling `within_construct` and providing a block. All files and directories that are created within that block are created within a temporary directory. The temporary directory is always deleted before `within_construct` finishes.
|
45
|
+
|
46
|
+
There is nothing special about the files and directories created with TestConstruct, so you can use plain old Ruby IO methods to interact with them.
|
47
|
+
|
48
|
+
### Creating files
|
49
|
+
|
50
|
+
The most basic use of TestConstruct is creating an empty file with the:
|
51
|
+
|
52
|
+
within_construct do |construct|
|
53
|
+
construct.file('foo.txt')
|
54
|
+
end
|
55
|
+
|
56
|
+
Note that the working directory is, by default, automatically changed to the temporary directory created by TestConstruct, so the following assertion will pass:
|
57
|
+
|
58
|
+
within_construct do |construct|
|
59
|
+
construct.file('foo.txt')
|
60
|
+
assert File.exist?('foo.txt')
|
61
|
+
end
|
62
|
+
|
63
|
+
You can also provide content for the file, either with an optional argument or using the return value of a supplied block:
|
64
|
+
|
65
|
+
within_construct do |construct|
|
66
|
+
construct.file('foo.txt', 'Here is some content')
|
67
|
+
construct.file('bar.txt') do
|
68
|
+
<<-EOS
|
69
|
+
The block will return this string, which will be used as the content.
|
70
|
+
EOS
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
If you provide block that accepts a parameter, construct will pass in the IO object. In this case, you are responsible for writing content to the file yourself - the return value of the block will not be used:
|
75
|
+
|
76
|
+
within_construct do |construct|
|
77
|
+
construct.file('foo.txt') do |file|
|
78
|
+
file << "Some content\n"
|
79
|
+
file << "Some more content"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
Finally, you can provide the entire path to a file and the parent directories will be created automatically:
|
84
|
+
|
85
|
+
within_construct do |construct|
|
86
|
+
construct.file('foo/bar/baz.txt')
|
87
|
+
end
|
88
|
+
|
89
|
+
### Creating directories
|
90
|
+
|
91
|
+
It is easy to create a directory:
|
92
|
+
|
93
|
+
within_construct do |construct|
|
94
|
+
construct.directory('foo')
|
95
|
+
end
|
96
|
+
|
97
|
+
You can also provide a block. The object passed to the block can be used to create nested files and directories (it's just a [Pathname](http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html) instance with some extra functionality, so you can use it to get the path of the current directory).
|
98
|
+
|
99
|
+
Again, note that the working directory is automatically changed while in the block:
|
100
|
+
|
101
|
+
within_construct do |construct|
|
102
|
+
construct.directory('foo') do |dir|
|
103
|
+
dir.file('bar.txt')
|
104
|
+
assert File.exist?('bar.txt') # This assertion will pass
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
Again, you can provide paths and the necessary directories will be automatically created:
|
109
|
+
|
110
|
+
|
111
|
+
within_construct do |construct|
|
112
|
+
construct.directory('foo/bar/') do |dir|
|
113
|
+
dir.directory('baz')
|
114
|
+
dir.directory('bazz')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
Please read test/construct_test.rb for more examples.
|
119
|
+
|
120
|
+
### Disabling chdir
|
121
|
+
|
122
|
+
In some cases, you may wish to disable the default behavior of automatically changing the current directory. For example, changing the current directory will prevent Ruby debuggers from displaying source code correctly.
|
123
|
+
|
124
|
+
If you disable, automatic chdir, note that your old assertions will not work:
|
125
|
+
|
126
|
+
```
|
127
|
+
within_construct(:chdir => false) do |construct|
|
128
|
+
construct.file("foo.txt")
|
129
|
+
# Fails. foo.txt was created in construct, but
|
130
|
+
# the current directory is not the construct!
|
131
|
+
assert File.exists?("foo.txt")
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
To fix, simply use the `Pathname` passed to the block:
|
136
|
+
|
137
|
+
```
|
138
|
+
within_construct(:chdir => false) do |construct|
|
139
|
+
construct.file("foo.txt")
|
140
|
+
# Passes
|
141
|
+
assert File.exists?(construct+"foo.txt")
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
22
145
|
|
23
146
|
## Contributing
|
24
147
|
|
data/Rakefile
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
module TestConstruct
|
2
|
+
|
3
|
+
module Helpers
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def within_construct(opts = {})
|
7
|
+
chdir = opts.fetch(:chdir, true)
|
8
|
+
container = create_construct(chdir)
|
9
|
+
container.maybe_change_dir(chdir) do
|
10
|
+
yield(container)
|
11
|
+
end
|
12
|
+
ensure
|
13
|
+
container.destroy! if container.respond_to?(:destroy!)
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_construct(chdir = true)
|
17
|
+
path = (Pathname(TestConstruct.tmpdir) +
|
18
|
+
"#{CONTAINER_PREFIX}-#{$PROCESS_ID}-#{rand(1_000_000_000)}")
|
19
|
+
path.mkpath
|
20
|
+
path.extend(PathnameExtensions)
|
21
|
+
path.construct__chdir_default = chdir
|
22
|
+
path
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
extend Helpers
|
28
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module TestConstruct
|
2
|
+
module PathnameExtensions
|
3
|
+
|
4
|
+
attr_accessor :construct__chdir_default
|
5
|
+
|
6
|
+
def directory(path, opts = {})
|
7
|
+
chdir = opts.fetch(:chdir, construct__chdir_default)
|
8
|
+
subdir = (self + path)
|
9
|
+
subdir.mkpath
|
10
|
+
subdir.extend(PathnameExtensions)
|
11
|
+
subdir.maybe_change_dir(chdir) do
|
12
|
+
yield(subdir) if block_given?
|
13
|
+
end
|
14
|
+
subdir
|
15
|
+
end
|
16
|
+
|
17
|
+
def file(filepath, contents = nil, &block)
|
18
|
+
path = (self+filepath)
|
19
|
+
path.dirname.mkpath
|
20
|
+
File.open(path,'w') do |f|
|
21
|
+
if(block)
|
22
|
+
if(block.arity==1)
|
23
|
+
block.call(f)
|
24
|
+
else
|
25
|
+
f << block.call
|
26
|
+
end
|
27
|
+
else
|
28
|
+
f << contents
|
29
|
+
end
|
30
|
+
end
|
31
|
+
path
|
32
|
+
end
|
33
|
+
|
34
|
+
def maybe_change_dir(chdir, &block)
|
35
|
+
if(chdir)
|
36
|
+
self.chdir(&block)
|
37
|
+
else
|
38
|
+
block.call
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Note: Pathname implements #chdir directly, but it is deprecated in favor
|
43
|
+
# of Dir.chdir
|
44
|
+
def chdir(&block)
|
45
|
+
Dir.chdir(self, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
def destroy!
|
49
|
+
rmtree
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
data/lib/test_construct.rb
CHANGED
@@ -1,5 +1,23 @@
|
|
1
|
+
require "tmpdir"
|
2
|
+
|
1
3
|
require "test_construct/version"
|
4
|
+
require "test_construct/helpers"
|
5
|
+
require "test_construct/pathname_extensions"
|
2
6
|
|
3
7
|
module TestConstruct
|
4
|
-
|
8
|
+
|
9
|
+
CONTAINER_PREFIX = 'construct_container'
|
10
|
+
|
11
|
+
def self.tmpdir
|
12
|
+
dir = nil
|
13
|
+
Dir.chdir Dir.tmpdir do dir = Dir.pwd end # HACK FOR OS X
|
14
|
+
dir
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.destroy_all!
|
18
|
+
Pathname.glob(File.join(tmpdir, CONTAINER_PREFIX + "*")) do |container|
|
19
|
+
container.rmtree
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
5
23
|
end
|
@@ -0,0 +1,475 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestConstructTest < Minitest::Test
|
4
|
+
include TestConstruct::Helpers
|
5
|
+
|
6
|
+
def teardown
|
7
|
+
TestConstruct.destroy_all!
|
8
|
+
end
|
9
|
+
|
10
|
+
testing 'using within_construct explicitly' do
|
11
|
+
|
12
|
+
test 'creates construct' do
|
13
|
+
num = rand(1_000_000_000)
|
14
|
+
TestConstruct.stubs(:rand).returns(num)
|
15
|
+
directory = false
|
16
|
+
TestConstruct::within_construct do |construct|
|
17
|
+
directory = File.directory?(File.join(TestConstruct.tmpdir, "construct_container-#{$PROCESS_ID}-#{num}"))
|
18
|
+
end
|
19
|
+
assert directory
|
20
|
+
|
21
|
+
directory = false
|
22
|
+
TestConstruct.within_construct do |construct|
|
23
|
+
directory = File.directory?(File.join(TestConstruct.tmpdir, "construct_container-#{$PROCESS_ID}-#{num}"))
|
24
|
+
end
|
25
|
+
assert directory
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
testing 'creating a construct container' do
|
31
|
+
|
32
|
+
test 'exists' do
|
33
|
+
num = rand(1_000_000_000)
|
34
|
+
self.stubs(:rand).returns(num)
|
35
|
+
within_construct do |construct|
|
36
|
+
assert File.directory?(File.join(TestConstruct.tmpdir, "construct_container-#{$PROCESS_ID}-#{num}"))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
test 'yields to its block' do
|
41
|
+
sensor = 'no yield'
|
42
|
+
within_construct do
|
43
|
+
sensor = 'yielded'
|
44
|
+
end
|
45
|
+
assert_equal 'yielded', sensor
|
46
|
+
end
|
47
|
+
|
48
|
+
test 'block argument is container directory Pathname' do
|
49
|
+
num = rand(1_000_000_000)
|
50
|
+
self.stubs(:rand).returns(num)
|
51
|
+
within_construct do |container_path|
|
52
|
+
expected_path = (Pathname(TestConstruct.tmpdir) +
|
53
|
+
"construct_container-#{$PROCESS_ID}-#{num}")
|
54
|
+
assert_equal(expected_path, container_path)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
test 'does not exist afterwards' do
|
59
|
+
path = nil
|
60
|
+
within_construct do |container_path|
|
61
|
+
path = container_path
|
62
|
+
end
|
63
|
+
assert !path.exist?
|
64
|
+
end
|
65
|
+
|
66
|
+
test 'removes entire tree afterwards' do
|
67
|
+
path = nil
|
68
|
+
within_construct do |container_path|
|
69
|
+
path = container_path
|
70
|
+
(container_path + 'foo').mkdir
|
71
|
+
end
|
72
|
+
assert !path.exist?
|
73
|
+
end
|
74
|
+
|
75
|
+
test 'removes dir if block raises exception' do
|
76
|
+
path = nil
|
77
|
+
begin
|
78
|
+
within_construct do |container_path|
|
79
|
+
path = container_path
|
80
|
+
raise 'something bad happens here'
|
81
|
+
end
|
82
|
+
rescue
|
83
|
+
end
|
84
|
+
assert !path.exist?
|
85
|
+
end
|
86
|
+
|
87
|
+
test 'does not capture exceptions raised in block' do
|
88
|
+
err = RuntimeError.new('an error')
|
89
|
+
begin
|
90
|
+
within_construct do
|
91
|
+
raise err
|
92
|
+
end
|
93
|
+
rescue RuntimeError => e
|
94
|
+
assert_same err, e
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
testing 'creating a file in a container' do
|
101
|
+
|
102
|
+
test 'exists while in construct block' do
|
103
|
+
within_construct do |construct|
|
104
|
+
construct.file('foo.txt')
|
105
|
+
assert File.exists?(construct + 'foo.txt')
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
test 'does not exist after construct block' do
|
110
|
+
filepath = 'unset'
|
111
|
+
within_construct do |construct|
|
112
|
+
filepath = construct.file('foo.txt')
|
113
|
+
end
|
114
|
+
assert !File.exists?(filepath)
|
115
|
+
end
|
116
|
+
|
117
|
+
test 'has empty file contents by default' do
|
118
|
+
within_construct do |construct|
|
119
|
+
construct.file('foo.txt')
|
120
|
+
assert_equal '', File.read(construct + 'foo.txt')
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
test 'writes contents to file' do
|
125
|
+
within_construct do |construct|
|
126
|
+
construct.file('foo.txt','abcxyz')
|
127
|
+
assert_equal 'abcxyz', File.read(construct+'foo.txt')
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
test 'contents can be given in a block' do
|
132
|
+
within_construct do |construct|
|
133
|
+
construct.file('foo.txt') do
|
134
|
+
<<-EOS
|
135
|
+
File
|
136
|
+
Contents
|
137
|
+
EOS
|
138
|
+
end
|
139
|
+
assert_equal "File\nContents\n", File.read(construct+'foo.txt')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
test 'contents block overwrites contents argument' do
|
144
|
+
within_construct do |construct|
|
145
|
+
construct.file('foo.txt','abc') do
|
146
|
+
'xyz'
|
147
|
+
end
|
148
|
+
assert_equal 'xyz', File.read(construct+'foo.txt')
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
test 'block is passed File object' do
|
153
|
+
within_construct do |construct|
|
154
|
+
construct.file('foo.txt') do |file|
|
155
|
+
assert_equal((construct+'foo.txt').to_s, file.path)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
test 'writes to File object passed to block' do
|
161
|
+
within_construct do |construct|
|
162
|
+
construct.file('foo.txt') do |file|
|
163
|
+
file << 'abc'
|
164
|
+
end
|
165
|
+
assert_equal 'abc', File.read(construct+'foo.txt')
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
test 'closes file after block ends' do
|
170
|
+
within_construct do |construct|
|
171
|
+
construct_file = nil
|
172
|
+
construct.file('foo.txt') do |file|
|
173
|
+
construct_file = file
|
174
|
+
end
|
175
|
+
assert construct_file.closed?
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
test 'block return value not used as content if passed File object' do
|
180
|
+
within_construct do |construct|
|
181
|
+
construct.file('foo.txt') do |file|
|
182
|
+
file << 'abc'
|
183
|
+
'xyz'
|
184
|
+
end
|
185
|
+
assert_equal 'abc', File.read(construct+'foo.txt')
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
test 'contents argument is ignored if block takes File arg' do
|
190
|
+
within_construct do |construct|
|
191
|
+
construct.file('foo.txt','xyz') do |file|
|
192
|
+
file << 'abc'
|
193
|
+
end
|
194
|
+
assert_equal 'abc', File.read(construct+'foo.txt')
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
test 'returns file path' do
|
199
|
+
within_construct do |construct|
|
200
|
+
assert_equal(construct+'foo.txt', construct.file('foo.txt'))
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
test 'creates file including path in one call' do
|
205
|
+
within_construct do |construct|
|
206
|
+
construct.file('foo/bar/baz.txt')
|
207
|
+
assert (construct+'foo/bar/baz.txt').exist?
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
test 'creates file including path in one call when directories exists' do
|
212
|
+
within_construct do |construct|
|
213
|
+
construct.directory('foo/bar')
|
214
|
+
construct.file('foo/bar/baz.txt')
|
215
|
+
assert (construct+'foo/bar/baz.txt').exist?
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
test 'creates file including path with chained calls' do
|
220
|
+
within_construct do |construct|
|
221
|
+
construct.directory('foo').directory('bar').file('baz.txt')
|
222
|
+
assert (construct+'foo/bar/baz.txt').exist?
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
end
|
227
|
+
|
228
|
+
testing 'creating a subdirectory in container' do
|
229
|
+
|
230
|
+
test 'exists while in construct block' do
|
231
|
+
within_construct do |construct|
|
232
|
+
construct.directory 'foo'
|
233
|
+
assert (construct+'foo').directory?
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
test 'does not exist after construct block' do
|
238
|
+
subdir = 'unset'
|
239
|
+
within_construct do |construct|
|
240
|
+
construct.directory 'foo'
|
241
|
+
subdir = construct + 'foo'
|
242
|
+
end
|
243
|
+
assert !subdir.directory?
|
244
|
+
end
|
245
|
+
|
246
|
+
test 'returns the new path name' do
|
247
|
+
within_construct do |construct|
|
248
|
+
assert_equal((construct+'foo'), construct.directory('foo'))
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
test 'yield to block' do
|
253
|
+
sensor = 'unset'
|
254
|
+
within_construct do |construct|
|
255
|
+
construct.directory('bar') do
|
256
|
+
sensor = 'yielded'
|
257
|
+
end
|
258
|
+
end
|
259
|
+
assert_equal 'yielded', sensor
|
260
|
+
end
|
261
|
+
|
262
|
+
test 'block argument is subdirectory path' do
|
263
|
+
within_construct do |construct|
|
264
|
+
construct.directory('baz') do |dir|
|
265
|
+
assert_equal((construct+'baz'),dir)
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
test 'creates nested directory in one call' do
|
271
|
+
within_construct do |construct|
|
272
|
+
construct.directory('foo/bar')
|
273
|
+
assert (construct+'foo/bar').directory?
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
test 'creates a nested directory in two calls' do
|
278
|
+
within_construct do |construct|
|
279
|
+
construct.directory('foo').directory('bar')
|
280
|
+
assert (construct+'foo/bar').directory?
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
end
|
285
|
+
|
286
|
+
testing "subdirectories changing the working directory" do
|
287
|
+
|
288
|
+
test 'forces directory stays the same' do
|
289
|
+
within_construct do |construct|
|
290
|
+
old_pwd = Dir.pwd
|
291
|
+
construct.directory('foo', :chdir => false) do
|
292
|
+
assert_equal old_pwd, Dir.pwd
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
test 'defaults chdir setting from construct' do
|
298
|
+
within_construct(:chdir => false) do |construct|
|
299
|
+
old_pwd = Dir.pwd
|
300
|
+
construct.directory('foo') do
|
301
|
+
assert_equal old_pwd, Dir.pwd
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
test 'overrides construct default' do
|
307
|
+
within_construct(:chdir => false) do |construct|
|
308
|
+
old_pwd = Dir.pwd
|
309
|
+
construct.directory('foo', :chdir => true) do |dir|
|
310
|
+
assert_equal dir.to_s, Dir.pwd
|
311
|
+
end
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
test 'current working directory is within subdirectory' do
|
316
|
+
within_construct do |construct|
|
317
|
+
construct.directory('foo') do |dir|
|
318
|
+
assert_equal dir.to_s, Dir.pwd
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
test 'current working directory is unchanged outside of subdirectory' do
|
324
|
+
within_construct do |construct|
|
325
|
+
old_pwd = Dir.pwd
|
326
|
+
construct.directory('foo')
|
327
|
+
assert_equal old_pwd, Dir.pwd
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
test 'current working directory is unchanged after exception' do
|
332
|
+
within_construct do |construct|
|
333
|
+
old_pwd = Dir.pwd
|
334
|
+
begin
|
335
|
+
construct.directory('foo') do
|
336
|
+
raise 'something bad happens here'
|
337
|
+
end
|
338
|
+
rescue
|
339
|
+
end
|
340
|
+
assert_equal old_pwd, Dir.pwd
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
test 'captures exceptions raised in block' do
|
345
|
+
within_construct do |construct|
|
346
|
+
error = assert_raises RuntimeError do
|
347
|
+
construct.directory('foo') do
|
348
|
+
raise 'fail!'
|
349
|
+
end
|
350
|
+
end
|
351
|
+
assert_equal 'fail!', error.message
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
test 'checking for a file is relative to subdirectory' do
|
356
|
+
within_construct do |construct|
|
357
|
+
construct.directory('bar') do |dir|
|
358
|
+
dir.file('foo.txt')
|
359
|
+
assert File.exists?('foo.txt')
|
360
|
+
end
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
test 'checking for a directory is relative to subdirectory' do
|
365
|
+
within_construct do |construct|
|
366
|
+
construct.directory('foo') do |dir|
|
367
|
+
dir.directory('mydir')
|
368
|
+
assert File.directory?('mydir')
|
369
|
+
end
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
end
|
374
|
+
|
375
|
+
testing "changing the working directory" do
|
376
|
+
|
377
|
+
test 'forces directory stays the same' do
|
378
|
+
old_pwd = Dir.pwd
|
379
|
+
within_construct(:chdir => false) do |construct|
|
380
|
+
assert_equal old_pwd, Dir.pwd
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
test 'current working directory is within construct' do
|
385
|
+
within_construct do |construct|
|
386
|
+
assert_equal construct.to_s, Dir.pwd
|
387
|
+
end
|
388
|
+
end
|
389
|
+
|
390
|
+
test 'current working directory is unchanged outside of construct' do
|
391
|
+
old_pwd = Dir.pwd
|
392
|
+
within_construct do |construct|
|
393
|
+
end
|
394
|
+
assert_equal old_pwd, Dir.pwd
|
395
|
+
end
|
396
|
+
|
397
|
+
test 'current working directory is unchanged after exception' do
|
398
|
+
old_pwd = Dir.pwd
|
399
|
+
begin
|
400
|
+
within_construct do |construct|
|
401
|
+
raise 'something bad happens here'
|
402
|
+
end
|
403
|
+
rescue
|
404
|
+
end
|
405
|
+
assert_equal old_pwd, Dir.pwd
|
406
|
+
end
|
407
|
+
|
408
|
+
test 'does not capture exceptions raised in block' do
|
409
|
+
error = assert_raises RuntimeError do
|
410
|
+
within_construct do
|
411
|
+
raise 'fail!'
|
412
|
+
end
|
413
|
+
end
|
414
|
+
assert_equal 'fail!', error.message
|
415
|
+
end
|
416
|
+
|
417
|
+
test 'checking for a file is relative to container' do
|
418
|
+
within_construct do |construct|
|
419
|
+
construct.file('foo.txt')
|
420
|
+
assert File.exists?('foo.txt')
|
421
|
+
end
|
422
|
+
end
|
423
|
+
|
424
|
+
test 'checking for a directory is relative to container' do
|
425
|
+
within_construct do |construct|
|
426
|
+
construct.directory('mydir')
|
427
|
+
assert File.directory?('mydir')
|
428
|
+
end
|
429
|
+
end
|
430
|
+
|
431
|
+
end
|
432
|
+
|
433
|
+
testing "#create_construct" do
|
434
|
+
|
435
|
+
test "returns a working Construct" do
|
436
|
+
it = create_construct
|
437
|
+
it.directory "foo"
|
438
|
+
it.file "bar", "CONTENTS"
|
439
|
+
assert (it + "foo").directory?
|
440
|
+
assert_equal "CONTENTS", (it + "bar").read
|
441
|
+
end
|
442
|
+
|
443
|
+
end
|
444
|
+
|
445
|
+
testing "#chdir" do
|
446
|
+
|
447
|
+
test "executes its block in the context of the construct" do
|
448
|
+
it = create_construct
|
449
|
+
refute_equal it.to_s, Dir.pwd
|
450
|
+
sensor = :unset
|
451
|
+
it.chdir do
|
452
|
+
sensor = Dir.pwd
|
453
|
+
end
|
454
|
+
assert_equal it.to_s, sensor
|
455
|
+
end
|
456
|
+
|
457
|
+
test "leaves construct directory on block exit" do
|
458
|
+
it = create_construct
|
459
|
+
it.chdir do
|
460
|
+
# NOOP
|
461
|
+
end
|
462
|
+
refute_equal it.to_s, Dir.pwd
|
463
|
+
end
|
464
|
+
end
|
465
|
+
|
466
|
+
testing "#destroy!" do
|
467
|
+
|
468
|
+
test "removes the construct container" do
|
469
|
+
it = create_construct
|
470
|
+
it.destroy!
|
471
|
+
assert !File.exist?(it.to_s)
|
472
|
+
end
|
473
|
+
end
|
474
|
+
|
475
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'minitest'
|
2
|
+
require "minitest/autorun"
|
3
|
+
|
4
|
+
require 'mocha/setup'
|
5
|
+
require 'test_construct'
|
6
|
+
|
7
|
+
class Minitest::Test
|
8
|
+
|
9
|
+
def self.testing(name)
|
10
|
+
@group = name
|
11
|
+
yield
|
12
|
+
@group = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.test(name, &block)
|
16
|
+
name = name.strip.gsub(/\s\s+/, " ")
|
17
|
+
group = "#{@group}: " if @group
|
18
|
+
test_name = "test_: #{group}#{name}".to_sym
|
19
|
+
defined = instance_methods.include? test_name
|
20
|
+
raise "#{test_name} is already defined in #{self}" if defined
|
21
|
+
define_method(test_name, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/test_construct.gemspec
CHANGED
@@ -20,4 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
22
|
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.0.8"
|
24
|
+
spec.add_development_dependency "mocha", "~> 0.14.0"
|
25
|
+
spec.add_development_dependency "debugger"
|
23
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_construct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-12-04 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -44,6 +44,54 @@ dependencies:
|
|
44
44
|
- - ! '>='
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: minitest
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.0.8
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 5.0.8
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: mocha
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ~>
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.14.0
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 0.14.0
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: debugger
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
type: :development
|
88
|
+
prerelease: false
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
47
95
|
description: Creates temporary files and directories for testing.
|
48
96
|
email:
|
49
97
|
- ben@bbrinck.com
|
@@ -58,7 +106,11 @@ files:
|
|
58
106
|
- README.md
|
59
107
|
- Rakefile
|
60
108
|
- lib/test_construct.rb
|
109
|
+
- lib/test_construct/helpers.rb
|
110
|
+
- lib/test_construct/pathname_extensions.rb
|
61
111
|
- lib/test_construct/version.rb
|
112
|
+
- test/test_construct_test.rb
|
113
|
+
- test/test_helper.rb
|
62
114
|
- test_construct.gemspec
|
63
115
|
homepage: ''
|
64
116
|
licenses:
|
@@ -85,4 +137,6 @@ rubygems_version: 1.8.23
|
|
85
137
|
signing_key:
|
86
138
|
specification_version: 3
|
87
139
|
summary: Creates temporary files and directories for testing.
|
88
|
-
test_files:
|
140
|
+
test_files:
|
141
|
+
- test/test_construct_test.rb
|
142
|
+
- test/test_helper.rb
|