add new flag AlwaysQuoteOutput for write quoting

This commit is contained in:
2025-01-07 10:36:02 +01:00
parent 35dc4c10c9
commit 3937d2423e
3 changed files with 20 additions and 4 deletions

View File

@@ -32,7 +32,7 @@ use Carp::Heavy;
use Carp; use Carp;
use Exporter; use Exporter;
$Config::General::VERSION = "2.65"; $Config::General::VERSION = "2.66";
use base qw(Exporter); use base qw(Exporter);
our @EXPORT_OK = qw(ParseConfig SaveConfig SaveConfigString); our @EXPORT_OK = qw(ParseConfig SaveConfig SaveConfigString);
@@ -96,7 +96,8 @@ sub new {
NormalizeValue => 0, NormalizeValue => 0,
Plug => {}, Plug => {},
UseApacheIfDefine => 0, UseApacheIfDefine => 0,
Define => {} Define => {},
AlwaysQuoteOutput => 0
}; };
# create the class instance # create the class instance
@@ -1424,7 +1425,7 @@ sub _write_scalar {
$line =~ s/([#\$\\\"])/\\$1/g; $line =~ s/([#\$\\\"])/\\$1/g;
} }
if ($line =~ /\s/) { if ($line =~ /^\s/ || $line =~ /\s$/ || $this->{AlwaysQuoteOutput}) {
# quote lines containing whitespace # quote lines containing whitespace
$line = "\"$line\""; $line = "\"$line\"";
} }
@@ -2119,6 +2120,11 @@ Same as B<-NormalizeBlock> but applied on options only.
Same as B<-NormalizeBlock> but applied on values only. Same as B<-NormalizeBlock> but applied on values only.
=item B<-AlwaysQuoteOutput>
If set to true, then values containing whitespace will always quoted
when calling C<save_string()> or C<save_file()>.
=back =back

1
t/cfg.59 Normal file
View File

@@ -0,0 +1 @@
foo = "bar baz"

11
t/run.t
View File

@@ -8,7 +8,7 @@
use Data::Dumper; use Data::Dumper;
use Test::More tests => 78; use Test::More tests => 79;
#use Test::More qw(no_plan); #use Test::More qw(no_plan);
# ahem, we deliver the test code with a local copy of # ahem, we deliver the test code with a local copy of
@@ -791,3 +791,12 @@ foreach my $def (keys %defs) {
my %hash = $conf->getall(); my %hash = $conf->getall();
is_deeply \%hash, $expected58, "UseApacheIfDefine, -Define => $def"; is_deeply \%hash, $expected58, "UseApacheIfDefine, -Define => $def";
} }
# force quoting
my $cfg59 = "t/cfg.59";
my $expected59 = qq(foo "bar baz"
); # newline is important here, as we check write output
my $conf = Config::General->new(-ConfigFile => $cfg59,
-AlwaysQuoteOutput => 1);
my $got59 = $conf->save_string();
is_deeply \$expected59, \$got59, "quotes";