NixExprs: extract the escape function and test it

This commit is contained in:
Graham Christensen 2021-03-17 11:07:19 -04:00
parent aeb3d2f44c
commit d62a2c1657
No known key found for this signature in database
GPG key ID: FE918C3A98C1030F
3 changed files with 48 additions and 16 deletions

View file

@ -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 . "\"";
}

View file

@ -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";
}

25
t/Helper/escape.t Normal file
View file

@ -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;