From d62a2c16574113828b36e363b9c332be9ca31b23 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Wed, 17 Mar 2021 11:07:19 -0400 Subject: [PATCH] NixExprs: extract the `escape` function and test it --- src/lib/Hydra/Helper/Escape.pm | 14 ++++++++++++++ src/lib/Hydra/View/NixExprs.pm | 25 +++++++++---------------- t/Helper/escape.t | 25 +++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 src/lib/Hydra/Helper/Escape.pm create mode 100644 t/Helper/escape.t diff --git a/src/lib/Hydra/Helper/Escape.pm b/src/lib/Hydra/Helper/Escape.pm new file mode 100644 index 00000000..3312c5ea --- /dev/null +++ b/src/lib/Hydra/Helper/Escape.pm @@ -0,0 +1,14 @@ +package Hydra::Helper::Escape; + +use strict; +use base qw(Exporter); + +our @EXPORT = qw(escapeString); + +sub escapeString { + my ($s) = @_; + $s =~ s|\\|\\\\|g; + $s =~ s|\"|\\\"|g; + $s =~ s|\$|\\\$|g; + return "\"" . $s . "\""; +} diff --git a/src/lib/Hydra/View/NixExprs.pm b/src/lib/Hydra/View/NixExprs.pm index 7bfa3109..fa2f7086 100644 --- a/src/lib/Hydra/View/NixExprs.pm +++ b/src/lib/Hydra/View/NixExprs.pm @@ -3,19 +3,12 @@ package Hydra::View::NixExprs; use strict; use base qw/Catalyst::View/; use Hydra::Helper::Nix; +use Hydra::Helper::Escape; use Archive::Tar; use IO::Compress::Bzip2 qw(bzip2); use Encode; -sub escape { - my ($s) = @_; - $s =~ s|\\|\\\\|g; - $s =~ s|\"|\\\"|g; - $s =~ s|\$|\\\$|g; - return "\"" . $s . "\""; -} - sub process { my ($self, $c) = @_; @@ -62,7 +55,7 @@ EOF my $first = 1; foreach my $system (keys %perSystem) { $res .= "else " if !$first; - $res .= "if system == ${\escape $system} then {\n\n"; + $res .= "if system == ${\escapeString $system} then {\n\n"; foreach my $job (keys %{$perSystem{$system}}) { my $pkg = $perSystem{$system}->{$job}; @@ -70,21 +63,21 @@ EOF $res .= " # Hydra build ${\$build->id}\n"; my $attr = $build->get_column('job'); $attr =~ s/\./-/g; - $res .= " ${\escape $attr} = (mkFakeDerivation {\n"; + $res .= " ${\escapeString $attr} = (mkFakeDerivation {\n"; $res .= " type = \"derivation\";\n"; - $res .= " name = ${\escape ($build->get_column('releasename') or $build->nixname)};\n"; - $res .= " system = ${\escape $build->system};\n"; + $res .= " name = ${\escapeString ($build->get_column('releasename') or $build->nixname)};\n"; + $res .= " system = ${\escapeString $build->system};\n"; $res .= " meta = {\n"; - $res .= " description = ${\escape $build->description};\n" + $res .= " description = ${\escapeString $build->description};\n" if $build->description; - $res .= " license = ${\escape $build->license};\n" + $res .= " license = ${\escapeString $build->license};\n" if $build->license; - $res .= " maintainers = ${\escape $build->maintainers};\n" + $res .= " maintainers = ${\escapeString $build->maintainers};\n" if $build->maintainers; $res .= " };\n"; $res .= " } {\n"; my @outputNames = sort (keys %{$pkg->{outputs}}); - $res .= " ${\escape $_} = ${\escape $pkg->{outputs}->{$_}};\n" foreach @outputNames; + $res .= " ${\escapeString $_} = ${\escapeString $pkg->{outputs}->{$_}};\n" foreach @outputNames; my $out = defined $pkg->{outputs}->{"out"} ? "out" : $outputNames[0]; $res .= " }).$out;\n\n"; } diff --git a/t/Helper/escape.t b/t/Helper/escape.t new file mode 100644 index 00000000..f614cec3 --- /dev/null +++ b/t/Helper/escape.t @@ -0,0 +1,25 @@ +use strict; +use Setup; +use Data::Dumper; +use Test2::V0; +use Hydra::Helper::Escape; + +subtest "checking individual attribute set elements" => sub { + my %values = ( + "" => '""', + "." => '"."', + "foobar" => '"foobar"', + "foo.bar" => '"foo.bar"', + "🌮" => '"🌮"', + 'foo"bar' => '"foo\"bar"', + 'foo\\bar' => '"foo\\\\bar"', + '$bar' => '"\\$bar"', + ); + + for my $input (keys %values) { + my $value = $values{$input}; + is(escapeString($input), $value, "Escaping the value: " . $input); + } +}; + +done_testing;