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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
use lib 't/lib';
use Test2::V0 -no_srand => 1;
use Test2::Plugin::FauxOS 'darwin';
use Test2::Tools::FauxDynaLoader;
use Test2::Tools::NoteStderr qw( note_stderr );
use FFI::CheckLib;
@$FFI::CheckLib::system_path = (
'corpus/darwin/usr/lib',
'corpus/darwin/lib',
);
my $mock = mock_dynaloader;
subtest 'find_lib (good)' => sub {
my($path) = find_lib( lib => 'foo' );
ok -r $path, "path = $path is readable";
my $path2 = find_lib( lib => 'foo' );
is $path, $path2, 'scalar context';
my $dll = TestDLL->new($path);
is $dll->name, 'foo', 'dll.name = foo';
is $dll->version, '1.2.3', 'dll.version = 1.2.3';
};
subtest 'find_lib (fail)' => sub {
my @path = find_lib( lib => 'foobar' );
ok @path == 0, 'libfoobar not found';
};
subtest 'find_lib list' => sub {
my @path = find_lib( lib => [ 'foo', 'bar' ] );
ok -r $path[0], "path[0] = $path[0] is readable";
ok -r $path[1], "path[1] = $path[1] is readable";
subtest foo => sub {
my($foo) = grep { $_->name eq 'foo' } map { TestDLL->new($_) } @path;
is $foo->name, 'foo', 'dll.name = foo';
is $foo->version, '1.2.3', 'dll.version = 1.2.3';
};
subtest bar => sub {
my($bar) = grep { $_->name eq 'bar' } map { TestDLL->new($_) } @path;
is $bar->name, 'bar', 'dll.name = bar';
is $bar->version, '1.2.3', 'dll.version = 1.2.3';
};
};
subtest 'find_lib libpath' => sub {
my($path) = find_lib( lib => 'foo', libpath => 'corpus/darwin/custom' );
ok -r $path, "path = $path is readable";
my $dll = TestDLL->new($path);
is $dll->name, 'foo', 'dll.name = foo';
is $dll->version, '1.2.3a', 'dll.version = 1.2.3a';
};
subtest 'find_lib libpath (list)' => sub {
my($path) = find_lib( lib => 'foo', libpath => ['corpus/darwin/custom']);
ok -r $path, "path = $path is readable";
my $dll = TestDLL->new($path);
is $dll->name, 'foo', 'dll.name = foo';
is $dll->version, '1.2.3a', 'dll.version = 1.2.3a';
};
subtest 'find_lib symbol' => sub {
my($path) = find_lib( lib => 'foo', symbol => 'foo_init' );
ok -r $path, "path = $path is readable";
my $dll = TestDLL->new($path);
is $dll->name, 'foo', 'dll.name = foo';
is $dll->version, '1.2.3', 'dll.version = 1.2.3';
};
subtest 'find_lib symbol (bad)' => sub {
my @path = find_lib( lib => 'foo', symbol => 'foo_initx' );
ok @path == 0, 'no path found';
};
subtest 'find_lib symbol (list)' => sub {
my($path) = find_lib( lib => 'foo', symbol => ['foo_init', 'foo_new', 'foo_delete'] );
ok -r $path, "path = $path is readable";
my $dll = TestDLL->new($path);
is $dll->name, 'foo', 'dll.name = foo';
is $dll->version, '1.2.3', 'dll.version = 1.2.3';
};
subtest 'find_lib symbol (list) (bad)' => sub {
my @path = find_lib( lib => 'foo', symbol => ['foo_init', 'foo_new', 'foo_delete', 'bogus'] );
ok @path == 0, 'no path found';
};
subtest 'assert_lib' => sub {
subtest 'found' => sub {
eval { assert_lib( lib => 'foo' ) };
is $@, '', 'no exception';
};
subtest 'not found' => sub {
eval { assert_lib( lib => 'foobar') };
isnt $@, '', 'exception';
};
};
subtest 'check_lib' => sub {
is check_lib( lib => 'foo' ), 1, 'found';
is check_lib( lib => 'foobar'), 0, 'not found';
};
subtest 'verify bad' => sub {
my @lib = find_lib(
lib => 'foo',
verify => sub { 0 },
);
ok @lib == 0, 'returned empty list';
@lib = find_lib(
lib => 'foo',
verify => [ sub { 0 } ],
);
ok @lib == 0, 'returned empty list';
};
subtest 'verify' => sub {
my($lib) = find_lib(
lib => 'foo',
verify => sub {
my($name, $path) = @_;
my $lib = TestDLL->new($path);
$lib->version ne '1.2.3'
},
);
ok -r $lib, "path = $lib is readable";
my $dll = TestDLL->new($lib);
is $dll->name, 'foo', 'dll.name = foo';
is $dll->version, '2.3.4', 'dll.version = 2.3.4';
};
sub p
{
my($path) = @_;
$path =~ s{/}{\\}g if $^O eq 'MSWin32';
$path;
}
subtest '_cmp' => sub {
my $process = sub {
[
sort { FFI::CheckLib::_cmp($a,$b) }
map { FFI::CheckLib::_matches($_, '/lib') }
@_
];
};
is(
$process->(qw( libfoo.1.2.3.dylib libbar.3.4.5.dylib libbaz.0.0.0.dylib )),
[
[ 'bar', p('/lib/libbar.3.4.5.dylib'), 3,4,5 ],
[ 'baz', p('/lib/libbaz.0.0.0.dylib'), 0,0,0 ],
[ 'foo', p('/lib/libfoo.1.2.3.dylib'), 1,2,3 ],
],
'name first 1',
);
is(
$process->(qw( libbaz.0.0.0.dylib libfoo.1.2.3.dylib libbar.3.4.5.dylib )),
[
[ 'bar', p('/lib/libbar.3.4.5.dylib'), 3,4,5 ],
[ 'baz', p('/lib/libbaz.0.0.0.dylib'), 0,0,0 ],
[ 'foo', p('/lib/libfoo.1.2.3.dylib'), 1,2,3 ],
],
'name first 2',
);
is(
$process->(qw( libbar.3.4.5.dylib libbaz.0.0.0.dylib libfoo.1.2.3.dylib )),
[
[ 'bar', p('/lib/libbar.3.4.5.dylib'), 3,4,5 ],
[ 'baz', p('/lib/libbaz.0.0.0.dylib'), 0,0,0 ],
[ 'foo', p('/lib/libfoo.1.2.3.dylib'), 1,2,3 ],
],
'name first 3',
);
is(
$process->(qw( libfoo.1.2.3.dylib libfoo.dylib libfoo.1.2.dylib libfoo.1.dylib )),
[
[ 'foo', p('/lib/libfoo.dylib'), ],
[ 'foo', p('/lib/libfoo.1.dylib'), 1 ],
[ 'foo', p('/lib/libfoo.1.2.dylib'), 1,2 ],
[ 'foo', p('/lib/libfoo.1.2.3.dylib'), 1,2,3 ],
],
'no version before version',
);
is(
$process->(qw( libfoo.2.3.4.dylib libfoo.1.2.3.dylib libfoo.3.4.5.dylib )),
[
[ 'foo', p('/lib/libfoo.3.4.5.dylib'), 3,4,5 ],
[ 'foo', p('/lib/libfoo.2.3.4.dylib'), 2,3,4 ],
[ 'foo', p('/lib/libfoo.1.2.3.dylib'), 1,2,3 ],
],
'newer version first',
);
};
subtest '_darwin_extra_libraries' => sub {
my $homebrew_lib_path = '/opt/homebrew/lib';
my $macports_lib_path = '/opt/local/lib';
my $mock = mock 'FFI::CheckLib';
$mock->override(
_homebrew_lib_path => sub {$homebrew_lib_path},
_macports_lib_path => sub {$macports_lib_path}
);
subtest 'default' => sub {
local %ENV = %ENV;
delete $ENV{FFI_CHECKLIB_PACKAGE};
is(
[ FFI::CheckLib::_darwin_extra_paths() ],
[ $homebrew_lib_path, $macports_lib_path ],
'homebrew and macports lib paths added'
);
};
subtest 'none' => sub {
local %ENV = %ENV;
$ENV{FFI_CHECKLIB_PACKAGE} = 'NONE';
is(
[ FFI::CheckLib::_darwin_extra_paths() ],
[],
'None extra lib paths added'
);
};
subtest 'homebrew' => sub {
local %ENV = %ENV;
$ENV{FFI_CHECKLIB_PACKAGE} = 'Homebrew';
is(
[ FFI::CheckLib::_darwin_extra_paths() ],
[ $homebrew_lib_path ],
'homebrew lib path added'
);
};
subtest 'macports' => sub {
local %ENV = %ENV;
$ENV{FFI_CHECKLIB_PACKAGE} = 'MacPorts';
is(
[ FFI::CheckLib::_darwin_extra_paths() ],
[ $macports_lib_path ],
'macports lib path loaded'
);
};
subtest 'macports,homebrew' => sub {
local %ENV = %ENV;
$ENV{FFI_CHECKLIB_PACKAGE} = 'macports,homebrew';
is(
[ FFI::CheckLib::_darwin_extra_paths() ],
[ $macports_lib_path, $homebrew_lib_path ],
'macports and homebrew lib paths added (order matters)'
);
};
};
done_testing;
|