[Bps-public-commit] Config-GitLike branch, master, updated. 1.00-24-gfcd8cd4

sunnavy at bestpractical.com sunnavy at bestpractical.com
Wed Aug 19 02:03:18 EDT 2009


The branch, master has been updated
       via  fcd8cd419e0efebb4480865e57deef05fbf44371 (commit)
      from  710eea409e02ea6574a917fd475630eb849a54cc (commit)

Summary of changes:
 lib/Config/GitLike.pm |   20 +++++++++-------
 t/t1300-repo-config.t |   59 ++++++++++++++++++++++--------------------------
 2 files changed, 38 insertions(+), 41 deletions(-)

- Log -----------------------------------------------------------------
commit fcd8cd419e0efebb4480865e57deef05fbf44371
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Aug 19 14:02:59 2009 +0800

    we should auto escape \ and " for subsection

diff --git a/lib/Config/GitLike.pm b/lib/Config/GitLike.pm
index 08a8063..c8911f1 100644
--- a/lib/Config/GitLike.pm
+++ b/lib/Config/GitLike.pm
@@ -229,7 +229,7 @@ sub parse_content {
             $section = lc $1;
             if ($2) {
                 my $subsection = $2;
-                my $check      = $2;
+                my $check = $2;
                 $check =~ s{\\\\}{}g;
                 $check =~ s{\\"}{}g;
                 return $args{error}->(
@@ -240,6 +240,8 @@ sub parse_content {
                     # double-quotes or backslashes
                 ) if $check =~ /\\|"/;
 
+                $subsection =~ s{\\\\}{\\}g;
+                $subsection =~ s{\\"}{"}g;
                 $section .= ".$subsection";
             }
 
@@ -717,12 +719,11 @@ sub group_set {
         die "Invalid section name $section\n"
             if $self->_invalid_section_name($section);
 
+        my $unescaped_subsection;
         if ( defined $subsection ) {
-            my $check = $subsection;
-            $check =~ s!\\\\!!g;
-            $check =~ s!\\"!!g;
-            die "Unescaped backslash or \" in subsection $subsection\n"
-              if $check =~ /\\|"/;
+            $unescaped_subsection = $subsection;
+            $subsection =~ s!\\!\\\\!g;
+            $subsection =~ s!"!\\"!g;
         }
 
         $args{value} = $self->cast(
@@ -735,9 +736,10 @@ sub group_set {
         my @replace;
 
         # use this for comparison
-        my $cmp_section
-            = defined $subsection ? join('.', lc $section, $subsection)
-                                  : lc $section;
+        my $cmp_section =
+          defined $unescaped_subsection
+          ? join( '.', lc $section, $unescaped_subsection )
+          : lc $section;
         # ...but this for writing (don't lowercase)
         my $combined_section
             = defined $subsection ? join('.', $section, $subsection)
diff --git a/t/t1300-repo-config.t b/t/t1300-repo-config.t
index 1d79659..6a10054 100644
--- a/t/t1300-repo-config.t
+++ b/t/t1300-repo-config.t
@@ -2,7 +2,7 @@ use strict;
 use warnings;
 
 use File::Copy;
-use Test::More tests => 140;
+use Test::More tests => 142;
 use Test::Exception;
 use File::Spec;
 use File::Temp qw/tempdir/;
@@ -1452,22 +1452,6 @@ lives_and {
 
 throws_ok {
     $config->set(
-        key => 'section.foo\bar.baz',
-        value => 'none',
-        filename => $config_filename,
-    ) } qr/unescaped backslash or \" in subsection/im,
-'subsection names cannot contain unescaped backslash in compat mode';
-
-throws_ok {
-    $config->set(
-        key => 'section.foo"bar.baz',
-        value => 'none',
-        filename => $config_filename,
-    ) } qr/unescaped backslash or \" in subsection/im,
-'subsection names cannot contain unescaped " in compat mode';
-
-throws_ok {
-    $config->set(
         key => "section.foo\nbar.baz",
         value => 'none',
         filename => $config_filename,
@@ -1476,13 +1460,6 @@ throws_ok {
 
 # these should be the case in no-compat mode too
 $config->compatible(0);
-throws_ok {
-    $config->set(
-        key => 'section.foo\bar.baz',
-        value => 'none',
-        filename => $config_filename,
-    ) } qr/unescaped backslash or \" in subsection/im,
-'subsection names cannot contain unescaped backslash in nocompat mode';
 
 throws_ok {
     $config->set(
@@ -1492,14 +1469,6 @@ throws_ok {
     ) } qr/invalid key/im,
 'subsection names cannot contain unescaped newlines in nocompat mode';
 
-throws_ok {
-    $config->set(
-        key => 'section.foo"bar.baz',
-        value => 'none',
-        filename => $config_filename,
-    ) } qr/unescaped backslash or \" in subsection/im,
-'subsection names cannot contain unescaped " in nocompat mode';
-
 # Make sure some bad configs throw errors.
 burp(
     $config_filename,
@@ -1567,3 +1536,29 @@ while ( my ( $k, $v ) = each %special_in_value ) {
           );
     }
 }
+
+# special chars in subsection
+my %special_in_subsection =
+  ( backslash => "\\", doublequote => q{"} );
+
+while ( my ( $k, $v ) = each %special_in_subsection ) {
+    for my $times ( 1 .. 3 ) {
+        my $key = 'section.foo' . $v x $times . 'bar' . $v x $times . 'baz';
+
+        lives_and {
+            $config->set(
+                key      => $key,
+                value    => 'none',
+                filename => $config_filename,
+            );
+            $config->load;
+            is( $config->get( key => $key ), 'none' );
+        }
+        "subsection with $k occurs with $times time"
+          . (
+            $times == 1
+            ? ''
+            : 's'
+          );
+    }
+}

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list