Skip to content

Commit

Permalink
Check flow style when removing quotes
Browse files Browse the repository at this point in the history
    [ "this { should keep quotes" ]
  • Loading branch information
perlpunk committed Feb 1, 2024
1 parent c6ded9c commit 6ea3bb4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
^/local/
/local/
^/.build/
/cover_db/
/gh-pages/
Expand Down
9 changes: 9 additions & 0 deletions .yamltidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
v: v0.1
indentation:
spaces: 2
block-sequence-in-mapping: 0
trailing-spaces: fix
header: true
scalar-style:
default: plain
27 changes: 21 additions & 6 deletions lib/YAML/Tidy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ sub _change_style($self, $node, $style) {
}
}

my $emit = $self->_emit_value($value, $style);
my $emit = $self->_emit_value($value, $style, $node->{flow});
chomp $emit;
return (0) if $emit =~ tr/\n//;
my $first = substr($emit, 0, 1);
Expand All @@ -368,16 +368,31 @@ sub _change_style($self, $node, $style) {
return (0);
}

sub _emit_value($self, $value, $style) {
sub _emit_value($self, $value, $style, $inflow) {
my $options = { unicode => 0 };
my $events = [
my @events = (
{ name => 'stream_start_event' },
{ name => 'document_start_event', implicit => 1 },
{ name => 'scalar_event', style => $style, value => $value },
);
if ($inflow) {
push @events, { name => 'sequence_start_event', style => YAML_FLOW_SEQUENCE_STYLE };
}
push @events, (
{ name => 'scalar_event', style => $style, value => $value }
);
if ($inflow) {
push @events, { name => 'sequence_end_event' }
}
push @events, (
{ name => 'document_end_event', implicit => 1 },
{ name => 'stream_end_event' },
];
return YAML::LibYAML::API::XS::emit_string_events($events, $options);
);
my $emit = YAML::LibYAML::API::XS::emit_string_events(\@events, $options);
if ($inflow) {
$emit =~ s/^ *\[ *//;
$emit =~ s/ *\] *$//;
}
return $emit;
}

sub _collect_aliases($self, $node) {
Expand Down
21 changes: 11 additions & 10 deletions t/02.run.t
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@ my $ytr;
my $tidied = <<'EOM';
---
some: input file
that: {should be: tidied}
that: {should be: tidied, "keep { quotes": "#here"}
EOM
my $filelist = <<'EOM';
a/b/1.yaml
c/d/2.yaml
EOM

my @config_args = ("-c", "$Bin/yamltidy");
subtest stdin => sub {
local @ARGV = qw/ - /;
local @ARGV = (@config_args, qw/ - /);
$ytr = YAML::Tidy::Run->new(stdin => \*DATA);
$ytr->run;
is $out[0], $tidied, 'Tidied stdin';
clean();

local @ARGV = qw/ - --debug /;
local @ARGV = (@config_args, qw/ - --debug /);
$ytr = YAML::Tidy::Run->new(stdin => \*DATA);
$ytr->run;
is $before[0], $data, 'debug before';
Expand All @@ -73,27 +74,27 @@ subtest information => sub {
};

subtest file => sub {
local @ARGV = $infile;
local @ARGV = (@config_args, $infile);
$ytr = YAML::Tidy::Run->new(stdin => \*DATA);
$ytr->run;
is $out[0], $tidied, 'Tidied file';
clean();

local @ARGV = (qw/ --debug /, $infile);
local @ARGV = (@config_args, qw/ --debug /, $infile);
$ytr = YAML::Tidy::Run->new(stdin => \*DATA);
$ytr->run;
is $before[0], $input, 'debug before';
is $after[0], $tidied, 'debug after';
clean();

local @ARGV = (qw/ --inplace /, $infile);
local @ARGV = (@config_args, qw/ --inplace /, $infile);
$ytr = YAML::Tidy::Run->new(stdin => \*DATA);
$ytr->run;
ok exists $write{ $infile }, 'inplace - file written';
is $write{ $infile }, $tidied, 'inplace - file content correct';
clean();

local @ARGV = (qw/ --inplace --verbose /, $infile);
local @ARGV = (@config_args, qw/ --inplace --verbose /, $infile);
$ytr = YAML::Tidy::Run->new(stdin => \*DATA);
$ytr->run;
like $out[0], qr{info.*Processed.*run.yaml.*\bchanged}, 'Verbose output';
Expand All @@ -104,14 +105,14 @@ subtest 'batch stdin' => sub {
my @f;
local *{"YAML::Tidy::Run::_process_file"} = sub($, $file) { push @f, $file };
open my $in, '<', \$filelist;
local @ARGV = (qw/ -b - --inplace /);
local @ARGV = (@config_args, qw/ -b - --inplace /);
$ytr = YAML::Tidy::Run->new(stdin => $in);
$ytr->run;
is $f[0], 'a/b/1.yaml', 'file 1';
is $f[1], 'c/d/2.yaml', 'file 2';
clean();

local @ARGV = (qw/ --batch - /);
local @ARGV = (@config_args, qw/ --batch - /);
$ytr = YAML::Tidy::Run->new(stdin => $in);
eval {
$ytr->run;
Expand All @@ -125,4 +126,4 @@ done_testing;

__DATA__
"some": input file
that: {"should be":tidied}
that: {"should be":tidied, "keep { quotes": "#here"}
2 changes: 1 addition & 1 deletion t/data/run.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"some": input file
that: {"should be":tidied}
that: {"should be":tidied, "keep { quotes": "#here"}

0 comments on commit 6ea3bb4

Please sign in to comment.