devver-construct 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+
2
+ if HAVE_SPEC_RAKE_SPECTASK and not PROJ.spec.files.to_a.empty?
3
+ require 'spec/rake/verify_rcov'
4
+
5
+ namespace :spec do
6
+
7
+ desc 'Run all specs with basic output'
8
+ Spec::Rake::SpecTask.new(:run) do |t|
9
+ t.ruby_opts = PROJ.ruby_opts
10
+ t.spec_opts = PROJ.spec.opts
11
+ t.spec_files = PROJ.spec.files
12
+ t.libs += PROJ.libs
13
+ end
14
+
15
+ desc 'Run all specs with text output'
16
+ Spec::Rake::SpecTask.new(:specdoc) do |t|
17
+ t.ruby_opts = PROJ.ruby_opts
18
+ t.spec_opts = PROJ.spec.opts + ['--format', 'specdoc']
19
+ t.spec_files = PROJ.spec.files
20
+ t.libs += PROJ.libs
21
+ end
22
+
23
+ if HAVE_RCOV
24
+ desc 'Run all specs with RCov'
25
+ Spec::Rake::SpecTask.new(:rcov) do |t|
26
+ t.ruby_opts = PROJ.ruby_opts
27
+ t.spec_opts = PROJ.spec.opts
28
+ t.spec_files = PROJ.spec.files
29
+ t.libs += PROJ.libs
30
+ t.rcov = true
31
+ t.rcov_dir = PROJ.rcov.dir
32
+ t.rcov_opts = PROJ.rcov.opts + ['--exclude', 'spec']
33
+ end
34
+
35
+ RCov::VerifyTask.new(:verify) do |t|
36
+ t.threshold = PROJ.rcov.threshold
37
+ t.index_html = File.join(PROJ.rcov.dir, 'index.html')
38
+ t.require_exact_threshold = PROJ.rcov.threshold_exact
39
+ end
40
+
41
+ task :verify => :rcov
42
+ remove_desc_for_task %w(spec:clobber_rcov)
43
+ end
44
+
45
+ end # namespace :spec
46
+
47
+ desc 'Alias to spec:run'
48
+ task :spec => 'spec:run'
49
+
50
+ task :clobber => 'spec:clobber_rcov' if HAVE_RCOV
51
+
52
+ end # if HAVE_SPEC_RAKE_SPECTASK
53
+
54
+ # EOF
@@ -0,0 +1,47 @@
1
+
2
+ if HAVE_SVN
3
+
4
+ unless PROJ.svn.root
5
+ info = %x/svn info ./
6
+ m = %r/^Repository Root:\s+(.*)$/.match(info)
7
+ PROJ.svn.root = (m.nil? ? '' : m[1])
8
+ end
9
+ PROJ.svn.root = File.join(PROJ.svn.root, PROJ.svn.path) unless PROJ.svn.path.empty?
10
+
11
+ namespace :svn do
12
+
13
+ # A prerequisites task that all other tasks depend upon
14
+ task :prereqs
15
+
16
+ desc 'Show tags from the SVN repository'
17
+ task :show_tags => 'svn:prereqs' do |t|
18
+ tags = %x/svn list #{File.join(PROJ.svn.root, PROJ.svn.tags)}/
19
+ tags.gsub!(%r/\/$/, '')
20
+ tags = tags.split("\n").sort {|a,b| b <=> a}
21
+ puts tags
22
+ end
23
+
24
+ desc 'Create a new tag in the SVN repository'
25
+ task :create_tag => 'svn:prereqs' do |t|
26
+ v = ENV['VERSION'] or abort 'Must supply VERSION=x.y.z'
27
+ abort "Versions don't match #{v} vs #{PROJ.version}" if v != PROJ.version
28
+
29
+ svn = PROJ.svn
30
+ trunk = File.join(svn.root, svn.trunk)
31
+ tag = "%s-%s" % [PROJ.name, PROJ.version]
32
+ tag = File.join(svn.root, svn.tags, tag)
33
+ msg = "Creating tag for #{PROJ.name} version #{PROJ.version}"
34
+
35
+ puts "Creating SVN tag '#{tag}'"
36
+ unless system "svn cp -m '#{msg}' #{trunk} #{tag}"
37
+ abort "Tag creation failed"
38
+ end
39
+ end
40
+
41
+ end # namespace :svn
42
+
43
+ task 'gem:release' => 'svn:create_tag'
44
+
45
+ end # if PROJ.svn.path
46
+
47
+ # EOF
@@ -0,0 +1,40 @@
1
+
2
+ if test(?e, PROJ.test.file) or not PROJ.test.files.to_a.empty?
3
+ require 'rake/testtask'
4
+
5
+ namespace :test do
6
+
7
+ Rake::TestTask.new(:run) do |t|
8
+ t.libs = PROJ.libs
9
+ t.test_files = if test(?f, PROJ.test.file) then [PROJ.test.file]
10
+ else PROJ.test.files end
11
+ t.ruby_opts += PROJ.ruby_opts
12
+ t.ruby_opts += PROJ.test.opts
13
+ end
14
+
15
+ if HAVE_RCOV
16
+ desc 'Run rcov on the unit tests'
17
+ task :rcov => :clobber_rcov do
18
+ opts = PROJ.rcov.opts.dup << '-o' << PROJ.rcov.dir
19
+ opts = opts.join(' ')
20
+ files = if test(?f, PROJ.test.file) then [PROJ.test.file]
21
+ else PROJ.test.files end
22
+ files = files.join(' ')
23
+ sh "#{RCOV} #{files} #{opts}"
24
+ end
25
+
26
+ task :clobber_rcov do
27
+ rm_r 'coverage' rescue nil
28
+ end
29
+ end
30
+
31
+ end # namespace :test
32
+
33
+ desc 'Alias to test:run'
34
+ task :test => 'test:run'
35
+
36
+ task :clobber => 'test:clobber_rcov' if HAVE_RCOV
37
+
38
+ end
39
+
40
+ # EOF
@@ -0,0 +1,36 @@
1
+ if HAVE_ZENTEST
2
+
3
+ # --------------------------------------------------------------------------
4
+ if test(?e, PROJ.test.file) or not PROJ.test.files.to_a.empty?
5
+ require 'autotest'
6
+
7
+ namespace :test do
8
+ task :autotest do
9
+ Autotest.run
10
+ end
11
+ end
12
+
13
+ desc "Run the autotest loop"
14
+ task :autotest => 'test:autotest'
15
+
16
+ end # if test
17
+
18
+ # --------------------------------------------------------------------------
19
+ if HAVE_SPEC_RAKE_SPECTASK and not PROJ.spec.files.to_a.empty?
20
+ require 'autotest/rspec'
21
+
22
+ namespace :spec do
23
+ task :autotest do
24
+ load '.autotest' if test(?f, '.autotest')
25
+ Autotest::Rspec.run
26
+ end
27
+ end
28
+
29
+ desc "Run the autotest loop"
30
+ task :autotest => 'spec:autotest'
31
+
32
+ end # if rspec
33
+
34
+ end # if HAVE_ZENTEST
35
+
36
+ # EOF
@@ -0,0 +1,420 @@
1
+ require File.join(File.dirname(__FILE__), %w[test_helper])
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'construct')
3
+
4
+ require 'tmpdir'
5
+ require 'English'
6
+ require 'ruby-debug'
7
+ require 'mocha'
8
+
9
+ class ConstructTest < Test::Unit::TestCase
10
+ include Construct::Helpers
11
+
12
+ testing 'using within_construct explicitly' do
13
+
14
+ test 'creates construct' do
15
+ num = rand(1_000_000_000)
16
+ Construct.stubs(:rand).returns(num)
17
+ Construct::within_construct do |construct|
18
+ assert File.directory?(File.join(Construct::Helpers::tmpdir, "construct_container-#{$PROCESS_ID}-#{num}"))
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ testing 'creating a construct container' do
25
+
26
+ test 'should exist' do
27
+ num = rand(1_000_000_000)
28
+ self.stubs(:rand).returns(num)
29
+ within_construct do |construct|
30
+ assert File.directory?(File.join(Construct::Helpers::tmpdir, "construct_container-#{$PROCESS_ID}-#{num}"))
31
+ end
32
+ end
33
+
34
+ test 'should yield to its block' do
35
+ sensor = 'no yield'
36
+ within_construct do
37
+ sensor = 'yielded'
38
+ end
39
+ assert_equal 'yielded', sensor
40
+ end
41
+
42
+ test 'block argument should be container directory Pathname' do
43
+ num = rand(1_000_000_000)
44
+ self.stubs(:rand).returns(num)
45
+ within_construct do |container_path|
46
+ expected_path = (Pathname(Construct::Helpers.tmpdir) +
47
+ "construct_container-#{$PROCESS_ID}-#{num}")
48
+ assert_equal(expected_path, container_path)
49
+ end
50
+ end
51
+
52
+ test 'should not exist afterwards' do
53
+ path = nil
54
+ within_construct do |container_path|
55
+ path = container_path
56
+ end
57
+ assert !path.exist?
58
+ end
59
+
60
+ test 'should remove entire tree afterwards' do
61
+ path = nil
62
+ within_construct do |container_path|
63
+ path = container_path
64
+ (container_path + 'foo').mkdir
65
+ end
66
+ assert !path.exist?
67
+ end
68
+
69
+ test 'should remove dir if block raises exception' do
70
+ path = nil
71
+ begin
72
+ within_construct do |container_path|
73
+ path = container_path
74
+ raise 'something bad happens here'
75
+ end
76
+ rescue
77
+ end
78
+ assert !path.exist?
79
+ end
80
+
81
+ test 'should not capture exceptions raised in block' do
82
+ err = RuntimeError.new('an error')
83
+ begin
84
+ within_construct do
85
+ raise err
86
+ end
87
+ rescue RuntimeError => e
88
+ assert_same err, e
89
+ end
90
+ end
91
+
92
+ end
93
+
94
+ testing 'creating a file in a container' do
95
+
96
+ test 'should exist while in construct block' do
97
+ within_construct do |construct|
98
+ construct.file('foo.txt')
99
+ assert File.exists?(construct+'foo.txt')
100
+ end
101
+ end
102
+
103
+ test 'should not exist after construct block' do
104
+ filepath = 'unset'
105
+ within_construct do |construct|
106
+ filepath = construct.file('foo.txt')
107
+ end
108
+ assert !File.exists?(filepath)
109
+ end
110
+
111
+ test 'writes contents to file' do
112
+ within_construct do |construct|
113
+ construct.file('foo.txt','abcxyz')
114
+ assert_equal 'abcxyz', File.read(construct+'foo.txt')
115
+ end
116
+ end
117
+
118
+ test 'contents can be given in a block' do
119
+ within_construct do |construct|
120
+ construct.file('foo.txt') do
121
+ <<-EOS
122
+ File
123
+ Contents
124
+ EOS
125
+ end
126
+ assert_equal "File\nContents\n", File.read(construct+'foo.txt')
127
+ end
128
+ end
129
+
130
+ test 'contents block overwrites contents argument' do
131
+ within_construct do |construct|
132
+ construct.file('foo.txt','abc') do
133
+ 'xyz'
134
+ end
135
+ assert_equal 'xyz', File.read(construct+'foo.txt')
136
+ end
137
+ end
138
+
139
+ test 'block is passed File object' do
140
+ within_construct do |construct|
141
+ construct.file('foo.txt') do |file|
142
+ assert_equal((construct+'foo.txt').to_s, file.path)
143
+ end
144
+ end
145
+ end
146
+
147
+ test 'can write to File object passed to block' do
148
+ within_construct do |construct|
149
+ construct.file('foo.txt') do |file|
150
+ file << 'abc'
151
+ end
152
+ assert_equal 'abc', File.read(construct+'foo.txt')
153
+ end
154
+ end
155
+
156
+ test 'file is closed after block ends' do
157
+ within_construct do |construct|
158
+ construct_file = nil
159
+ construct.file('foo.txt') do |file|
160
+ construct_file = file
161
+ end
162
+ assert construct_file.closed?
163
+ end
164
+ end
165
+
166
+ test 'block return value not used as content if passed File object' do
167
+ within_construct do |construct|
168
+ construct.file('foo.txt') do |file|
169
+ file << 'abc'
170
+ 'xyz'
171
+ end
172
+ assert_equal 'abc', File.read(construct+'foo.txt')
173
+ end
174
+ end
175
+
176
+ test 'contents argument is ignored if block takes File arg' do
177
+ within_construct do |construct|
178
+ construct.file('foo.txt','xyz') do |file|
179
+ file << 'abc'
180
+ end
181
+ assert_equal 'abc', File.read(construct+'foo.txt')
182
+ end
183
+ end
184
+
185
+ test 'returns file path' do
186
+ within_construct do |construct|
187
+ assert_equal(construct+'foo.txt', construct.file('foo.txt'))
188
+ end
189
+ end
190
+
191
+ test 'can create file including path in one call' do
192
+ within_construct do |construct|
193
+ construct.file('foo/bar/baz.txt')
194
+ assert (construct+'foo/bar/baz.txt').exist?
195
+ end
196
+ end
197
+
198
+ test 'can create file including path in one call when directories exists' do
199
+ within_construct do |construct|
200
+ construct.directory('foo/bar')
201
+ construct.file('foo/bar/baz.txt')
202
+ assert (construct+'foo/bar/baz.txt').exist?
203
+ end
204
+ end
205
+
206
+ test 'can create file including path with chained calls' do
207
+ within_construct do |construct|
208
+ construct.directory('foo').directory('bar').file('baz.txt')
209
+ assert (construct+'foo/bar/baz.txt').exist?
210
+ end
211
+ end
212
+
213
+ end
214
+
215
+ testing 'creating a subdirectory in container' do
216
+
217
+ test 'should exist while in construct block' do
218
+ within_construct do |construct|
219
+ construct.directory 'foo'
220
+ assert (construct+'foo').directory?
221
+ end
222
+ end
223
+
224
+ test 'should not exist after construct block' do
225
+ subdir = 'unset'
226
+ within_construct do |construct|
227
+ construct.directory 'foo'
228
+ subdir = construct + 'foo'
229
+ end
230
+ assert !subdir.directory?
231
+ end
232
+
233
+ test 'returns the new path name' do
234
+ within_construct do |construct|
235
+ assert_equal((construct+'foo'), construct.directory('foo'))
236
+ end
237
+ end
238
+
239
+ test 'yield to block' do
240
+ sensor = 'unset'
241
+ within_construct do |construct|
242
+ construct.directory('bar') do
243
+ sensor = 'yielded'
244
+ end
245
+ end
246
+ assert_equal 'yielded', sensor
247
+ end
248
+
249
+ test 'block argument is subdirectory path' do
250
+ within_construct do |construct|
251
+ construct.directory('baz') do |dir|
252
+ assert_equal((construct+'baz'),dir)
253
+ end
254
+ end
255
+ end
256
+
257
+ test 'can create nested directory in one call' do
258
+ within_construct do |construct|
259
+ construct.directory('foo/bar')
260
+ assert (construct+'foo/bar').directory?
261
+ end
262
+ end
263
+
264
+ test 'can create a nested directory in two calls' do
265
+ within_construct do |construct|
266
+ construct.directory('foo').directory('bar')
267
+ assert (construct+'foo/bar').directory?
268
+ end
269
+ end
270
+
271
+ end
272
+
273
+ testing "subdirectories changing the working directory" do
274
+
275
+ test 'can force directory stays the same' do
276
+ within_construct do |construct|
277
+ old_pwd = Dir.pwd
278
+ construct.directory('foo',false) do
279
+ assert_equal old_pwd, Dir.pwd
280
+ end
281
+ end
282
+ end
283
+
284
+ test 'defaults chdir setting from construct' do
285
+ within_construct(false) do |construct|
286
+ old_pwd = Dir.pwd
287
+ construct.directory('foo') do
288
+ assert_equal old_pwd, Dir.pwd
289
+ end
290
+ end
291
+ end
292
+
293
+ test 'can override construct default' do
294
+ within_construct(false) do |construct|
295
+ old_pwd = Dir.pwd
296
+ construct.directory('foo', true) do |dir|
297
+ assert_equal dir.to_s, Dir.pwd
298
+ end
299
+ end
300
+ end
301
+
302
+ test 'current working directory is within subdirectory' do
303
+ within_construct do |construct|
304
+ construct.directory('foo') do |dir|
305
+ assert_equal dir.to_s, Dir.pwd
306
+ end
307
+ end
308
+ end
309
+
310
+ test 'current working directory is unchanged outside of subdirectory' do
311
+ within_construct do |construct|
312
+ old_pwd = Dir.pwd
313
+ construct.directory('foo')
314
+ assert_equal old_pwd, Dir.pwd
315
+ end
316
+ end
317
+
318
+ test 'current working directory is unchanged after exception' do
319
+ within_construct do |construct|
320
+ old_pwd = Dir.pwd
321
+ begin
322
+ construct.directory('foo') do
323
+ raise 'something bad happens here'
324
+ end
325
+ rescue
326
+ end
327
+ assert_equal old_pwd, Dir.pwd
328
+ end
329
+ end
330
+
331
+ test 'should not capture exceptions raised in block' do
332
+ within_construct do |construct|
333
+ error = assert_raises RuntimeError do
334
+ construct.directory('foo') do
335
+ raise 'fail!'
336
+ end
337
+ end
338
+ assert_equal 'fail!', error.message
339
+ end
340
+ end
341
+
342
+ test 'checking for a file is relative to subdirectory' do
343
+ within_construct do |construct|
344
+ construct.directory('bar') do |dir|
345
+ dir.file('foo.txt')
346
+ assert File.exists?('foo.txt')
347
+ end
348
+ end
349
+ end
350
+
351
+ test 'checking for a directory is relative to subdirectory' do
352
+ within_construct do |construct|
353
+ construct.directory('foo') do |dir|
354
+ dir.directory('mydir')
355
+ assert File.directory?('mydir')
356
+ end
357
+ end
358
+ end
359
+
360
+ end
361
+
362
+ testing "changing the working directory" do
363
+
364
+ test 'can force directory stays the same' do
365
+ old_pwd = Dir.pwd
366
+ within_construct(false) do |construct|
367
+ assert_equal old_pwd, Dir.pwd
368
+ end
369
+ end
370
+
371
+ test 'current working directory is within construct' do
372
+ within_construct do |construct|
373
+ assert_equal construct.to_s, Dir.pwd
374
+ end
375
+ end
376
+
377
+ test 'current working directory is unchanged outside of construct' do
378
+ old_pwd = Dir.pwd
379
+ within_construct do |construct|
380
+ end
381
+ assert_equal old_pwd, Dir.pwd
382
+ end
383
+
384
+ test 'current working directory is unchanged after exception' do
385
+ old_pwd = Dir.pwd
386
+ begin
387
+ within_construct do |construct|
388
+ raise 'something bad happens here'
389
+ end
390
+ rescue
391
+ end
392
+ assert_equal old_pwd, Dir.pwd
393
+ end
394
+
395
+ test 'should not capture exceptions raised in block' do
396
+ error = assert_raises RuntimeError do
397
+ within_construct do
398
+ raise 'fail!'
399
+ end
400
+ end
401
+ assert_equal 'fail!', error.message
402
+ end
403
+
404
+ test 'checking for a file is relative to container' do
405
+ within_construct do |construct|
406
+ construct.file('foo.txt')
407
+ assert File.exists?('foo.txt')
408
+ end
409
+ end
410
+
411
+ test 'checking for a directory is relative to container' do
412
+ within_construct do |construct|
413
+ construct.directory('mydir')
414
+ assert File.directory?('mydir')
415
+ end
416
+ end
417
+
418
+ end
419
+
420
+ end