Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Validate prereqs.

  • Loading branch information...
commit a38590efec2ecb1810f024e14643a464ac8b89fc 1 parent 6639e3a
David E. Wheeler authored May 24, 2011
25  lib/PGXN/Meta/Validator.pm
@@ -521,7 +521,7 @@ sub _string_class {
521 521
         }
522 522
         return 1;
523 523
     } else {
524  
-        $self->_error("value is not a valid $type");
  524
+        $self->_error("'$value' is not a valid $type");
525 525
         return 0;
526 526
     }
527 527
 }
@@ -543,13 +543,24 @@ sub file {
543 543
 
544 544
 sub exversion {
545 545
     my ($self,$key,$value) = @_;
546  
-    if(defined $value && ($value || $value =~ /0/)) {
  546
+    if (defined $value && ($value || $value eq '0')) {
547 547
         my $pass = 1;
548  
-        for my $val (split ',', $value) {
549  
-            unless (defined $val && (
550  
-                $val eq '0' || eval { SemVer->new($val) }
551  
-            )) {
552  
-                $self->_error( "'$val' for '$key' is not a valid version." );
  548
+        for my $val (split /,\s*/, $value) {
  549
+            if ($val ne '') {
  550
+                next if $val eq '0';
  551
+                if ($val =~ s/^([^\d\s]+)\s*//) {
  552
+                    my $op = $1;
  553
+                    if ($op !~ /^[<>](?:=)?|==|!=$/) {
  554
+                        $self->_error("'$op' for '$key' is not a valid version range operator");
  555
+                        $pass = 0;
  556
+                    }
  557
+                }
  558
+                unless (eval { SemVer->new($val) }) {
  559
+                    $self->_error( "'$val' for '$key' is not a valid version." );
  560
+                    $pass = 0;
  561
+                }
  562
+            } else {
  563
+                $self->_error( "'<undef>' for '$key' is not a valid version." );
553 564
                 $pass = 0;
554 565
             }
555 566
         }
97  t/validator.t
@@ -101,6 +101,39 @@ for my $spec (
101 101
         directory => 'baz',
102 102
     }}],
103 103
     ['no index custom key' => sub { shift->{no_index}{X_foo} = 1 }],
  104
+    (map {
  105
+        my $phase = $_;
  106
+        map {
  107
+            my $rel = $_;
  108
+            [
  109
+                "$phase $rel prereq",
  110
+                sub { my $m = shift; $m->{prereqs}{$phase}{$rel} = { foo => '1.2.0' }},
  111
+            ]
  112
+        } qw(requires recommends suggests);
  113
+    } qw(configure runtime build test develop)),
  114
+    (map {
  115
+        my $op = $_;
  116
+        [
  117
+            "version range with $op operator",
  118
+            sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = "$op 1.8.0"},
  119
+        ],
  120
+        [
  121
+            "version range with unspaced $op operator",
  122
+            sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = "${op}1.8.0"},
  123
+        ],
  124
+    } qw(== != < <= > >=)),
  125
+    [
  126
+        'prereq complex version range',
  127
+        sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = '>= 1.2.0, != 1.5.0, < 2.0.0'},
  128
+    ],
  129
+    [
  130
+        'prereq complex unspaced version range',
  131
+        sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = '>=1.2.0,!=1.5.0,<2.0.0'},
  132
+    ],
  133
+    [
  134
+        'prereq version 0',
  135
+        sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = 0 },
  136
+    ],
104 137
 ) {
105 138
     my ($desc, $sub) = @{ $spec };
106 139
     my $dm = clone $distmeta;
@@ -259,27 +292,27 @@ for my $spec (
259 292
     [
260 293
         'name with newline',
261 294
         sub { shift->{name} = "foo\nbar" },
262  
-        "value is not a valid term (name) [Validation: 1.0.0]",
  295
+        "'foo\nbar' is not a valid term (name) [Validation: 1.0.0]",
263 296
     ],
264 297
     [
265 298
         'name with return',
266 299
         sub { shift->{name} = "foo\rbar" },
267  
-        "value is not a valid term (name) [Validation: 1.0.0]",
  300
+        "'foo\rbar' is not a valid term (name) [Validation: 1.0.0]",
268 301
     ],
269 302
     [
270 303
         'name with slash',
271 304
         sub { shift->{name} = "foo/bar" },
272  
-        "value is not a valid term (name) [Validation: 1.0.0]",
  305
+        "'foo/bar' is not a valid term (name) [Validation: 1.0.0]",
273 306
     ],
274 307
     [
275 308
         'name with backslash',
276 309
         sub { shift->{name} = "foo\\bar" },
277  
-        "value is not a valid term (name) [Validation: 1.0.0]",
  310
+        "'foo\\bar' is not a valid term (name) [Validation: 1.0.0]",
278 311
     ],
279 312
     [
280 313
         'name with space',
281 314
         sub { shift->{name} = "foo bar" },
282  
-        "value is not a valid term (name) [Validation: 1.0.0]",
  315
+        "'foo bar' is not a valid term (name) [Validation: 1.0.0]",
283 316
     ],
284 317
     [
285 318
         'short name',
@@ -304,12 +337,12 @@ for my $spec (
304 337
     [
305 338
         'empty tag',
306 339
         sub { shift->{tags} = '' },
307  
-        "value is not a valid tag (tags -> <undef>) [Validation: 1.0.0]",
  340
+        "'' is not a valid tag (tags -> <undef>) [Validation: 1.0.0]",
308 341
     ],
309 342
     [
310 343
         'empty tag item',
311 344
         sub { shift->{tags} = ['foo', ''] },
312  
-        "value is not a valid tag (tags -> <undef>) [Validation: 1.0.0]",
  345
+        "'' is not a valid tag (tags -> <undef>) [Validation: 1.0.0]",
313 346
     ],
314 347
     [
315 348
         'undef tag item',
@@ -366,6 +399,56 @@ for my $spec (
366 399
         sub { shift->{no_index}{foo} = ['hi'] },
367 400
         "Custom key 'foo' must begin with 'x_' or 'X_'. (no_index -> foo) [Validation: 1.0.0]",
368 401
     ],
  402
+    [
  403
+        'prereq undef version',
  404
+        sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = undef },
  405
+        "'<undef>' for 'PostgreSQL' is not a valid version. (prereqs -> runtime -> requires -> PostgreSQL) [Validation: 1.0.0]",
  406
+    ],
  407
+    [
  408
+        'prereq invalid version',
  409
+        sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = '1.0' },
  410
+        "'1.0' for 'PostgreSQL' is not a valid version. (prereqs -> runtime -> requires -> PostgreSQL) [Validation: 1.0.0]",
  411
+    ],
  412
+    [
  413
+        'prereq undef version',
  414
+        sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = undef },
  415
+        "'<undef>' for 'PostgreSQL' is not a valid version. (prereqs -> runtime -> requires -> PostgreSQL) [Validation: 1.0.0]",
  416
+    ],
  417
+    [
  418
+        'prereq invalid version op',
  419
+        sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = '= 1.0.0' },
  420
+        "'=' for 'PostgreSQL' is not a valid version range operator (prereqs -> runtime -> requires -> PostgreSQL) [Validation: 1.0.0]",
  421
+    ],
  422
+    [
  423
+        'prereq wtf version op',
  424
+        sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = '*** 1.0.0' },
  425
+        "'***' for 'PostgreSQL' is not a valid version range operator (prereqs -> runtime -> requires -> PostgreSQL) [Validation: 1.0.0]",
  426
+    ],
  427
+    [
  428
+        'prereq verersion leading comma',
  429
+        sub { shift->{prereqs}{runtime}{requires}{PostgreSQL} = ',1.0.0' },
  430
+        "'<undef>' for 'PostgreSQL' is not a valid version. (prereqs -> runtime -> requires -> PostgreSQL) [Validation: 1.0.0]",
  431
+    ],
  432
+    [
  433
+        'invalid prereq phase',
  434
+        sub { shift->{prereqs}{howdy}{requires}{PostgreSQL} = '1.0.0' },
  435
+        "Key 'howdy' is not a legal phase. (prereqs -> howdy) [Validation: 1.0.0]",
  436
+    ],
  437
+    [
  438
+        'invalid prereq phase',
  439
+        sub { shift->{prereqs}{runtime}{wanking}{PostgreSQL} = '1.0.0' },
  440
+        "Key 'wanking' is not a legal prereq relationship. (prereqs -> runtime -> wanking) [Validation: 1.0.0]",
  441
+    ],
  442
+    [
  443
+        'non-map prereq',
  444
+        sub { shift->{prereqs}{runtime}{requires} = [ PostgreSQL => '1.0.0' ] },
  445
+        "Expected a map structure. (prereqs -> runtime -> requires) [Validation: 1.0.0]",
  446
+    ],
  447
+    [
  448
+        'non-term prereq',
  449
+        sub { shift->{prereqs}{runtime}{requires}{'foo/bar'} = '1.0.0' },
  450
+        "'foo/bar' is not a valid term (prereqs -> runtime -> requires -> foo/bar) [Validation: 1.0.0]",
  451
+    ],
369 452
 ) {
370 453
     my ($desc, $sub, $err) = @{ $spec };
371 454
     my $dm = clone $distmeta;

0 notes on commit a38590e

Please sign in to comment.
Something went wrong with that request. Please try again.