@@ -8,6 +8,7 @@ use Test::AnyOf;
88use Test::More 0.96;
99
1010use File::LibMagic;
11+ use List::Util qw( first ) ;
1112
1213# If this is populated then libmagic will use it to find the magic file.
1314delete $ENV {MAGIC };
@@ -47,17 +48,11 @@ delete $ENV{MAGIC};
4748
4849SKIP:
4950{
50- my $standard_file = _scrape_file_version();
51-
52- note " Found magic file at $standard_file " ;
51+ my $standard_file = _get_magic_file_path();
5352
5453 # # no critic (ValuesAndExpressions::ProhibitFiletest_f)
55- skip " The standard magic file must exist at $standard_file " , 1
56- unless -l $standard_file || -f _;
57-
58- my $info = File::LibMagic-> new()-> info_from_filename($standard_file );
59- skip " The file at $standard_file is not a magic file" , 1
60- unless $info && $info -> {description } =~ / magic binary file/ ;
54+ skip ' Could not find the standard magic file' , 1
55+ unless $standard_file ;
6156
6257 my %custom = (
6358 ' foo.foo' => [
@@ -91,23 +86,39 @@ SKIP:
9186 );
9287}
9388
94- sub _scrape_file_version {
89+ sub _get_magic_file_path {
90+ my @paths ;
9591 for (` file -v` ) { # # no critic (ProhibitBacktickOperators)
9692 chomp ;
9793 next unless m /\A magic file from (.*)/ ;
9894
99- my $magic = $1 ;
95+ # This may contain a colon-separated list.
96+ @paths = split /:/, $1 ;
97+ last ;
98+ }
99+
100+ push @paths , ' /usr/share/file' ;
100101
101- # # no critic (ValuesAndExpressions::ProhibitFiletest_f)
102- return " $magic .mgc"
103- if $magic !~ m / [.] mgc \z / smx && -f " $magic .mgc" ;
104- return $magic
105- if -f $magic ;
102+ my $magic
103+ = first { _is_valid_magic_file($_ ) } map { ( " $_ .mgc" , $_ ) } @paths ;
106104
107- last ;
105+ if ($magic ) {
106+ note " Found magic file at $magic " ;
107+ return $magic ;
108108 }
109109
110- return ' /usr/share/file/magic.mgc' ;
110+ return ;
111+ }
112+
113+ sub _is_valid_magic_file {
114+ my $path = shift ;
115+
116+ # # no critic (ValuesAndExpressions::ProhibitFiletest_f)
117+ return unless -l $path || -f _;
118+
119+ my $info = File::LibMagic-> new( follow_symlinks => 1 )
120+ -> info_from_filename($path );
121+ return $info && $info -> {description } =~ / magic binary file/ ;
111122}
112123
113124sub _test_flm {
0 commit comments