| 1 |
package Test2::Harness::Finder; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
our $VERSION = '1.000179'; |
| 6 |
|
| 7 |
use Test2::Harness::Util qw/clean_path mod2file/; |
| 8 |
use Test2::Harness::Util::JSON qw/decode_json encode_json/; |
| 9 |
use List::Util qw/first/; |
| 10 |
use Cwd qw/getcwd/; |
| 11 |
use Carp qw/croak/; |
| 12 |
use Time::HiRes qw/time/; |
| 13 |
use Text::ParseWords qw/quotewords/; |
| 14 |
|
| 15 |
use Test2::Harness::TestFile; |
| 16 |
use File::Spec; |
| 17 |
|
| 18 |
use Test2::Harness::Util::HashBase qw{ |
| 19 |
<default_search <default_at_search |
| 20 |
|
| 21 |
<durations <maybe_durations +duration_data <durations_threshold |
| 22 |
|
| 23 |
<exclude_files <exclude_patterns <exclude_lists |
| 24 |
|
| 25 |
<no_long <only_long |
| 26 |
|
| 27 |
<rerun <rerun_modes <rerun_plugin |
| 28 |
|
| 29 |
search <extensions |
| 30 |
|
| 31 |
<multi_project |
| 32 |
|
| 33 |
<changed <changed_only <changes_plugin <show_changed_files <changes_diff |
| 34 |
<changes_filter_file <changes_filter_pattern |
| 35 |
<changes_exclude_file <changes_exclude_pattern |
| 36 |
<changes_include_whitespace <changes_exclude_nonsub |
| 37 |
<changes_exclude_loads <changes_exclude_opens |
| 38 |
}; |
| 39 |
|
| 40 |
sub munge_settings {} |
| 41 |
|
| 42 |
sub init { |
| 43 |
my $self = shift; |
| 44 |
|
| 45 |
$self->{+EXCLUDE_FILES} = { map {( $_ => 1 )} @{$self->{+EXCLUDE_FILES}} } if ref($self->{+EXCLUDE_FILES}) eq 'ARRAY'; |
| 46 |
|
| 47 |
if (my $plugins = $self->{+RERUN_PLUGIN}) { |
| 48 |
for (@$plugins) { |
| 49 |
$_ = "App::Yath::Plugin::$_" unless s/^\+// or m/^(App::Yath|Test2::Harness)::Plugin::/; |
| 50 |
my $file = mod2file($_); |
| 51 |
require $file; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
sub duration_data { |
| 57 |
my $self = shift; |
| 58 |
my ($plugins, $settings, $test_files) = @_; |
| 59 |
|
| 60 |
$self->{+DURATION_DATA} //= $self->pull_durations(); |
| 61 |
|
| 62 |
return $self->{+DURATION_DATA} if $self->{+DURATION_DATA}; |
| 63 |
|
| 64 |
for my $plugin (@$plugins) { |
| 65 |
next unless $plugin->can('duration_data'); |
| 66 |
$self->{+DURATION_DATA} = $plugin->duration_data($settings, $test_files) or next; |
| 67 |
last; |
| 68 |
} |
| 69 |
|
| 70 |
return $self->{+DURATION_DATA} //= {}; |
| 71 |
} |
| 72 |
|
| 73 |
sub pull_durations { |
| 74 |
my $self = shift; |
| 75 |
|
| 76 |
my $primary = delete $self->{+MAYBE_DURATIONS}; |
| 77 |
my $fallback = delete $self->{+DURATIONS}; |
| 78 |
|
| 79 |
my @args = ( |
| 80 |
name => 'durations', |
| 81 |
is_json => 1, |
| 82 |
http_args => [{headers => {'Content-Type' => 'application/json'}}], |
| 83 |
); |
| 84 |
|
| 85 |
if ($primary) { |
| 86 |
local $@; |
| 87 |
|
| 88 |
my $durations = eval { $self->_pull_from_file_or_url(source => $primary, @args) } |
| 89 |
or print "Could not fetch optional durations '$primary', ignoring...\n"; |
| 90 |
|
| 91 |
if ($durations) { |
| 92 |
print "Found durations: $primary\n"; |
| 93 |
return $durations; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $self->_pull_from_file_or_url(source => $fallback, @args) |
| 98 |
if $fallback; |
| 99 |
|
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
sub add_exclusions_from_lists { |
| 104 |
my $self = shift; |
| 105 |
|
| 106 |
my @lists = ref($self->{+EXCLUDE_LISTS}) eq 'ARRAY' ? @{$self->{+EXCLUDE_LISTS}} : ($self->{+EXCLUDE_LISTS}); |
| 107 |
|
| 108 |
for my $path (@lists) { |
| 109 |
my $content = $self->_pull_from_file_or_url( |
| 110 |
source => $path, |
| 111 |
name => 'exclusion lists', |
| 112 |
); |
| 113 |
|
| 114 |
next unless $content; |
| 115 |
|
| 116 |
for (split(/\r?\n\r?/, $content)) { |
| 117 |
$self->{+EXCLUDE_FILES}->{$_} = 1 unless /^\s*#/; |
| 118 |
}; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
sub _pull_from_file_or_url { |
| 123 |
my $self = shift; |
| 124 |
my %params = @_; |
| 125 |
|
| 126 |
my $in = $params{source} // croak "No file or url provided"; |
| 127 |
my $name = $params{name} // croak "No name provided"; |
| 128 |
|
| 129 |
my $is_json = $params{is_json}; |
| 130 |
|
| 131 |
if (my $type = ref($in)) { |
| 132 |
return $in if $is_json && ($type eq 'HASH' || $type eq 'ARRAY'); |
| 133 |
} |
| 134 |
elsif (-f $in) { |
| 135 |
if ($is_json) { |
| 136 |
require Test2::Harness::Util::File::JSON; |
| 137 |
my $file = Test2::Harness::Util::File::JSON->new(name => $in); |
| 138 |
return $file->read(); |
| 139 |
} |
| 140 |
else { |
| 141 |
require Test2::Harness::Util::File; |
| 142 |
my $f = Test2::Harness::Util::File->new(name => $in); |
| 143 |
return $f->read(); |
| 144 |
} |
| 145 |
} |
| 146 |
elsif ($in =~ m{^https?://}) { |
| 147 |
my $meth = $params{http_method} // 'get'; |
| 148 |
my $args = $params{http_args}; |
| 149 |
|
| 150 |
require HTTP::Tiny; |
| 151 |
my $ht = HTTP::Tiny->new(); |
| 152 |
my $res = $ht->$meth($in, $args ? (@$args) : ()); |
| 153 |
|
| 154 |
die "Could not query $name from '$in'\n$res->{status}: $res->{reason}\n$res->{content}\n" |
| 155 |
unless $res->{success}; |
| 156 |
|
| 157 |
return $is_json ? decode_json($res->{content}) : $res->{content}; |
| 158 |
} |
| 159 |
|
| 160 |
die "Invalid $name specification: $in"; |
| 161 |
} |
| 162 |
|
| 163 |
sub find_files { |
| 164 |
my $self = shift; |
| 165 |
my ($plugins, $settings) = @_; |
| 166 |
|
| 167 |
$self->add_exclusions_from_lists() if $self->{+EXCLUDE_LISTS}; |
| 168 |
|
| 169 |
my $add_changes = 0; |
| 170 |
$add_changes ||= $self->{+CHANGED} && @{$self->{+CHANGED}}; |
| 171 |
$add_changes ||= $self->{+CHANGED_ONLY}; |
| 172 |
$add_changes ||= $self->{+CHANGES_PLUGIN}; |
| 173 |
$add_changes ||= $self->{+CHANGES_DIFF}; |
| 174 |
|
| 175 |
$self->add_changed_to_search($plugins, $settings) if $add_changes; |
| 176 |
|
| 177 |
my $add_rerun = $self->{+RERUN}; |
| 178 |
$self->add_rerun_to_search($plugins, $settings, $add_rerun) if $add_rerun; |
| 179 |
|
| 180 |
return $self->find_multi_project_files($plugins, $settings) if $self->multi_project; |
| 181 |
|
| 182 |
return $self->find_project_files($plugins, $settings, $self->search); |
| 183 |
} |
| 184 |
|
| 185 |
sub check_plugins { |
| 186 |
my $self = shift; |
| 187 |
my ($plugins, $settings) = @_; |
| 188 |
|
| 189 |
my $check_plugins = $plugins; |
| 190 |
my $plugin; |
| 191 |
if (my $p = $self->{+CHANGES_PLUGIN}) { |
| 192 |
$plugin = $p =~ s/^\+// ? $p : "App::Yath::Plugin::$p"; |
| 193 |
$check_plugins = [$plugin]; |
| 194 |
} |
| 195 |
|
| 196 |
return $check_plugins // []; |
| 197 |
} |
| 198 |
|
| 199 |
sub get_diff { |
| 200 |
my $self = shift; |
| 201 |
my ($plugins, $settings) = @_; |
| 202 |
|
| 203 |
return (file => $self->{+CHANGES_DIFF}) if $self->{+CHANGES_DIFF}; |
| 204 |
|
| 205 |
my $check_plugins = $self->check_plugins($plugins, $settings); |
| 206 |
|
| 207 |
for my $plugin (@$check_plugins) { |
| 208 |
if ($plugin->can('changed_diff')) { |
| 209 |
my ($type, $data) = $plugin->changed_diff($settings); |
| 210 |
next unless $type && $data; |
| 211 |
|
| 212 |
return ($type => $data); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
return (); |
| 217 |
} |
| 218 |
|
| 219 |
sub find_changes { |
| 220 |
my $self = shift; |
| 221 |
my ($plugins, $settings) = @_; |
| 222 |
|
| 223 |
my @listed_changes; |
| 224 |
@listed_changes = @{$self->{+CHANGED}} if $self->{+CHANGED}; |
| 225 |
|
| 226 |
my ($type, $diff) = $self->get_diff($plugins, $settings); |
| 227 |
|
| 228 |
my (@found_changes); |
| 229 |
if ($type && $diff) { |
| 230 |
@found_changes = $self->changes_from_diff($type => $diff, $settings); |
| 231 |
} |
| 232 |
|
| 233 |
unless (@found_changes) { |
| 234 |
my $check_plugins = $self->check_plugins($plugins, $settings); |
| 235 |
|
| 236 |
for my $plugin (@$check_plugins) { |
| 237 |
next unless $plugin->can('changed_files'); |
| 238 |
|
| 239 |
push @found_changes => $plugin->changed_files($settings); |
| 240 |
last if @found_changes; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
my $filter_patterns = @{$self->{+CHANGES_FILTER_PATTERN}} ? $self->{+CHANGES_FILTER_PATTERN} : undef; |
| 245 |
my $filter_files = @{$self->{+CHANGES_FILTER_FILE}} ? {map { $_ => 1 } @{$self->{+CHANGES_FILTER_FILE}}} : undef; |
| 246 |
|
| 247 |
my $exclude_patterns = @{$self->{+CHANGES_EXCLUDE_PATTERN}} ? $self->{+CHANGES_EXCLUDE_PATTERN} : undef; |
| 248 |
my $exclude_files = @{$self->{+CHANGES_EXCLUDE_FILE}} ? {map { $_ => 1 } @{$self->{+CHANGES_EXCLUDE_FILE}}} : undef; |
| 249 |
|
| 250 |
my %changed_map; |
| 251 |
for my $change (@listed_changes, @found_changes) { |
| 252 |
next unless $change; |
| 253 |
my ($file, @parts) = ref($change) ? @$change : ($change); |
| 254 |
|
| 255 |
next if $filter_files && !$filter_files->{$file}; |
| 256 |
next if $exclude_files && $exclude_files->{$file}; |
| 257 |
next if $filter_patterns && !first { $file =~ m/$_/ } @$filter_patterns; |
| 258 |
next if $exclude_patterns && first { $file =~ m/$_/ } @$exclude_patterns; |
| 259 |
|
| 260 |
@parts = ('*') unless @parts; |
| 261 |
$changed_map{$file}{$_} = 1 for @parts; |
| 262 |
} |
| 263 |
|
| 264 |
return \%changed_map; |
| 265 |
} |
| 266 |
|
| 267 |
sub get_capable_plugins { |
| 268 |
my $self = shift; |
| 269 |
my ($method, $plugins) = @_; |
| 270 |
|
| 271 |
my %seen; |
| 272 |
return grep { $_ && !$seen{$_}++ && $_->can($method) } @$plugins; |
| 273 |
} |
| 274 |
|
| 275 |
sub add_rerun_to_search { |
| 276 |
my $self = shift; |
| 277 |
my ($plugins, $settings, $rerun) = @_; |
| 278 |
|
| 279 |
my $search = $self->search; |
| 280 |
unless ($search) { |
| 281 |
$search = []; |
| 282 |
$self->set_search($search); |
| 283 |
} |
| 284 |
|
| 285 |
my $modes = $self->{+RERUN_MODES}; |
| 286 |
my $mode_hash = { map {$_ => 1} @$modes }; |
| 287 |
|
| 288 |
my ($grabbed, $data); |
| 289 |
for my $p ($self->get_capable_plugins(grab_rerun => [@{$self->{+RERUN_PLUGIN} // []}, @$plugins])) { |
| 290 |
($grabbed, $data) = $p->grab_rerun($rerun, modes => $modes, mode_hash => $mode_hash, settings => $settings); |
| 291 |
next unless $grabbed; |
| 292 |
|
| 293 |
unless ($data && keys %$data) { |
| 294 |
print "No files found to rerun.\n"; |
| 295 |
exit 0; |
| 296 |
} |
| 297 |
|
| 298 |
last if $grabbed; |
| 299 |
} |
| 300 |
|
| 301 |
unless ($grabbed) { |
| 302 |
if ($rerun eq '1') { |
| 303 |
$rerun = first { -e $_ } qw{ ./lastlog.jsonl ./lastlog.jsonl.bz2 ./lastlog.jsonl.gz }; |
| 304 |
|
| 305 |
die "Could not find a lastlog.jsonl(.bz2|.gz) file for re-running, you may need to provide a full path to --rerun=... or --rerun-failed=..." |
| 306 |
unless $rerun; |
| 307 |
} |
| 308 |
|
| 309 |
die "'$rerun' is not a valid log file, and no plugin intercepted it.\n" unless -f $rerun; |
| 310 |
|
| 311 |
my $stream = Test2::Harness::Util::File::JSONL->new(name => $rerun, skip_bad_decode => 1); |
| 312 |
|
| 313 |
my %files; |
| 314 |
while (1) { |
| 315 |
my @events = $stream->poll(max => 1000) or last; |
| 316 |
|
| 317 |
for my $event (@events) { |
| 318 |
my $f = $event->{facet_data} or next; |
| 319 |
|
| 320 |
for my $type (qw/seen queued start end/) { |
| 321 |
my $field = $type eq 'seen' ? "harness_job" : "harness_job_$type"; |
| 322 |
|
| 323 |
my $data = $f->{$field} or next; |
| 324 |
|
| 325 |
my $file = $data->{rel_file} // $data->{run_file} // $data->{file} // $data->{abs_file}; |
| 326 |
next unless $file; |
| 327 |
|
| 328 |
my $ref = $files{$file} //= {}; |
| 329 |
$ref->{$type}++; |
| 330 |
|
| 331 |
$ref->{$data->{fail} ? 'fail' : 'pass'}++ if $type eq 'end'; |
| 332 |
$ref->{retry}++ if $data->{is_try}; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
$data = \%files; |
| 338 |
} |
| 339 |
|
| 340 |
my @add = map { $data->{$_}->{add} // $_ } grep { |
| 341 |
my $entry = $data->{$_}; |
| 342 |
|
| 343 |
my $keep = $mode_hash->{all} ? 1 : 0; |
| 344 |
$keep ||= 1 if $mode_hash->{failed} && $entry->{fail} && !$entry->{pass}; |
| 345 |
$keep ||= 1 if $mode_hash->{retried} && $entry->{retry}; |
| 346 |
$keep ||= 1 if $mode_hash->{passed} && $entry->{pass}; |
| 347 |
$keep ||= 1 if $mode_hash->{missed} && !$entry->{end}; |
| 348 |
|
| 349 |
$keep |
| 350 |
} sort keys %$data; |
| 351 |
|
| 352 |
unless (@add) { |
| 353 |
print "No files found to rerun.\n"; |
| 354 |
exit 0; |
| 355 |
} |
| 356 |
|
| 357 |
push @$search => @add; |
| 358 |
} |
| 359 |
|
| 360 |
sub add_changed_to_search { |
| 361 |
my $self = shift; |
| 362 |
my ($plugins, $settings) = @_; |
| 363 |
|
| 364 |
my $search = $self->search; |
| 365 |
unless ($search) { |
| 366 |
$search = []; |
| 367 |
$self->set_search($search); |
| 368 |
} |
| 369 |
|
| 370 |
my $changed_map = $self->find_changes($plugins, $settings); |
| 371 |
my $found_changed = keys %$changed_map; |
| 372 |
|
| 373 |
die "Could not find any changed files.\n" if $self->{+CHANGED_ONLY} && !$found_changed; |
| 374 |
|
| 375 |
if ($self->{+CHANGED_ONLY}) { |
| 376 |
die "Can not add test or directory names when using --changed-only (saw: " . join(", " => @$search) . ")\n" |
| 377 |
if @$search; |
| 378 |
} |
| 379 |
|
| 380 |
if ($self->{+SHOW_CHANGED_FILES} && $found_changed) { |
| 381 |
print "Found the following changed files:\n"; |
| 382 |
for my $file (keys %$changed_map) { |
| 383 |
print " $file: ", join(", ", sort keys %{$changed_map->{$file}}), "\n"; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
my @add; |
| 388 |
for my $p ($self->get_capable_plugins(get_coverage_tests => $plugins)) { |
| 389 |
for my $set ($p->get_coverage_tests($settings, $changed_map)) { |
| 390 |
my $test = ref($set) ? $set->[0] : $set; |
| 391 |
|
| 392 |
unless (-e $test) { |
| 393 |
print STDERR "Coverage wants to run test '$test', but it does not exist, skipping...\n"; |
| 394 |
next; |
| 395 |
} |
| 396 |
|
| 397 |
push @add => $set; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
for my $p ($self->get_capable_plugins(post_process_coverage_tests => $plugins)) { |
| 402 |
$p->post_process_coverage_tests($settings, \@add); |
| 403 |
} |
| 404 |
|
| 405 |
if ($self->{+SHOW_CHANGED_FILES} && @add) { |
| 406 |
print "Found " . scalar(@add) . " test files to run based on changed files.\n"; |
| 407 |
print ref($_) ? " $_->[0]" : " $_\n" for @add; |
| 408 |
print "\n"; |
| 409 |
} |
| 410 |
|
| 411 |
push @$search => @add; |
| 412 |
|
| 413 |
return; |
| 414 |
} |
| 415 |
|
| 416 |
sub changes_from_diff { |
| 417 |
my $self = shift; |
| 418 |
my ($type, $data, $settings) = @_; |
| 419 |
|
| 420 |
my $next; |
| 421 |
if ($type eq 'lines') { |
| 422 |
$next = sub { shift @$data }; |
| 423 |
} |
| 424 |
elsif ($type eq 'diff') { |
| 425 |
my $lines = [split /\n/, $data]; |
| 426 |
$next = sub { shift @$lines }; |
| 427 |
} |
| 428 |
elsif ($type eq 'file') { |
| 429 |
die "'$data' is not a valid diff file.\n" unless -f $data; |
| 430 |
open(my $fh, '<', $data) or die "Could not open diff file '$data': $!"; |
| 431 |
$next = sub { |
| 432 |
my $line = <$fh>; |
| 433 |
close($fh) unless defined $line; |
| 434 |
return $line; |
| 435 |
}; |
| 436 |
} |
| 437 |
elsif ($type eq 'line_sub') { |
| 438 |
$next = $data; |
| 439 |
} |
| 440 |
elsif ($type eq 'handle') { |
| 441 |
$next = sub { scalar <$data> }; |
| 442 |
} |
| 443 |
else { |
| 444 |
die "Invalid diff type '$type'"; |
| 445 |
} |
| 446 |
|
| 447 |
my %changed; |
| 448 |
|
| 449 |
# Only perl can parse perl, and nothing can parse perl diff. What this does |
| 450 |
# is take a diff of every file with 100% context so we see the entire file |
| 451 |
# with the +, minus, or space prefix. As we scan it we look for subs. We |
| 452 |
# track what files and subs we are in. When we see a change we |
| 453 |
# {$file}{$sub}++. |
| 454 |
# |
| 455 |
# This of course is broken if you make a change between |
| 456 |
# subs as it will attribute it to the previous sub, however tracking |
| 457 |
# indentation is equally flawed as things like heredocs and other special |
| 458 |
# perl things can also trigger that to prematurely think we are out of a |
| 459 |
# sub. |
| 460 |
# |
| 461 |
# PPI and similar do a better job parsing perl, but using them and also |
| 462 |
# tracking changes from the diff, or even asking them to parse a diff where |
| 463 |
# some lines are added and others removed is also a huge hassle. |
| 464 |
# |
| 465 |
# The current algorith is "good enough", not perfect. |
| 466 |
my ($file, $sub, $indent, $is_perl); |
| 467 |
while (my $line = $next->()) { |
| 468 |
chomp($line); |
| 469 |
if ($line =~ m{^(?:---|\+\+\+) ([ab]/)?(.*)$}) { |
| 470 |
my $maybe_prefix = $1; |
| 471 |
my $maybe_file = $2; |
| 472 |
next if $maybe_file =~ m{/dev/null}; |
| 473 |
if ($maybe_prefix) { |
| 474 |
$file = -f "$maybe_prefix$maybe_file" ? "$maybe_prefix$maybe_file" : $maybe_file; |
| 475 |
} |
| 476 |
else { |
| 477 |
$file = $maybe_file; |
| 478 |
} |
| 479 |
$is_perl = 1 if $file =~ m/\.(pl|pm|t2?)$/; |
| 480 |
$sub = '*'; # Wildcard, changes to the code outside of a sub potentially effects all subs |
| 481 |
next; |
| 482 |
} |
| 483 |
|
| 484 |
next unless $file; |
| 485 |
|
| 486 |
$line =~ m/^( |-|\+)(.*)$/ or next; |
| 487 |
my ($prefix, $statement) = ($1, $2); |
| 488 |
my $changed = $prefix eq ' ' ? 0 : 1; |
| 489 |
|
| 490 |
$is_perl = 1 if $statement =~ m/^#!.*perl/; |
| 491 |
|
| 492 |
if ($statement =~ m/^(\s*)sub\s+(\w+)/) { |
| 493 |
$indent = $1 // ''; |
| 494 |
$sub = $2; |
| 495 |
|
| 496 |
# 1-line sub: sub foo { ... } |
| 497 |
if ($statement =~ m/}/) { |
| 498 |
$changed{$file}{$sub}++ if $changed; |
| 499 |
$sub = '*'; |
| 500 |
$indent = undef; |
| 501 |
next; |
| 502 |
} |
| 503 |
} |
| 504 |
elsif(defined($indent) && $statement =~ m/^$indent\}/) { |
| 505 |
$indent = undef; |
| 506 |
$sub = "*"; |
| 507 |
|
| 508 |
# If this is nothing but whitespace and a closing paren we can skip it. |
| 509 |
next if $statement =~ m/^\s*\}?\s*$/ && !$self->{+CHANGES_INCLUDE_WHITESPACE}; |
| 510 |
} |
| 511 |
|
| 512 |
next unless $sub; # If sub is empty then we are not even in a file yet |
| 513 |
next unless $changed; # If we are not on a changed line no need to add it |
| 514 |
unless ($self->{+CHANGES_INCLUDE_WHITESPACE}) { |
| 515 |
next if !length($statement); # If there is no statement length then this is whitespace only |
| 516 |
next if $statement =~ m/^\s+$/; # Do not care about whitespace only changes |
| 517 |
} |
| 518 |
|
| 519 |
next if $is_perl && $self->{+CHANGES_EXCLUDE_NONSUB} && $sub eq '*'; |
| 520 |
|
| 521 |
$changed{$file}{$sub}++; |
| 522 |
} |
| 523 |
|
| 524 |
return map {([$_ => sort keys %{$changed{$_}}])} sort keys %changed; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
sub find_multi_project_files { |
| 529 |
my $self = shift; |
| 530 |
my ($plugins, $settings) = @_; |
| 531 |
|
| 532 |
my $search = $self->search // []; |
| 533 |
|
| 534 |
die "multi-project search must be a single directory, or the current directory" if @$search > 1; |
| 535 |
my ($pdir) = @$search; |
| 536 |
my $dir = clean_path(getcwd()); |
| 537 |
|
| 538 |
my $out = []; |
| 539 |
my $ok = eval { |
| 540 |
chdir($pdir) if defined $pdir; |
| 541 |
my $ret = clean_path(getcwd()); |
| 542 |
|
| 543 |
opendir(my $dh, '.') or die "Could not open project dir: $!"; |
| 544 |
for my $subdir (readdir($dh)) { |
| 545 |
chdir($ret); |
| 546 |
|
| 547 |
next if $subdir =~ m/^\./; |
| 548 |
my $path = clean_path(File::Spec->catdir($ret, $subdir)); |
| 549 |
next unless -d $path; |
| 550 |
|
| 551 |
chdir($path) or die "Could not chdir to $path: $!\n"; |
| 552 |
|
| 553 |
for my $item (@{$self->find_project_files($plugins, $settings, [])}) { |
| 554 |
push @{$item->queue_args} => ('ch_dir' => $path); |
| 555 |
push @$out => $item; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
chdir($ret); |
| 560 |
1; |
| 561 |
}; |
| 562 |
my $err = $@; |
| 563 |
|
| 564 |
chdir($dir); |
| 565 |
die $err unless $ok; |
| 566 |
|
| 567 |
return $out; |
| 568 |
} |
| 569 |
|
| 570 |
sub find_project_files { |
| 571 |
my $self = shift; |
| 572 |
my ($plugins, $settings, $input) = @_; |
| 573 |
|
| 574 |
$input //= []; |
| 575 |
$plugins //= []; |
| 576 |
|
| 577 |
my $default_search = [@{$self->default_search}]; |
| 578 |
push @$default_search => @{$self->default_at_search} if $settings->check_prefix('run') && $settings->run->author_testing; |
| 579 |
|
| 580 |
$_->munge_search($input, $default_search, $settings) for @$plugins; |
| 581 |
|
| 582 |
my $search = @$input ? $input : $self->{+CHANGED_ONLY} ? [] : $default_search; |
| 583 |
|
| 584 |
die "No tests to run, search is empty\n" unless @$search; |
| 585 |
|
| 586 |
|
| 587 |
my (%seen, @tests, @dirs); |
| 588 |
|
| 589 |
for my $item (@$search) { |
| 590 |
my ($path, $test_params); |
| 591 |
|
| 592 |
if (ref $item) { |
| 593 |
($path, $test_params) = @$item; |
| 594 |
} |
| 595 |
else { |
| 596 |
my ($type, $data); |
| 597 |
($path, $type, $data) = split /(:<|:@|:=)/, $item, 2; |
| 598 |
if ($type && $data) { |
| 599 |
$test_params = {}; |
| 600 |
if ($type eq ':<') { |
| 601 |
$test_params->{stdin} = $data; |
| 602 |
} |
| 603 |
elsif ($type eq ':@') { |
| 604 |
$test_params->{argv} = decode_json($data); |
| 605 |
} |
| 606 |
elsif ($type eq ':=') { |
| 607 |
$test_params->{env} = decode_json($data); |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
push @dirs => $path and next if -d $path; |
| 613 |
|
| 614 |
unless(-f $path) { |
| 615 |
my ($actual, $args) = split /=/, $path, 2; |
| 616 |
if (-f $actual) { |
| 617 |
$path = $actual; |
| 618 |
$test_params = {%{$test_params // {}}, argv => [quotewords('\s+', 0, $args)]}; |
| 619 |
} |
| 620 |
else { |
| 621 |
die "'$path' is not a valid file or directory.\n" if @$input; |
| 622 |
next; |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
$path = clean_path($path, 0); |
| 627 |
$seen{$path}++; |
| 628 |
|
| 629 |
my $test; |
| 630 |
unless (first { $test = $_->claim_file($path, $settings, from => 'listed') } @$plugins) { |
| 631 |
$test = Test2::Harness::TestFile->new(file => $path); |
| 632 |
} |
| 633 |
|
| 634 |
if (my @exclude = $self->exclude_file($test)) { |
| 635 |
if (@$input) { |
| 636 |
print STDERR "File '$path' was listed on the command line, but has been exluded for the following reasons:\n"; |
| 637 |
print STDERR " $_\n" for @exclude; |
| 638 |
} |
| 639 |
|
| 640 |
next; |
| 641 |
} |
| 642 |
|
| 643 |
if ($test_params) { |
| 644 |
$test->set_input($test_params->{stdin}) if $test_params->{stdin}; |
| 645 |
$test->set_test_args($test_params->{argv}) if $test_params->{argv}; |
| 646 |
$test->set_env_vars($test_params->{env}) if $test_params->{env}; |
| 647 |
} |
| 648 |
|
| 649 |
push @tests => $test; |
| 650 |
} |
| 651 |
|
| 652 |
if (@dirs) { |
| 653 |
require File::Find; |
| 654 |
File::Find::find( |
| 655 |
{ |
| 656 |
no_chdir => 1, |
| 657 |
wanted => sub { |
| 658 |
no warnings 'once'; |
| 659 |
|
| 660 |
my $file = clean_path($File::Find::name, 0); |
| 661 |
|
| 662 |
return if $seen{$file}++; |
| 663 |
return unless -f $file; |
| 664 |
|
| 665 |
my $test; |
| 666 |
unless(first { $test = $_->claim_file($file, $settings, from => 'search') } @$plugins) { |
| 667 |
for my $ext (@{$self->extensions}) { |
| 668 |
next unless m/\.\Q$ext\E$/; |
| 669 |
$test = Test2::Harness::TestFile->new(file => $file); |
| 670 |
last; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
return unless $test; |
| 675 |
return unless $self->include_file($test); |
| 676 |
push @tests => $test; |
| 677 |
}, |
| 678 |
}, |
| 679 |
@dirs |
| 680 |
); |
| 681 |
} |
| 682 |
|
| 683 |
my $test_count = @tests; |
| 684 |
my $threshold = $settings->finder->durations_threshold // 0; |
| 685 |
if ($threshold && $test_count >= $threshold) { |
| 686 |
my $start = time; |
| 687 |
my $durations = $self->duration_data($plugins, $settings, [map { $_->relative } @tests]); |
| 688 |
my $end = time; |
| 689 |
if ($durations && keys %$durations) { |
| 690 |
printf("Fetched duration data (Took %0.2f seconds)\n", $end - $start); |
| 691 |
for my $test (@tests) { |
| 692 |
my $rel = $test->relative; |
| 693 |
$test->set_duration($durations->{$rel}) if $durations->{$rel}; |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
$_->munge_files(\@tests, $settings) for @$plugins; |
| 699 |
|
| 700 |
return [ sort { $a->rank <=> $b->rank || $a->file cmp $b->file } @tests ]; |
| 701 |
} |
| 702 |
|
| 703 |
sub include_file { |
| 704 |
my $self = shift; |
| 705 |
my ($test) = @_; |
| 706 |
|
| 707 |
my @exclude = $self->exclude_file($test); |
| 708 |
|
| 709 |
return !@exclude; |
| 710 |
} |
| 711 |
|
| 712 |
sub exclude_file { |
| 713 |
my $self = shift; |
| 714 |
my ($test) = @_; |
| 715 |
|
| 716 |
my @out; |
| 717 |
|
| 718 |
push @out => "File has a do-not-run directive inside it." unless $test->check_feature(run => 1); |
| 719 |
|
| 720 |
my $full = $test->file; |
| 721 |
my $rel = $test->relative; |
| 722 |
|
| 723 |
push @out => 'File is in the exclude list.' if $self->exclude_files->{$full} || $self->exclude_files->{$rel}; |
| 724 |
push @out => 'File matches an exclusion pattern.' if first { $rel =~ m/$_/ } @{$self->exclude_patterns}; |
| 725 |
|
| 726 |
push @out => 'File is marked as "long", but the "no long tests" opition was specified.' |
| 727 |
if $self->no_long && $test->check_duration eq 'long'; |
| 728 |
|
| 729 |
push @out => 'File is not marked "long", but the "only long tests" option was specified.' |
| 730 |
if $self->only_long && $test->check_duration ne 'long'; |
| 731 |
|
| 732 |
return @out; |
| 733 |
} |
| 734 |
|
| 735 |
1; |
| 736 |
|
| 737 |
__END__ |
| 738 |
|
| 739 |
=pod |
| 740 |
|
| 741 |
=encoding UTF-8 |
| 742 |
|
| 743 |
=head1 NAME |
| 744 |
|
| 745 |
Test2::Harness::Finder - Library that searches for test files |
| 746 |
|
| 747 |
=head1 DESCRIPTION |
| 748 |
|
| 749 |
The finder is responsible for locating test files that should be run. You can |
| 750 |
subclass the finder and instruct yath to use your subclass. |
| 751 |
|
| 752 |
=head1 SYNOPSIS |
| 753 |
|
| 754 |
=head2 USING A CUSTOM FINDER |
| 755 |
|
| 756 |
To use Test2::Harness::Finder::MyFinder: |
| 757 |
|
| 758 |
$ yath test --finder MyFinder |
| 759 |
|
| 760 |
To use Another::Finder |
| 761 |
|
| 762 |
$ yath test --finder +Another::Finder |
| 763 |
|
| 764 |
By default C<Test2::Harness::Finder::> is prefixed onto your custom finder, use |
| 765 |
'+' before the class name or prevent this. |
| 766 |
|
| 767 |
=head2 SUBCLASSING |
| 768 |
|
| 769 |
use parent 'Test2::Harness::Finder'; |
| 770 |
use Test2::Harness::TestFile; |
| 771 |
|
| 772 |
# Custom finders may provide their own options if desired. |
| 773 |
# This is optional. |
| 774 |
use App::Yath::Options; |
| 775 |
option foo => ( |
| 776 |
... |
| 777 |
); |
| 778 |
|
| 779 |
# This is the main method to override. |
| 780 |
sub find_project_files { |
| 781 |
my $self = shift; |
| 782 |
my ($plugins, $settings, $search) = @_; |
| 783 |
|
| 784 |
return [ |
| 785 |
Test2::Harness::TestFile->new(...), |
| 786 |
Test2::Harness::TestFile->new(...), |
| 787 |
..., |
| 788 |
]; |
| 789 |
} |
| 790 |
|
| 791 |
=head1 METHODS |
| 792 |
|
| 793 |
These are important state methods, as well as utility methods for use in your |
| 794 |
subclasses. |
| 795 |
|
| 796 |
=over 4 |
| 797 |
|
| 798 |
=item $bool = $finder->multi_project |
| 799 |
|
| 800 |
True if the C<yath projects> command was used. |
| 801 |
|
| 802 |
=item $arrayref = $finder->find_files($plugins, $settings) |
| 803 |
|
| 804 |
This is the main method. This method returns an arrayref of |
| 805 |
L<Test2::Harness::TestFile> instances, each one representing a single test to |
| 806 |
run. |
| 807 |
|
| 808 |
$plugins is a list of plugins, some may be class names, others may be |
| 809 |
instances. |
| 810 |
|
| 811 |
$settings is an L<Test2::Harness::Settings> instance. |
| 812 |
|
| 813 |
B<Note:> In many cases it is better to override C<find_project_files()> in your |
| 814 |
subclasses. |
| 815 |
|
| 816 |
=item $durations = $finder->duration_data |
| 817 |
|
| 818 |
This will fetch the durations data if any was provided. This is a hashref of |
| 819 |
relative test paths as keys where the value is the duration of the file (SHORT, |
| 820 |
MEDIUM or LONG). |
| 821 |
|
| 822 |
B<Note:> The result is cached, see L<pull_durations()> to refresh the data. |
| 823 |
|
| 824 |
=item @reasons = $finder->exclude_file($test) |
| 825 |
|
| 826 |
The input argument should be an L<Test2::Harness::Test> instance. This will |
| 827 |
return a list of human readable reasons a test file should be excluded. If the |
| 828 |
file should not be excluded the list will be empty. |
| 829 |
|
| 830 |
This is a utility method that verifies the file is not in an exclude |
| 831 |
list/pattern. The reasons are provided back in case you need to inform the |
| 832 |
user. |
| 833 |
|
| 834 |
=item $bool = $finder->include_file($test) |
| 835 |
|
| 836 |
The input argument should be an L<Test2::Harness::Test> instance. This is a |
| 837 |
convenience method around C<exclude_file()>, it will return true when |
| 838 |
C<exclude_file()> returns an empty list. |
| 839 |
|
| 840 |
=item $arrayref = $finder->find_multi_project_files($plugins, $settings) |
| 841 |
|
| 842 |
=item $arrayref = $finder->find_project_files($plugins, $settings, $search) |
| 843 |
|
| 844 |
These do the heavy lifting for C<find_files> |
| 845 |
|
| 846 |
The default C<find_files()> implementation is this: |
| 847 |
|
| 848 |
sub find_files { |
| 849 |
my $self = shift; |
| 850 |
my ($plugins, $settings) = @_; |
| 851 |
|
| 852 |
return $self->find_multi_project_files($plugins, $settings) if $self->multi_project; |
| 853 |
return $self->find_project_files($plugins, $settings, $self->search); |
| 854 |
} |
| 855 |
|
| 856 |
Each one returns an arrayref of L<Test2::Harness::TestFile> instances. |
| 857 |
|
| 858 |
Note that C<find_multi_project_files()> uses C<find_project_files()> internally, |
| 859 |
once per project directory. |
| 860 |
|
| 861 |
$plugins is a list of plugins, some may be class names, others may be |
| 862 |
instances. |
| 863 |
|
| 864 |
$settings is an L<Test2::Harness::Settings> instance. |
| 865 |
|
| 866 |
$search is an arrayref of search paths. |
| 867 |
|
| 868 |
=item $finder->munge_settings($settings, $options) |
| 869 |
|
| 870 |
A callback that lets you munge settings and options. |
| 871 |
|
| 872 |
=item $finder->pull_durations |
| 873 |
|
| 874 |
This will fetch the durations data if ant was provided. This is a hashref of |
| 875 |
relative test paths as keys where the value is the duration of the file (SHORT, |
| 876 |
MEDIUM or LONG). |
| 877 |
|
| 878 |
L<duration_data()> is a cached version of this. This method will refresh the |
| 879 |
cache for the other. |
| 880 |
|
| 881 |
=back |
| 882 |
|
| 883 |
=head2 FROM SETTINGS |
| 884 |
|
| 885 |
See L<App::Yath::Options::Finder> for up to date documentation on these. |
| 886 |
|
| 887 |
=over 4 |
| 888 |
|
| 889 |
=item $finder->default_search |
| 890 |
|
| 891 |
=item $finder->default_at_search |
| 892 |
|
| 893 |
=item $finder->durations |
| 894 |
|
| 895 |
=item $finder->maybe_durations |
| 896 |
|
| 897 |
=item $finder->exclude_files |
| 898 |
|
| 899 |
=item $finder->exclude_patterns |
| 900 |
|
| 901 |
=item $finder->no_long |
| 902 |
|
| 903 |
=item $finder->only_long |
| 904 |
|
| 905 |
=item $finder->search |
| 906 |
|
| 907 |
=item $finder->extensions |
| 908 |
|
| 909 |
=back |
| 910 |
|
| 911 |
=head1 SOURCE |
| 912 |
|
| 913 |
The source code repository for Test2-Harness can be found at |
| 914 |
F<http://github.com/Test-More/Test2-Harness/>. |
| 915 |
|
| 916 |
=head1 MAINTAINERS |
| 917 |
|
| 918 |
=over 4 |
| 919 |
|
| 920 |
=item Chad Granum E<lt>[email protected]<gt> |
| 921 |
|
| 922 |
=back |
| 923 |
|
| 924 |
=head1 AUTHORS |
| 925 |
|
| 926 |
=over 4 |
| 927 |
|
| 928 |
=item Chad Granum E<lt>[email protected]<gt> |
| 929 |
|
| 930 |
=back |
| 931 |
|
| 932 |
=head1 COPYRIGHT |
| 933 |
|
| 934 |
Copyright 2020 Chad Granum E<lt>[email protected]<gt>. |
| 935 |
|
| 936 |
This program is free software; you can redistribute it and/or |
| 937 |
modify it under the same terms as Perl itself. |
| 938 |
|
| 939 |
See F<http://dev.perl.org/licenses/> |
| 940 |
|
| 941 |
=cut |