From 8757fdfe85774a447ede4c8962bdc165618485c2 Mon Sep 17 00:00:00 2001 From: wickedOne Date: Tue, 20 Feb 2024 21:02:53 +0100 Subject: [PATCH] exclude issue handler - fixed a bug which defined the exludes array as scalar - added tests to make coverage complete --- lib/GPH.pm | 4 +- lib/GPH/PHPUnit.pm | 47 ++++++++++++++++------ lib/GPH/Psalm.pm | 4 +- t/share/Psalm/psalm-issue-handlers.xml | 1 + t/share/Psalm/{psalm.xml => psalm-max.xml} | 1 + t/share/Psalm/psalm-min.xml | 8 ++++ t/unit/GPH/PHPUnit.t | 4 +- t/unit/GPH/Psalm.t | 30 +++++++++++--- 8 files changed, 76 insertions(+), 23 deletions(-) rename t/share/Psalm/{psalm.xml => psalm-max.xml} (95%) create mode 100644 t/share/Psalm/psalm-min.xml diff --git a/lib/GPH.pm b/lib/GPH.pm index c773db5..598d8da 100644 --- a/lib/GPH.pm +++ b/lib/GPH.pm @@ -3,7 +3,7 @@ package GPH; use strict; use warnings FATAL => 'all'; -our $VERSION = '1.1.2'; +our $VERSION = '1.1.3'; 1; @@ -17,7 +17,7 @@ GPH - Gitlab Perl Helpers set of modules and integration scripts to support building code owner specific ci jobs for gitlab -=head1 API +=head1 MODULES =over 4 diff --git a/lib/GPH/PHPUnit.pm b/lib/GPH/PHPUnit.pm index 406a336..84bda3e 100755 --- a/lib/GPH/PHPUnit.pm +++ b/lib/GPH/PHPUnit.pm @@ -27,18 +27,7 @@ sub new { bless $self, $class; - if (exists($args{baseline}) and defined $args{baseline}) { - open my $fh, '<', $args{baseline} or die $!; - my @lines = (); - - while (<$fh>) { - chomp $_; - push(@lines, $_); - } - close($fh); - - $self->{baseline} = \@lines; - } + $self->parseBaseline(%args); return $self; } @@ -68,6 +57,26 @@ sub parse { return ($self->{stats}->exitCode()); } +sub parseBaseline { + my ($self, %args) = @_; + my ($fh, @lines); + + if (exists($args{baseline}) and defined $args{baseline}) { + + open($fh, '<', $args{baseline}) or die $!; + + @lines = (); + + while (<$fh>) { + chomp $_; + push(@lines, $_); + } + close($fh); + + $self->{baseline} = \@lines; + } +} + sub classReport { my $self = shift; @@ -141,6 +150,20 @@ parse PHPUnit output from <>, print warning or note when appropriate and return print matched coverage lines +=item C<< -EparseBaseline(%args) >> B<< (internal) >> + +parse baseline file. takes a hash of options, valid option keys include: + +=over + +=item baseline + +path to baseline file which contains paths to ignore while parsing PHPUnit's output + +=back + +code owner name + =back =head1 AUTHOR diff --git a/lib/GPH/Psalm.pm b/lib/GPH/Psalm.pm index 2478917..dab9505 100644 --- a/lib/GPH/Psalm.pm +++ b/lib/GPH/Psalm.pm @@ -87,14 +87,14 @@ sub getConfig { } sub getConfigWithIssueHandlers { - my ($self, $path, $blacklist) = @_; + my ($self, $path, @blacklist) = @_; my $dom = XML::LibXML->load_xml(location => $path); my $config = XML::LibXML->load_xml(string => $self->getConfig()); my ($handlers) = $dom->findnodes('//*[local-name()="issueHandlers"]'); - foreach my $exclude ($blacklist) { + foreach my $exclude (@blacklist) { next unless defined $exclude; my ($remove) = $handlers->findnodes("//*[local-name()=\"${exclude}\"]"); diff --git a/t/share/Psalm/psalm-issue-handlers.xml b/t/share/Psalm/psalm-issue-handlers.xml index ecd1a6e..0cee8cb 100644 --- a/t/share/Psalm/psalm-issue-handlers.xml +++ b/t/share/Psalm/psalm-issue-handlers.xml @@ -6,6 +6,7 @@ + diff --git a/t/share/Psalm/psalm.xml b/t/share/Psalm/psalm-max.xml similarity index 95% rename from t/share/Psalm/psalm.xml rename to t/share/Psalm/psalm-max.xml index 8450a57..00d3093 100644 --- a/t/share/Psalm/psalm.xml +++ b/t/share/Psalm/psalm-max.xml @@ -6,6 +6,7 @@ + diff --git a/t/share/Psalm/psalm-min.xml b/t/share/Psalm/psalm-min.xml new file mode 100644 index 0000000..072f23d --- /dev/null +++ b/t/share/Psalm/psalm-min.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/t/unit/GPH/PHPUnit.t b/t/unit/GPH/PHPUnit.t index 3ee0668..0590246 100644 --- a/t/unit/GPH/PHPUnit.t +++ b/t/unit/GPH/PHPUnit.t @@ -26,8 +26,8 @@ describe "class `$CLASS`" => sub { ok(lives {$CLASS->new(('owner' => '@teams/alpha', 'codeowners' => CODEOWNERS_FILE, classmap => CLASSMAP_FILE))}, 'lives with mandatory config settings') or note($@); }; - tests "baseline file not found" => sub { - ok(dies {$CLASS->new((codeowners => CODEOWNERS_FILE, owner => '@teams/alpha', baseline => 'foo.txt'))}, 'died with baseline not found') or note($@); + tests "baseline file tests" => sub { + ok(dies {$CLASS->new((codeowners => CODEOWNERS_FILE, owner => '@teams/alpha', classmap => CLASSMAP_FILE, baseline => 'foo.txt'))}, 'died with baseline not found') or note($@); ok(lives {$CLASS->new((codeowners => CODEOWNERS_FILE, owner => '@teams/alpha', classmap => CLASSMAP_FILE, baseline => PHPUNIT_BASELINE_FILE))}, 'lives with correct baseline') or note($@); }; }; diff --git a/t/unit/GPH/Psalm.t b/t/unit/GPH/Psalm.t index 71dedf8..6f23783 100644 --- a/t/unit/GPH/Psalm.t +++ b/t/unit/GPH/Psalm.t @@ -113,20 +113,35 @@ describe "class `$CLASS` config generation" => sub { my %config = ( level => 2, paths => \@paths, - ignored_directories => [ 'vendor' ], + ignored_directories => [ 'vendor', 'example.php' ], baseline => 'baselines/psalm-baseline.xml', baseline_check => 'true', cache_dir => './psalm', plugins => [ 'Psalm\SymfonyPsalmPlugin\Plugin' ], ); - my $object = $CLASS->new(%config); + tests 'compare max config contents' => sub { + my $object = $CLASS->new(%config); + my $config = $object->getConfig(); + my $mock; + + open(my $fh, '<', './t/share/Psalm/psalm-max.xml'); + + local $/; + $mock = <$fh>; + + close($fh); + + is($config, $mock, 'config content correct'); + }; + + tests 'compare min config contents' => sub { + my $object = $CLASS->new((level => 2, paths => \@paths)); - tests 'compare config contents' => sub { my $config = $object->getConfig(); my $mock; - open(my $fh, '<', './t/share/Psalm/psalm.xml'); + open(my $fh, '<', './t/share/Psalm/psalm-min.xml'); local $/; $mock = <$fh>; @@ -137,7 +152,12 @@ describe "class `$CLASS` config generation" => sub { }; tests 'compare config with issue handlers content' => sub { - my $config = $object->getConfigWithIssueHandlers('./t/share/Psalm/psalm-stub.xml', qw{MoreSpecificImplementedParamType NonExistingHandler}); + my $object = $CLASS->new(%config); + + my @blacklist = qw{MoreSpecificImplementedParamType NonExistingHandler}; + $blacklist[2] = undef; + + my $config = $object->getConfigWithIssueHandlers('./t/share/Psalm/psalm-stub.xml', @blacklist); my $mock; open(my $fh, '<', './t/share/Psalm/psalm-issue-handlers.xml');