em-fs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ module EventMachine
2
+ class FS
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/em-fs.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'eventmachine'
2
+ require 'em-systemcommand'
3
+
4
+ require 'em-fs/version'
5
+ require 'em-fs/core_ext'
6
+ require 'em-fs/fs'
7
+ require 'em-fs/file'
8
+ require 'em-fs/dir'
9
+ require 'em-fs/fileutils'
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe EM::Dir::Glob do
4
+
5
+ end
data/spec/dir_spec.rb ADDED
@@ -0,0 +1,16 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe EM::Dir do
5
+ describe '.[]' do
6
+ it 'should create a `EM::Dir::Glob` instance' do
7
+ EM::Dir['./**/*.rb'].should be_a EM::Dir::Glob
8
+ end
9
+ end
10
+
11
+ describe '.glob' do
12
+ it 'should create a `EM::Dir::Glob` instance' do
13
+ EM::Dir['./**/*.rb'].should be_a EM::Dir::Glob
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe EM::File::Stat do
4
+ describe '.parse' do
5
+ context 'parsed stat' do
6
+ subject do
7
+ EM::File::Stat.parse "644 427272 512 2050 81a4 'regular file' 100 1 2623327 '/' '/home/arthur/test' 4096 218759168 0 0 1000 0 1340357826 1340357846 1340357846 802h 2050d"
8
+ end
9
+
10
+ its(:atime) { should == Time.at(1340357826) }
11
+ its(:blksize) { should == 512 }
12
+ its(:blockdev?) { should == false }
13
+ its(:blocks) { should == 427272 }
14
+ its(:chardev?) { should == false }
15
+ its(:ctime) { should == Time.at(1340357846) }
16
+ its(:dev) { should == 2050 }
17
+ its(:dev_major) { should == 0 }
18
+ its(:dev_minor) { should == 0 }
19
+ its(:directory?) { should == false }
20
+ its(:executable?) { should == false }
21
+ its(:executable_real?) { should == false }
22
+ its(:file?) { should == true }
23
+ its(:ftype) { should == EM::File::Stat::S_IFREG }
24
+ its(:gid) { should == 100 }
25
+ its(:grpowned?) { should == true }
26
+ its(:ino) { should == 2623327 }
27
+ its(:mode) { should == '644' }
28
+ its(:mtime) { should == Time.at(1340357846) }
29
+ its(:nlink) { should == 1 }
30
+ its(:owned?) { should == true }
31
+ its(:pipe?) { should == false }
32
+ its(:readable?) { should == true }
33
+ its(:readable_real?) { should == true }
34
+ its(:size) { should == 218759168 }
35
+ its(:socket?) { should == false }
36
+ its(:symlink?) { should == false }
37
+ its(:uid) { should == 1000 }
38
+ its(:world_readable?) { should == 420 }
39
+ its(:world_writable?) { should == nil }
40
+ its(:writable?) { should == true }
41
+ its(:writable_real?) { should == true }
42
+ its(:zero?) { should == false }
43
+ end
44
+ end
45
+ end
data/spec/file_spec.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe EM::File do
4
+ describe '.stat' do
5
+ before :all do
6
+ @stat = nil
7
+ EM.run do
8
+ EM::File.stat File.join(SPEC_ROOT, 'data') do |stat|
9
+ @stat = stat
10
+ EM.stop_event_loop
11
+ end
12
+ end
13
+ end
14
+
15
+ it 'should return a `EM::File::Stat`' do
16
+ @stat.should be_a EM::File::Stat
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,432 @@
1
+ require 'spec_helper'
2
+
3
+ describe EM::FileUtils do
4
+ describe '.mkdir' do
5
+ before :all do
6
+ @dir1 = File.join(SPEC_ROOT, 'data', 'dir1')
7
+ @dir2 = File.join(SPEC_ROOT, 'data', 'dir1', 'sub', 'sub', 'sub')
8
+ end
9
+
10
+ after :all do
11
+ FileUtils.rm_rf @dir1
12
+ FileUtils.rm_rf @dir2
13
+ end
14
+
15
+ it 'should create a directory' do
16
+ EM.run do
17
+ EM::FileUtils.mkdir @dir1 do |on|
18
+ on.success do
19
+ EM.stop_event_loop
20
+ File.should exist(@dir1)
21
+ end
22
+ on.failure do
23
+ raise on.stderr.output
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'with :parents options' do
30
+ it 'should create directories for path' do
31
+ EM.run do
32
+ EM::FileUtils.mkdir_p @dir2 do |on|
33
+ on.success do
34
+ EM.stop_event_loop
35
+ File.should exist(@dir2)
36
+ end
37
+ on.failure do
38
+ raise on.stderr.output
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ describe '.rmdir' do
47
+ before :all do
48
+ @dir = File.join SPEC_ROOT, 'data', 'testdir'
49
+ FileUtils.mkdir @dir
50
+ end
51
+
52
+ after :all do
53
+ FileUtils.rm_rf @dir if File.exists?(@dir)
54
+ end
55
+
56
+ it 'should should remove the empty directory' do
57
+ EM.run do
58
+ EM::FileUtils.rmdir @dir do |on|
59
+ on.success do
60
+ EM.stop_event_loop
61
+ File.should_not exist(@dir)
62
+ end
63
+ on.failure do
64
+ raise on.stderr.output
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ describe '.ln' do
72
+ before :all do
73
+ @link = File.join SPEC_ROOT, 'data', 'link'
74
+ @target = File.join SPEC_ROOT, 'data', 'link_target'
75
+ FileUtils.touch @target
76
+ end
77
+
78
+ after :each do
79
+ FileUtils.rm_rf @link
80
+ end
81
+
82
+ after :all do
83
+ FileUtils.rm_rf @target
84
+ end
85
+
86
+ it 'should create a link' do
87
+ EM.run do
88
+ EM::FileUtils.ln @target, @link do |on|
89
+ on.success do
90
+ EM.stop_event_loop
91
+ File.should exist(@link)
92
+ end
93
+ on.failure do
94
+ EM.stop_event_loop
95
+ raise on.stderr.output
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ it 'should create a symbolic link' do
102
+ EM.run do
103
+ EM::FileUtils.ln_s @target, @link do |on|
104
+ on.success do
105
+ EM.stop_event_loop
106
+ File.should exist @link
107
+ end
108
+ on.failure do
109
+ EM.stop_event_loop
110
+ raise on.stderr.output
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ it 'should force link creation' do
117
+ EM.run do
118
+ EM::FileUtils.ln_sf @target, @link do |on|
119
+ on.success do
120
+ EM.stop_event_loop
121
+ File.should exist @link
122
+ end
123
+ on.failure do
124
+ EM.stop_event_loop
125
+ raise on.stderr.output
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ describe '.cp' do
133
+
134
+ context 'copying one file' do
135
+ before :all do
136
+ @source = File.join SPEC_ROOT, 'data', 'test'
137
+ @target = File.join SPEC_ROOT, 'data', 'test.copy'
138
+ EM.run do
139
+ EM::FileUtils.cp @source, @target do |on|
140
+ on.exit do |status|
141
+ EM.stop_event_loop
142
+ raise on.stderr.output if status.exitstatus != 0
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ after :all do
149
+ FileUtils.rm_rf @target
150
+ end
151
+
152
+ it 'should create a copy' do
153
+ File.should exist @target
154
+ end
155
+ end
156
+
157
+ context 'copying multiple files to folder' do
158
+ before :all do
159
+ @source1 = File.join SPEC_ROOT, 'data', 'test'
160
+ @source2 = File.join SPEC_ROOT, 'data', 'test2'
161
+ @source3 = File.join SPEC_ROOT, 'data', 'test3'
162
+ @target = File.join SPEC_ROOT, 'data', 'test_dir'
163
+
164
+ FileUtils.mkdir_p @target
165
+
166
+ EM.run do
167
+ EM::FileUtils.cp @source1, @source2, @source3, @target do |on|
168
+ on.exit do |status|
169
+ EM.stop_event_loop
170
+ raise on.stderr.output if status.exitstatus != 0
171
+ end
172
+ end
173
+ end
174
+ end
175
+
176
+ after :all do
177
+ FileUtils.rm_rf @target
178
+ end
179
+
180
+ it 'should create a copy' do
181
+ File.should exist @target
182
+ File.should be_directory @target
183
+ File.should exist File.join(@target, 'test')
184
+ File.should exist File.join(@target, 'test2')
185
+ File.should exist File.join(@target, 'test3')
186
+ end
187
+ end
188
+ end
189
+
190
+ describe '.cp_r' do
191
+ before :all do
192
+ @source_dir = File.join(SPEC_ROOT, 'data', 'source.dir/')
193
+ @target_dir = File.join(SPEC_ROOT, 'data', 'target.dir')
194
+ FileUtils.mkdir_p @source_dir
195
+ FileUtils.cp [
196
+ File.join(SPEC_ROOT, 'data', 'test'),
197
+ File.join(SPEC_ROOT, 'data', 'test2'),
198
+ File.join(SPEC_ROOT, 'data', 'test3')
199
+ ], @source_dir
200
+
201
+ EM.run do
202
+ EM::FileUtils.cp_r @source_dir, @target_dir do |on|
203
+ on.exit do |status|
204
+ EM.stop_event_loop
205
+ raise on.stderr.output if status.exitstatus != 0
206
+ end
207
+ end
208
+ end
209
+ end
210
+
211
+ after :all do
212
+ FileUtils.rm_rf @source_dir
213
+ FileUtils.rm_rf @target_dir
214
+ end
215
+
216
+ it 'should copy directory recursively' do
217
+ File.should exist File.join(@target_dir, 'test')
218
+ File.should exist File.join(@target_dir, 'test2')
219
+ File.should exist File.join(@target_dir, 'test3')
220
+ end
221
+ end
222
+
223
+ describe '.mv' do
224
+ context 'moving one file' do
225
+ before :all do
226
+ @source = File.join(SPEC_ROOT, 'data', 'test')
227
+ @target = File.join(SPEC_ROOT, 'data', 'moved_test')
228
+
229
+ EM.run do
230
+ EM::FileUtils.mv @source, @target do |on|
231
+ on.exit do |status|
232
+ EM.stop_event_loop
233
+ raise on.stderr.output if status.exitstatus != 0
234
+ end
235
+ end
236
+ end
237
+ end
238
+
239
+ after :all do
240
+ FileUtils.mv @target, @source
241
+ end
242
+
243
+ it 'should copy to target file' do
244
+ File.should exist @target
245
+ end
246
+
247
+ it 'should remove source file' do
248
+ File.should_not exist @source
249
+ end
250
+ end
251
+
252
+ context 'moving multiple files' do
253
+ before :all do
254
+ @source = File.join(SPEC_ROOT, 'data', 'test')
255
+ @source2 = File.join(SPEC_ROOT, 'data', 'test2')
256
+ @source3 = File.join(SPEC_ROOT, 'data', 'test3')
257
+ @target_dir = File.join(SPEC_ROOT, 'data', 'target_dir/')
258
+
259
+ FileUtils.mkdir_p @target_dir
260
+
261
+ EM.run do
262
+ EM::FileUtils.mv @source, @source2, @source3, @target_dir do |on|
263
+ on.exit do |status|
264
+ EM.stop_event_loop
265
+ raise on.stderr.output if status.exitstatus != 0
266
+ end
267
+ end
268
+ end
269
+ end
270
+
271
+ after :all do
272
+ FileUtils.mv Dir.glob(File.join(@target_dir, '*')), File.join(SPEC_ROOT, 'data')
273
+ FileUtils.rm_rf @target_dir
274
+ end
275
+
276
+ it 'should copy to target directory' do
277
+ File.should exist File.join(@target_dir, 'test')
278
+ File.should exist File.join(@target_dir, 'test2')
279
+ File.should exist File.join(@target_dir, 'test3')
280
+ end
281
+
282
+ it 'should remove source files' do
283
+ File.should_not exist @source
284
+ File.should_not exist @source2
285
+ File.should_not exist @source3
286
+ end
287
+ end
288
+
289
+ context 'moving directories' do
290
+ before :all do
291
+ @source_dir = File.join(SPEC_ROOT, 'data', 'source.dir/')
292
+ @target_dir = File.join(SPEC_ROOT, 'data', 'target.dir')
293
+ FileUtils.mkdir_p @source_dir
294
+ FileUtils.cp [
295
+ File.join(SPEC_ROOT, 'data', 'test'),
296
+ File.join(SPEC_ROOT, 'data', 'test2'),
297
+ File.join(SPEC_ROOT, 'data', 'test3')
298
+ ], @source_dir
299
+
300
+ EM.run do
301
+ EM::FileUtils.mv @source_dir, @target_dir do |on|
302
+ on.exit do |status|
303
+ EM.stop_event_loop
304
+ raise on.stderr.output if status.exitstatus != 0
305
+ end
306
+ end
307
+ end
308
+
309
+ end
310
+
311
+ after :all do
312
+ FileUtils.rm_rf @target_dir
313
+ FileUtils.rm_rf @source_dir
314
+ end
315
+
316
+ it 'should delete the source directory' do
317
+ File.should_not exist File.join(@source_dir, 'test')
318
+ File.should_not exist File.join(@source_dir, 'test2')
319
+ File.should_not exist File.join(@source_dir, 'test3')
320
+ end
321
+
322
+ it 'should create the target directory' do
323
+ File.should exist @target_dir
324
+ end
325
+
326
+ it 'should have moved the directory content to the target directory' do
327
+ File.should exist File.join(@target_dir, 'test')
328
+ File.should exist File.join(@target_dir, 'test2')
329
+ File.should exist File.join(@target_dir, 'test3')
330
+ end
331
+ end
332
+ end
333
+
334
+ describe '.rm' do
335
+ it 'should invoke `rm file`' do
336
+ EM.run do
337
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
338
+ cmd = EM::FileUtils.rm 'file'
339
+ cmd.command.should == 'rm file'
340
+ EM.stop_event_loop
341
+ end
342
+ end
343
+ end
344
+
345
+ describe '.rm_r' do
346
+ it 'should invoke `rm file`' do
347
+ EM.run do
348
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
349
+ cmd = EM::FileUtils.rm_r 'file'
350
+ cmd.command.should == 'rm -r file'
351
+ EM.stop_event_loop
352
+ end
353
+ end
354
+ end
355
+
356
+ describe '.rm_rf' do
357
+ it 'should invoke `rm file`' do
358
+ EM.run do
359
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
360
+ cmd = EM::FileUtils.rm_rf 'file'
361
+ cmd.command.should == 'rm -r -f file'
362
+ EM.stop_event_loop
363
+ end
364
+ end
365
+ end
366
+
367
+ describe '.install' do
368
+ it 'should invoke `install` command' do
369
+ EM.run do
370
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
371
+ cmd = EM::FileUtils.install 'file', '/usr/bin/test', mode: '777'
372
+ cmd.command.should == 'install --mode=777 file /usr/bin/test'
373
+ EM.stop_event_loop
374
+ end
375
+ end
376
+ end
377
+
378
+ describe '.chmod' do
379
+ it 'should invoke `chmod` command' do
380
+ EM.run do
381
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
382
+ cmd = EM::FileUtils.chmod 777, '/usr/bin/test'
383
+ cmd.command.should == 'chmod 777 /usr/bin/test'
384
+ EM.stop_event_loop
385
+ end
386
+ end
387
+ end
388
+
389
+ describe '.chmod_R' do
390
+ it 'should invoke `chmod -R` command' do
391
+ EM.run do
392
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
393
+ cmd = EM::FileUtils.chmod_R 777, '/usr/bin/test'
394
+ cmd.command.should == 'chmod -R 777 /usr/bin/test'
395
+ EM.stop_event_loop
396
+ end
397
+ end
398
+ end
399
+
400
+ describe '.chown' do
401
+ it 'should invoke `chown`' do
402
+ EM.run do
403
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
404
+ cmd = EM::FileUtils.chown 'arthur', 'users', '/usr/bin/test'
405
+ cmd.command.should == 'chown arthur:users /usr/bin/test'
406
+ EM.stop_event_loop
407
+ end
408
+ end
409
+ end
410
+
411
+ describe '.chown_R' do
412
+ it 'should invoke `chown -r`' do
413
+ EM.run do
414
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
415
+ cmd = EM::FileUtils.chown_R 'arthur', 'users', '/usr/bin/test'
416
+ cmd.command.should == 'chown -R arthur:users /usr/bin/test'
417
+ EM.stop_event_loop
418
+ end
419
+ end
420
+ end
421
+
422
+ describe '.touch' do
423
+ it 'should invoke `touch`' do
424
+ EM.run do
425
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
426
+ cmd = EM::FileUtils.touch '/usr/bin/test'
427
+ cmd.command.should == 'touch /usr/bin/test'
428
+ EM.stop_event_loop
429
+ end
430
+ end
431
+ end
432
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe EM::FS::RsyncCommand do
4
+ context 'copying one file' do
5
+ before :all do
6
+ @source = File.join SPEC_ROOT, 'data', 'test'
7
+ @target = File.join SPEC_ROOT, 'data', 'test.copy'
8
+ @progress_updates = {}
9
+ EM.run do
10
+ EM::FS.rsync @source, @target do |on|
11
+ on.progress do |file, bytes|
12
+ (@progress_updates[file] ||= []) << bytes
13
+ end
14
+
15
+ on.exit do |status|
16
+ EM.stop_event_loop
17
+ raise on.stderr.output if status.exitstatus != 0
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ after :all do
24
+ FileUtils.rm_rf @target
25
+ end
26
+
27
+ it 'should create a copy' do
28
+ File.should exist @target
29
+ end
30
+
31
+ it 'should track progress' do
32
+ @progress_updates['test'].length.should be > 0
33
+ @progress_updates['test'].last.should == 102400
34
+ end
35
+ end
36
+
37
+ context 'copying multiple files' do
38
+ before :all do
39
+ @source1 = File.join SPEC_ROOT, 'data', 'test'
40
+ @source2 = File.join SPEC_ROOT, 'data', 'test2'
41
+ @source3 = File.join SPEC_ROOT, 'data', 'test3'
42
+ @target = File.join SPEC_ROOT, 'data', 'test.dir'
43
+ @progress_updates = {}
44
+ EM.run do
45
+ EM::FS.rsync @source1, @source2, @source3, @target do |on|
46
+ on.progress do |file, bytes|
47
+ (@progress_updates[file] ||= []) << bytes
48
+ end
49
+
50
+ on.exit do |status|
51
+ EM.stop_event_loop
52
+ raise on.stderr.output if status.exitstatus != 0
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ after :all do
59
+ FileUtils.rm_rf @target
60
+ end
61
+
62
+ it 'should create a copy' do
63
+ File.should exist @target
64
+ File.should be_directory @target
65
+ File.should exist File.join(@target, 'test')
66
+ File.should exist File.join(@target, 'test2')
67
+ File.should exist File.join(@target, 'test3')
68
+ end
69
+
70
+ it 'should track progress for each file' do
71
+ @progress_updates['test'].length.should be > 0
72
+ @progress_updates['test'].last.should == 102400
73
+ @progress_updates['test2'].length.should be > 0
74
+ @progress_updates['test2'].last.should == 102400
75
+ @progress_updates['test3'].length.should be > 0
76
+ @progress_updates['test3'].last.should == 102400
77
+ end
78
+ end
79
+ end
data/spec/fs_spec.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe EM::FS do
4
+ describe '.rsync' do
5
+ it 'should create an `RsyncCommand` with :progress flag' do
6
+ EM.run do
7
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
8
+ cmd = EM::FS.rsync '/usr/share/test1', '/usr/share/test2'
9
+ cmd.should be_a EM::FS::RsyncCommand
10
+ cmd.command.should == 'rsync --progress /usr/share/test1 /usr/share/test2'
11
+ EM.stop_event_loop
12
+ end
13
+ end
14
+ end
15
+
16
+ describe '.find' do
17
+ it 'should create `SystemCommand`' do
18
+ EM.run do
19
+ @dir = File.join(SPEC_ROOT, 'data', 'test')
20
+ cmd = EM::FS.find '.'
21
+ cmd.should be_a EM::SystemCommand
22
+ cmd.command.should == 'find .'
23
+ EM.stop_event_loop
24
+ end
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,30 @@
1
+ require 'eventmachine'
2
+ require 'em-fs'
3
+
4
+ SPEC_ROOT = File.expand_path(File.dirname(__FILE__))
5
+
6
+ EventMachine.instance_eval do
7
+ def assertions time = 1
8
+ EM.add_timer(time) do
9
+ EM.stop_event_loop
10
+ yield
11
+ end
12
+ end
13
+ end unless EM.respond_to?(:assertions)
14
+
15
+ unless File.exists?(File.join(SPEC_ROOT, 'data', 'test')) and
16
+ File.exists?(File.join(SPEC_ROOT, 'data', 'test2')) and
17
+ File.exists?(File.join(SPEC_ROOT, 'data', 'test3'))
18
+ puts "Creating test dummy data"
19
+ system "mkdir -p #{File.join(SPEC_ROOT, 'data')}"
20
+ system "dd if=/dev/urandom of=#{File.join(SPEC_ROOT, 'data', 'test')} bs=1024 count=100"
21
+ system "dd if=/dev/urandom of=#{File.join(SPEC_ROOT, 'data', 'test2')} bs=1024 count=100"
22
+ system "dd if=/dev/urandom of=#{File.join(SPEC_ROOT, 'data', 'test3')} bs=1024 count=100"
23
+ end
24
+
25
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
26
+ RSpec.configure do |config|
27
+ config.treat_symbols_as_metadata_keys_with_true_values = true
28
+ config.run_all_when_everything_filtered = true
29
+ config.filter_run :focus
30
+ end