@@ -516,6 +516,194 @@ mod test {
516
516
use super::*;
517
517
use tempfile;
518
518
519
+ #[test]
520
+ fn test_relative_pattern() {
521
+
522
+ fn change_then_remove(p: &Path, f: &fn()) {
523
+ do (|| {
524
+ unstable::change_dir_locked(p, || f());
525
+ }).finally {
526
+ os::remove_dir_recursive(p);
527
+ }
528
+ }
529
+
530
+ fn mk_file(path: &str, directory: bool) {
531
+ if directory {
532
+ os::make_dir(&Path(path), 0xFFFF);
533
+ } else {
534
+ io::mk_file_writer(&Path(path), [io::Create]);
535
+ }
536
+ }
537
+
538
+ fn abs_path(path: &str) -> Path {
539
+ os::getcwd().push_many(Path(path).components)
540
+ }
541
+
542
+ fn glob_vec(pattern: &str) -> ~[Path] {
543
+ glob(pattern).collect()
544
+ }
545
+
546
+ let root = tempfile::mkdtemp(&os::tmpdir(), "glob-tests");
547
+ let root = root.expect("Should have created a temp directory");
548
+
549
+ do change_then_remove(&root) {
550
+
551
+ mk_file("aaa", true);
552
+ mk_file("aaa/apple", true);
553
+ mk_file("aaa/orange", true);
554
+ mk_file("aaa/tomato", true);
555
+ mk_file("aaa/tomato/tomato.txt", false);
556
+ mk_file("aaa/tomato/tomoto.txt", false);
557
+ mk_file("bbb", true);
558
+ mk_file("bbb/specials", true);
559
+ mk_file("bbb/specials/!", false);
560
+
561
+ // windows does not allow `*` or `?` characters to exist in filenames
562
+ if os::consts::FAMILY != os::consts::windows::FAMILY {
563
+ mk_file("bbb/specials/*", false);
564
+ mk_file("bbb/specials/?", false);
565
+ }
566
+
567
+ mk_file("bbb/specials/[", false);
568
+ mk_file("bbb/specials/]", false);
569
+ mk_file("ccc", true);
570
+ mk_file("xyz", true);
571
+ mk_file("xyz/x", false);
572
+ mk_file("xyz/y", false);
573
+ mk_file("xyz/z", false);
574
+
575
+ assert_eq!(glob_vec(""), ~[]);
576
+ assert_eq!(glob_vec("."), ~[]);
577
+ assert_eq!(glob_vec(".."), ~[]);
578
+
579
+ assert_eq!(glob_vec("aaa"), ~[abs_path("aaa")]);
580
+ assert_eq!(glob_vec("aaa/"), ~[abs_path("aaa")]);
581
+ assert_eq!(glob_vec("a"), ~[]);
582
+ assert_eq!(glob_vec("aa"), ~[]);
583
+ assert_eq!(glob_vec("aaaa"), ~[]);
584
+
585
+ assert_eq!(glob_vec("aaa/apple"), ~[abs_path("aaa/apple")]);
586
+ assert_eq!(glob_vec("aaa/apple/nope"), ~[]);
587
+
588
+ // windows should support both / and \ as directory separators
589
+ if os::consts::FAMILY == os::consts::windows::FAMILY {
590
+ assert_eq!(glob_vec("aaa\\apple"), ~[abs_path("aaa/apple")]);
591
+ }
592
+
593
+ assert_eq!(glob_vec("???/"), ~[
594
+ abs_path("aaa"),
595
+ abs_path("bbb"),
596
+ abs_path("ccc"),
597
+ abs_path("xyz")]);
598
+
599
+ assert_eq!(glob_vec("aaa/tomato/tom?to.txt"), ~[
600
+ abs_path("aaa/tomato/tomato.txt"),
601
+ abs_path("aaa/tomato/tomoto.txt")]);
602
+
603
+ assert_eq!(glob_vec("xyz/?"), ~[
604
+ abs_path("xyz/x"),
605
+ abs_path("xyz/y"),
606
+ abs_path("xyz/z")]);
607
+
608
+ assert_eq!(glob_vec("a*"), ~[abs_path("aaa")]);
609
+ assert_eq!(glob_vec("*a*"), ~[abs_path("aaa")]);
610
+ assert_eq!(glob_vec("a*a"), ~[abs_path("aaa")]);
611
+ assert_eq!(glob_vec("aaa*"), ~[abs_path("aaa")]);
612
+ assert_eq!(glob_vec("*aaa"), ~[abs_path("aaa")]);
613
+ assert_eq!(glob_vec("*aaa*"), ~[abs_path("aaa")]);
614
+ assert_eq!(glob_vec("*a*a*a*"), ~[abs_path("aaa")]);
615
+ assert_eq!(glob_vec("aaa*/"), ~[abs_path("aaa")]);
616
+
617
+ assert_eq!(glob_vec("aaa/*"), ~[
618
+ abs_path("aaa/apple"),
619
+ abs_path("aaa/orange"),
620
+ abs_path("aaa/tomato")]);
621
+
622
+ assert_eq!(glob_vec("aaa/*a*"), ~[
623
+ abs_path("aaa/apple"),
624
+ abs_path("aaa/orange"),
625
+ abs_path("aaa/tomato")]);
626
+
627
+ assert_eq!(glob_vec("*/*/*.txt"), ~[
628
+ abs_path("aaa/tomato/tomato.txt"),
629
+ abs_path("aaa/tomato/tomoto.txt")]);
630
+
631
+ assert_eq!(glob_vec("*/ * /t[ aob] m?to[ . ] t[ !y] t") , ~[
632
+ abs_path ( "aaa/tomato/tomato.txt" ) ,
633
+ abs_path ( "aaa/tomato/tomoto.txt" ) ] ) ;
634
+
635
+ assert_eq ! ( glob_vec( "aa[a]" ) , ~[ abs_path( "aaa" ) ] ) ;
636
+ assert_eq ! ( glob_vec( "aa[abc]" ) , ~[ abs_path( "aaa" ) ] ) ;
637
+ assert_eq ! ( glob_vec( "a[bca]a" ) , ~[ abs_path( "aaa" ) ] ) ;
638
+ assert_eq ! ( glob_vec( "aa[b]" ) , ~[ ] ) ;
639
+ assert_eq ! ( glob_vec( "aa[xyz]" ) , ~[ ] ) ;
640
+ assert_eq ! ( glob_vec( "aa[]]" ) , ~[ ] ) ;
641
+
642
+ assert_eq ! ( glob_vec( "aa[!b]" ) , ~[ abs_path( "aaa" ) ] ) ;
643
+ assert_eq ! ( glob_vec( "aa[!bcd]" ) , ~[ abs_path( "aaa" ) ] ) ;
644
+ assert_eq ! ( glob_vec( "a[!bcd]a" ) , ~[ abs_path( "aaa" ) ] ) ;
645
+ assert_eq ! ( glob_vec( "aa[!a]" ) , ~[ ] ) ;
646
+ assert_eq ! ( glob_vec( "aa[!abc]" ) , ~[ ] ) ;
647
+
648
+ assert_eq ! ( glob_vec( "bbb/specials/[[]" ) , ~[ abs_path( "bbb/specials/[" ) ] ) ;
649
+ assert_eq ! ( glob_vec( "bbb/specials/!" ) , ~[ abs_path( "bbb/specials/!" ) ] ) ;
650
+ assert_eq ! ( glob_vec( "bbb/specials/[]]" ) , ~[ abs_path( "bbb/specials/]" ) ] ) ;
651
+
652
+ if os:: consts:: FAMILY != os:: consts:: windows:: FAMILY {
653
+ assert_eq ! ( glob_vec( "bbb/specials/[*]" ) , ~[ abs_path( "bbb/specials/*" ) ] ) ;
654
+ assert_eq ! ( glob_vec( "bbb/specials/[?]" ) , ~[ abs_path( "bbb/specials/?" ) ] ) ;
655
+ }
656
+
657
+ if os:: consts:: FAMILY == os:: consts:: windows:: FAMILY {
658
+
659
+ assert_eq ! ( glob_vec( "bbb/specials/[![]" ) , ~[
660
+ abs_path( "bbb/specials/!" ) ,
661
+ abs_path( "bbb/specials/]" ) ] ) ;
662
+
663
+ assert_eq ! ( glob_vec( "bbb/specials/[!]]" ) , ~[
664
+ abs_path( "bbb/specials/!" ) ,
665
+ abs_path( "bbb/specials/[" ) ] ) ;
666
+
667
+ assert_eq ! ( glob_vec( "bbb/specials/[!!]" ) , ~[
668
+ abs_path( "bbb/specials/[" ) ,
669
+ abs_path( "bbb/specials/]" ) ] ) ;
670
+
671
+ } else {
672
+
673
+ assert_eq ! ( glob_vec( "bbb/specials/[![]" ) , ~[
674
+ abs_path( "bbb/specials/!" ) ,
675
+ abs_path( "bbb/specials/*" ) ,
676
+ abs_path( "bbb/specials/?" ) ,
677
+ abs_path( "bbb/specials/]" ) ] ) ;
678
+
679
+ assert_eq ! ( glob_vec( "bbb/specials/[!]]" ) , ~[
680
+ abs_path( "bbb/specials/!" ) ,
681
+ abs_path( "bbb/specials/*" ) ,
682
+ abs_path( "bbb/specials/?" ) ,
683
+ abs_path( "bbb/specials/[" ) ] ) ;
684
+
685
+ assert_eq ! ( glob_vec( "bbb/specials/[!!]" ) , ~[
686
+ abs_path( "bbb/specials/*" ) ,
687
+ abs_path( "bbb/specials/?" ) ,
688
+ abs_path( "bbb/specials/[" ) ,
689
+ abs_path( "bbb/specials/]" ) ] ) ;
690
+
691
+ assert_eq ! ( glob_vec( "bbb/specials/[!*]" ) , ~[
692
+ abs_path( "bbb/specials/!" ) ,
693
+ abs_path( "bbb/specials/?" ) ,
694
+ abs_path( "bbb/specials/[" ) ,
695
+ abs_path( "bbb/specials/]" ) ] ) ;
696
+
697
+ assert_eq ! ( glob_vec( "bbb/specials/[!?]" ) , ~[
698
+ abs_path( "bbb/specials/!" ) ,
699
+ abs_path( "bbb/specials/*" ) ,
700
+ abs_path( "bbb/specials/[" ) ,
701
+ abs_path( "bbb/specials/]" ) ] ) ;
702
+
703
+ }
704
+ } ;
705
+ }
706
+
519
707
#[ test]
520
708
fn test_absolute_pattern( ) {
521
709
// assume that the filesystem is not empty!
0 commit comments