made compare_versions func into its own module. made tests

This commit is contained in:
frank-cizmich
2016-02-24 17:17:40 -03:00
parent 219b70b366
commit 7b6eee3509
4 changed files with 165 additions and 2453 deletions

View File

@@ -16,6 +16,7 @@ BEGIN {
OptionParser
DSNParser
Daemon
VersionCompare
));
}
@@ -1727,6 +1728,52 @@ sub _d {
# End Daemon package
# ###########################################################################
# ###########################################################################
# VersionCompare package
# This package is a copy without comments from the original. The original
# with comments and its test file can be found in the Bazaar repository at,
# lib/VersionCompare.pm
# t/lib/VersionCompare.t
# See https://launchpad.net/percona-toolkit for more information.
# ###########################################################################
{
package VersionCompare;
use strict;
use English qw(-no_match_vars);
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
sub cmp {
my ($v1, $v2) = @_;
$v1 =~ s/[^\d\.]//;
$v2 =~ s/[^\d\.]//;
my @a = ( $v1 =~ /(\d+)\.?/g );
my @b = ( $v2 =~ /(\d+)\.?/g );
foreach my $n1 (@a) {
$n1 += 0;
if (!@b) {
return 1;
}
my $n2 = shift @b;
$n2 += 0;
if ($n1 == $n2) {
next;
}
else {
return $n1 <=> $n2;
}
}
return @b ? -1 : 0;
}
1;
}
# ###########################################################################
# End VersionCompare package
# ###########################################################################
# ###########################################################################
# This is a combination of modules and programs in one -- a runnable module.
@@ -1858,9 +1905,9 @@ sub main {
PTDEBUG && _d('Checking user', $user_host);
}
# If MySQL 5.7+ then we need to use SHOW CREATE USER
# If MySQL 5.7.6+ then we need to use SHOW CREATE USER
my @create_user;
if ( compare_versions($version, '5.7.6') >= 0 ) {
if ( VersionCompare::cmp($version, '5.7.6') >= 0 ) {
eval {
@create_user = @{ $dbh->selectcol_arrayref("SHOW CREATE USER $user_host") };
};
@@ -2033,36 +2080,6 @@ sub split_grants {
return @grants;
}
sub compare_versions {
my ($v1, $v2) = @_;
# Remove all but numbers and dots.
# Assume simple 1.2.3 style
$v1 =~ s/[^\d\.]//;
$v2 =~ s/[^\d\.]//;
my @a = ( $v1 =~ /(\d+)\.?/g );
my @b = ( $v2 =~ /(\d+)\.?/g );
foreach my $n1 (@a) {
$n1 += 0; #convert to number
if (!@b) {
# b ran out of digits, a is larger
return 1;
}
my $n2 = shift @b;
$n2 += 0; # convert to number
if ($n1 == $n2) {
# still tied?, fetch next
next;
}
else {
# difference! return result
return $n1 <=> $n2;
}
}
# b still has digits? it's larger, else it's a tie
return @b ? -1 : 0;
}
sub _d {
my ($package, undef, $line) = caller 0;