File: filepath_spec.rb

package info (click to toggle)
ruby-filepath 0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 248 kB
  • ctags: 95
  • sloc: ruby: 1,683; makefile: 5
file content (1054 lines) | stat: -rw-r--r-- 23,134 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# This is free software released into the public domain (CC0 license).

require File.join(File.dirname(__FILE__), 'spec_helper')

describe FilePath do
	before(:all) do
		@root = FilePath.new(FIXTURES_DIR)
	end

	it "can be created from a string" do
		FilePath.new("foo").should be_a FilePath
	end

	it "can be created from another FilePath" do
		orig = FilePath.new("foo")
		FilePath.new(orig).should be_a FilePath
	end

	describe "#/" do
		test_data = [
			['foo', 'bar', 'foo/bar'],
			['foo', '.', 'foo'],
			['foo', '..', '.'],
			['foo/bar', 'baz', 'foo/bar/baz'],
			['', 'foo/bar', './foo/bar'],
		]
		test_data.each do |base, extra, result|
			it "concatenates `#{base}` and `#{extra}` (as String) into `#{result}`" do
				ph = FilePath.new(base) / extra
				ph.should == result
			end
		end

		test_data.each do |base, extra, result|
			it "concatenates `#{base}` and `#{extra}` (as FilePath) into `#{result}`" do
				ph = FilePath.new(base) / FilePath.new(extra)
				ph.should == result
			end
		end
	end

	describe "#+" do
		it "is deprecated but performs as FilePath#/" do
			p1 = FilePath.new("a")
			p2 = FilePath.new("b")

			p1.should_receive(:warn).with(/is deprecated/)
			(p1 + p2).should == (p1 / p2)
		end
	end

	describe "#join" do
		test_data = [
			['', ['bar'], './bar'],
			['foo/quux', ['bar', 'baz'], 'foo/quux/bar/baz'],
			['/', ['a', 'b', 'c'], '/a/b/c'],
		]
		test_data.each do |base, extra, result|
			args = extra.map { |x| x.inspect }.join(',')
			it "appends #{args} to '#{base}' to get <#{result}>" do
				base.as_path.join(*extra).should == result
			end
		end
	end

	describe "filename" do
		test_data = [
			['/foo/bar', 'bar'],
			['foo', 'foo'],
			['/', ''],
			['a/b/../../', ''],
			['/foo/bar/.', 'bar'],
			['a/b/../c', 'c'],
		]
		test_data.each do |path, result|
			it "says that `#{result}` is the filename of `#{path}`" do
				ph = FilePath.new(path)
				ph.filename.should == result
			end
		end
	end

	describe "parent_dir" do
		test_data = [
			['/foo/bar', '/foo'],
			['foo', '.'],
			['/', '/'],
			['/foo/bar/.', '/foo'],
			['a/b/../c', 'a'],
		]
		test_data.each do |path, result|
			it "says that `#{result}` is the parent dir of `#{path}`" do
				ph = FilePath.new(path)
				ph.parent_dir.should == result
			end
		end
	end

	describe "#relative_to" do
		test_data = [
			['/a/b/c', '/a/b', 'c'],
			['/a/b/c', '/a/d', '../b/c'],
			['/a/b/c', '/a/b/c/d', '..'],
			['/a/b/c', '/a/b/c', '.'],
			['a/d', 'a/b/c', '../../d'],
			['a/e/f', 'a/b/c/d', '../../../e/f'],
			['a/c', 'a/b/..', 'c'],
		]
		test_data.each do |path, base, result|
			it "says that `#{path}` relative to `#{base}` is `#{result}`" do
				ph = FilePath.new(path)
				ph.relative_to(base).should == result
			end
		end

		test_data2 = [
			# FIXME: testare /a/b/c con ../d (bisogna prima rendere assoluto quel path)
			['../e', '/a/b/c'],
			['g', '/a/b/c'],
			['/a/b/c', 'm'],
		]
		test_data2.each do |path, base|
			it "raise an exception because `#{path}` and `#{base}` have different prefixes" do
				ph = FilePath.new(path)
				expect { ph.relative_to(base) }.to raise_error(ArgumentError)
			end
		end
	end

	describe "#relative_to_file" do
				test_data = [
			['/a/b/c', '/a/d', 'b/c'],
			['/a/b/c', '/a/b/c/d', '.'],
			['/a/b/c', '/a/b/c', 'c'],
			['a/d', 'a/b/c', '../d'],
			['a/e/f', 'a/b/c/d', '../../e/f'],
		]
		test_data.each do |path, base, result|
			it "says that `#{path}` relative to the file `#{base}` is `#{result}`" do
				ph = FilePath.new(path)
				ph.relative_to_file(base).should == result
			end
		end
	end

	describe "#with_filename" do
		test_data = [
			['foo/bar', 'quux', 'foo/quux'],
			['foo/baz/..', 'quux', 'quux'],
			['/', 'foo', '/foo'],
		]
		test_data.each do |base, new, result|
			it "changes `#{base}` + `#{new}` into `#{result}`" do
				ph = FilePath.new(base)
				ph.with_filename(new).should == result
			end
		end
	end

	describe "#extension" do
		test_data = [
			['foo.bar', 'bar'],
			['foo.', ''],
			['foo', nil],
			['foo.bar/baz.buz', 'buz'],
			['foo.bar/baz', nil],
			['.foo', nil],
			['.foo.conf', 'conf'],
		]
		test_data.each do |path, ext|
			it "says that `#{path}` has extension `#{ext}`" do
				FilePath.new(path).extension.should == ext
			end
		end
	end

	describe "#extension?" do
		with_extension = [
			'foo.bar',
			'foo.',
			'.foo.conf',
		]
		with_extension.each do |path|
			it "says that <#{path}> has an extension" do
				FilePath.new(path).extension?.should be_true
			end
		end

		no_extension = [
			'foo',
			'foo.bar/baz',
			'.foo',
		]
		no_extension.each do |path|
			it "says that <#{path}> has no extension" do
				FilePath.new(path).extension?.should be_false
			end
		end

		extension_data = [
			['foo.bar', 'bar'],
			['/foo/bar.', ''],
			['foo/bar.baz.conf', 'conf'],
			['foo.bar.boom', /oo/],
		]
		extension_data.each do |path, ext|
			it "says that <#{path}> extesions is #{ext.inspect}" do
				FilePath.new(path).extension?(ext).should be_true
			end
		end

		it "says that `foo.bar` extension is not `baz`" do
			FilePath.new('foo.bar').extension?('baz').should be_false
		end
	end

	describe "#with_extension(String)" do
		test_data = [
			['foo.bar', 'foo.baz'],
			['foo.', 'foo.baz'],
			['foo', 'foo.baz'],
			['foo.bar/baz.buz', 'baz.baz'],
			['foo.bar/baz', 'baz.baz'],
		]
		test_data.each do |path, result|
			it "replaces `#{path}` with `baz` into `#{result}`" do
				new = FilePath.new(path).with_extension('baz')
				new.basename.to_s.should == result
			end
		end
	end

	describe "#without_extension" do
		test_data = [
			['foo.bar', 'foo'],
			['foo.', 'foo'],
			['foo', 'foo'],
			['foo.bar/baz.buz', 'baz'],
			['foo.bar/baz', 'baz'],
		]
		test_data.each do |path, result|
			it "turns `#{path}` into `#{result}`" do
				new = FilePath.new(path).without_extension
				new.basename.to_s.should == result
			end
		end
	end

	describe "=~" do
		it "matches `/foo/bar` with /foo/" do
			FilePath.new('/foo/bar').should =~ /foo/
		end

		it "does not match `/foo/bar` with /baz/" do
			FilePath.new('/foo/bar').should_not =~ /baz/
		end

		it "matches `/foo/bar` with /o\\/ba" do
			FilePath.new('/foo/bar').should =~ /o\/b/
		end

		it "matches `/foo/bar/../quux` with /foo\\/quux/" do
			FilePath.new('/foo/bar/../quux').should =~ /foo\/quux/
		end
	end

	describe "#root?" do
		it "says that </> points to the root directory" do
			FilePath.new('/').should be_root
		end

		it "says that </..> points to the root directory" do
			FilePath.new('/..').should be_root
		end

		it "says that <a/b> does not point to the root directory" do
			FilePath.new('a/b').should_not be_root
		end

		it "says that </foo> does not point to the root directory" do
			FilePath.new('/foo/bar').should_not be_root
		end
	end

	describe "#absolute?" do
		it "says that `/foo/bar` is absolute" do
			FilePath.new('/foo/bar').should be_absolute
		end

		it "sasys that `foo/bar` is not absolute" do
			FilePath.new('foo/bar').should_not be_absolute
		end
	end

	describe "#normalized" do
		test_data = [
			['a', 'a'],
			['a/b/c', 'a/b/c'],
			['a/../c', 'c'],
			['a/b/..', 'a'],
			['../a', '../a'],
			['../../a', '../../a'],
			['../a/..', '..'],
			['/', '/'],
			['/..', '/'],
			['/../../../a', '/a'],
			['a/b/../..', '.'],
		]
		test_data.each do |path, result|
			it "turns `#{path}` into `#{result}`" do
				FilePath.new(path).normalized.to_raw_string.should == result
			end
		end
	end

	describe "#each_segment" do
		it "goes through all the segments of an absolute path" do
			steps = []
			FilePath.new("/a/b/c").each_segment do |seg|
				steps << seg
			end

			steps.should have(4).items
			steps[0].should eq("/")
			steps[1].should eq("a")
			steps[2].should eq("b")
			steps[3].should eq("c")
		end

		it "goes through all the segments of a relative path" do
			steps = []
			FilePath.new("a/b/c").each_segment do |seg|
				steps << seg
			end

			steps.should have(3).items
			steps[0].should eq("a")
			steps[1].should eq("b")
			steps[2].should eq("c")
		end

		it "returns the path itself" do
			path = FilePath.new("/a/b/c/")
			path.each_segment { }.should be(path)
		end
	end

	describe "#ascend" do
		it "goes through all the segments of an absolute path" do
			steps = []
			FilePath.new("/a/b/c").ascend do |seg|
				steps << seg
			end

			steps.should have(4).items
			steps[0].should eq("/a/b/c")
			steps[1].should eq("/a/b")
			steps[2].should eq("/a")
			steps[3].should eq("/")
		end

		it "goes through all the segments of a relative path" do
			steps = []
			FilePath.new("a/b/c").ascend do |seg|
				steps << seg
			end

			steps.should have(3).items
			steps[0].should eq("a/b/c")
			steps[1].should eq("a/b")
			steps[2].should eq("a")
		end

		it "returns the path itself" do
			path = FilePath.new("/a/b/c/")
			path.ascend { }.should be(path)
		end
	end

	describe "#descend" do
		it "goes through all the segments of an absolute path" do
			steps = []
			FilePath.new("/a/b/c").descend do |seg|
				steps << seg
			end

			steps.should have(4).items
			steps[0].should eq("/")
			steps[1].should eq("/a")
			steps[2].should eq("/a/b")
			steps[3].should eq("/a/b/c")
		end

		it "goes through all the segments of a relative path" do
			steps = []
			FilePath.new("a/b/c").descend do |seg|
				steps << seg
			end

			steps.should have(3).items
			steps[0].should eq("a")
			steps[1].should eq("a/b")
			steps[2].should eq("a/b/c")
		end

		it "returns the path itself" do
			path = FilePath.new("/a/b/c/")
			path.descend { }.should be(path)
		end
	end

	describe "#to_s" do
		it "works on computed absolute paths" do
			(FilePath.new('/') / 'a' / 'b').to_s.should eql('/a/b')
		end

		it "works on computed relative paths" do
			(FilePath.new('a') / 'b').to_s.should eql('a/b')
		end

		it "returns normalized paths" do
			FilePath.new("/foo/bar/..").to_s.should eql('/foo')
		end

		it "returns '.' for empty paths" do
			FilePath.new('').to_s.should eql('.')
		end
	end

	describe "#as_path" do
		it "returns the path itself" do
			@root.as_path.should be(@root)
		end
	end

	describe "#==(String)" do
		test_data = [
			['./', '.'],
			['a/../b', 'b'],
			['a/.././b', 'b'],
			['a/./../b', 'b'],
			['./foo', 'foo'],
			['a/./b/c', 'a/b/c'],
			['a/b/.', 'a/b'],
			['a/b/', 'a/b'],
			['../a/../b/c/d/../../e', '../b/e'],
		]
		test_data.each do |ver1, ver2|
			it "says that `#{ver1}` is equivalent to `#{ver2}`" do
				ph = FilePath.new(ver1)
				ph.should == ver2
			end
		end
	end

	describe "#eql?" do
		it "is always true when an object is compared to itself" do
			ph = 'foo/bar/baz'.as_path

			ph.should eql(ph)
		end

		it "matches two different object representing the same path" do
			p1 = '/foo/bar'.as_path
			p2 = '/foo/bar'.as_path

			p1.should eql(p2)
		end

		it "does not match different objects representing different paths" do
			p1 = '/foo/bar'.as_path
			p2 = '/foo/bar/baz'.as_path

			p1.should_not eql(p2)
		end

		it "does not match objects that are not FilePaths" do
			p1 = '/foo/bar/baz'.as_path
			p2 = '/foo/bar/baz'

			p1.should eq(p2)
			p1.should_not eql(p2)
		end
	end

	describe "#<=>" do
		test_data = [
			['a/', 'b'],
			['/a', 'a'],
			['../b', 'a'],
		]
		test_data.each do |path1, path2|
			it "says that `#{path1}` precedes `#{path2}`" do
				p1 = path1.as_path
				p2 = path2.as_path

				order = p1 <=> p2
				order.should == -1
			end
		end
	end

	describe "#hash" do
		it "has the same value for similar paths" do
			p1 = '/foo/bar'.as_path
			p2 = '/foo/bar'.as_path

			p1.hash.should == p2.hash
		end

		it "has different values for different paths" do
			p1 = '/foo/bar'.as_path
			p2 = 'foo/quuz'.as_path

			p1.hash.should_not == p2.hash
		end

		it "has different values for different paths with same normalized path" do
			p1 = '/foo/bar/..'.as_path
			p2 = '/foo'.as_path

			p1.should eq(p2)
			p1.hash.should_not eq(p2.hash)
		end
	end

	describe FilePath::MetadataInfo do
		describe "#stat" do
			it "returns a stat for the file" do
				(@root / 'd1').stat.should be_directory
				(@root / 'f1').stat.size.should be_zero
			end

			it "follows links" do
				(@root / 'd1' / 'l11').stat.should == '/dev/null'.as_path.stat
			end

			it "raises Errno::ENOENT for non-existing files" do
				expect { (@root / 'foobar').stat }.to raise_error(Errno::ENOENT)
			end
		end

		describe "#lstat" do
			it "does not follow links" do
				link_lstat = (@root / 'd1' / 'l11').lstat

				link_lstat.should_not eq('/dev/null'.as_path.stat)
				link_lstat.should be_symlink
			end
		end
	end

	describe FilePath::MetadataChanges do
		describe "#chtime" do
			it "change mtime" do
				ph = @root / 'f1'
				orig_mtime = ph.mtime

				ph.chtime(Time.now, 0)
				ph.mtime.to_i.should eq(0)

				ph.chtime(Time.now, orig_mtime)
				ph.mtime.should eq(orig_mtime)
			end
		end

		describe "#chmod" do
			it "changes file permissions", :broken => true do
				ph = @root / 'f1'
				orig_mode = ph.stat.mode

				ph.should be_readable

				ph.chmod(000)
				ph.should_not be_readable

				ph.chmod(orig_mode)
				ph.should be_readable
			end
		end
	end

	describe FilePath::MetadataTests do
		describe "#file?" do
			it "says that `f1` is a file" do
				(@root / 'f1').should be_file
			end

			it "says that `d1/l11` is not a file" do
				(@root / 'd1' / 'l11').should_not be_file
			end

			it "says that the fixture root directory is not a file" do
				@root.should_not be_file
			end
		end

		describe "#link?" do
			it "says that `f1` is not a link" do
				(@root / 'f1').should_not be_link
			end

			it "says that `d1/l11` is a link" do
				(@root / 'd1' / 'l11').should be_link
			end

			it "says that the fixture root directory is not a link" do
				@root.should_not be_link
			end
		end

		describe "#directory?" do
			it "says that `f1` is not a directory" do
				(@root / 'f1').should_not be_directory
			end

			it "says that `d1/l11` is not a directory" do
				(@root / 'd1' / 'l11').should_not be_directory
			end

			it "says that the fixture root directory is a directory" do
				@root.should be_directory
			end
		end

		describe "#pipe?" do
			it "says that `p1` is a pipe" do
				(@root / 'p1').should be_pipe
			end

			it "says that `f1` is not a pipe" do
				(@root / 'f1').should_not be_pipe
			end

			it "says that the fixture root directory is not a pipe" do
				@root.should_not be_pipe
			end
		end

		describe "#socket?" do
			it "says that `s1` is a socket" do
				(@root / 's1').should be_socket
			end

			it "says that `f1` is not a socket" do
				(@root / 'f1').should_not be_socket
			end

			it "says that the fixture root directory is not a socket" do
				@root.should_not be_socket
			end
		end

		describe "#hidden?" do
			hidden_paths = [
				'.foorc',
				'foo/.bar',
				'.foo.bar',
			]
			hidden_paths.each do |path|
				it "says that <#{path}> is an hidden file" do
					path.as_path.should be_hidden
				end
			end

			non_hidden_paths = [
				'foo.bar',
				'foo/.bar/baz',
			]
			non_hidden_paths.each do |path|
				it "says that <#{path}> not an hidden file" do
					path.as_path.should_not be_hidden
				end
			end
		end
	end

	describe FilePath::FilesystemInfo do
		describe "#absolute_path" do
			test_data = [
				['d1/l11', File.expand_path('d1/l11', FIXTURES_DIR), FIXTURES_DIR],
				['/foo/bar', '/foo/bar', '.'],
			]
			test_data.each do |path, abs_path, cwd|
				it "resolves <#{path}> to <#{abs_path}> (in #{cwd})" do
					Dir.chdir(cwd) do # FIXME
						FilePath.new(path).absolute_path.should == abs_path
					end
				end
			end
		end

		describe "#real_path" do
			it "resolves <d1/l11> to </dev/null>" do
				(@root / 'd1' / 'l11').real_path.should == '/dev/null'
			end
		end
	end

	describe FilePath::FilesystemChanges do
		let(:ph) { @root / 'd1' / 'test-file' }

		before(:each) do
			ph.should_not exist
		end

		after(:each) do
			File.delete(ph) if File.exists?(ph)
		end

		describe "#touch" do
			it "creates an empty file" do
				ph.touch
				ph.should exist
			end

			it "updates the modification date of an existing file", :broken => true do
				File.open(ph, "w+") { |file| file << "abc" }
				File.utime(0, Time.now - 3200, ph)

				before_stat = File.stat(ph)
				before_time = Time.now

				#sleep(5) # let Ruby flush its stat buffer to the disk
				ph.touch

				after_time = Time.now
				after_stat = File.stat(ph)

				before_stat.should_not eq(after_stat)

				after_stat.size.should eq(before_stat.size)
				after_stat.mtime.should be_between(before_time, after_time)
			end
		end
	end

	describe FilePath::FilesystemTests do
		describe "mountpoint?" do
			it "says that </proc> is a mount point" do
				"/proc".as_path.should be_mountpoint
			end

			it "says that this RSpec file is not a mount point" do
				__FILE__.as_path.should_not be_mountpoint
			end

			it "says that an non-existing file is not a mount point" do
				"/foo/bar".as_path.should_not be_mountpoint
			end

			it "says that </> is a mount point" do
				"/".as_path.should be_mountpoint
			end
		end
	end

	describe FilePath::ContentInfo do
		let(:ph) { @root / 'd1' / 'test-file' }

		before(:each) do
			ph.should_not exist
		end

		after(:each) do
			File.delete(ph) if File.exists?(ph)
		end

		describe "#read" do
			let(:content) { "a"*20 + "b"*10 + "c"*5 }

			before(:each) do
				ph.open('w') { |f| f << content }
			end

			it "reads the complete content of a file" do
				c = ph.read
				c.should == content
			end

			it "reads the content in chunks of arbitrary sizes" do
				sum = ""
				len = 8

				num_chunks = (content.length.to_f / len).ceil
				num_chunks.times do |i|
					c = ph.read(len, len*i)
					sum += c
					c.should == content[len*i, len]
				end

				sum.should == content
			end
		end

		describe "#readlines" do
			let(:line) { "abcd12" }
			let(:lines) { Array.new(3) { line } }

			it "reads all the lines in the file" do
				ph.open('w') { |file| file << lines.join("\n") }
				readlines = ph.readlines

				readlines.should have(3).lines
				readlines.all? { |l| l.chomp.should == line }
			end

			it "read lines separated by arbitrary separators" do
				sep = ','

				ph.open('w') { |file| file << lines.join(sep) }
				readlines = ph.readlines(sep)

				readlines.should have(3).lines
				readlines[0..-2].all? { |l| l.should == line + sep}
				readlines.last.should == line
			end
		end

		describe "#size" do
			before(:each) do
				ph.touch
			end

			it "says that an empty file contains 0 bytes" do
				ph.size.should be_zero
			end

			it "reports the size of a non-empty file" do
				ph.size.should be_zero

				ph.open("a") { |f| f << "abc" }
				ph.size.should eq(3)

				ph.open("a") { |f| f << "defg" }
				ph.size.should eq(3+4)
			end
		end
	end

	describe FilePath::ContentChanges do
		let(:ph) { @root / 'd1' / 'test-file' }
		let(:content) { "a"*20 + "b"*10 + "c"*5 }

		before(:each) do
			ph.should_not exist
		end

		after(:each) do
			File.delete(ph) if File.exists?(ph)
		end

		describe "#open" do
			before(:each) do
				ph.touch
			end

			it "opens files" do
				file = ph.open
				file.should be_a(File)
			end

			it "opens files in read-only mode" do
				ph.open do |file|
					expect { file << "abc" }.to raise_error(IOError)
				end
			end

			it "opens files in read-write mode" do
				ph.open('w') do |file|
					file << "abc"
				end

				ph.size.should == 3
			end
		end

		describe "#write" do
			it "writes data passed as argument" do
				ph.write(content)

				ph.read.should == content
			end

			it "overwrites an existing file" do
				ph.write(content * 2)
				ph.size.should eq(content.length * 2)

				ph.write(content)
				ph.size.should eq(content.length)

				ph.read.should == content
			end
		end

		describe "#append" do
			it "appends data to an existing file" do
				ph.write(content)
				ph.append(content)

				ph.size.should eq(content.length * 2)
				ph.read.should == content * 2
			end
		end

		describe "#truncate" do
			before(:each) do
				ph.open('w') { |f| f << content }
			end

			it "truncates a file to 0 bytes" do
				ph.size.should_not be_zero
				ph.truncate
				ph.size.should be_zero
			end

			it "truncates a file to an arbitrary size" do
				ph.size.should_not be_zero
				ph.truncate(2)
				ph.size.should == 2
			end
		end
	end

	describe FilePath::ContentTests do
		let(:ph) { @root / 'd1' / 'test-file' }

		before(:each) do
			ph.should_not exist
		end

		after(:each) do
			File.delete(ph) if File.exists?(ph)
		end

		describe "#empty?" do
			before(:each) do
				ph.touch
			end

			it "says that an empty file is empty" do
				ph.should be_empty
			end

			it "says that a non-empyt file is not empty" do
				ph.open('w') { |f| f << "abc" }
				ph.should_not be_empty
			end

			it "says that </dev/null> is empty" do
				'/dev/null'.as_path.should be_empty
			end
		end
	end

	describe FilePath::SearchMethods do
		describe "#entries" do
			it "raises when path is not a directory" do
				expect { (@root / 'f1').entries(:files) }.to raise_error(Errno::ENOTDIR)
			end
		end

		describe "#find" do
			it "finds all paths matching a glob string" do
				list = @root.find('*1')

				list.should have(8).items
				list.each { |path| path.should =~ /1/ }
			end

			it "finds all paths matching a Regex" do
				list = @root.find(/2/)

				list.should have(6).items
				list.each { |path| path.should =~ /2/ }
			end

			it "finds all paths for which the block returns true" do
				list = @root.find { |path| path.directory? }

				list.should have(9).items
				list.each { |path| path.filename.should =~ /^d/ }
			end
		end

		describe "#files" do
			it "finds 1 file in the root directory" do
				@root.files.should have(1).item
			end

			it "finds 3 files in the root directory and its sub directories" do
				@root.files(true).should have(3).item
			end

			it "finds 2 files in directory <d1>" do
				(@root / 'd1').files.should have(2).items
			end

			it "finds no files in directory <d1/d12>" do
				(@root / 'd1' / 'd12').files.should have(0).items
			end
		end

		describe "#directories" do
			it "finds 4 directories in the root directory" do
				@root.directories.should have(4).items
			end

			it "finds 9 directories in the root directory and its sub directories" do
				@root.directories(true).should have(9).item
			end

			it "finds 2 directories in directory <d2>" do
				(@root / 'd2').directories.should have(2).items
			end

			it "finds no directories in directory <d1/d13>" do
				(@root / 'd1' / 'd13').directories.should have(0).items
			end
		end

		describe "#links" do
			it "finds no links in the root directory" do
				@root.links.should have(0).items
			end

			it "finds 1 link in directory <d1>" do
				(@root / 'd1').links.should have(1).item
			end
		end
	end

	describe FilePath::EnvironmentInfo
end

describe String do
	describe "#as_path" do
		it "generates a FilePath from a String" do
			path = "/a/b/c".as_path
			path.should be_a(FilePath)
			path.should eq("/a/b/c")
		end
	end
end

describe Array do
	describe "#as_path" do
		it "generates a FilePath from a String" do
			path = ['/', 'a', 'b', 'c'].as_path
			path.should be_a(FilePath)
			path.should eq("/a/b/c")
		end
	end
end