| 1 | # Copyright 2001-2026, Paul Johnson (paul@pjcj.net) | ||
| 2 | |||
| 3 | # This software is free. It is licensed under the same terms as Perl itself. | ||
| 4 | |||
| 5 | # The latest version of this software should be available from my homepage: | ||
| 6 | # https://pjcj.net | ||
| 7 | |||
| 8 | package Devel::Cover::Util; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | our $VERSION = '1.52'; # VERSION | ||
| 14 | |||
| 15 | use Cwd "abs_path"; | ||
| 16 | use File::Spec; | ||
| 17 | use base "Exporter"; | ||
| 18 | |||
| 19 | our @EXPORT_OK = qw( remove_contained_paths ); | ||
| 20 | |||
| 21 | sub remove_contained_paths { | ||
| 22 | my ($container, @paths) = @_; | ||
| 23 | |||
| 24 | # File::Spec's case tolerancy detection on *nix/Mac systems does not | ||
| 25 | # take actual file system properties into account, but is better than | ||
| 26 | # trying to normalise paths with per-os logic. On Windows it is | ||
| 27 | # properly determined per drive. | ||
| 28 | my ($drive) = File::Spec->splitpath($container); | ||
| 29 | my $ignore_case = "(?i)"; | ||
| 30 | $ignore_case = "" if !File::Spec->case_tolerant($drive); | ||
| 31 | |||
| 32 | my $regex = qr[ | ||
| 33 | $ignore_case # ignore case on tolerant filesystems | ||
| 34 | ^ # string to match starts with: | ||
| 35 | \Q$container\E # path, meta-quoted for safety | ||
| 36 | ($|/) # followed by either the end of the string, or another | ||
| 37 | # slash, to avoid removing paths in directories named | ||
| 38 | # similar to the container | ||
| 39 | ]x; | ||
| 40 | |||
| 41 | @paths = grep { | ||
| 42 | my $path = abs_path $_; # normalise backslashes | ||
| 43 | $path !~ $regex; # check if path is inside the container | ||
| 44 | } @paths; | ||
| 45 | |||
| 46 | return @paths; | ||
| 47 | } | ||
| 48 | |||
| 49 | 1 | ||
| 50 | |||
| 51 | __END__ |