Permalink
Browse files

make apply command convert true/false to boolean values

  • Loading branch information...
1 parent 73df675 commit a8d072bdb9dcd994f391a1942dc029aecf5f1b86 @xdg xdg committed Aug 2, 2012
Showing with 56 additions and 3 deletions.
  1. +2 −1 Changes
  2. +1 −0 META.json
  3. +3 −0 README.pod
  4. +15 −1 lib/Pantry/App/Command/apply.pm
  5. +3 −0 pod/bin/pantry
  6. +32 −1 t/app/apply.t
View
3 Changes
@@ -4,7 +4,8 @@ Revision history for pantry
[Added]
- - Node model now tested with JSON::Boolean values
+ - 'pantry apply' now converts 'true' and 'false' to
+ actual JSON booleans in the configuration file
0.006 2012-07-12 14:34:17 America/New_York
View
1 META.json
@@ -64,6 +64,7 @@
"App::Cmd::Tester::CaptureExternal" : "0",
"File::Find" : "0",
"File::pushd" : "1.00",
+ "Test::Deep" : "0.110",
"Test::More" : "0.96",
"parent" : "0"
}
View
3 README.pod
@@ -137,6 +137,9 @@ will be added to the node's data structure like this:
Both C<KEY> and C<VALUE> support periods and commas (respectively) to be
escaped by a backslash.
+If a C<VALUE> is a literal string containing 'true' or 'false', it will be
+replaced in the configuration data with actual JSON boolean values.
+
N.B. While the term C<--default> is used for command line consistency, attributes
set on nodes actually have what Chef terms "normal" precedence.
View
16 lib/Pantry/App/Command/apply.pm
@@ -7,6 +7,7 @@ package Pantry::App::Command::apply;
use Pantry::App -command;
use autodie;
+use JSON;
use namespace::clean;
@@ -87,14 +88,27 @@ sub _set_attributes {
my ($key, $value) = split /=/, $attr, 2; # split on first '='
if ( $value =~ /(?<!\\),/ ) {
# split on unescaped commas, then unescape escaped commas
- $value = [ map { s/\\,/,/gr } split /(?<!\\),/, $value ];
+ $value = [ map { $self->_boolify($_) } map { s/\\,/,/gr } split /(?<!\\),/, $value ];
+ }
+ else {
+ $value = $self->_boolify($value);
}
$obj->$method($key, $value);
}
}
return;
}
+sub _boolify {
+ my ($self, $value) = @_;
+ if ($value eq 'false') {
+ $value = JSON::false;
+ }
+ elsif ( $value eq 'true' ) {
+ $value = JSON::true;
+ }
+ return $value;
+}
1;
View
3 pod/bin/pantry
@@ -129,6 +129,9 @@ will be added to the node's data structure like this:
Both C<KEY> and C<VALUE> support periods and commas (respectively) to be
escaped by a backslash.
+If a C<VALUE> is a literal string containing 'true' or 'false', it will be
+replaced in the configuration data with actual JSON boolean values.
+
N.B. While the term C<--default> is used for command line consistency, attributes
set on nodes actually have what Chef terms "normal" precedence.
View
33 t/app/apply.t
@@ -4,6 +4,7 @@ use warnings;
no warnings 'qw'; # separating words with commas
use autodie;
use Test::More 0.92;
+use Test::Deep 0.110 qw/cmp_deeply/;
use lib 't/lib';
use TestHelper;
@@ -67,6 +68,20 @@ my @cases = (
'nginx.port' => [80,'8000,8080'],
},
},
+ {
+ argv => [ qw/-d nginx\.enable=false/ ],
+ expected => {
+ run_list => [],
+ 'nginx.enable' => JSON::false,
+ },
+ },
+ {
+ argv => [ qw/-d nginx\.enable=true/ ],
+ expected => {
+ run_list => [],
+ 'nginx.enable' => JSON::true,
+ },
+ },
],
},
@@ -119,6 +134,22 @@ my @cases = (
},
},
},
+ {
+ argv => [ qw/-d nginx\.enable=false/ ],
+ expected => {
+ default_attributes => {
+ 'nginx.enable' => JSON::false,
+ }
+ },
+ },
+ {
+ argv => [ qw/-d nginx\.enable=true/ ],
+ expected => {
+ default_attributes => {
+ 'nginx.enable' => JSON::true,
+ }
+ },
+ },
],
},
);
@@ -138,7 +169,7 @@ for my $c ( @cases ) {
$st->{expected}{$k} //= $templates{$c->{type}}{$k};
}
- is_deeply( $data, $st->{expected}, "data file correct" )
+ cmp_deeply( $data, $st->{expected}, "data file correct" )
or diag explain $data;
};
}

0 comments on commit a8d072b

Please sign in to comment.