/
flags.t
99 lines (80 loc) · 2.47 KB
/
flags.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use v6;
use lib 'lib';
use Test;
use File::LibMagic;
use File::Temp;
subtest {
test-with-flag(
't/samples/tiny-pdf.gz',
{ :uncompress },
set( 'application/gzip', 'application/x-gzip' ),
'application/pdf',
);
}, 'gzip file with and without uncompress flag';
subtest
{
my $dir = tempdir();
my $link-file = $*SPEC.catfile( $dir, 'link-to-tiny.pdf' );
$link-file.IO.symlink( IO::Path.new('t/samples/tiny.pdf').absolute );
test-with-flag(
$link-file,
{ :follow-symlinks },
'inode/symlink',
'application/pdf',
);
}, 'symlinked file';
subtest {
todo( q{:preserve-atime doesn't work for all cases yet}, 2 );
my $path = IO::Path.new('t/samples/foo.c');
my $atime = $path.accessed;
File::LibMagic.new( :preserve-atime ).from-filename($path);
File::LibMagic.new.from-filename( :preserve-atime, $path );
my $handle = $path.open;
File::LibMagic.new( :preserve-atime ).from-handle($handle);
File::LibMagic.new.from-handle( :preserve-atime, $handle );
is(
$path.accessed,
$atime,
'atime did not change when file magic was retrieved with :preserve-atime flag'
);
File::LibMagic.new.from-filename($path);
File::LibMagic.new.from-handle($handle);
isnt(
$path.accessed,
$atime,
'atime changes when :preserve-atime flag is not passed'
);
}, 'preserve-atime flag';
# XXX - is there any sane way to test the open-devices flag?
# XXX - I'm not sure what raw does or how to test. Same with the apple flag.
sub test-with-flag ($file, %flag, $expect-without-flag, Str $expect-with-flag) {
my %info = File::LibMagic.new.from-filename($file);
if $expect-without-flag ~~ Set {
cmp-ok(
%info<mime-type>,
'(elem)',
$expect-without-flag,
"file matches $expect-without-flag without flag",
);
}
else {
is(
%info<mime-type>,
$expect-without-flag,
"file is $expect-without-flag without flag",
);
}
%info = File::LibMagic.new(|%flag).from-filename($file);
is(
%info<mime-type>,
$expect-with-flag,
"file is $expect-with-flag with flag passed to constructor",
);
%info = File::LibMagic.new.from-filename( $file, |%flag );
is(
%info<mime-type>,
$expect-with-flag,
"file is $expect-with-flag with flag passed to from-filename",
);
}
done-testing;