filepath 0.6 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -5
- data/README.md +14 -14
- data/filepath.gemspec +8 -10
- data/lib/filepath/core_ext/array.rb +10 -10
- data/lib/filepath/core_ext/string.rb +4 -4
- data/lib/filepath/filepath.rb +38 -38
- data/lib/filepath/filepathlist.rb +12 -12
- data/spec/filepath_spec.rb +67 -67
- data/spec/filepathlist_spec.rb +53 -53
- data/spec/fixtures.rb +21 -0
- data/spec/spec_helper.rb +13 -3
- data/spec/tasks.rb +1 -19
- metadata +36 -21
@@ -1,7 +1,7 @@
|
|
1
1
|
# This is free software released into the public domain (CC0 license).
|
2
2
|
|
3
3
|
|
4
|
-
class
|
4
|
+
class FilepathList
|
5
5
|
include Enumerable
|
6
6
|
|
7
7
|
SEPARATOR = ':'.freeze
|
@@ -13,7 +13,7 @@ class FilePathList
|
|
13
13
|
|
14
14
|
def select_entries(type)
|
15
15
|
raw_entries = @entries.delete_if { |e| !e.send(type.to_s + '?') }
|
16
|
-
return
|
16
|
+
return FilepathList.new(raw_entries)
|
17
17
|
end
|
18
18
|
|
19
19
|
def files
|
@@ -33,26 +33,26 @@ class FilePathList
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def +(extra_entries)
|
36
|
-
return
|
36
|
+
return FilepathList.new(@entries + extra_entries.to_a)
|
37
37
|
end
|
38
38
|
|
39
39
|
def -(others)
|
40
40
|
remaining_entries = @entries - others.as_path_list.to_a
|
41
41
|
|
42
|
-
return
|
42
|
+
return FilepathList.new(remaining_entries)
|
43
43
|
end
|
44
44
|
|
45
45
|
def <<(extra_path)
|
46
|
-
return
|
46
|
+
return FilepathList.new(@entries + [extra_path.as_path])
|
47
47
|
end
|
48
48
|
|
49
49
|
def *(other_list)
|
50
|
-
if !other_list.is_a?
|
51
|
-
other_list =
|
50
|
+
if !other_list.is_a? FilepathList
|
51
|
+
other_list = FilepathList.new(Array(other_list))
|
52
52
|
end
|
53
53
|
other_entries = other_list.entries
|
54
54
|
paths = @entries.product(other_entries).map { |p1, p2| p1 / p2 }
|
55
|
-
return
|
55
|
+
return FilepathList.new(paths)
|
56
56
|
end
|
57
57
|
|
58
58
|
def remove_common_segments
|
@@ -75,10 +75,10 @@ class FilePathList
|
|
75
75
|
|
76
76
|
remaining_segs = all_segs.map { |segs| segs[idx_different..-1] }
|
77
77
|
|
78
|
-
return
|
78
|
+
return FilepathList.new(remaining_segs)
|
79
79
|
end
|
80
80
|
|
81
|
-
# @return [
|
81
|
+
# @return [FilepathList] the path list itself
|
82
82
|
|
83
83
|
def as_path_list
|
84
84
|
self
|
@@ -130,7 +130,7 @@ class FilePathList
|
|
130
130
|
module EntriesMethods
|
131
131
|
def map(&block)
|
132
132
|
mapped_entries = @entries.map(&block)
|
133
|
-
return
|
133
|
+
return FilepathList.new(mapped_entries)
|
134
134
|
end
|
135
135
|
|
136
136
|
def select(pattern = nil, &block)
|
@@ -140,7 +140,7 @@ class FilePathList
|
|
140
140
|
|
141
141
|
remaining_entries = @entries.select { |e| block.call(e) }
|
142
142
|
|
143
|
-
return
|
143
|
+
return FilepathList.new(remaining_entries)
|
144
144
|
end
|
145
145
|
|
146
146
|
def exclude(pattern = nil, &block)
|
data/spec/filepath_spec.rb
CHANGED
@@ -2,18 +2,18 @@
|
|
2
2
|
|
3
3
|
require File.join(File.dirname(__FILE__), 'spec_helper')
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe Filepath do
|
6
6
|
before(:all) do
|
7
|
-
@root =
|
7
|
+
@root = Filepath.new(FIXTURES_DIR)
|
8
8
|
end
|
9
9
|
|
10
10
|
it "can be created from a string" do
|
11
|
-
|
11
|
+
Filepath.new("foo").should be_a Filepath
|
12
12
|
end
|
13
13
|
|
14
|
-
it "can be created from another
|
15
|
-
orig =
|
16
|
-
|
14
|
+
it "can be created from another Filepath" do
|
15
|
+
orig = Filepath.new("foo")
|
16
|
+
Filepath.new(orig).should be_a Filepath
|
17
17
|
end
|
18
18
|
|
19
19
|
describe "#/" do
|
@@ -26,23 +26,23 @@ describe FilePath do
|
|
26
26
|
]
|
27
27
|
test_data.each do |base, extra, result|
|
28
28
|
it "concatenates `#{base}` and `#{extra}` (as String) into `#{result}`" do
|
29
|
-
ph =
|
29
|
+
ph = Filepath.new(base) / extra
|
30
30
|
ph.should == result
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
test_data.each do |base, extra, result|
|
35
|
-
it "concatenates `#{base}` and `#{extra}` (as
|
36
|
-
ph =
|
35
|
+
it "concatenates `#{base}` and `#{extra}` (as Filepath) into `#{result}`" do
|
36
|
+
ph = Filepath.new(base) / Filepath.new(extra)
|
37
37
|
ph.should == result
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
describe "#+" do
|
43
|
-
it "is deprecated but performs as
|
44
|
-
p1 =
|
45
|
-
p2 =
|
43
|
+
it "is deprecated but performs as Filepath#/" do
|
44
|
+
p1 = Filepath.new("a")
|
45
|
+
p2 = Filepath.new("b")
|
46
46
|
|
47
47
|
p1.should_receive(:warn).with(/is deprecated/)
|
48
48
|
(p1 + p2).should == (p1 / p2)
|
@@ -74,7 +74,7 @@ describe FilePath do
|
|
74
74
|
]
|
75
75
|
test_data.each do |path, result|
|
76
76
|
it "says that `#{result}` is the filename of `#{path}`" do
|
77
|
-
ph =
|
77
|
+
ph = Filepath.new(path)
|
78
78
|
ph.filename.should == result
|
79
79
|
end
|
80
80
|
end
|
@@ -90,7 +90,7 @@ describe FilePath do
|
|
90
90
|
]
|
91
91
|
test_data.each do |path, result|
|
92
92
|
it "says that `#{result}` is the parent dir of `#{path}`" do
|
93
|
-
ph =
|
93
|
+
ph = Filepath.new(path)
|
94
94
|
ph.parent_dir.should == result
|
95
95
|
end
|
96
96
|
end
|
@@ -108,7 +108,7 @@ describe FilePath do
|
|
108
108
|
]
|
109
109
|
test_data.each do |path, base, result|
|
110
110
|
it "says that `#{path}` relative to `#{base}` is `#{result}`" do
|
111
|
-
ph =
|
111
|
+
ph = Filepath.new(path)
|
112
112
|
ph.relative_to(base).should == result
|
113
113
|
end
|
114
114
|
end
|
@@ -121,7 +121,7 @@ describe FilePath do
|
|
121
121
|
]
|
122
122
|
test_data2.each do |path, base|
|
123
123
|
it "raise an exception because `#{path}` and `#{base}` have different prefixes" do
|
124
|
-
ph =
|
124
|
+
ph = Filepath.new(path)
|
125
125
|
expect { ph.relative_to(base) }.to raise_error(ArgumentError)
|
126
126
|
end
|
127
127
|
end
|
@@ -137,7 +137,7 @@ describe FilePath do
|
|
137
137
|
]
|
138
138
|
test_data.each do |path, base, result|
|
139
139
|
it "says that `#{path}` relative to the file `#{base}` is `#{result}`" do
|
140
|
-
ph =
|
140
|
+
ph = Filepath.new(path)
|
141
141
|
ph.relative_to_file(base).should == result
|
142
142
|
end
|
143
143
|
end
|
@@ -151,7 +151,7 @@ describe FilePath do
|
|
151
151
|
]
|
152
152
|
test_data.each do |base, new, result|
|
153
153
|
it "changes `#{base}` + `#{new}` into `#{result}`" do
|
154
|
-
ph =
|
154
|
+
ph = Filepath.new(base)
|
155
155
|
ph.with_filename(new).should == result
|
156
156
|
end
|
157
157
|
end
|
@@ -169,7 +169,7 @@ describe FilePath do
|
|
169
169
|
]
|
170
170
|
test_data.each do |path, ext|
|
171
171
|
it "says that `#{path}` has extension `#{ext}`" do
|
172
|
-
|
172
|
+
Filepath.new(path).extension.should == ext
|
173
173
|
end
|
174
174
|
end
|
175
175
|
end
|
@@ -182,7 +182,7 @@ describe FilePath do
|
|
182
182
|
]
|
183
183
|
with_extension.each do |path|
|
184
184
|
it "says that <#{path}> has an extension" do
|
185
|
-
|
185
|
+
Filepath.new(path).extension?.should be true
|
186
186
|
end
|
187
187
|
end
|
188
188
|
|
@@ -193,7 +193,7 @@ describe FilePath do
|
|
193
193
|
]
|
194
194
|
no_extension.each do |path|
|
195
195
|
it "says that <#{path}> has no extension" do
|
196
|
-
|
196
|
+
Filepath.new(path).extension?.should be false
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
@@ -205,12 +205,12 @@ describe FilePath do
|
|
205
205
|
]
|
206
206
|
extension_data.each do |path, ext|
|
207
207
|
it "says that <#{path}> extesions is #{ext.inspect}" do
|
208
|
-
|
208
|
+
Filepath.new(path).extension?(ext).should be true
|
209
209
|
end
|
210
210
|
end
|
211
211
|
|
212
212
|
it "says that `foo.bar` extension is not `baz`" do
|
213
|
-
|
213
|
+
Filepath.new('foo.bar').extension?('baz').should be false
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
@@ -224,7 +224,7 @@ describe FilePath do
|
|
224
224
|
]
|
225
225
|
test_data.each do |path, result|
|
226
226
|
it "replaces `#{path}` with `baz` into `#{result}`" do
|
227
|
-
new =
|
227
|
+
new = Filepath.new(path).with_extension('baz')
|
228
228
|
new.basename.to_s.should == result
|
229
229
|
end
|
230
230
|
end
|
@@ -240,7 +240,7 @@ describe FilePath do
|
|
240
240
|
]
|
241
241
|
test_data.each do |path, result|
|
242
242
|
it "turns `#{path}` into `#{result}`" do
|
243
|
-
new =
|
243
|
+
new = Filepath.new(path).without_extension
|
244
244
|
new.basename.to_s.should == result
|
245
245
|
end
|
246
246
|
end
|
@@ -248,47 +248,47 @@ describe FilePath do
|
|
248
248
|
|
249
249
|
describe "=~" do
|
250
250
|
it "matches `/foo/bar` with /foo/" do
|
251
|
-
|
251
|
+
Filepath.new('/foo/bar').should =~ /foo/
|
252
252
|
end
|
253
253
|
|
254
254
|
it "does not match `/foo/bar` with /baz/" do
|
255
|
-
|
255
|
+
Filepath.new('/foo/bar').should_not =~ /baz/
|
256
256
|
end
|
257
257
|
|
258
258
|
it "matches `/foo/bar` with /o\\/ba" do
|
259
|
-
|
259
|
+
Filepath.new('/foo/bar').should =~ /o\/b/
|
260
260
|
end
|
261
261
|
|
262
262
|
it "matches `/foo/bar/../quux` with /foo\\/quux/" do
|
263
|
-
|
263
|
+
Filepath.new('/foo/bar/../quux').should =~ /foo\/quux/
|
264
264
|
end
|
265
265
|
end
|
266
266
|
|
267
267
|
describe "#root?" do
|
268
268
|
it "says that </> points to the root directory" do
|
269
|
-
|
269
|
+
Filepath.new('/').should be_root
|
270
270
|
end
|
271
271
|
|
272
272
|
it "says that </..> points to the root directory" do
|
273
|
-
|
273
|
+
Filepath.new('/..').should be_root
|
274
274
|
end
|
275
275
|
|
276
276
|
it "says that <a/b> does not point to the root directory" do
|
277
|
-
|
277
|
+
Filepath.new('a/b').should_not be_root
|
278
278
|
end
|
279
279
|
|
280
280
|
it "says that </foo> does not point to the root directory" do
|
281
|
-
|
281
|
+
Filepath.new('/foo/bar').should_not be_root
|
282
282
|
end
|
283
283
|
end
|
284
284
|
|
285
285
|
describe "#absolute?" do
|
286
286
|
it "says that `/foo/bar` is absolute" do
|
287
|
-
|
287
|
+
Filepath.new('/foo/bar').should be_absolute
|
288
288
|
end
|
289
289
|
|
290
290
|
it "sasys that `foo/bar` is not absolute" do
|
291
|
-
|
291
|
+
Filepath.new('foo/bar').should_not be_absolute
|
292
292
|
end
|
293
293
|
end
|
294
294
|
|
@@ -308,7 +308,7 @@ describe FilePath do
|
|
308
308
|
]
|
309
309
|
test_data.each do |path, result|
|
310
310
|
it "turns `#{path}` into `#{result}`" do
|
311
|
-
|
311
|
+
Filepath.new(path).normalized.to_raw_string.should == result
|
312
312
|
end
|
313
313
|
end
|
314
314
|
end
|
@@ -316,7 +316,7 @@ describe FilePath do
|
|
316
316
|
describe "#each_segment" do
|
317
317
|
it "goes through all the segments of an absolute path" do
|
318
318
|
steps = []
|
319
|
-
|
319
|
+
Filepath.new("/a/b/c").each_segment do |seg|
|
320
320
|
steps << seg
|
321
321
|
end
|
322
322
|
|
@@ -329,7 +329,7 @@ describe FilePath do
|
|
329
329
|
|
330
330
|
it "goes through all the segments of a relative path" do
|
331
331
|
steps = []
|
332
|
-
|
332
|
+
Filepath.new("a/b/c").each_segment do |seg|
|
333
333
|
steps << seg
|
334
334
|
end
|
335
335
|
|
@@ -340,7 +340,7 @@ describe FilePath do
|
|
340
340
|
end
|
341
341
|
|
342
342
|
it "returns the path itself" do
|
343
|
-
path =
|
343
|
+
path = Filepath.new("/a/b/c/")
|
344
344
|
path.each_segment { }.should be(path)
|
345
345
|
end
|
346
346
|
end
|
@@ -348,7 +348,7 @@ describe FilePath do
|
|
348
348
|
describe "#ascend" do
|
349
349
|
it "goes through all the segments of an absolute path" do
|
350
350
|
steps = []
|
351
|
-
|
351
|
+
Filepath.new("/a/b/c").ascend do |seg|
|
352
352
|
steps << seg
|
353
353
|
end
|
354
354
|
|
@@ -361,7 +361,7 @@ describe FilePath do
|
|
361
361
|
|
362
362
|
it "goes through all the segments of a relative path" do
|
363
363
|
steps = []
|
364
|
-
|
364
|
+
Filepath.new("a/b/c").ascend do |seg|
|
365
365
|
steps << seg
|
366
366
|
end
|
367
367
|
|
@@ -372,7 +372,7 @@ describe FilePath do
|
|
372
372
|
end
|
373
373
|
|
374
374
|
it "returns the path itself" do
|
375
|
-
path =
|
375
|
+
path = Filepath.new("/a/b/c/")
|
376
376
|
path.ascend { }.should be(path)
|
377
377
|
end
|
378
378
|
end
|
@@ -380,7 +380,7 @@ describe FilePath do
|
|
380
380
|
describe "#descend" do
|
381
381
|
it "goes through all the segments of an absolute path" do
|
382
382
|
steps = []
|
383
|
-
|
383
|
+
Filepath.new("/a/b/c").descend do |seg|
|
384
384
|
steps << seg
|
385
385
|
end
|
386
386
|
|
@@ -393,7 +393,7 @@ describe FilePath do
|
|
393
393
|
|
394
394
|
it "goes through all the segments of a relative path" do
|
395
395
|
steps = []
|
396
|
-
|
396
|
+
Filepath.new("a/b/c").descend do |seg|
|
397
397
|
steps << seg
|
398
398
|
end
|
399
399
|
|
@@ -404,26 +404,26 @@ describe FilePath do
|
|
404
404
|
end
|
405
405
|
|
406
406
|
it "returns the path itself" do
|
407
|
-
path =
|
407
|
+
path = Filepath.new("/a/b/c/")
|
408
408
|
path.descend { }.should be(path)
|
409
409
|
end
|
410
410
|
end
|
411
411
|
|
412
412
|
describe "#to_s" do
|
413
413
|
it "works on computed absolute paths" do
|
414
|
-
(
|
414
|
+
(Filepath.new('/') / 'a' / 'b').to_s.should eql('/a/b')
|
415
415
|
end
|
416
416
|
|
417
417
|
it "works on computed relative paths" do
|
418
|
-
(
|
418
|
+
(Filepath.new('a') / 'b').to_s.should eql('a/b')
|
419
419
|
end
|
420
420
|
|
421
421
|
it "returns normalized paths" do
|
422
|
-
|
422
|
+
Filepath.new("/foo/bar/..").to_s.should eql('/foo')
|
423
423
|
end
|
424
424
|
|
425
425
|
it "returns '.' for empty paths" do
|
426
|
-
|
426
|
+
Filepath.new('').to_s.should eql('.')
|
427
427
|
end
|
428
428
|
end
|
429
429
|
|
@@ -447,7 +447,7 @@ describe FilePath do
|
|
447
447
|
]
|
448
448
|
test_data.each do |ver1, ver2|
|
449
449
|
it "says that `#{ver1}` is equivalent to `#{ver2}`" do
|
450
|
-
ph =
|
450
|
+
ph = Filepath.new(ver1)
|
451
451
|
ph.should == ver2
|
452
452
|
end
|
453
453
|
end
|
@@ -474,7 +474,7 @@ describe FilePath do
|
|
474
474
|
p1.should_not eql(p2)
|
475
475
|
end
|
476
476
|
|
477
|
-
it "does not match objects that are not
|
477
|
+
it "does not match objects that are not Filepaths" do
|
478
478
|
p1 = '/foo/bar/baz'.as_path
|
479
479
|
p2 = '/foo/bar/baz'
|
480
480
|
|
@@ -524,7 +524,7 @@ describe FilePath do
|
|
524
524
|
end
|
525
525
|
end
|
526
526
|
|
527
|
-
describe
|
527
|
+
describe Filepath::MetadataInfo do
|
528
528
|
describe "#stat" do
|
529
529
|
it "returns a stat for the file" do
|
530
530
|
(@root / 'd1').stat.should be_directory
|
@@ -550,7 +550,7 @@ describe FilePath do
|
|
550
550
|
end
|
551
551
|
end
|
552
552
|
|
553
|
-
describe
|
553
|
+
describe Filepath::MetadataChanges do
|
554
554
|
describe "#chtime" do
|
555
555
|
it "change mtime" do
|
556
556
|
ph = @root / 'f1'
|
@@ -580,7 +580,7 @@ describe FilePath do
|
|
580
580
|
end
|
581
581
|
end
|
582
582
|
|
583
|
-
describe
|
583
|
+
describe Filepath::MetadataTests do
|
584
584
|
describe "#file?" do
|
585
585
|
it "says that `f1` is a file" do
|
586
586
|
(@root / 'f1').should be_file
|
@@ -675,7 +675,7 @@ describe FilePath do
|
|
675
675
|
end
|
676
676
|
end
|
677
677
|
|
678
|
-
describe
|
678
|
+
describe Filepath::FilesystemInfo do
|
679
679
|
describe "#absolute_path" do
|
680
680
|
test_data = [
|
681
681
|
['d1/l11', File.expand_path('d1/l11', FIXTURES_DIR), FIXTURES_DIR],
|
@@ -684,7 +684,7 @@ describe FilePath do
|
|
684
684
|
test_data.each do |path, abs_path, cwd|
|
685
685
|
it "resolves <#{path}> to <#{abs_path}> (in #{cwd})" do
|
686
686
|
Dir.chdir(cwd) do # FIXME
|
687
|
-
|
687
|
+
Filepath.new(path).absolute_path.should == abs_path
|
688
688
|
end
|
689
689
|
end
|
690
690
|
end
|
@@ -697,7 +697,7 @@ describe FilePath do
|
|
697
697
|
end
|
698
698
|
end
|
699
699
|
|
700
|
-
describe
|
700
|
+
describe Filepath::FilesystemChanges do
|
701
701
|
let(:ph) { @root / 'd1' / 'test-file' }
|
702
702
|
|
703
703
|
before(:each) do
|
@@ -735,7 +735,7 @@ describe FilePath do
|
|
735
735
|
end
|
736
736
|
end
|
737
737
|
|
738
|
-
describe
|
738
|
+
describe Filepath::FilesystemTests do
|
739
739
|
describe "mountpoint?" do
|
740
740
|
it "says that </proc> is a mount point" do
|
741
741
|
"/proc".as_path.should be_mountpoint
|
@@ -755,7 +755,7 @@ describe FilePath do
|
|
755
755
|
end
|
756
756
|
end
|
757
757
|
|
758
|
-
describe
|
758
|
+
describe Filepath::ContentInfo do
|
759
759
|
let(:ph) { @root / 'd1' / 'test-file' }
|
760
760
|
|
761
761
|
before(:each) do
|
@@ -838,7 +838,7 @@ describe FilePath do
|
|
838
838
|
end
|
839
839
|
end
|
840
840
|
|
841
|
-
describe
|
841
|
+
describe Filepath::ContentChanges do
|
842
842
|
let(:ph) { @root / 'd1' / 'test-file' }
|
843
843
|
let(:content) { "a"*20 + "b"*10 + "c"*5 }
|
844
844
|
|
@@ -922,7 +922,7 @@ describe FilePath do
|
|
922
922
|
end
|
923
923
|
end
|
924
924
|
|
925
|
-
describe
|
925
|
+
describe Filepath::ContentTests do
|
926
926
|
let(:ph) { @root / 'd1' / 'test-file' }
|
927
927
|
|
928
928
|
before(:each) do
|
@@ -953,7 +953,7 @@ describe FilePath do
|
|
953
953
|
end
|
954
954
|
end
|
955
955
|
|
956
|
-
describe
|
956
|
+
describe Filepath::SearchMethods do
|
957
957
|
describe "#entries" do
|
958
958
|
it "raises when path is not a directory" do
|
959
959
|
expect { (@root / 'f1').entries(:files) }.to raise_error(Errno::ENOTDIR)
|
@@ -1030,14 +1030,14 @@ describe FilePath do
|
|
1030
1030
|
end
|
1031
1031
|
end
|
1032
1032
|
|
1033
|
-
describe
|
1033
|
+
describe Filepath::EnvironmentInfo
|
1034
1034
|
end
|
1035
1035
|
|
1036
1036
|
describe String do
|
1037
1037
|
describe "#as_path" do
|
1038
|
-
it "generates a
|
1038
|
+
it "generates a Filepath from a String" do
|
1039
1039
|
path = "/a/b/c".as_path
|
1040
|
-
path.should be_a(
|
1040
|
+
path.should be_a(Filepath)
|
1041
1041
|
path.should eq("/a/b/c")
|
1042
1042
|
end
|
1043
1043
|
end
|
@@ -1045,9 +1045,9 @@ end
|
|
1045
1045
|
|
1046
1046
|
describe Array do
|
1047
1047
|
describe "#as_path" do
|
1048
|
-
it "generates a
|
1048
|
+
it "generates a Filepath from a String" do
|
1049
1049
|
path = ['/', 'a', 'b', 'c'].as_path
|
1050
|
-
path.should be_a(
|
1050
|
+
path.should be_a(Filepath)
|
1051
1051
|
path.should eq("/a/b/c")
|
1052
1052
|
end
|
1053
1053
|
end
|