[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[SCM] Debian package checker branch, master, updated. 2.5.10-128-g58d86f4



The following commit has been merged in the master branch:
commit 58d86f4873ac23b2d3efe9d6a8acea953ea88b6f
Author: Niels Thykier <niels@thykier.net>
Date:   Thu Sep 6 17:41:26 2012 +0200

    L::Util: Add parse_boolean sub
    
    Unify the two boolean parsers in lintian into one and put it in
    L::Util.
    
    Signed-off-by: Niels Thykier <niels@thykier.net>

diff --git a/frontend/lintian b/frontend/lintian
index 4cf137a..c98dcf8 100755
--- a/frontend/lintian
+++ b/frontend/lintian
@@ -626,6 +626,8 @@ require Lintian::Profile;
 require Lintian::Tags;
 import Lintian::Tags qw(tag);
 require Lintian::Unpacker;
+require Lintian::Util;
+import Lintian::Util qw(parse_boolean);
 
 if (defined $experimental_output_opts) {
     my %opts = map { split(/=/) } split( /,/, $experimental_output_opts );
@@ -707,11 +709,10 @@ if ($opt{'LINTIAN_CFG'}) {
                 }
                 $conf_opt{$var} = 1;
                 $found = 1;
-                if ($val =~ m/^y(?:es)?|true$/oi){
-                    $val = 1;
-                } elsif ($val =~ m/^no?|false$/oi){
-                    $val = 0;
-                }
+                # Translate boolean strings to "0" or "1"; ignore
+                # errors as not all values are (intended to be)
+                # booleans.
+                eval { $val = parse_boolean ($val); };
                 if (ref $ref eq 'SCALAR'){
                     # Check it was already set
                     next if defined $$ref;
diff --git a/lib/Lintian/Profile.pm b/lib/Lintian/Profile.pm
index 01c8fd1..4a183e2 100644
--- a/lib/Lintian/Profile.pm
+++ b/lib/Lintian/Profile.pm
@@ -27,7 +27,7 @@ use warnings;
 use Carp qw(croak);
 
 use Lintian::CheckScript;
-use Lintian::Util qw(read_dpkg_control);
+use Lintian::Util qw(parse_boolean read_dpkg_control);
 
 =head1 NAME
 
@@ -39,7 +39,7 @@ Lintian::Profile - Profile parser for Lintian
  my $profile = Lintian::Profile->new ('debian', $ENV{'LINTIAN_ROOT'});
  # Load the debian profile using an explicit search path
  $profile = Lintian::Profile->new ('debian', $ENV{'LINTIAN_ROOT'},
-    ['/path/to/profiles', "$ENV{'LINTIAN_ROOT'}/profiles"])
+    ['/path/to/profiles', "$ENV{'LINTIAN_ROOT'}/profiles"]);
  # Load the "default" profile for the current vendor
  $profile = Lintian::Profile->new (undef, $ENV{'LINTIAN_ROOT'});
  foreach my $tag ($profile->tags) {
@@ -453,13 +453,12 @@ sub _check_duplicates{
 # error reporting.
 sub _parse_boolean {
     my ($self, $bool, $def, $pname, $sno) = @_;
+    my $val;
     return $def unless defined $bool;
-    $bool = lc $bool;
-    return 1 if $bool eq 'yes' || $bool eq 'true' ||
-        ($bool =~ m/^\d++$/o && $bool != 0);
-    return 0  if $bool eq 'no' || $bool eq 'false' ||
-        ($bool =~ m/^\d++$/o && $bool == 0);
-    croak "\"$bool\" is not a boolean value in $pname (section $sno)";
+    eval { $val = parse_boolean ($bool); };
+    croak "\"$bool\" is not a boolean value in $pname (section $sno)"
+        if $@;
+    return $val;
 }
 
 # $self->_split_comma_sep_field($data)
diff --git a/lib/Lintian/Util.pm b/lib/Lintian/Util.pm
index 61437f7..43d5ba7 100644
--- a/lib/Lintian/Util.pm
+++ b/lib/Lintian/Util.pm
@@ -23,6 +23,8 @@ package Lintian::Util;
 use strict;
 use warnings;
 
+use Carp qw(croak);
+
 use base 'Exporter';
 
 use constant {
@@ -65,6 +67,7 @@ BEGIN {
                  check_path
                  clean_env
                  resolve_pkg_path
+                 parse_boolean
                  $PKGNAME_REGEX),
                  @{ $EXPORT_TAGS{constants} }
     );
@@ -940,6 +943,38 @@ sub resolve_pkg_path {
     return join '/', @cc;
 }
 
+=item parse_boolean (STR)
+
+Attempt to parse STR as a boolean and return its value.
+If STR is not a valid/recognised boolean, the sub will
+invoke croak.
+
+The following values recognised (string checks are not
+case sensitive):
+
+=over 4
+
+=item The integer 0 is considered false
+
+=item Any non-zero integer is considered true
+
+=item "true", "y" and "yes" are considered true
+
+=item "false", "n" and "no" are considered false
+
+=back
+
+=cut
+
+sub parse_boolean {
+    my ($str) = @_;
+    return $str == 0 ? 0 : 1 if $str =~ m/^-?\d++$/o;
+    $str = lc $str;
+    return 1 if $str eq 'true' or $str =~ m/^y(?:es)?$/;
+    return 0 if $str eq 'false' or $str =~ m/^no?$/;
+    croak "\"$str\" is not a valid boolean value";
+}
+
 =back
 
 =head1 SEE ALSO
diff --git a/t/scripts/Lintian/Util/parse_bool.t b/t/scripts/Lintian/Util/parse_bool.t
new file mode 100644
index 0000000..3cf76cc
--- /dev/null
+++ b/t/scripts/Lintian/Util/parse_bool.t
@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+BEGIN { use_ok('Lintian::Util', qw(parse_boolean)); }
+
+my @truth_vals = (qw(
+   YES
+   y
+   TrUE
+   1 10 1 01
+));
+
+my @false_vals = (qw(
+   No
+   n
+   0 00
+));
+
+my @not_bools = ('', 'random-string', ' 0', '0 ', '1 ', ' 1');
+
+foreach my $truth (@truth_vals) {
+    eval {
+        ok (parse_boolean ($truth), "$truth should be true value");
+    };
+    fail ("$truth should be parsable as a bool") if $@;
+}
+
+foreach my $false (@false_vals) {
+    eval {
+        ok (! parse_boolean ($false), "$false should be false");
+    };
+    fail ("$false should be parsable as a bool") if $@;
+}
+
+foreach my $not_bool (@not_bools) {
+    eval {
+        parse_boolean ($not_bool);
+        fail ("$not_bool should not be parsed as a bool");
+    };
+    if ($@) {
+        pass ("$not_bool is not a boolean");
+    }
+}
+
+plan tests => (1 + scalar @truth_vals +
+    scalar @false_vals +
+    scalar @not_bools);

-- 
Debian package checker


Reply to: