This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
MacOS Classic catdir() rewrite from Thomas Wegner
[perl5.git] / lib / File / Spec.t
index c6d155f..9baa5a6 100755 (executable)
@@ -1,17 +1,52 @@
 #!./perl
 
 BEGIN {
-    $^O = '';
     chdir 't' if -d 't';
     @INC = '../lib';
 }
+# Grab all of the plain routines from File::Spec
+use File::Spec @File::Spec::EXPORT_OK ;
+
+require File::Spec::Unix ;
+require File::Spec::Win32 ;
+
+eval {
+   require VMS::Filespec ;
+} ;
+
+my $skip_exception = "Install VMS::Filespec (from vms/ext)" ;
+
+if ( $@ ) {
+   # Not pretty, but it allows testing of things not implemented soley
+   # on VMS.  It might be better to change File::Spec::VMS to do this,
+   # making it more usable when running on (say) Unix but working with
+   # VMS paths.
+   eval qq-
+      sub File::Spec::VMS::vmsify  { die "$skip_exception" }
+      sub File::Spec::VMS::unixify { die "$skip_exception" }
+      sub File::Spec::VMS::vmspath { die "$skip_exception" }
+   - ;
+   $INC{"VMS/Filespec.pm"} = 1 ;
+}
+require File::Spec::VMS ;
+
+require File::Spec::OS2 ;
+require File::Spec::Mac ;
+
+# $root is only needed by Mac OS tests; these particular
+# tests are skipped on other OSs
+my $root;
+if  ($^O eq 'MacOS') {
+       $root = File::Spec::Mac->rootdir();
+}
 
 # Each element in this array is a single test. Storing them this way makes
 # maintenance easy, and should be OK since perl should be pretty functional
 # before these tests are run.
 
 @tests = (
-# Function                      Expected
+# [ Function          ,            Expected          ,         Platform ]
+
 [ "Unix->catfile('a','b','c')", 'a/b/c'  ],
 
 [ "Unix->splitpath('file')",            ',,file'            ],
@@ -53,7 +88,10 @@ BEGIN {
 
 [ "Unix->canonpath('')",                                      ''          ],
 [ "Unix->canonpath('///../../..//./././a//b/.././c/././')",   '/a/b/../c' ],
-[ "Unix->canonpath('/.')",                                    '/.'        ],
+[ "Unix->canonpath('/.')",                                    '/'         ],
+[ "Unix->canonpath('/./')",                                   '/'         ],
+[ "Unix->canonpath('/a/./')",                                 '/a'        ],
+[ "Unix->canonpath('/a/.')",                                  '/a'        ],
 
 [  "Unix->abs2rel('/t1/t2/t3','/t1/t2/t3')",          ''                   ],
 [  "Unix->abs2rel('/t1/t2/t4','/t1/t2/t3')",          '../t4'              ],
@@ -63,7 +101,7 @@ BEGIN {
 #[ "Unix->abs2rel('../t4','/t1/t2/t3')",              '../t4'              ],
 [  "Unix->abs2rel('/','/t1/t2/t3')",                  '../../..'           ],
 [  "Unix->abs2rel('///','/t1/t2/t3')",                '../../..'           ],
-[  "Unix->abs2rel('/.','/t1/t2/t3')",                 '../../../.'         ],
+[  "Unix->abs2rel('/.','/t1/t2/t3')",                 '../../..'           ],
 [  "Unix->abs2rel('/./','/t1/t2/t3')",                '../../..'           ],
 #[ "Unix->abs2rel('../t4','/t1/t2/t3')",              '../t4'              ],
 
@@ -236,7 +274,7 @@ BEGIN {
 [  "VMS->abs2rel('[t1.t2]file','[t1.t2.t3]')",             '[-]file'          ],
 [  "VMS->abs2rel('[t1.t2.t3.t4]','[t1.t2.t3]')",           '[t4]'             ],
 [  "VMS->abs2rel('[t4.t5.t6]','[t1.t2.t3]')",              '[---.t4.t5.t6]'   ],
-[ "VMS->abs2rel('[000000]','[t1.t2.t3]')",                 '[---.000000]'     ],
+[ "VMS->abs2rel('[000000]','[t1.t2.t3]')",                 '[---]'            ],
 [ "VMS->abs2rel('a:[t1.t2.t4]','[t1.t2.t3]')",             '[-.t4]'           ],
 [ "VMS->abs2rel('[a.-.b.c.-]','[t1.t2.t3]')",              '[---.b]'          ],
 
@@ -250,78 +288,194 @@ BEGIN {
 [ "OS2->catdir('A:/d1','B:/d2','d3','')", 'A:/d1/B:/d2/d3' ],
 [ "OS2->catfile('a','b','c')",            'a/b/c'          ],
 
-[ "Mac->splitpath('file')",          ',,file'          ],
-[ "Mac->splitpath(':file')",         ',:,file'         ],
-[ "Mac->splitpath(':d1',1)",         ',:d1:,'          ],
-[ "Mac->splitpath('d1',1)",          'd1:,,'           ],
-[ "Mac->splitpath('d1:d2:d3:')",     'd1:,d2:d3:,'     ],
-[ "Mac->splitpath('d1:d2:d3',1)",    'd1:,d2:d3:,'     ],
-[ "Mac->splitpath(':d1:d2:d3:')",    ',:d1:d2:d3:,'    ],
-[ "Mac->splitpath(':d1:d2:d3:',1)",  ',:d1:d2:d3:,'    ],
-[ "Mac->splitpath('d1:d2:d3:file')", 'd1:,d2:d3:,file' ],
-[ "Mac->splitpath('d1:d2:d3',1)",    'd1:,d2:d3:,'     ],
-
-[ "Mac->catdir('')",                ':'           ],
-[ "Mac->catdir('d1','d2','d3')",    'd1:d2:d3:'   ],
-[ "Mac->catdir('d1','d2/','d3')",   'd1:d2/:d3:'  ],
-[ "Mac->catdir('','d1','d2','d3')", ':d1:d2:d3:'  ],
-[ "Mac->catdir('','','d2','d3')",   '::d2:d3:'    ],
-[ "Mac->catdir('','','','d3')",     ':::d3:'      ],
-[ "Mac->catdir(':name')",           ':name:'      ],
-[ "Mac->catdir(':name',':name')",   ':name:name:' ],
-
-[ "Mac->catfile('a','b','c')", 'a:b:c' ],
 
+[ "Mac->catpath('','','')",              ''                ],
+[ "Mac->catpath('',':','')",             ':'               ],
+[ "Mac->catpath('','::','')",            '::'              ],
+
+[ "Mac->catpath('hd','','')",            'hd:'             ],
+[ "Mac->catpath('hd:','','')",           'hd:'             ],
+[ "Mac->catpath('hd:',':','')",          'hd:'             ], 
+[ "Mac->catpath('hd:','::','')",         'hd::'            ],
+
+[ "Mac->catpath('hd','','file')",       'hd:file'          ],
+[ "Mac->catpath('hd',':','file')",      'hd:file'          ],
+[ "Mac->catpath('hd','::','file')",     'hd::file'         ],
+[ "Mac->catpath('hd',':::','file')",    'hd:::file'        ],
+
+[ "Mac->catpath('hd:','',':file')",      'hd:file'         ],
+[ "Mac->catpath('hd:',':',':file')",     'hd:file'         ],
+[ "Mac->catpath('hd:','::',':file')",    'hd::file'        ],
+[ "Mac->catpath('hd:',':::',':file')",   'hd:::file'       ],
+
+[ "Mac->catpath('hd:','d1','file')",     'hd:d1:file'      ],
+[ "Mac->catpath('hd:',':d1:',':file')",  'hd:d1:file'      ],
+
+[ "Mac->catpath('','d1','')",            ':d1:'            ],
+[ "Mac->catpath('',':d1','')",           ':d1:'            ],
+[ "Mac->catpath('',':d1:','')",          ':d1:'            ],
+
+[ "Mac->catpath('','d1','file')",        ':d1:file'        ],
+[ "Mac->catpath('',':d1:',':file')",     ':d1:file'        ],
+
+[ "Mac->catpath('','','file')",          'file'            ],
+[ "Mac->catpath('','',':file')",         'file'            ], # !
+[ "Mac->catpath('',':',':file')",        ':file'           ], # !
+
+
+[ "Mac->splitpath(':')",              ',:,'               ],
+[ "Mac->splitpath('::')",             ',::,'              ],
+[ "Mac->splitpath(':::')",            ',:::,'             ],
+
+[ "Mac->splitpath('file')",           ',,file'            ],
+[ "Mac->splitpath(':file')",          ',:,file'           ],
+
+[ "Mac->splitpath('d1',1)",           ',:d1:,'            ], # dir, not volume
+[ "Mac->splitpath(':d1',1)",          ',:d1:,'            ],
+[ "Mac->splitpath(':d1:',1)",         ',:d1:,'            ],
+[ "Mac->splitpath(':d1:')",           ',:d1:,'            ],
+[ "Mac->splitpath(':d1:d2:d3:')",     ',:d1:d2:d3:,'      ],
+[ "Mac->splitpath(':d1:d2:d3:',1)",   ',:d1:d2:d3:,'      ],
+[ "Mac->splitpath(':d1:file')",       ',:d1:,file'        ],
+[ "Mac->splitpath('::d1:file')",      ',::d1:,file'       ],
+
+[ "Mac->splitpath('hd:', 1)",         'hd:,,'             ],
+[ "Mac->splitpath('hd:')",            'hd:,,'             ],
+[ "Mac->splitpath('hd:d1:d2:')",      'hd:,:d1:d2:,'      ],
+[ "Mac->splitpath('hd:d1:d2',1)",     'hd:,:d1:d2:,'      ],
+[ "Mac->splitpath('hd:d1:d2:file')",  'hd:,:d1:d2:,file'  ],
+[ "Mac->splitpath('hd:d1:d2::file')", 'hd:,:d1:d2::,file' ],
+[ "Mac->splitpath('hd::d1:d2:file')", 'hd:,::d1:d2:,file' ], # invalid path
+[ "Mac->splitpath('hd:file')",        'hd:,,file'         ],
+
+[ "Mac->splitdir()",                   ''            ],
+[ "Mac->splitdir('')",                 ''            ],
+[ "Mac->splitdir(':')",                ':'           ],
+[ "Mac->splitdir('::')",               '::'          ],
+[ "Mac->splitdir(':::')",              '::,::'       ],
+[ "Mac->splitdir(':::d1:d2')",         '::,::,d1,d2' ],
+
+[ "Mac->splitdir(':d1:d2:d3::')",      'd1,d2,d3,::'],
+[ "Mac->splitdir(':d1:d2:d3:')",       'd1,d2,d3'   ],
+[ "Mac->splitdir(':d1:d2:d3')",        'd1,d2,d3'   ],
+
+# absolute paths in splitdir() work, but you'd better use splitpath()
+[ "Mac->splitdir('hd:')",              'hd:'              ],
+[ "Mac->splitdir('hd::')",             'hd:,::'           ], # invalid path, but it works
+[ "Mac->splitdir('hd::d1:')",          'hd:,::,d1'        ], # invalid path, but it works
+[ "Mac->splitdir('hd:d1:d2:::')",      'hd:,d1,d2,::,::'  ],
+[ "Mac->splitdir('hd:d1:d2::')",       'hd:,d1,d2,::'     ],
+[ "Mac->splitdir('hd:d1:d2:')",        'hd:,d1,d2'        ],
+[ "Mac->splitdir('hd:d1:d2')",         'hd:,d1,d2'        ],
+[ "Mac->splitdir('hd:d1::d2::')",      'hd:,d1,::,d2,::'  ],
+
+[ "Mac->catdir()",                 ''             ],
+[ "Mac->catdir('')",               $root, 'MacOS' ], # skipped on other OS
+[ "Mac->catdir(':')",              ':'            ],
+
+[ "Mac->catdir('', '')",           $root, 'MacOS' ], # skipped on other OS
+[ "Mac->catdir('', ':')",          $root, 'MacOS' ], # skipped on other OS 
+[ "Mac->catdir(':', ':')",         ':'            ],  
+[ "Mac->catdir(':', '')",          ':'            ], 
+
+[ "Mac->catdir('', '::')",         $root, 'MacOS' ], # skipped on other OS
+[ "Mac->catdir(':', '::')",        '::'           ], 
+
+[ "Mac->catdir('::', '')",         '::'           ],  
+[ "Mac->catdir('::', ':')",        '::'           ], 
+
+[ "Mac->catdir('::', '::')",       ':::'          ], 
+
+[ "Mac->catdir(':d1')",                    ':d1:'        ],
+[ "Mac->catdir(':d1:')",                   ':d1:'        ],
+[ "Mac->catdir(':d1','d2')",               ':d1:d2:'     ],
+[ "Mac->catdir(':d1',':d2')",              ':d1:d2:'     ],
+[ "Mac->catdir(':d1',':d2:')",             ':d1:d2:'     ],
+[ "Mac->catdir(':d1',':d2::')",            ':d1:d2::'     ],
+[ "Mac->catdir(':',':d1',':d2')",          ':d1:d2:'     ],
+[ "Mac->catdir('::',':d1',':d2')",         '::d1:d2:'    ],
+[ "Mac->catdir('::','::',':d1',':d2')",    ':::d1:d2:'   ],
+[ "Mac->catdir(':',':',':d1',':d2')",      ':d1:d2:'     ],
+[ "Mac->catdir('::',':',':d1',':d2')",     '::d1:d2:'    ],
+
+[ "Mac->catdir('d1')",                    ':d1:'         ],
+[ "Mac->catdir('d1','d2','d3')",          ':d1:d2:d3:'   ],
+[ "Mac->catdir('d1','d2/','d3')",         ':d1:d2/:d3:'  ],
+[ "Mac->catdir('d1','',':d2')",           ':d1:d2:'      ],
+[ "Mac->catdir('d1',':',':d2')",          ':d1:d2:'      ],
+[ "Mac->catdir('d1','::',':d2')",         ':d1::d2:'     ],
+[ "Mac->catdir('d1',':::',':d2')",        ':d1:::d2:'    ],
+[ "Mac->catdir('d1','::','::',':d2')",    ':d1:::d2:'    ],
+[ "Mac->catdir('d1','d2')",               ':d1:d2:'      ],
+[ "Mac->catdir('d1','d2', '')",           ':d1:d2:'      ],
+[ "Mac->catdir('d1','d2', ':')",          ':d1:d2:'      ],
+[ "Mac->catdir('d1','d2', '::')",         ':d1:d2::'     ],
+[ "Mac->catdir('d1','d2','','')",         ':d1:d2:'      ],
+[ "Mac->catdir('d1','d2',':','::')",      ':d1:d2::'     ],
+[ "Mac->catdir('d1','d2','::','::')",     ':d1:d2:::'    ],
+[ "Mac->catdir('d1',':d2')",              ':d1:d2:'      ],
+[ "Mac->catdir('d1',':d2:')",             ':d1:d2:'      ],
+
+[ "Mac->catdir('','d1','d2','d3')",        $root . 'd1:d2:d3:', 'MacOS' ], # skipped on other OS
+[ "Mac->catdir('',':','d1','d2')",         $root . 'd1:d2:'   , 'MacOS' ], # skipped on other OS
+[ "Mac->catdir('','::','d1','d2')",        $root . 'd1:d2:'   , 'MacOS' ], # skipped on other OS
+[ "Mac->catdir('',':','','d1')",           $root . 'd1:'      , 'MacOS' ], # skipped on other OS
+[ "Mac->catdir('', ':d1',':d2')",          $root . 'd1:d2:'   , 'MacOS' ], # skipped on other OS
+[ "Mac->catdir('','',':d1',':d2')",        $root . 'd1:d2:'   , 'MacOS' ], # skipped on other OS
+
+[ "Mac->catdir('hd:',':d1')",       'hd:d1:'      ],
+[ "Mac->catdir('hd:d1:',':d2')",    'hd:d1:d2:'   ],
+[ "Mac->catdir('hd:','d1')",        'hd:d1:'      ],
+[ "Mac->catdir('hd:d1:',':d2')",    'hd:d1:d2:'   ],
+[ "Mac->catdir('hd:d1:',':d2:')",   'hd:d1:d2:'   ],
+
+[ "Mac->catfile()",                      ''                      ], 
+[ "Mac->catfile('')",                    ''                      ],
+[ "Mac->catfile('', '')",                $root         , 'MacOS' ], # skipped on other OS 
+[ "Mac->catfile('', 'file')",            $root . 'file', 'MacOS' ], # skipped on other OS
+[ "Mac->catfile(':')",                   ':'                     ],
+[ "Mac->catfile(':', '')",               ':'                     ],
+
+[ "Mac->catfile('d1','d2','file')",      ':d1:d2:file' ],
+[ "Mac->catfile('d1','d2',':file')",     ':d1:d2:file' ],
+[ "Mac->catfile('file')",                'file'        ], 
+[ "Mac->catfile(':', 'file')",           ':file'       ], 
 [ "Mac->canonpath('')",                   ''     ],
 [ "Mac->canonpath(':')",                  ':'    ],
 [ "Mac->canonpath('::')",                 '::'   ],
 [ "Mac->canonpath('a::')",                'a::'  ],
 [ "Mac->canonpath(':a::')",               ':a::' ],
 
-[ "Mac->abs2rel('t1:t2:t3','t1:t2:t3')",    ':'            ],
-[ "Mac->abs2rel('t1:t2','t1:t2:t3')",       '::'           ],
-[ "Mac->abs2rel('t1:t4','t1:t2:t3')",       ':::t4'        ],
-[ "Mac->abs2rel('t1:t2:t4','t1:t2:t3')",    '::t4'         ],
-[ "Mac->abs2rel('t1:t2:t3:t4','t1:t2:t3')", ':t4'          ],
-[ "Mac->abs2rel('t4:t5:t6','t1:t2:t3')",    '::::t4:t5:t6' ],
-[ "Mac->abs2rel('t1','t1:t2:t3')",          ':::'          ],
-
-[ "Mac->rel2abs(':t4','t1:t2:t3')",          't1:t2:t3:t4'    ],
-[ "Mac->rel2abs(':t4:t5','t1:t2:t3')",       't1:t2:t3:t4:t5' ],
-[ "Mac->rel2abs('','t1:t2:t3')",             ''               ],
-[ "Mac->rel2abs('::','t1:t2:t3')",           't1:t2:t3::'     ],
-[ "Mac->rel2abs('::t4','t1:t2:t3')",         't1:t2:t3::t4'   ],
-[ "Mac->rel2abs('t1','t1:t2:t3')",           't1'             ],
+[ "Mac->abs2rel('hd:d1:d2:','hd:d1:d2:')",            ':'            ],
+[ "Mac->abs2rel('hd:d1:d2:','hd:d1:d2:file')",        ':'            ], # ignore base's file portion
+[ "Mac->abs2rel('hd:d1:d2:file','hd:d1:d2:')",        ':file'        ], 
+[ "Mac->abs2rel('hd:d1:','hd:d1:d2:')",               '::'           ],
+[ "Mac->abs2rel('hd:d3:','hd:d1:d2:')",               ':::d3:'       ],
+[ "Mac->abs2rel('hd:d3:','hd:d1:d2::')",              '::d3:'        ],
+[ "Mac->abs2rel('hd:d1:d4:d5:','hd:d1::d2:d3::')",    '::d1:d4:d5:'  ],
+[ "Mac->abs2rel('hd:d1:d4:d5:','hd:d1::d2:d3:')",     ':::d1:d4:d5:' ], # first, resolve updirs in base
+[ "Mac->abs2rel('hd:d1:d3:','hd:d1:d2:')",            '::d3:'        ],
+[ "Mac->abs2rel('hd:d1::d3:','hd:d1:d2:')",           ':::d3:'       ],
+[ "Mac->abs2rel('hd:d3:','hd:d1:d2:')",               ':::d3:'       ], # same as above
+[ "Mac->abs2rel('hd:d1:d2:d3:','hd:d1:d2:')",         ':d3:'         ],
+[ "Mac->abs2rel('hd:d1:d2:d3::','hd:d1:d2:')",        ':d3::'        ],
+[ "Mac->abs2rel('hd1:d3:d4:d5:','hd2:d1:d2:')",       ':::d3:d4:d5:' ], # ignore base's volume
+[ "Mac->abs2rel('hd:','hd:d1:d2:')",                  ':::'          ],
+
+[ "Mac->rel2abs(':d3:','hd:d1:d2:')",          'hd:d1:d2:d3:'     ], 
+[ "Mac->rel2abs(':d3:d4:','hd:d1:d2:')",       'hd:d1:d2:d3:d4:'  ], 
+[ "Mac->rel2abs('','hd:d1:d2:')",              ''                 ],
+[ "Mac->rel2abs('::','hd:d1:d2:')",            'hd:d1:d2::'       ],
+[ "Mac->rel2abs('::','hd:d1:d2:file')",        'hd:d1:d2::'       ],# ignore base's file portion
+[ "Mac->rel2abs(':file','hd:d1:d2:')",         'hd:d1:d2:file'    ],
+[ "Mac->rel2abs('::file','hd:d1:d2:')",        'hd:d1:d2::file'   ],
+[ "Mac->rel2abs('::d3:','hd:d1:d2:')",         'hd:d1:d2::d3:'    ],
+[ "Mac->rel2abs('hd:','hd:d1:d2:')",           'hd:'              ], # path already absolute
+[ "Mac->rel2abs('hd:d3:file','hd:d1:d2:')",    'hd:d3:file'       ],
+[ "Mac->rel2abs('hd:d3:','hd:d1:file')",       'hd:d3:'           ],
 ) ;
 
-# Grab all of the plain routines from File::Spec
-use File::Spec @File::Spec::EXPORT_OK ;
-
-require File::Spec::Unix ;
-require File::Spec::Win32 ;
-
-eval {
-   require VMS::Filespec ;
-} ;
-
-my $skip_exception = "Install VMS::Filespec (from vms/ext)" ;
-
-if ( $@ ) {
-   # Not pretty, but it allows testing of things not implemented soley
-   # on VMS.  It might be better to change File::Spec::VMS to do this,
-   # making it more usable when running on (say) Unix but working with
-   # VMS paths.
-   eval qq-
-      sub File::Spec::VMS::vmsify  { die "$skip_exception" }
-      sub File::Spec::VMS::unixify { die "$skip_exception" }
-      sub File::Spec::VMS::vmspath { die "$skip_exception" }
-   - ;
-   $INC{"VMS/Filespec.pm"} = 1 ;
-}
-require File::Spec::VMS ;
-
-require File::Spec::OS2 ;
-require File::Spec::Mac ;
 
 print "1..", scalar( @tests ), "\n" ;
 
@@ -333,7 +487,6 @@ for ( @tests ) {
 }
 
 
-
 #
 # Tries a named function with the given args and compares the result against
 # an expected result. Works with functions that return scalars or arrays.