epitools 0.5.7 → 0.5.8
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/epitools.gemspec +3 -3
- data/lib/epitools/autoloads.rb +1 -0
- data/lib/epitools/core_ext.rb +85 -33
- data/lib/epitools/core_ext/array.rb +16 -13
- data/lib/epitools/core_ext/enumerable.rb +27 -0
- data/lib/epitools/hexdump.rb +47 -38
- data/lib/epitools/iter.rb +1 -1
- data/lib/epitools/numwords.rb +1 -1
- data/lib/epitools/path.rb +215 -185
- data/lib/epitools/sys.rb +25 -1
- data/lib/epitools/term.rb +37 -31
- data/lib/epitools/wm.rb +293 -2
- data/spec/core_ext_spec.rb +20 -1
- data/spec/path_spec.rb +95 -87
- data/spec/wm_spec.rb +10 -0
- metadata +3 -3
data/spec/core_ext_spec.rb
CHANGED
@@ -162,7 +162,7 @@ describe Numeric do
|
|
162
162
|
1.5.exp.should == Math.exp(1.5)
|
163
163
|
|
164
164
|
253.log(5).should == Math.log(253,5)
|
165
|
-
(2**(4212.log(2))).should == 4212.0
|
165
|
+
(2**(4212.log(2))).round.should == 4212.0
|
166
166
|
end
|
167
167
|
|
168
168
|
end
|
@@ -424,6 +424,17 @@ describe Enumerable do
|
|
424
424
|
|
425
425
|
end
|
426
426
|
|
427
|
+
describe Enumerator do
|
428
|
+
|
429
|
+
it "spins" do
|
430
|
+
lambda {
|
431
|
+
(1..20).each.with_spinner(1).each { }
|
432
|
+
}.should_not raise_error
|
433
|
+
end
|
434
|
+
|
435
|
+
end
|
436
|
+
|
437
|
+
|
427
438
|
describe Hash do
|
428
439
|
|
429
440
|
before :each do
|
@@ -542,6 +553,14 @@ describe Range do
|
|
542
553
|
|
543
554
|
end
|
544
555
|
|
556
|
+
describe ObjectSpace do
|
557
|
+
|
558
|
+
it "is Enumerable" do
|
559
|
+
ObjectSpace.select { |o| o.is_a? String }.map { |o| o.class.name }.uniq.should == ["String"]
|
560
|
+
end
|
561
|
+
|
562
|
+
end
|
563
|
+
|
545
564
|
describe "truthiness" do
|
546
565
|
|
547
566
|
it "is truthy!" do
|
data/spec/path_spec.rb
CHANGED
@@ -1,78 +1,78 @@
|
|
1
1
|
require 'epitools'
|
2
2
|
|
3
3
|
describe Path do
|
4
|
-
|
4
|
+
|
5
5
|
it "initializes and accesses everything" do
|
6
6
|
path = Path.new("/blah/what.mp4/.mp3/hello.avi")
|
7
|
-
|
7
|
+
|
8
8
|
path.dirs.should == %w[ blah what.mp4 .mp3 ]
|
9
9
|
path.dir.should == "/blah/what.mp4/.mp3"
|
10
10
|
path.filename.should == "hello.avi"
|
11
11
|
path.ext.should == "avi"
|
12
12
|
path.base.should == "hello"
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
it "works with relative paths" do
|
16
16
|
path = Path.new("../hello.mp3/blah")
|
17
|
-
|
17
|
+
|
18
18
|
path.filename.should == "blah"
|
19
19
|
path.ext.should == nil
|
20
|
-
|
20
|
+
|
21
21
|
abs_path = File.join(File.expand_path(".."), "hello.mp3")
|
22
|
-
path.dir.should == abs_path
|
22
|
+
path.dir.should == abs_path
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
it "'relative_to's" do
|
26
26
|
Path["/etc"].relative_to(Path["/tmp"]).should == "../tmp"
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
it "handles directories" do
|
30
30
|
path = Path.new("/etc/")
|
31
|
-
|
31
|
+
|
32
32
|
path.dirs.should_not == nil
|
33
33
|
path.dir.should == "/etc"
|
34
34
|
path.filename.should == nil
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
it "replaces ext" do
|
38
38
|
path = Path.new("/blah/what.mp4/.mp3/hello.avi")
|
39
39
|
path.ext.should == "avi"
|
40
40
|
|
41
41
|
path.ext = "mkv"
|
42
42
|
path.ext.should == "mkv"
|
43
|
-
|
43
|
+
|
44
44
|
path.filename[-4..-1].should == ".mkv"
|
45
45
|
end
|
46
46
|
|
47
47
|
it "replaces filename" do
|
48
48
|
path = Path.new(__FILE__)
|
49
49
|
path.dir?.should == false
|
50
|
-
|
50
|
+
|
51
51
|
path.filename = nil
|
52
52
|
path.dir?.should == true
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
it "fstats" do
|
56
56
|
path = Path.new(__FILE__)
|
57
|
-
|
57
|
+
|
58
58
|
path.exists?.should == true
|
59
59
|
path.dir?.should == false
|
60
60
|
path.file?.should == true
|
61
61
|
path.symlink?.should == false
|
62
62
|
path.mtime.class.should == Time
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
it "globs" do
|
66
66
|
path = Path.new(__FILE__)
|
67
67
|
glob = path.dir + "/*spec.rb"
|
68
68
|
specs = Path.glob(glob)
|
69
69
|
path.in?(specs).should == true
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
it "Path[file] and Path[glob]s" do
|
73
73
|
path = Path.new(__FILE__)
|
74
74
|
path.should == Path[__FILE__]
|
75
|
-
|
75
|
+
|
76
76
|
glob = path.dir + "/*spec.rb"
|
77
77
|
specs = Path.glob(glob)
|
78
78
|
Path[glob].should == specs
|
@@ -83,35 +83,35 @@ describe Path do
|
|
83
83
|
data = File.read(path)
|
84
84
|
data.size.should > 0
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
it "opens & read files" do
|
88
88
|
path = Path.new(__FILE__)
|
89
|
-
|
89
|
+
|
90
90
|
path.open do |f|
|
91
91
|
f.read.size.should > 0
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
path.read.size.should > 0
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
it "reads with length and offset" do
|
98
98
|
path = Path.new(__FILE__)
|
99
99
|
|
100
100
|
path.read(25).size.should == 25
|
101
|
-
|
101
|
+
|
102
102
|
s1 = path.read(25,15)
|
103
103
|
s2 = path.read(40)
|
104
|
-
|
105
|
-
s2[15..-1].should == s1
|
104
|
+
|
105
|
+
s2[15..-1].should == s1
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
it "reads/writes various formats (json, yaml, etc.)" do
|
109
109
|
data = { "hello" => "there", "amazing" => [1,2,3,4] }
|
110
|
-
|
110
|
+
|
111
111
|
yaml = Path.tmpfile
|
112
112
|
yaml.write_yaml(data)
|
113
113
|
yaml.read_yaml.should == data
|
114
|
-
|
114
|
+
|
115
115
|
json = Path.tmpfile
|
116
116
|
json.write_json(data)
|
117
117
|
json.read_json.should == data
|
@@ -129,7 +129,7 @@ describe Path do
|
|
129
129
|
f.write_json(data)
|
130
130
|
f.parse.should == data
|
131
131
|
end
|
132
|
-
|
132
|
+
|
133
133
|
it "makes paths THREE WAYS!" do
|
134
134
|
[
|
135
135
|
Path(__FILE__),
|
@@ -139,12 +139,12 @@ describe Path do
|
|
139
139
|
p1.path.should == p2.path
|
140
140
|
end
|
141
141
|
end
|
142
|
-
|
142
|
+
|
143
143
|
it "appending to paths with /" do
|
144
144
|
( Path['/etc']/'passwd' ).should == Path['/etc/passwd']
|
145
145
|
( Path['/etc']/Path['passwd'] ).should_not == Path['/etc/passwd']
|
146
146
|
end
|
147
|
-
|
147
|
+
|
148
148
|
it "lets you change the dirs array" do
|
149
149
|
path = Path['/spam/spam/spam/']
|
150
150
|
path.dirs.should == %w[ spam spam spam ]
|
@@ -152,7 +152,7 @@ describe Path do
|
|
152
152
|
path.dir.should == '/spam/spam/spam/humbug'
|
153
153
|
path.path.should == '/spam/spam/spam/humbug/'
|
154
154
|
end
|
155
|
-
|
155
|
+
|
156
156
|
it "handles URLs" do
|
157
157
|
path = Path["http://google.com/?search=blah"]
|
158
158
|
path.host.should == "google.com"
|
@@ -160,44 +160,44 @@ describe Path do
|
|
160
160
|
path.query.should == {"search" => "blah"}
|
161
161
|
path.uri?.should == true
|
162
162
|
end
|
163
|
-
|
163
|
+
|
164
164
|
it "tempfiles" do
|
165
165
|
path = Path.tmpfile
|
166
166
|
path.exists?.should == true
|
167
|
-
|
167
|
+
|
168
168
|
path.write "blah"
|
169
169
|
path.read.should == "blah"
|
170
|
-
|
170
|
+
|
171
171
|
path.delete!
|
172
172
|
path.exists?.should == false
|
173
173
|
end
|
174
|
-
|
174
|
+
|
175
175
|
it "appends and writes" do
|
176
176
|
path = Path.tmpfile
|
177
177
|
path.exists?.should == true
|
178
|
-
|
178
|
+
|
179
179
|
path.write "blah"
|
180
|
-
path.append "what"
|
180
|
+
path.append "what"
|
181
181
|
(path << "lul").should == path
|
182
|
-
|
182
|
+
|
183
183
|
path.read.should == "blahwhatlul"
|
184
|
-
|
184
|
+
|
185
185
|
path.write "rawr"
|
186
|
-
|
186
|
+
|
187
187
|
path.read.should == "rawr"
|
188
188
|
end
|
189
|
-
|
189
|
+
|
190
190
|
it "renames" do
|
191
191
|
path = Path.tmpfile
|
192
|
-
|
192
|
+
|
193
193
|
str = path.to_s
|
194
|
-
|
194
|
+
|
195
195
|
path.rename(:ext=>".dat")
|
196
|
-
|
197
|
-
path.to_s.should_not == str
|
196
|
+
|
197
|
+
path.to_s.should_not == str
|
198
198
|
path.to_s.should == str+".dat"
|
199
199
|
end
|
200
|
-
|
200
|
+
|
201
201
|
it "rms" do
|
202
202
|
path = Path.tmpfile
|
203
203
|
path << "data"
|
@@ -205,7 +205,7 @@ describe Path do
|
|
205
205
|
path.rm.should == true
|
206
206
|
path.exists?.should == false
|
207
207
|
end
|
208
|
-
|
208
|
+
|
209
209
|
it "truncates" do
|
210
210
|
path = Path.tmpfile
|
211
211
|
|
@@ -218,7 +218,7 @@ describe Path do
|
|
218
218
|
path.truncate
|
219
219
|
path.size.should == 0
|
220
220
|
end
|
221
|
-
|
221
|
+
|
222
222
|
it "checksums" do
|
223
223
|
a, b = Path["*.rb"].take(2)
|
224
224
|
[:sha1, :sha2, :md5].each do |meth|
|
@@ -230,7 +230,7 @@ describe Path do
|
|
230
230
|
sum2.should =~ /[a-z0-9]/
|
231
231
|
end
|
232
232
|
end
|
233
|
-
|
233
|
+
|
234
234
|
it "mkdirs" do
|
235
235
|
tmp = Path.tmpfile
|
236
236
|
lambda { tmp.mkdir }.should raise_error
|
@@ -238,97 +238,97 @@ describe Path do
|
|
238
238
|
tmp.mkdir.should be_truthy
|
239
239
|
tmp.rm.should be_truthy
|
240
240
|
end
|
241
|
-
|
241
|
+
|
242
242
|
it "has classmethods" do
|
243
243
|
path = Path.tmpfile
|
244
|
-
|
244
|
+
|
245
245
|
path << "whee"*100
|
246
246
|
path.sha1.should == Path.sha1(path)
|
247
247
|
end
|
248
|
-
|
248
|
+
|
249
249
|
it "gzips and gunzips" do
|
250
250
|
path = Path.tmpfile
|
251
251
|
500.times { path << "whee" }
|
252
|
-
|
252
|
+
|
253
253
|
path.ext.should_not == "gz"
|
254
254
|
gzipped = path.gzip
|
255
|
-
|
255
|
+
|
256
256
|
before = path.size
|
257
257
|
after = gzipped.size
|
258
258
|
before.should > after
|
259
259
|
gzipped.ext.should == "gz"
|
260
|
-
|
260
|
+
|
261
261
|
gunzipped = gzipped.gunzip
|
262
262
|
gunzipped.size.should == before
|
263
263
|
gunzipped.should == path
|
264
264
|
end
|
265
|
-
|
265
|
+
|
266
266
|
it "exts" do
|
267
267
|
path = Path["file.tar.gz"]
|
268
268
|
path.ext.should == "gz"
|
269
269
|
path.exts.should == ["tar", "gz"]
|
270
270
|
end
|
271
|
-
|
271
|
+
|
272
272
|
it "ios and streams" do
|
273
273
|
path = Path.tmpfile
|
274
274
|
f = open(path)
|
275
275
|
f.inspect.should == path.io.inspect
|
276
276
|
f.inspect.should == path.stream.inspect
|
277
277
|
end
|
278
|
-
|
278
|
+
|
279
279
|
it "mimes" do
|
280
280
|
Path[__FILE__].mimetype.should == "application/x-ruby"
|
281
281
|
end
|
282
|
-
|
282
|
+
|
283
283
|
it "magic types" do
|
284
284
|
Path[__FILE__].type.should == "rb"
|
285
285
|
end
|
286
|
-
|
286
|
+
|
287
287
|
it "whiches" do
|
288
288
|
Path.which("ruby").should_not be_nil
|
289
289
|
Path.which("asdfasdfhkajlsdhfkljashdf").should be_nil
|
290
290
|
Path.which("ruby").class.should == Path
|
291
291
|
Path.which("gzip", "ls", "rm").should == ["/bin/gzip", "/bin/ls", "/bin/rm"]
|
292
292
|
end
|
293
|
-
|
293
|
+
|
294
294
|
it "Path[]s another path" do
|
295
295
|
path = Path.tmpfile
|
296
296
|
Path[path].path.should == path.path
|
297
297
|
end
|
298
|
-
|
298
|
+
|
299
299
|
it "uses advanced glob features" do
|
300
300
|
# ruby-1.9.2-p180 :001 > Path["~/.ssh/id_{dsa,rsa}.pub"]
|
301
|
-
# => /home/epi/.ssh/id_{dsa,rsa}.pub
|
301
|
+
# => /home/epi/.ssh/id_{dsa,rsa}.pub
|
302
302
|
# ruby-1.9.2-p180 :002 > Dir["~/.ssh/id_{dsa,rsa}.pub"]
|
303
|
-
# => []
|
303
|
+
# => []
|
304
304
|
# ruby-1.9.2-p180 :003 > Dir["../../.ssh/id_{dsa,rsa}.pub"]
|
305
305
|
# => ["../../.ssh/id_rsairb.pub"]
|
306
|
-
|
306
|
+
|
307
307
|
Path["~/.ssh/id_{dsa,rsa}.pub"].size.should > 0
|
308
308
|
end
|
309
|
-
|
309
|
+
|
310
310
|
it "modes" do
|
311
311
|
Path.tmpfile.mode.class.should == Fixnum
|
312
312
|
end
|
313
|
-
|
313
|
+
|
314
314
|
it "chmods and chmod_Rs" do
|
315
315
|
tmp = Path.tmpfile
|
316
316
|
tmp2 = Path.tmpfile
|
317
317
|
tmp.touch
|
318
318
|
tmp2.touch
|
319
|
-
|
320
|
-
newmode = tmp.mode
|
319
|
+
|
320
|
+
newmode = tmp.mode
|
321
321
|
tmp.chmod("+x")
|
322
322
|
system("chmod", "+x", tmp2)
|
323
323
|
tmp.mode.should == tmp2.mode
|
324
324
|
end
|
325
|
-
|
325
|
+
|
326
326
|
it "siblingses" do
|
327
327
|
sibs = Path.tempfile.siblings
|
328
328
|
sibs.is_an?(Array).should == true
|
329
329
|
sibs.include?(self).should == false
|
330
330
|
end
|
331
|
-
|
331
|
+
|
332
332
|
it 'path/".."s shows parent dir of file' do
|
333
333
|
# path/
|
334
334
|
tmp = Path.tmpfile
|
@@ -343,14 +343,14 @@ describe Path do
|
|
343
343
|
file.dirs.should == tmp.dirs
|
344
344
|
file.filename.should != tmp.filename
|
345
345
|
end
|
346
|
-
|
346
|
+
|
347
347
|
it 'parents and childs properly' do
|
348
348
|
root = Path["/"]
|
349
349
|
parent = Path["/blah/stuff"]
|
350
350
|
child = Path["/blah/stuff/what"]
|
351
351
|
neither = Path["/whee/yay"]
|
352
|
-
|
353
|
-
# Table of parent=>child relationships
|
352
|
+
|
353
|
+
# Table of parent=>child relationships
|
354
354
|
{
|
355
355
|
parent => child,
|
356
356
|
root => parent,
|
@@ -359,15 +359,15 @@ describe Path do
|
|
359
359
|
}.each do |p, c|
|
360
360
|
p.parent_of?(c).should == true
|
361
361
|
c.parent_of?(p).should == false
|
362
|
-
|
362
|
+
|
363
363
|
c.child_of?(p).should == true
|
364
364
|
p.child_of?(c).should == false
|
365
365
|
end
|
366
|
-
|
366
|
+
|
367
367
|
neither.parent_of?(child).should == false
|
368
368
|
neither.parent_of?(parent).should == false
|
369
369
|
end
|
370
|
-
|
370
|
+
|
371
371
|
it "checks file modes" do
|
372
372
|
path = Path.tmpfile
|
373
373
|
path.exe?.should == false
|
@@ -375,39 +375,47 @@ describe Path do
|
|
375
375
|
p path.mode
|
376
376
|
(path.mode & 0o666).should > 0
|
377
377
|
end
|
378
|
-
|
378
|
+
|
379
379
|
it 'symlinks and symlink_targets' do
|
380
380
|
path = Path.tmpfile
|
381
381
|
path << "some data"
|
382
|
-
|
382
|
+
|
383
383
|
target = "#{path}-symlinked"
|
384
|
-
|
384
|
+
|
385
385
|
path.ln_s target
|
386
|
-
|
386
|
+
|
387
387
|
target = Path[target]
|
388
388
|
target.symlink?.should == true
|
389
389
|
target.read.should == path.read
|
390
|
-
target.symlink_target.should == path
|
390
|
+
target.symlink_target.should == path
|
391
391
|
end
|
392
|
-
|
392
|
+
|
393
393
|
it 'swaps two files' do
|
394
394
|
raise "errorn!"
|
395
395
|
# swap two regular files
|
396
396
|
# swap a symlink and a regular file
|
397
397
|
# swap two symlinks
|
398
398
|
end
|
399
|
-
|
399
|
+
|
400
400
|
it 'realpaths' do
|
401
401
|
etc = Path["/etc"]
|
402
402
|
tmp = Path.tmpfile
|
403
403
|
tmp.rm
|
404
404
|
etc.ln_s tmp
|
405
|
-
|
405
|
+
|
406
406
|
tmp.symlink_target.should == etc
|
407
407
|
tmp.realpath.should == etc
|
408
408
|
Path["/etc/../etc"].realpath.should == etc
|
409
409
|
end
|
410
|
-
|
411
|
-
|
410
|
+
|
411
|
+
it "shouldn't glob with Path#join" do
|
412
|
+
path = Path["/etc"].join("blah{}")
|
413
|
+
path.should == "/etc/blah{}"
|
414
|
+
end
|
415
|
+
|
416
|
+
it "should glob with Path#/" do
|
417
|
+
entries = Path["/etc"]/"*"
|
418
|
+
entries.should be_an Array
|
419
|
+
end
|
412
420
|
|
413
421
|
end
|