Skip to content

Commit

Permalink
perlcritic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wickedOne committed Feb 11, 2024
1 parent 7310e19 commit 3e75c82
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 52 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ WriteMakefile(
"Test2::V0" => 0,
"Test2::Tools::Spec" => 0,
"Data::Dumper" => 0,
"Readonly" => 0,
},
'test' => {
TESTS => 't/unit/GPH/*.t t/unit/GPH/*/*.t'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ the following environment variables are used by all scripts
most scripts generate a custom config file on the fly.

for more details on supported (static) analysis tools and implementation examples see the following:
1. [PHPUNIT](doc/PHPUnit.md)
1. [PHPUnit](doc/PHPUnit.md)
2. [Infection](doc/Infection.md)
3. [Psalm](doc/Psalm.md)
4. [PHPStan](doc/PHPStan.md)
Expand Down
21 changes: 10 additions & 11 deletions lib/GPH/Composer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ package GPH::Composer;
use strict;
use warnings FATAL => 'all';

#------------------------------------------------------------------------------
# Static properties
# classmap contains hash of all relevant classes and their paths
my %classmap = ();

#------------------------------------------------------------------------------
# Construct new class
#
Expand Down Expand Up @@ -67,15 +62,19 @@ sub match {
# Returns: hash reference with ${classname} => $path
sub parseClassMap {
my ($self, $path) = @_;
my %classmap = ();

open(my $fh, '<', $path) or die "can't open classmap file $!";

open(my $fh, $path) or die "can't open classmap file $!";
my @lines = <$fh>;

close($fh);

while (<$fh>) {
chomp $_;
next unless /\$baseDir\s\./;
for my $line (@lines) {
next unless $line =~ /\$baseDir\s\./;

my ($class, $path) = split / => \$baseDir \. /;
$classmap{strip($class)} = strip($path);
my ($class, $code_path) = split(/ => \$baseDir \. /, $line);
$classmap{strip($class)} = strip($code_path);
}

close($fh);
Expand Down
17 changes: 10 additions & 7 deletions lib/GPH/Gitlab.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,29 @@ sub new {
my ($class, $path, $owner, @excludes) = @_;
my (%codeowners, %excludeHash, %blacklist);

open(my $fh, $path) or die "unable to open codeowners file, initialization failed $!";

# build excludes hash for quick lookup
foreach my $item (@excludes) {
$excludeHash{$item} = 1;
}

while (<$fh>) {
chomp $_;
open(my $fh, '<', $path) or die "unable to open codeowners file, initialization failed $!";

my @lines = <$fh>;

close($fh);

for my $line (@lines) {

# skip if line does not contain @
next unless /^.*\s\@[\w]+\/.*$/;
next unless $line =~ /^.*\s\@[\w]+\/.*$/x;

my ($class_path, $owners) = split(' ', $_, 2);
my ($class_path, $owners) = split(' ', $line, 2);

# skip if path is excluded
next if exists $excludeHash{$class_path};

foreach (split(' ', $owners)) {
next unless /(\@[\w\-\/]{0,})$/;
next unless /(\@[\w\-\/]{0,})$/x;

if (not exists $codeowners{$1}) {
$codeowners{$1} = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/GPH/Infection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ sub parse {
my $msi = 0;
my $covered = 0;

while (<STDIN>) {
while (<>) {
if ($_ =~ '([\d]+) covered mutants were not detected') {
$escapees = $1;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/GPH/PHPUnit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ sub new {
bless $self, $class;

if (defined $baseline) {
open(my $fh, $baseline) or die "unable to open phpunit baseline file ${baseline} $!";
open(my $fh, '<', $baseline) or die "unable to open phpunit baseline file ${baseline} $!";
my @lines = ();

while (<$fh>) {
Expand All @@ -74,7 +74,7 @@ sub new {
sub parse {
my ($self) = @_;

while (<STDIN>) {
while (<>) {
chomp $_;

# ignore lines with spaces
Expand All @@ -84,7 +84,7 @@ sub parse {
next if $self->{composer}->match($_, @{$self->{baseline}});
next if $self->{composer}->match($_, @{$self->{gitlab}->getBlacklistPaths()});
# read next line for stats
my $stats = <STDIN>;
my $stats = <>;

$self->{classreport}{$_} = $stats;
$self->{stats}->add($stats);
Expand Down
13 changes: 7 additions & 6 deletions t/unit/GPH/Composer.t
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/usr/bin/perl
package t::unit::GPH::Composer;

use strict;
use warnings;

use Test2::V0 -target => 'GPH::Composer';
use Test2::Tools::Spec;

use Data::Dumper;
use Readonly;

use constant CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php';
local $SIG{__WARN__} = sub {};

local $SIG{__WARN__} = sub {
# warn $_[0] unless ($_[0] =~ /^Module::Pluggable will be removed/);
};
Readonly my $CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php';

describe "class `$CLASS`" => sub {
tests 'it can be instantiated' => sub {
Expand All @@ -23,7 +24,7 @@ describe "class `$CLASS`" => sub {

$exception = dies {
$warnings = warns {
$object = $CLASS->new(CLASSMAP_FILE);
$object = $CLASS->new($CLASSMAP_FILE);
};
};

Expand Down Expand Up @@ -64,7 +65,7 @@ describe 'test matching' => sub {

$exception = dies {
$warnings = warns {
$object = $CLASS->new(CLASSMAP_FILE);
$object = $CLASS->new($CLASSMAP_FILE);
$result = $object->match($className, @paths);
};
};
Expand Down
8 changes: 5 additions & 3 deletions t/unit/GPH/Gitlab.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use warnings;

use Test2::V0 -target => 'GPH::Gitlab';
use Test2::Tools::Spec;

use Data::Dumper;
use Readonly;

use constant CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS';
Readonly my $CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS';

local $SIG{__WARN__} = sub {};

Expand All @@ -23,7 +25,7 @@ describe "class `$CLASS`" => sub {
@excludes = qw{.gitlab-ci.yml};
$exception = dies {
$warnings = warns {
$object = $CLASS->new(CODEOWNERS_FILE, '@teams/alpha', @excludes);
$object = $CLASS->new($CODEOWNERS_FILE, '@teams/alpha', @excludes);
};
};

Expand All @@ -40,7 +42,7 @@ describe "class `$CLASS`" => sub {
@excludes = qw{.gitlab-ci.yml};
$exception = dies {
$warnings = warns {
$object = $CLASS->new(CODEOWNERS_FILE, '@teams/alpha', @excludes);
$object = $CLASS->new($CODEOWNERS_FILE, '@teams/alpha', @excludes);
};
};

Expand Down
12 changes: 6 additions & 6 deletions t/unit/GPH/Infection.t
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ Metrics:
Covered Code MSI: ${output_msi}%
";

open my $fh, "<", \$stdin;
local *STDIN = $fh;
open (my $LOG, '>', \$stdout);
select $LOG;

$exception = dies {
$warnings = warns {
local *ARGV;
open *ARGV, '<', \$stdin or die "no can do $!";

$object = $CLASS->new($msi, $covered);
$exit_code = $object->parse();

close *ARGV;
};
};

Expand All @@ -139,7 +139,7 @@ Metrics:
is($exit_code, $expected_code, 'expected code returned');

if ($expected_code == 3 and $escapees > 0) {
like($stdout, '\[warning\]', 'warning')
like($stdout, '\[warning\]', 'warning');
}
}
};
Expand Down
26 changes: 12 additions & 14 deletions t/unit/GPH/PHPUnit.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use Test2::V0 -target => 'GPH::PHPUnit';
use Test2::Tools::Spec;

use Data::Dumper;
use Readonly;

use constant CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS';
use constant CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php';
use constant PHPUNIT_OUTPUT_FILE => './t/share/PHPUnit/phpunit-output.txt';
use constant PHPUNIT_BASELINE_FILE => './t/share/PHPUnit/phpunit-baseline.txt';
Readonly my $CODEOWNERS_FILE => './t/share/Gitlab/CODEOWNERS';
Readonly my $CLASSMAP_FILE => './t/share/Composer/autoload_classmap.php';
Readonly my $PHPUNIT_OUTPUT_FILE => './t/share/PHPUnit/phpunit-output.txt';
Readonly my $PHPUNIT_BASELINE_FILE => './t/share/PHPUnit/phpunit-baseline.txt';

local $SIG{__WARN__} = sub {};

Expand All @@ -39,7 +40,7 @@ describe 'configuration options' => sub {
case 'maximal options' => sub {
$threshold = 95.5;
@excludes = qw{.gitlab-ci.yml};
$baseline = PHPUNIT_BASELINE_FILE;
$baseline = $PHPUNIT_BASELINE_FILE;
$expected_threshold = $threshold;
@expected_baseline = qw{/src/Service/Provider/ /src/Mapper/};
};
Expand All @@ -49,7 +50,7 @@ describe 'configuration options' => sub {

$exception = dies {
$warnings = warns {
$object = $CLASS->new($owner, CODEOWNERS_FILE, CLASSMAP_FILE, $threshold, \@excludes, $baseline);
$object = $CLASS->new($owner, $CODEOWNERS_FILE, $CLASSMAP_FILE, $threshold, \@excludes, $baseline);
};
};

Expand Down Expand Up @@ -94,22 +95,19 @@ describe "parsing phpunit report output" => sub {
};

tests "test `$CLASS` exit code" => sub {
my ($stdin, $stdout, $object, $exception, $warnings, $exit_code);
my ($stdout, $object, $exception, $warnings, $exit_code);
my @excludes = qw{.gitlab-ci.yml};

$object = $CLASS->new('@teams/alpha', CODEOWNERS_FILE, CLASSMAP_FILE, $threshold, \@excludes, PHPUNIT_BASELINE_FILE);

$exception = dies {
$warnings = warns {
open ($stdin, '<', PHPUNIT_OUTPUT_FILE);
local *STDIN = $stdin;
open(my $output, '>', \$stdout) or die;
my $fh = select $output;
local *ARGV;
open *ARGV, '<', $PHPUNIT_OUTPUT_FILE or die "can't open ${PHPUNIT_OUTPUT_FILE}";

$object = $CLASS->new('@teams/alpha', $CODEOWNERS_FILE, $CLASSMAP_FILE, $threshold, \@excludes, $PHPUNIT_BASELINE_FILE);
$exit_code = $object->parse();

select $fh;
close $output;
close *ARGV;
};
};

Expand Down

0 comments on commit 3e75c82

Please sign in to comment.