Fix incorrect whitespace test outside of checkouts
[dbsrgits/DBIx-Class.git] / t / 00describe_environment.t
CommitLineData
cebc0cc8 1###
2### This version is rather 5.8-centric, because DBIC itself is 5.8
3### It certainly can be rewritten to degrade well on 5.6
4###
5
3cd1b172 6# Very important to grab the snapshot early, as we will be reporting
7# the INC indices from the POV of whoever ran the script, *NOT* from
8# the POV of the internals
9my @initial_INC;
cebc0cc8 10BEGIN {
3cd1b172 11 @initial_INC = @INC;
12}
13
c0329273 14BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
3cd1b172 15
c0329273 16BEGIN {
750a4ad2 17 if ( "$]" < 5.010) {
cebc0cc8 18
19 # Pre-5.10 perls pollute %INC on unsuccesfull module
20 # require, making it appear as if the module is already
21 # loaded on subsequent require()s
22 # Can't seem to find the exact RT/perldelta entry
23 #
24 # The reason we can't just use a sane, clean loader, is because
25 # if a Module require()s another module the %INC will still
26 # get filled with crap and we are back to square one. A global
27 # fix is really the only way for this test, as we try to load
28 # each available module separately, and have no control (nor
29 # knowledge) over their common dependencies.
30 #
31 # we want to do this here, in the very beginning, before even
32 # warnings/strict are loaded
33
cebc0cc8 34 require DBICTest::Util::OverrideRequire;
35
36 DBICTest::Util::OverrideRequire::override_global_require( sub {
37 my $res = eval { $_[0]->() };
38 if ($@ ne '') {
39 delete $INC{$_[1]};
40 die $@;
41 }
42 return $res;
43 } );
44 }
45}
46
cebc0cc8 47use strict;
48use warnings;
49
50use Test::More 'no_plan';
51use Config;
52use File::Find 'find';
3cd1b172 53use Digest::MD5 ();
54use Cwd 'abs_path';
c6b73be9 55use File::Spec;
843b427a 56use List::Util 'max';
cebc0cc8 57use ExtUtils::MakeMaker;
cebc0cc8 58
c6b73be9 59use DBICTest::RunMode;
60use DBICTest::Util 'visit_namespaces';
61use DBIx::Class::Optional::Dependencies;
cebc0cc8 62
c6b73be9 63my $known_paths = {
55c6fb91 64 SA => {
65 config_key => 'sitearch',
66 },
67 SL => {
68 config_key => 'sitelib',
69 },
c6b73be9 70 SS => {
71 config_key => 'sitelib_stem',
72 match_order => 1,
73 },
74 SP => {
75 config_key => 'siteprefix',
76 match_order => 2,
77 },
55c6fb91 78 VA => {
79 config_key => 'vendorarch',
80 },
81 VL => {
82 config_key => 'vendorlib',
83 },
c6b73be9 84 VS => {
85 config_key => 'vendorlib_stem',
86 match_order => 3,
87 },
88 VP => {
89 config_key => 'vendorprefix',
90 match_order => 4,
91 },
55c6fb91 92 PA => {
93 config_key => 'archlib',
94 },
95 PL => {
96 config_key => 'privlib',
97 },
c6b73be9 98 PP => {
99 config_key => 'prefix',
100 match_order => 5,
101 },
102 BLA => {
103 rel_path => './blib/arch',
104 skip_unversioned_modules => 1,
105 },
106 BLL => {
107 rel_path => './blib/lib',
108 skip_unversioned_modules => 1,
109 },
55c6fb91 110 INC => {
c6b73be9 111 rel_path => './inc',
55c6fb91 112 },
113 LIB => {
c6b73be9 114 rel_path => './lib',
115 skip_unversioned_modules => 1,
55c6fb91 116 },
3cd1b172 117 T => {
c6b73be9 118 rel_path => './t',
119 skip_unversioned_modules => 1,
120 },
65ea9cad 121 XT => {
122 rel_path => './xt',
123 skip_unversioned_modules => 1,
124 },
c6b73be9 125 CWD => {
126 rel_path => '.',
3cd1b172 127 },
55c6fb91 128 HOME => {
c6b73be9 129 rel_path => '~',
130 abs_unix_path => abs_unix_path (
55c6fb91 131 eval { require File::HomeDir and File::HomeDir->my_home }
132 ||
c6b73be9 133 $ENV{USERPROFILE}
134 ||
55c6fb91 135 $ENV{HOME}
136 ||
137 glob('~')
138 ),
139 },
cebc0cc8 140};
141
c6b73be9 142for my $k (keys %$known_paths) {
143 my $v = $known_paths->{$k};
4bfba4ee 144
55c6fb91 145 # never use home as a found-in-dir marker - it is too broad
146 # HOME is only used by the shortener
3cd1b172 147 $v->{marker} = $k unless $k eq 'HOME';
4bfba4ee 148
c6b73be9 149 unless ( $v->{abs_unix_path} ) {
150 if ( $v->{rel_path} ) {
151 $v->{abs_unix_path} = abs_unix_path( $v->{rel_path} );
55c6fb91 152 }
153 elsif ( $Config{ $v->{config_key} || '' } ) {
c6b73be9 154 $v->{abs_unix_path} = abs_unix_path (
55c6fb91 155 $Config{"$v->{config_key}exp"} || $Config{$v->{config_key}}
156 );
157 }
158 }
cebc0cc8 159
c6b73be9 160 delete $known_paths->{$k} unless $v->{abs_unix_path} and -d $v->{abs_unix_path};
cebc0cc8 161}
c6b73be9 162my $seen_markers = {};
cebc0cc8 163
55c6fb91 164# first run through lib/ and *try* to load anything we can find
cebc0cc8 165# within our own project
166find({
167 wanted => sub {
168 -f $_ or return;
169
170 # can't just `require $fn`, as we need %INC to be
171 # populated properly
172 my ($mod) = $_ =~ /^ lib [\/\\] (.+) \.pm $/x
173 or return;
174
55c6fb91 175 try_module_require(join ('::', File::Spec->splitdir($mod)) )
cebc0cc8 176 },
177 no_chdir => 1,
178}, 'lib' );
179
55c6fb91 180
181
cebc0cc8 182# now run through OptDeps and attempt loading everything else
183#
184# some things needs to be sorted before other things
185# positive - load first
186# negative - load last
187my $load_weights = {
188 # Make sure oracle is tried last - some clients (e.g. 10.2) have symbol
189 # clashes with libssl, and will segfault everything coming after them
190 "DBD::Oracle" => -999,
191};
3cd1b172 192
193my @known_modules = sort
cebc0cc8 194 { ($load_weights->{$b}||0) <=> ($load_weights->{$a}||0) }
8fc4291e 195 qw( Data::Dumper ),
cebc0cc8 196 keys %{
197 DBIx::Class::Optional::Dependencies->req_list_for([
250d9e55 198 grep
199 # some DBDs are notoriously problematic to load
200 # hence only show stuff based on test_rdbms which will
201 # take into account necessary ENVs
55c6fb91 202 { $_ !~ /^ (?: rdbms | dist )_ /x }
250d9e55 203 keys %{DBIx::Class::Optional::Dependencies->req_group_list}
cebc0cc8 204 ])
205 }
206;
207
3cd1b172 208try_module_require($_) for @known_modules;
cebc0cc8 209
55c6fb91 210my $has_versionpm = eval { require version };
3cd1b172 211
212
213# At this point we've loaded everything we ever could, but some modules
214# (understandably) crapped out. For an even more thorough report, note
215# everthing present in @INC we excplicitly know about (via OptDeps)
216# *even though* it didn't load
217my $known_failed_loads;
218
219for my $mod (@known_modules) {
220 my $inc_key = module_notional_filename($mod);
221 next if defined $INC{$inc_key};
222
223 if (defined( my $idx = module_found_at_inc_index( $mod, \@INC ) ) ) {
c6b73be9 224 $known_failed_loads->{$mod} = abs_unix_path( "$INC[$idx]/$inc_key" );
3cd1b172 225 }
226
227}
228
229my $perl = 'perl';
230
231# This is a cool idea, but the line is too long even with shortening :(
232#
233#for my $i ( 1 .. $Config{config_argc} ) {
234# my $conf_arg = $Config{"config_arg$i"};
235# $conf_arg =~ s!
236# \= (.+)
237# !
c6b73be9 238# '=' . shorten_fn($1)
3cd1b172 239# !ex;
240#
241# $perl .= " $conf_arg";
242#}
243
244my $interesting_modules = {
245 # pseudo module
246 $perl => {
247 version => $],
65ea9cad 248 abs_unix_path => abs_unix_path($^X),
3cd1b172 249 }
55c6fb91 250};
3cd1b172 251
252
65ea9cad 253# drill through the *ENTIRE* symtable and build a map of interesting modules
cebc0cc8 254visit_namespaces( action => sub {
4bfba4ee 255 no strict 'refs';
cebc0cc8 256 my $pkg = shift;
257
258 # keep going, but nothing to see here
259 return 1 if $pkg eq 'main';
260
4bfba4ee 261 # private - not interested, including no further descent
cebc0cc8 262 return 0 if $pkg =~ / (?: ^ | :: ) _ /x;
263
3cd1b172 264 my $inc_key = module_notional_filename($pkg);
cebc0cc8 265
c6b73be9 266 my $abs_unix_path = (
3cd1b172 267 $INC{$inc_key}
4bfba4ee 268 and
3cd1b172 269 -f $INC{$inc_key}
4bfba4ee 270 and
3cd1b172 271 -r $INC{$inc_key}
55c6fb91 272 and
c6b73be9 273 abs_unix_path($INC{$inc_key})
3cd1b172 274 );
cebc0cc8 275
3cd1b172 276 # handle versions first (not interested in synthetic classes)
cebc0cc8 277 if (
3cd1b172 278 defined ${"${pkg}::VERSION"}
cebc0cc8 279 and
3cd1b172 280 ${"${pkg}::VERSION"} !~ /\Qset by base.pm/
cebc0cc8 281 ) {
cebc0cc8 282
3cd1b172 283 # make sure a version can be extracted, be noisy when it doesn't work
284 # do this even if we are throwing away the result below in lieu of EUMM
285 my $mod_ver = eval { $pkg->VERSION };
286
287 if (my $err = $@) {
288 $err =~ s/^/ /mg;
289 say_err (
290 "Calling `$pkg->VERSION` resulted in an exception, which should never "
291 . "happen - please file a bug with the distribution containing $pkg. "
292 . "Complete exception text below:\n\n$err"
293 );
294 }
295 elsif( ! defined $mod_ver or ! length $mod_ver ) {
296 my $ret = defined $mod_ver
297 ? "the empty string ''"
298 : "'undef'"
299 ;
300
301 say_err (
302 "Calling `$pkg->VERSION` returned $ret, even though \$${pkg}::VERSION "
303 . "is defined, which should never happen - please file a bug with the "
304 . "distribution containing $pkg."
305 );
306
307 undef $mod_ver;
308 }
309
310 if (
c6b73be9 311 $abs_unix_path
3cd1b172 312 and
c6b73be9 313 defined ( my $eumm_ver = eval { MM->parse_version( $abs_unix_path ) } )
3cd1b172 314 ) {
315
316 # can only run the check reliably if v.pm is there
317 if (
318 $has_versionpm
319 and
320 defined $mod_ver
321 and
322 $eumm_ver ne $mod_ver
323 and
324 (
325 ( eval { version->parse( do { (my $v = $eumm_ver) =~ s/_//g; $v } ) } || 0 )
326 !=
327 ( eval { version->parse( do { (my $v = $mod_ver) =~ s/_//g; $v } ) } || 0 )
328 )
329 ) {
330 say_err (
331 "Mismatch of versions '$mod_ver' and '$eumm_ver', obtained respectively "
c6b73be9 332 . "via `$pkg->VERSION` and parsing the version out of @{[ shorten_fn( $abs_unix_path ) ]} "
3cd1b172 333 . "with ExtUtils::MakeMaker\@@{[ ExtUtils::MakeMaker->VERSION ]}. "
334 . "This should never happen - please check whether this is still present "
335 . "in the latest version, and then file a bug with the distribution "
336 . "containing $pkg."
337 );
338 }
339
340 $interesting_modules->{$pkg}{version} = $eumm_ver;
341 }
342 elsif( defined $mod_ver ) {
343
344 $interesting_modules->{$pkg}{version} = $mod_ver;
345 }
cebc0cc8 346 }
65ea9cad 347 elsif ( $known_failed_loads->{$pkg} ) {
348 $abs_unix_path = $known_failed_loads->{$pkg};
c6b73be9 349 $interesting_modules->{$pkg}{version} = '!! LOAD FAIL !!';
55c6fb91 350 }
351
c6b73be9 352 if ($abs_unix_path) {
353 my ($marker, $initial_inc_idx);
354
355 my $current_inc_idx = module_found_at_inc_index($pkg, \@INC);
356 my $p = subpath_of_known_path( $abs_unix_path );
357
358 if (
359 defined $current_inc_idx
360 and
361 $p->{marker}
362 and
363 abs_unix_path($INC[$current_inc_idx]) eq $p->{abs_unix_path}
364 ) {
365 $marker = $p->{marker};
3cd1b172 366 }
c6b73be9 367 elsif (defined ( $initial_inc_idx = module_found_at_inc_index($pkg, \@initial_INC) ) ) {
368 $marker = "\$INC[$initial_inc_idx]";
3cd1b172 369 }
55c6fb91 370
3cd1b172 371 # we are only interested if there was a declared version already above
c6b73be9 372 # OR if the module came from somewhere other than skip_unversioned_modules
3cd1b172 373 if (
374 $marker
375 and
376 (
377 $interesting_modules->{$pkg}
378 or
c6b73be9 379 !$p->{skip_unversioned_modules}
3cd1b172 380 )
381 ) {
382 $interesting_modules->{$pkg}{source_marker} = $marker;
c6b73be9 383 $seen_markers->{$marker} = 1;
55c6fb91 384 }
3cd1b172 385
386 # at this point only fill in the path (md5 calc) IFF it is interesting
387 # in any respect
c6b73be9 388 $interesting_modules->{$pkg}{abs_unix_path} = $abs_unix_path
3cd1b172 389 if $interesting_modules->{$pkg};
cebc0cc8 390 }
391
392 1;
393});
394
65ea9cad 395# compress identical versions sourced from ./blib, ./lib, ./t and ./xt
396# as close to the root of a namespace as we can
c6b73be9 397purge_identically_versioned_submodules_with_markers([ map {
398 ( $_->{skip_unversioned_modules} && $_->{marker} ) || ()
399} values %$known_paths ]);
cebc0cc8 400
3cd1b172 401ok 1, (scalar keys %$interesting_modules) . " distinctly versioned modules found";
cebc0cc8 402
403# do not announce anything under ci - we are watching for STDERR silence
3cd1b172 404exit 0 if DBICTest::RunMode->is_ci;
cebc0cc8 405
cebc0cc8 406
55c6fb91 407# diag the result out
408my $max_ver_len = max map
409 { length "$_" }
3cd1b172 410 ( 'xxx.yyyzzz_bbb', map { $_->{version} || '' } values %$interesting_modules )
55c6fb91 411;
c6b73be9 412my $max_marker_len = max map { length $_ } ( '$INC[999]', keys %$seen_markers );
55c6fb91 413
414my $discl = <<'EOD';
415
65ea9cad 416List of loadable modules within both the core and *OPTIONAL* dependency chains
417present on this system (modules sourced from ./blib, ./lib, ./t, and ./xt
c6b73be9 418with versions identical to their parent namespace were omitted for brevity)
419
420 *** Note that *MANY* of these modules will *NEVER* be loaded ***
421 *** during normal operation of DBIx::Class ***
55c6fb91 422EOD
cebc0cc8 423
3cd1b172 424# pre-assemble everything and print it in one shot
425# makes it less likely for parallel test execution to insert bogus lines
426my $final_out = "\n$discl\n";
cebc0cc8 427
c6b73be9 428$final_out .= "\@INC at startup (does not reflect manipulation at runtime):\n";
cebc0cc8 429
c6b73be9 430my $in_inc_skip;
431for (0.. $#initial_INC) {
432
65ea9cad 433 my $shortname = shorten_fn( $initial_INC[$_] );
c6b73be9 434
65ea9cad 435 # when *to* print a line of INC
c6b73be9 436 if (
437 ! $ENV{AUTOMATED_TESTING}
438 or
439 @initial_INC < 11
440 or
441 $seen_markers->{"\$INC[$_]"}
442 or
65ea9cad 443 ! -e $shortname
c6b73be9 444 or
65ea9cad 445 ! File::Spec->file_name_is_absolute($shortname)
c6b73be9 446 ) {
447 $in_inc_skip = 0;
448 $final_out .= sprintf ( "% 3s: %s\n",
449 $_,
65ea9cad 450 $shortname
c6b73be9 451 );
452 }
453 elsif(! $in_inc_skip++) {
454 $final_out .= " ...\n";
455 }
456}
457
458$final_out .= "\n";
459
460if (my @seen_known_paths = grep { $known_paths->{$_} } keys %$seen_markers) {
3cd1b172 461
462 $final_out .= join "\n", 'Sourcing markers:', (map
463 {
464 sprintf "%*s: %s",
465 $max_marker_len => $_->{marker},
c6b73be9 466 ($_->{config_key} ? "\$Config{$_->{config_key}}" : "$_->{rel_path}/" )
3cd1b172 467 }
468 sort
55c6fb91 469 {
3cd1b172 470 !!$b->{config_key} cmp !!$a->{config_key}
471 or
472 ( $a->{marker}||'') cmp ($b->{marker}||'')
cebc0cc8 473 }
c6b73be9 474 @{$known_paths}{@seen_known_paths}
3cd1b172 475 ), '', '';
cebc0cc8 476
cebc0cc8 477}
478
3cd1b172 479$final_out .= "=============================\n";
cebc0cc8 480
3cd1b172 481$final_out .= join "\n", (map
482 { sprintf (
843b427a 483 "%*s %*s %*s%s",
3cd1b172 484 $max_marker_len => $interesting_modules->{$_}{source_marker} || '',
485 $max_ver_len => ( defined $interesting_modules->{$_}{version}
486 ? $interesting_modules->{$_}{version}
487 : ''
488 ),
c6b73be9 489 -78 => $_,
490 ($interesting_modules->{$_}{abs_unix_path}
491 ? " [ MD5: @{[ get_md5( $interesting_modules->{$_}{abs_unix_path} ) ]} ]"
492 : "! -f \$INC{'@{[ module_notional_filename($_) ]}'}"
3cd1b172 493 ),
494 ) }
495 sort { lc($a) cmp lc($b) } keys %$interesting_modules
496), '';
497
498$final_out .= "=============================\n$discl\n\n";
4bfba4ee 499
3cd1b172 500diag $final_out;
4bfba4ee 501
55c6fb91 502exit 0;
4bfba4ee 503
4bfba4ee 504
4bfba4ee 505
c6b73be9 506sub say_err { print STDERR "\n", @_, "\n\n" };
cebc0cc8 507
55c6fb91 508# do !!!NOT!!! use Module::Runtime's require_module - it breaks CORE::require
509sub try_module_require {
510 # trap deprecation warnings and whatnot
511 local $SIG{__WARN__} = sub {};
512 local $@;
513 eval "require $_[0]";
514}
cebc0cc8 515
c6b73be9 516sub abs_unix_path {
517 return '' unless (
518 defined $_[0]
519 and
520 ( -e $_[0] or File::Spec->file_name_is_absolute($_[0]) )
521 );
55c6fb91 522
3cd1b172 523 # File::Spec's rel2abs does not resolve symlinks
524 # we *need* to look at the filesystem to be sure
c6b73be9 525 my $abs_fn = abs_path($_[0]);
55c6fb91 526
c6b73be9 527 if ( $^O eq 'MSWin32' and $abs_fn ) {
55c6fb91 528
529 # sometimes we can get a short/longname mix, normalize everything to longnames
c6b73be9 530 $abs_fn = Win32::GetLongPathName($abs_fn);
55c6fb91 531
532 # Fixup (native) slashes in Config not matching (unixy) slashes in INC
c6b73be9 533 $abs_fn =~ s|\\|/|g;
55c6fb91 534 }
cebc0cc8 535
c6b73be9 536 $abs_fn;
cebc0cc8 537}
538
55c6fb91 539sub shorten_fn {
540 my $fn = shift;
541
c6b73be9 542 my $abs_fn = abs_unix_path($fn);
55c6fb91 543
c6b73be9 544 if (my $p = subpath_of_known_path( $fn ) ) {
545 $abs_fn =~ s| (?<! / ) $|/|x
546 if -d $abs_fn;
547
548 if ($p->{rel_path}) {
549 $abs_fn =~ s!\Q$p->{abs_unix_path}!$p->{rel_path}!
550 and return $abs_fn;
551 }
552 elsif ($p->{config_key}) {
553 $abs_fn =~ s!\Q$p->{abs_unix_path}!<<$p->{marker}>>!
554 and
555 $seen_markers->{$p->{marker}} = 1
556 and
557 return $abs_fn;
558 }
55c6fb91 559 }
560
c6b73be9 561 # we got so far - not a known path
562 # return the unixified version it if was absolute, leave as-is otherwise
65ea9cad 563 my $rv = ( $abs_fn and File::Spec->file_name_is_absolute( $fn ) )
c6b73be9 564 ? $abs_fn
565 : $fn
566 ;
65ea9cad 567
568 $rv = "( ! -e ) $rv" unless -e $rv;
569
570 return $rv;
55c6fb91 571}
572
c6b73be9 573sub subpath_of_known_path {
574 my $abs_fn = abs_unix_path( $_[0] )
55c6fb91 575 or return '';
576
c6b73be9 577 for my $p (
578 sort {
579 length( $b->{abs_unix_path} ) <=> length( $a->{abs_unix_path} )
580 or
581 ( $a->{match_order} || 0 ) <=> ( $b->{match_order} || 0 )
582 }
583 values %$known_paths
55c6fb91 584 ) {
3cd1b172 585 # run through the matcher twice - first always append a /
586 # then try without
587 # important to avoid false positives
588 for my $suff ( '/', '' ) {
c6b73be9 589 return { %$p } if 0 == index( $abs_fn, "$p->{abs_unix_path}$suff" );
3cd1b172 590 }
55c6fb91 591 }
592}
593
3cd1b172 594sub module_found_at_inc_index {
c6b73be9 595 my ($mod, $inc_dirs) = @_;
596
597 return undef unless @$inc_dirs;
3cd1b172 598
599 my $fn = module_notional_filename($mod);
600
c6b73be9 601 for my $i ( 0 .. $#$inc_dirs ) {
602
3cd1b172 603 # searching from here on out won't mean anything
c6b73be9 604 # FIXME - there is actually a way to interrogate this safely, but
605 # that's a fight for another day
606 return undef if length ref $inc_dirs->[$i];
3cd1b172 607
608 if (
c6b73be9 609 -d $inc_dirs->[$i]
3cd1b172 610 and
c6b73be9 611 -f "$inc_dirs->[$i]/$fn"
3cd1b172 612 and
c6b73be9 613 -r "$inc_dirs->[$i]/$fn"
3cd1b172 614 ) {
615 return $i;
616 }
617 }
618
619 return undef;
620}
621
622sub purge_identically_versioned_submodules_with_markers {
623 my $markers = shift;
624
625 return unless @$markers;
626
627 for my $mod ( sort { length($b) <=> length($a) } keys %$interesting_modules ) {
628
629 next unless defined $interesting_modules->{$mod}{version};
630
631 my $marker = $interesting_modules->{$mod}{source_marker}
632 or next;
633
634 next unless grep { $marker eq $_ } @$markers;
635
636 my $parent = $mod;
637
638 while ( $parent =~ s/ :: (?: . (?! :: ) )+ $ //x ) {
639 $interesting_modules->{$parent}
640 and
641 ($interesting_modules->{$parent}{version}||'') eq $interesting_modules->{$mod}{version}
642 and
643 ($interesting_modules->{$parent}{source_marker}||'') eq $interesting_modules->{$mod}{source_marker}
644 and
645 delete $interesting_modules->{$mod}
646 and
647 last
648 }
649 }
650}
651
652sub module_notional_filename {
653 (my $fn = $_[0] . '.pm') =~ s|::|/|g;
654 $fn;
655}
656
55c6fb91 657sub get_md5 {
658 # we already checked for -r/-f, just bail if can't open
659 open my $fh, '<:raw', $_[0] or return '';
55c6fb91 660 Digest::MD5->new->addfile($fh)->hexdigest;
661}