"Fossies" - the Fresh Open Source Software Archive

Member "dpkg-1.19.7/scripts/t/Dpkg_Version.t" (19 Apr 2019, 7079 Bytes) of package /linux/misc/dpkg_1.19.7.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl
    2 #
    3 # This program is free software; you can redistribute it and/or modify
    4 # it under the terms of the GNU General Public License as published by
    5 # the Free Software Foundation; either version 2 of the License, or
    6 # (at your option) any later version.
    7 #
    8 # This program is distributed in the hope that it will be useful,
    9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11 # GNU General Public License for more details.
   12 #
   13 # You should have received a copy of the GNU General Public License
   14 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
   15 
   16 use strict;
   17 use warnings;
   18 
   19 use Test::More;
   20 
   21 use Dpkg::ErrorHandling;
   22 use Dpkg::IPC;
   23 use Dpkg::Path qw(find_command);
   24 use Dpkg::Version;
   25 
   26 report_options(quiet_warnings => 1);
   27 
   28 my @tests = <DATA>;
   29 my @ops = ('<', '<<', 'lt',
   30        '<=', 'le',
   31        '=', 'eq',
   32        '>=', 'ge',
   33        '>', '>>', 'gt');
   34 
   35 plan tests => scalar(@tests) * (3 * scalar(@ops) + 4) + 27;
   36 
   37 my $have_dpkg = find_command('dpkg');
   38 
   39 sub dpkg_vercmp {
   40      my ($a, $cmp, $b) = @_;
   41      my $stderr;
   42 
   43      spawn(exec => [ 'dpkg', '--compare-versions', '--', $a, $cmp, $b ],
   44            error_to_string => \$stderr, wait_child => 1, nocheck => 1);
   45      diag("dpkg --compare-versions error=$?: $stderr") if $? and $? != 256;
   46 
   47      return $? == 0;
   48 }
   49 
   50 sub obj_vercmp {
   51      my ($a, $cmp, $b) = @_;
   52      return $a < $b  if $cmp eq '<<';
   53      return $a lt $b if $cmp eq 'lt';
   54      return $a <= $b if $cmp eq '<=' or $cmp eq '<';
   55      return $a le $b if $cmp eq 'le';
   56      return $a == $b if $cmp eq '=';
   57      return $a eq $b if $cmp eq 'eq';
   58      return $a >= $b if $cmp eq '>=' or $cmp eq '>';
   59      return $a ge $b if $cmp eq 'ge';
   60      return $a > $b  if $cmp eq '>>';
   61      return $a gt $b if $cmp eq 'gt';
   62 }
   63 
   64 my $truth = {
   65     '-1' => {
   66     '<<' => 1, 'lt' => 1,
   67     '<=' => 1, 'le' => 1, '<' => 1,
   68     '=' => 0, 'eq' => 0,
   69     '>=' => 0, 'ge' => 0, '>' => 0,
   70     '>>' => 0, 'gt' => 0,
   71     },
   72     '0' => {
   73     '<<' => 0, 'lt' => 0,
   74     '<=' => 1, 'le' => 1, '<' => 1,
   75     '=' => 1, 'eq' => 1,
   76     '>=' => 1, 'ge' => 1, '>' => 1,
   77     '>>' => 0, 'gt' => 0,
   78     },
   79     '1' => {
   80     '<<' => 0, 'lt' => 0,
   81     '<=' => 0, 'le' => 0, '<' => 0,
   82     '=' => 0, 'eq' => 0,
   83     '>=' => 1, 'ge' => 1, '>' => 1,
   84     '>>' => 1, 'gt' => 1,
   85     },
   86 };
   87 
   88 # XXX: Some of the tests check the bool overload, which currently emits
   89 # the semantic_change warning. Disable it until we stop emitting the
   90 # warning in dpkg 1.20.x.
   91 ## no critic (TestingAndDebugging::ProhibitNoWarnings)
   92 no warnings(qw(Dpkg::Version::semantic_change::overload::bool));
   93 
   94 # Handling of empty/invalid versions
   95 my $empty = Dpkg::Version->new('');
   96 ok($empty eq '', "Dpkg::Version->new('') eq ''");
   97 ok($empty->as_string() eq '', "Dpkg::Version->new('')->as_string() eq ''");
   98 ok(!$empty->is_valid(), 'empty version is invalid');
   99 $empty = Dpkg::Version->new('-0');
  100 ok($empty eq '', "Dpkg::Version->new('-0') eq '-0'");
  101 ok($empty->as_string() eq '-0', "Dpkg::Version->new('-0')->as_string() eq '-0'");
  102 ok(!$empty->is_valid(), 'empty upstream version is invalid');
  103 $empty = Dpkg::Version->new('0:-0');
  104 ok($empty eq '0:-0', "Dpkg::Version->new('0:-0') eq '0:-0'");
  105 ok($empty->as_string() eq '0:-0', "Dpkg::Version->new('0:-0')->as_string() eq '0:-0'");
  106 ok(!$empty->is_valid(), 'empty upstream version with epoch is invalid');
  107 $empty = Dpkg::Version->new(':1.0');
  108 ok($empty eq ':1.0', "Dpkg::Version->new(':1.0') eq ':1.0'");
  109 ok($empty->as_string() eq ':1.0', "Dpkg::Version->new(':1.0')->as_string() eq ':1.0'");
  110 ok(!$empty->is_valid(), 'empty epoch is invalid');
  111 $empty = Dpkg::Version->new('1.0-');
  112 ok($empty eq '1.0-', "Dpkg::Version->new('1.0-') eq '1.0-'");
  113 ok($empty->as_string() eq '1.0-', "Dpkg::Version->new('1.0-')->as_string() eq '1.0-'");
  114 ok(!$empty->is_valid(), 'empty revision is invalid');
  115 my $ver = Dpkg::Version->new('10a:5.2');
  116 ok(!$ver->is_valid(), 'bad epoch is invalid');
  117 ok(!$ver, 'bool eval of invalid leads to false');
  118 ok($ver eq '10a:5.2', 'invalid still same string 1/2');
  119 $ver = Dpkg::Version->new('5.2@3-2');
  120 ok($ver eq '5.2@3-2', 'invalid still same string 2/2');
  121 ok(!$ver->is_valid(), 'illegal character is invalid');
  122 $ver = Dpkg::Version->new('foo5.2');
  123 ok(!$ver->is_valid(), 'version does not start with digit 1/2');
  124 $ver = Dpkg::Version->new('0:foo5.2');
  125 ok(!$ver->is_valid(), 'version does not start with digit 2/2');
  126 
  127 # Native and non-native versions
  128 $ver = Dpkg::Version->new('1.0');
  129 ok($ver->is_native(), 'upstream version is native');
  130 $ver = Dpkg::Version->new('1:1.0');
  131 ok($ver->is_native(), 'upstream version w/ epoch is native');
  132 $ver = Dpkg::Version->new('1:1.0:1.0');
  133 ok($ver->is_native(), 'upstream version w/ epoch and colon is native');
  134 $ver = Dpkg::Version->new('1.0-1');
  135 ok(!$ver->is_native(), 'upstream version w/ revision is not native');
  136 $ver = Dpkg::Version->new('1.0-1.0-1');
  137 ok(!$ver->is_native(), 'upstream version w/ dash and revision is not native');
  138 
  139 # Comparisons
  140 foreach my $case (@tests) {
  141     my ($a, $b, $res) = split ' ', $case;
  142     my $va = Dpkg::Version->new($a, check => 1);
  143     my $vb = Dpkg::Version->new($b, check => 1);
  144 
  145     is("$va", $a, "String representation of Dpkg::Version($a) is $a");
  146     is("$vb", $b, "String representation of Dpkg::Version($b) is $b");
  147 
  148     is(version_compare($a, $b), $res, "$a cmp $b => $res");
  149     is($va <=> $vb, $res, "Dpkg::Version($a) <=> Dpkg::Version($b) => $res");
  150     foreach my $op (@ops) {
  151         my $norm_op = version_normalize_relation($op);
  152     if ($truth->{$res}{$op}) {
  153         ok(version_compare_relation($a, $norm_op, $b), "$a $op $b => true");
  154         ok(obj_vercmp($va, $op, $vb), "Dpkg::Version($a) $op Dpkg::Version($b) => true");
  155 
  156             SKIP: {
  157                 skip 'dpkg not available', 1 if not $have_dpkg;
  158 
  159                 ok(dpkg_vercmp($a, $op, $b), "dpkg --compare-versions -- $a $op $b => true");
  160             }
  161     } else {
  162         ok(!version_compare_relation($a, $norm_op, $b), "$a $op $b => false");
  163         ok(!obj_vercmp($va, $op, $vb), "Dpkg::Version($a) $op Dpkg::Version($b) => false");
  164 
  165             SKIP: {
  166                 skip 'dpkg not available', 1 if not $have_dpkg;
  167 
  168                 ok(!dpkg_vercmp($a, $op, $b), "dpkg --compare-versions -- $a $op $b => false");
  169             }
  170     }
  171     }
  172 }
  173 
  174 __DATA__
  175 1.0-1 2.0-2 -1
  176 2.2~rc-4 2.2-1 -1
  177 2.2-1 2.2~rc-4 1
  178 1.0000-1 1.0-1 0
  179 1 0:1 0
  180 0 0:0-0 0
  181 2:2.5 1:7.5 1
  182 1:0foo 0foo 1
  183 0:0foo 0foo 0
  184 0foo 0foo 0
  185 0foo-0 0foo 0
  186 0foo 0foo-0 0
  187 0foo 0fo 1
  188 0foo-0 0foo+ -1
  189 0foo~1 0foo -1
  190 0foo~foo+Bar 0foo~foo+bar -1
  191 0foo~~ 0foo~ -1
  192 1~ 1 -1
  193 12345+that-really-is-some-ver-0 12345+that-really-is-some-ver-10 -1
  194 0foo-0 0foo-01 -1
  195 0foo.bar 0foobar 1
  196 0foo.bar 0foo1bar 1
  197 0foo.bar 0foo0bar 1
  198 0foo1bar-1 0foobar-1 -1
  199 0foo2.0 0foo2 1
  200 0foo2.0.0 0foo2.10.0 -1
  201 0foo2.0 0foo2.0.0 -1
  202 0foo2.0 0foo2.10 -1
  203 0foo2.1 0foo2.10 -1
  204 1.09 1.9 0
  205 1.0.8+nmu1 1.0.8 1
  206 3.11 3.10+nmu1 1
  207 0.9j-20080306-4 0.9i-20070324-2 1
  208 1.2.0~b7-1 1.2.0~b6-1 1
  209 1.011-1 1.06-2 1
  210 0.0.9+dfsg1-1 0.0.8+dfsg1-3 1
  211 4.6.99+svn6582-1 4.6.99+svn6496-1 1
  212 53 52 1
  213 0.9.9~pre122-1 0.9.9~pre111-1 1
  214 2:2.3.2-2+lenny2 2:2.3.2-2 1
  215 1:3.8.1-1 3.8.GA-1 1
  216 1.0.1+gpl-1 1.0.1-2 1
  217 1a 1000a -1