Skip to content

Commit ffec9a2

Browse files
authored
Merge pull request #6 from wickedOne/phpunit-baseline
phpunit baseline
2 parents 8fba99a + ce4ab45 commit ffec9a2

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

coverage2codeowner.pl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
my $paths = $ENV{'EXCLUDE_PATHS'} || '';
1818
my @excludes = split /,/, $paths;
1919

20-
my $phpunit = PHPUnit->new($owner, CODEOWNERS_FILE, CLASSMAP_FILE, $coverage, @excludes);
20+
my $baseline = $ENV{'PHPUNIT_BASELINE'} || undef;
21+
22+
my $phpunit = PHPUnit->new($owner, CODEOWNERS_FILE, CLASSMAP_FILE, $coverage, \@excludes, $baseline);
2123

2224
exit $phpunit->Parse();

doc/PHPUnit.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ the following environment variables are used by all scripts
1414

1515
the following environment variable is used by the coverage2codeowner script
1616
- `MIN_COVERAGE` (optional): minimum coverage required for the job to succeed. defaults to 0.1
17+
- `PHPUNIT_BASELINE` (optional): filepath of file containing paths to files and / or directories which are within the defined code space, but should be ignored while calculating coverage statistics.
1718

1819
### assumptions
1920

@@ -32,9 +33,10 @@ this way the other coverage artifacts (junit & xml) can be used by for example i
3233
> needs:
3334
> - composer-install
3435
> variables:
35-
> MIN_COVERAGE: '85.00'
3636
> DEV_TEAM: '@team-awesome'
3737
> EXCLUDE_PATHS: 'legacy/'
38+
> MIN_COVERAGE: '85.00'
39+
> PHPUNIT_BASELINE: './baselines/phpunit.txt'
3840
> artifacts:
3941
> expire_in: 1 hour
4042
> when: always

lib/PHPUnit.pm

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#------------------------------------------------------------------------------
22
# File: PHPUnit.pm
33
#
4-
# Description: Parses PHPUnit coverage output and filters it for given
4+
# Description: Parses PHPUnit coverage output and filters it for given
55
# code owner. Provides option to fail if coverage is below
6-
# rquired threshold
6+
# required threshold
77
#
88
# Revisions: 2023-01-21 - created
9+
# 2024-01-23 - added baseline option to ignore certain paths / files
10+
# within owner's code-space.
11+
# sort stats output for ease of lookup
912
#------------------------------------------------------------------------------
1013
package PHPUnit;
1114

@@ -48,23 +51,38 @@ my %classreport = ();
4851
# 2) string path to classmap file
4952
# 3) float minimal coverage percentage threshold, defaults to 0.0
5053
# 4) array code owner paths to exclude
54+
# 5) string path to baseline file, defaults to undef
5155
#
5256
# Returns: reference to PHPUnit object
5357
sub new {
54-
my ($class, $owner, $codeowners, $classmap, $threshold, @excludes) = @_;
58+
my ($class, $owner, $codeowners, $classmap, $threshold, $excludes, $baseline) = @_;
5559

56-
my $gitlab = Gitlab->new($codeowners, $owner, @excludes);
60+
my $gitlab = Gitlab->new($codeowners, $owner, @{$excludes});
5761
my $composer = Composer->new($classmap);
5862

5963
my $self = {
6064
gitlab => $gitlab,
6165
composer => $composer,
6266
owner => $owner,
6367
threshold => $threshold || 0.0,
68+
baseline => [],
6469
};
6570

6671
bless $self, $class;
6772

73+
if (defined $baseline) {
74+
open(FH, $baseline) or die "unable to open phpunit baseline file ${baseline} $!";
75+
my @lines = ();
76+
77+
while (<FH>) {
78+
chomp $_;
79+
push(@lines, $_);
80+
}
81+
close(FH);
82+
83+
$self->{baseline} = \@lines;
84+
}
85+
6886
return $self;
6987
}
7088

@@ -79,8 +97,9 @@ sub Parse {
7997
chomp $_;
8098

8199
# ignore lines with spaces
82-
next unless $_ =~ /[^ ]/;
100+
next unless $_ =~ /^[^ ]+$/;
83101
next unless $self->{composer}->Match($_, $self->{gitlab}->GetPaths());
102+
next if $self->{composer}->Match($_, @{$self->{baseline}});
84103
next if $self->{composer}->Match($_, $self->{gitlab}->GetBlacklistPaths());
85104

86105
# read next line for stats
@@ -150,8 +169,8 @@ sub Report {
150169
print "% (" . $lines{'covered'} . "/" . $lines{'total'} . ")\n\n";
151170
}
152171

153-
while (my ($key, $value) = each %classreport) {
154-
print "$key\n$value\n";
172+
foreach my $stats (sort keys %classreport) {
173+
printf "%s\n%s\n", $stats , $classreport{$stats};
155174
}
156175
}
157176

@@ -184,4 +203,4 @@ sub CollectStats {
184203
$lines{'total'} += $linesTotal;
185204
}
186205

187-
1;
206+
1;

0 commit comments

Comments
 (0)