| 1 |
use Test2::V0; |
| 2 |
use Test2::Harness::Util::JSON qw/decode_json/; |
| 3 |
use Test2::Require::Module 'Test2::Plugin::Cover' => '0.000029'; |
| 4 |
use Test2::Require::AuthorTesting; |
| 5 |
|
| 6 |
use App::Yath::Tester qw/yath/; |
| 7 |
use Test2::Harness::Util qw/clean_path/; |
| 8 |
use File::Spec(); |
| 9 |
|
| 10 |
use File::Temp qw/tempfile/; |
| 11 |
|
| 12 |
my $dir = __FILE__; |
| 13 |
$dir =~ s{\.t$}{}g; |
| 14 |
$dir =~ s{^\./}{}; |
| 15 |
|
| 16 |
# Coverage paths are relative to the run root, so every comparison below needs |
| 17 |
# this to be relative too, however the file was invoked. |
| 18 |
$dir = File::Spec->abs2rel($dir) if File::Spec->file_name_is_absolute($dir); |
| 19 |
|
| 20 |
my $abs_dir = clean_path($dir); |
| 21 |
|
| 22 |
my $KEEP = "$dir/lib/Keep.pm"; |
| 23 |
my $DEP = "$dir/deps/lib/Dep.pm"; |
| 24 |
my $DEP2 = "$dir/deps2/Dep2.pm"; |
| 25 |
my $VENDOR = "$dir/vendor/Vendor.pm"; |
| 26 |
|
| 27 |
# Every run also covers harness modules under lib/, which are not what this |
| 28 |
# file is about. Only the fixture tree is compared. |
| 29 |
sub fixture_files { |
| 30 |
my ($files) = @_; |
| 31 |
return [sort grep { m{^\Q$dir\E/} && m{\.pm$} } keys %{$files // {}}]; |
| 32 |
} |
| 33 |
|
| 34 |
sub event_files { |
| 35 |
my ($log) = @_; |
| 36 |
|
| 37 |
my %files; |
| 38 |
my @events = $log->poll(); |
| 39 |
while (@events) { |
| 40 |
my $event = shift @events; |
| 41 |
if ($event) { |
| 42 |
my $cov = $event->{facet_data}->{coverage}; |
| 43 |
@files{keys %{$cov->{files}}} = values %{$cov->{files}} if $cov; |
| 44 |
} |
| 45 |
|
| 46 |
push @events => $log->poll; |
| 47 |
} |
| 48 |
|
| 49 |
return fixture_files(\%files); |
| 50 |
} |
| 51 |
|
| 52 |
sub written_files { |
| 53 |
my ($cfile) = @_; |
| 54 |
|
| 55 |
open(my $fh, '<', $cfile) or die "Could not open coverage file '$cfile': $!"; |
| 56 |
my $data = decode_json(join '' => <$fh>); |
| 57 |
|
| 58 |
return fixture_files($data->{files}); |
| 59 |
} |
| 60 |
|
| 61 |
sub cover_run { |
| 62 |
my %params = @_; |
| 63 |
|
| 64 |
my ($fh, $cfile) = tempfile("cover-exclude-$$-XXXXXXXX", TMPDIR => 1, UNLINK => 1, SUFFIX => '.json'); |
| 65 |
close($fh); |
| 66 |
|
| 67 |
my $out = yath( |
| 68 |
command => 'test', |
| 69 |
log => 1, |
| 70 |
args => [ |
| 71 |
"-I$dir/lib", $dir, '--ext=tx', '-v', |
| 72 |
"--cover-write=$cfile", |
| 73 |
@{$params{args} // []}, |
| 74 |
], |
| 75 |
exit => 0, |
| 76 |
test => sub { |
| 77 |
my $out = shift; |
| 78 |
|
| 79 |
like($out->{output}, qr{PRELOAD: \Q$params{preload}\E$}m, "Ran under the expected transport") |
| 80 |
if defined $params{preload}; |
| 81 |
|
| 82 |
is(event_files($out->{log}), $params{expect}, "Coverage event has the expected files"); |
| 83 |
is(written_files($cfile), $params{expect}, "Aggregated run data has the expected files"); |
| 84 |
|
| 85 |
$params{test}->($out) if $params{test}; |
| 86 |
}, |
| 87 |
); |
| 88 |
|
| 89 |
return $out; |
| 90 |
} |
| 91 |
|
| 92 |
subtest no_exclusions => sub { |
| 93 |
cover_run( |
| 94 |
preload => 0, |
| 95 |
expect => [sort($DEP, $DEP2, $KEEP, $VENDOR)], |
| 96 |
); |
| 97 |
}; |
| 98 |
|
| 99 |
subtest one_exclusion => sub { |
| 100 |
cover_run( |
| 101 |
preload => 0, |
| 102 |
args => ["--cover-exclude-dirs=$dir/deps"], |
| 103 |
expect => [sort($DEP2, $KEEP, $VENDOR)], |
| 104 |
); |
| 105 |
}; |
| 106 |
|
| 107 |
subtest multiple_exclusions => sub { |
| 108 |
cover_run( |
| 109 |
preload => 0, |
| 110 |
args => ["--cover-exclude-dirs=$dir/deps", "--cover-exclude-dir=$dir/vendor"], |
| 111 |
expect => [sort($DEP2, $KEEP)], |
| 112 |
); |
| 113 |
}; |
| 114 |
|
| 115 |
subtest path_needing_normalization => sub { |
| 116 |
cover_run( |
| 117 |
preload => 0, |
| 118 |
args => ["--cover-exclude-dirs=./$dir/vendor/../deps/lib/.."], |
| 119 |
expect => [sort($DEP2, $KEEP, $VENDOR)], |
| 120 |
); |
| 121 |
}; |
| 122 |
|
| 123 |
subtest wildcard_exclusion => sub { |
| 124 |
cover_run( |
| 125 |
preload => 0, |
| 126 |
args => ["--cover-exclude-dirs=$dir/deps*"], |
| 127 |
expect => [sort($KEEP, $VENDOR)], |
| 128 |
); |
| 129 |
}; |
| 130 |
|
| 131 |
subtest preload => sub { |
| 132 |
cover_run( |
| 133 |
preload => 1, |
| 134 |
args => ['-PCoverExcludePreload', "--cover-exclude-dirs=$dir/deps", "--cover-exclude-dirs=$dir/vendor"], |
| 135 |
expect => [sort($DEP2, $KEEP)], |
| 136 |
); |
| 137 |
}; |
| 138 |
|
| 139 |
# --cover-dirs picks the files metrics are calculated over, and exclusions are |
| 140 |
# subtracted from that set, so an excluded file is counted in neither the |
| 141 |
# totals nor the untested list. |
| 142 |
subtest metrics => sub { |
| 143 |
cover_run( |
| 144 |
expect => [sort($DEP, $DEP2, $KEEP, $VENDOR)], |
| 145 |
args => ['--cover-metrics', '--no-cover-dirs', "--cover-dirs=$dir"], |
| 146 |
test => sub { |
| 147 |
my $out = shift; |
| 148 |
like($out->{output}, qr{^\|\s*files\s*\|\s*5\s*\|\s*4\s*\|}m, "All 5 fixture modules counted, 4 tested"); |
| 149 |
like($out->{output}, qr{^\|\s*subs\s*\|\s*4\s*\|\s*4\s*\|}m, "All 4 fixture subs counted and tested"); |
| 150 |
}, |
| 151 |
); |
| 152 |
|
| 153 |
cover_run( |
| 154 |
expect => [sort($DEP2, $KEEP, $VENDOR)], |
| 155 |
args => ['--cover-metrics', '--no-cover-dirs', "--cover-dirs=$dir", "--cover-exclude-dirs=$dir/deps"], |
| 156 |
test => sub { |
| 157 |
my $out = shift; |
| 158 |
like($out->{output}, qr{^\|\s*files\s*\|\s*4\s*\|\s*3\s*\|}m, "Excluded module dropped from the total"); |
| 159 |
like($out->{output}, qr{^\|\s*subs\s*\|\s*3\s*\|\s*3\s*\|}m, "Excluded sub dropped from the total"); |
| 160 |
}, |
| 161 |
); |
| 162 |
}; |
| 163 |
|
| 164 |
subtest exclusions_in_verbose_output => sub { |
| 165 |
yath( |
| 166 |
command => 'test', |
| 167 |
args => ["-I$dir/lib", $dir, '--ext=tx', '-vv', '--cover-files', "--cover-exclude-dirs=$dir/deps"], |
| 168 |
exit => 0, |
| 169 |
test => sub { |
| 170 |
my $out = shift; |
| 171 |
|
| 172 |
like($out->{output}, qr{RUN INFO.*"exclude",}, "Verbose output shows the exclusion parameter"); |
| 173 |
like($out->{output}, qr{RUN INFO.*"\Q$abs_dir\E/deps"}, "Verbose output shows the normalized exclusion path"); |
| 174 |
}, |
| 175 |
); |
| 176 |
}; |
| 177 |
|
| 178 |
subtest comma_is_rejected => sub { |
| 179 |
yath( |
| 180 |
command => 'test', |
| 181 |
args => ["-I$dir/lib", $dir, '--ext=tx', '--cover-files', "--cover-exclude-dirs=$dir/deps,$dir/vendor"], |
| 182 |
exit => T(), |
| 183 |
test => sub { |
| 184 |
my $out = shift; |
| 185 |
|
| 186 |
like( |
| 187 |
$out->{output}, |
| 188 |
qr{--cover-exclude-dirs cannot use a path containing a comma}, |
| 189 |
"Rejected the path and said why", |
| 190 |
); |
| 191 |
|
| 192 |
like( |
| 193 |
$out->{output}, |
| 194 |
qr{'\Q$abs_dir\E/deps,}, |
| 195 |
"Checked the resolved path, not the value as typed", |
| 196 |
); |
| 197 |
}, |
| 198 |
); |
| 199 |
}; |
| 200 |
|
| 201 |
done_testing; |