Refactor filenames.t to use unlike() and cmp_ok() for better diagnostics.
authorNicholas Clark <nick@ccl4.org>
Tue, 4 Jan 2011 08:29:35 +0000 (08:29 +0000)
committerNicholas Clark <nick@ccl4.org>
Tue, 4 Jan 2011 08:29:35 +0000 (08:29 +0000)
Also, use regexs in place of the loops over @dont and @more_dont, the lists
of Win32 (was DOS (was CP/M)) reserved device filenames and of disallowed
characters. This makes the intent clearer, at the cost of some runtime.

t/porting/filenames.t

index 67dc89a..3cb7ad2 100644 (file)
@@ -35,9 +35,6 @@ plan('no_plan');
 
 my $manifest = File::Spec->catfile(File::Spec->updir(), 'MANIFEST');
 
-my @dont = qw/CON PRN AUX NUL COM1 COM2 COM3 COM4 COM5 COM6 COM7 COM8 COM9 LPT1 LPT2 LPT3 LPT4 LPT5 LPT6 LPT7 LPT8 LPT9/;
-my @more_dont = ('\s','\(','\&');
-
 open my $m, '<', $manifest or die "Can't open '$manifest': $!";
 my @files;
 while (<$m>) {
@@ -52,51 +49,35 @@ sub validate_file_name {
     my $path = shift;
     my $filename = basename $path;
 
+    note("testing $path");
 
     my @path_components = split('/',$path);
     pop @path_components; # throw away the filename
     for my $component (@path_components) {
-        if ($component =~ /\..*?\./) {
-            fail("$path has a directory component containing more than one '.'");
-            return;
-        }
-
-        if (length($component) > 32) {
-            fail("$path has a directory with a name over 32 characters. This fails on VOS");
-        }
+        unlike($component, qr/\..*?\./,
+             "no directory components containing more than one '.'")
+           or return;
+
+        cmp_ok(length $component, '<=', 32,
+              "no directory with a name over 32 characters (VOS requirement)")
+           or return;
     }
 
 
-    if ($filename =~ m/^\-/) {
-        fail("starts with -: $path");
-        return;
-    }
+    unlike($filename, qr/^\-/, "filename does not start with -");
 
     my($before, $after) = split /\./, $filename;
-    if (length $before > 39) {
-        fail("more than 39 characters before the dot: $path");
-        return;
-    }
-    if ($after and (length $after > 39)) {
-        fail("more than 39 characters after the dot: $path");
-        return;
+    cmp_ok(length $before, '<=', 39,
+          "filename has 39 or fewer characters before the dot");
+    if ($after) {
+       cmp_ok(length $after, '<=', 39,
+              "filename has 39 or fewer characters after the dot");
     }
 
-    foreach (@dont) {
-        if ($filename =~ m/^$_\./i) {
-            fail("found $_ before the dot: $path");
-            return;
-        }
-    }
-
-    foreach (@more_dont) {
-        if ($filename =~ m/$_/) {
-            fail("found $_: $path");
-            return;
-        }
-    }
+    unlike($filename, qr/^(?:CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])\./i,
+          "filename has a reserved name");
 
-    ok($filename, $path);
+    unlike($filename, qr/\s|\(|\&/, "filename has a reserved character");
 }
 
 # EOF