File: 00describe_environment.t

package info (click to toggle)
libdbix-class-perl 0.082844-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (696 lines) | stat: -rw-r--r-- 17,807 bytes parent folder | download | duplicates (3)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
###
### This version is rather 5.8-centric, because DBIC itself is 5.8
### It certainly can be rewritten to degrade well on 5.6
###

# Very important to grab the snapshot early, as we will be reporting
# the INC indices from the POV of whoever ran the script, *NOT* from
# the POV of the internals
my @initial_INC;
BEGIN {
  @initial_INC = @INC;
}

BEGIN {
  local @INC = ( 't/lib', @INC );


  if ( "$]" < 5.010) {

    # Pre-5.10 perls pollute %INC on unsuccesfull module
    # require, making it appear as if the module is already
    # loaded on subsequent require()s
    # Can't seem to find the exact RT/perldelta entry
    #
    # The reason we can't just use a sane, clean loader, is because
    # if a Module require()s another module the %INC will still
    # get filled with crap and we are back to square one. A global
    # fix is really the only way for this test, as we try to load
    # each available module separately, and have no control (nor
    # knowledge) over their common dependencies.
    #
    # we want to do this here, in the very beginning, before even
    # warnings/strict are loaded

    require DBICTest::Util::OverrideRequire;

    DBICTest::Util::OverrideRequire::override_global_require( sub {
      my $res = eval { $_[0]->() };
      if ($@ ne '') {
        delete $INC{$_[1]};
        die $@;
      }
      return $res;
    } );

  }

  require DBICTest::RunMode;
  require DBICTest::Util;
}

use strict;
use warnings;

use Test::More 'no_plan';

# Things happen... unfortunately
$SIG{__DIE__} = sub {
  die $_[0] unless defined $^S and ! $^S;

  diag "Something horrible happened while assembling the diag data\n$_[0]";
  exit 0;
};

use Config;
use File::Find 'find';
use Digest::MD5 ();
use Cwd 'abs_path';
use File::Spec;
use List::Util 'max';
use ExtUtils::MakeMaker;

use DBIx::Class::Optional::Dependencies;

my $known_paths = {
  SA => {
    config_key => 'sitearch',
  },
  SL => {
    config_key => 'sitelib',
  },
  SS => {
    config_key => 'sitelib_stem',
    match_order => 1,
  },
  SP => {
    config_key => 'siteprefix',
    match_order => 2,
  },
  VA => {
    config_key => 'vendorarch',
  },
  VL => {
    config_key => 'vendorlib',
  },
  VS => {
    config_key => 'vendorlib_stem',
    match_order => 3,
  },
  VP => {
    config_key => 'vendorprefix',
    match_order => 4,
  },
  PA => {
    config_key => 'archlib',
  },
  PL => {
    config_key => 'privlib',
  },
  PP => {
    config_key => 'prefix',
    match_order => 5,
  },
  BLA => {
    rel_path => './blib/arch',
    skip_unversioned_modules => 1,
  },
  BLL => {
    rel_path => './blib/lib',
    skip_unversioned_modules => 1,
  },
  INC => {
    rel_path => './inc',
  },
  LIB => {
    rel_path => './lib',
    skip_unversioned_modules => 1,
  },
  T => {
    rel_path => './t',
    skip_unversioned_modules => 1,
  },
  XT => {
    rel_path => './xt',
    skip_unversioned_modules => 1,
  },
  CWD => {
    rel_path => '.',
  },
  HOME => {
    rel_path => '~',
    abs_unix_path => abs_unix_path (
      eval { require File::HomeDir and File::HomeDir->my_home }
        ||
      $ENV{USERPROFILE}
        ||
      $ENV{HOME}
        ||
      glob('~')
    ),
  },
};

for my $k (keys %$known_paths) {
  my $v = $known_paths->{$k};

  # never use home as a found-in-dir marker - it is too broad
  # HOME is only used by the shortener
  $v->{marker} = $k unless $k eq 'HOME';

  unless ( $v->{abs_unix_path} ) {
    if ( $v->{rel_path} ) {
      $v->{abs_unix_path} = abs_unix_path( $v->{rel_path} );
    }
    elsif ( $Config{ $v->{config_key} || '' } ) {
      $v->{abs_unix_path} = abs_unix_path (
        $Config{"$v->{config_key}exp"} || $Config{$v->{config_key}}
      );
    }
  }

  delete $known_paths->{$k} unless $v->{abs_unix_path} and -d $v->{abs_unix_path};
}
my $seen_markers = {};

# first run through lib/ and *try* to load anything we can find
# within our own project
find({
  wanted => sub {
    -f $_ or return;

    $_ =~ m|lib/DBIx/Class/_TempExtlib| and return;

    # can't just `require $fn`, as we need %INC to be
    # populated properly
    my ($mod) = $_ =~ /^ lib [\/\\] (.+) \.pm $/x
      or return;

    try_module_require(join ('::', File::Spec->splitdir($mod)) )
  },
  no_chdir => 1,
}, 'lib' );



# now run through OptDeps and attempt loading everything else
#
# some things needs to be sorted before other things
# positive - load first
# negative - load last
my $load_weights = {
  # Make sure oracle is tried last - some clients (e.g. 10.2) have symbol
  # clashes with libssl, and will segfault everything coming after them
  "DBD::Oracle" => -999,
};

my @known_modules = sort
  { ($load_weights->{$b}||0) <=> ($load_weights->{$a}||0) }
  qw( Data::Dumper DBD::SQLite ),
  map
    { $_ => 1 }
    map
      { keys %{ DBIx::Class::Optional::Dependencies->req_list_for($_) } }
      grep
        # some DBDs are notoriously problematic to load
        # hence only show stuff based on test_rdbms which will
        # take into account necessary ENVs
        { $_ !~ /^ (?: rdbms | dist )_ /x }
        keys %{DBIx::Class::Optional::Dependencies->req_group_list}
;

try_module_require($_) for @known_modules;

my $has_versionpm = eval { require version };


# At this point we've loaded everything we ever could, but some modules
# (understandably) crapped out. For an even more thorough report, note
# everthing present in @INC we excplicitly know about (via OptDeps)
# *even though* it didn't load
my $known_failed_loads;

for my $mod (@known_modules) {
  my $inc_key = module_notional_filename($mod);
  next if defined $INC{$inc_key};

  if (defined( my $idx = module_found_at_inc_index( $mod, \@INC ) ) ) {
    $known_failed_loads->{$mod} = abs_unix_path( "$INC[$idx]/$inc_key" );
  }

}

my $perl = 'perl';

# This is a cool idea, but the line is too long even with shortening :(
#
#for my $i ( 1 .. $Config{config_argc} ) {
#  my $conf_arg = $Config{"config_arg$i"};
#  $conf_arg =~ s!
#    \= (.+)
#  !
#    '=' . shorten_fn($1)
#  !ex;
#
#  $perl .= " $conf_arg";
#}

my $interesting_modules = {
  # pseudo module
  $perl => {
    version => $],
    abs_unix_path => abs_unix_path($^X),
  }
};


# drill through the *ENTIRE* symtable and build a map of interesting modules
DBICTest::Util::visit_namespaces( action => sub {
  no strict 'refs';
  my $pkg = shift;

  # keep going, but nothing to see here
  return 1 if $pkg eq 'main';

  # private - not interested, including no further descent
  return 0 if $pkg =~ / (?: ^ | :: ) _ /x;

  my $inc_key = module_notional_filename($pkg);

  my $abs_unix_path = (
    $INC{$inc_key}
      and
    -f $INC{$inc_key}
      and
    -r $INC{$inc_key}
      and
    abs_unix_path($INC{$inc_key})
  );

  # handle versions first (not interested in synthetic classes)
  if (
    defined ${"${pkg}::VERSION"}
      and
    ${"${pkg}::VERSION"} !~ /\Qset by base.pm/
  ) {

    # make sure a version can be extracted, be noisy when it doesn't work
    # do this even if we are throwing away the result below in lieu of EUMM
    my $mod_ver = eval { $pkg->VERSION };

    if (my $err = $@) {
      $err =~ s/^/  /mg;
      say_err (
        "Calling `$pkg->VERSION` resulted in an exception, which should never "
      . "happen - please file a bug with the distribution containing $pkg. "
      . "Complete exception text below:\n\n$err"
      );
    }
    elsif( ! defined $mod_ver or ! length $mod_ver ) {
      my $ret = defined $mod_ver
        ? "the empty string ''"
        : "'undef'"
      ;

      say_err (
        "Calling `$pkg->VERSION` returned $ret, even though \$${pkg}::VERSION "
      . "is defined, which should never happen - please file a bug with the "
      . "distribution containing $pkg."
      );

      undef $mod_ver;
    }

    if (
      $abs_unix_path
        and
      defined ( my $eumm_ver = eval { MM->parse_version( $abs_unix_path ) } )
    ) {

      # can only run the check reliably if v.pm is there
      if (
        $has_versionpm
          and
        defined $mod_ver
          and
        $eumm_ver ne $mod_ver
          and
        (
          ( eval { version->parse( do { (my $v = $eumm_ver) =~ s/_//g; $v } ) } || 0 )
            !=
          ( eval { version->parse( do { (my $v = $mod_ver) =~ s/_//g; $v } ) } || 0 )
        )
      ) {
        say_err (
          "Mismatch of versions '$mod_ver' and '$eumm_ver', obtained respectively "
        . "via `$pkg->VERSION` and parsing the version out of @{[ shorten_fn( $abs_unix_path ) ]} "
        . "with ExtUtils::MakeMaker\@@{[ ExtUtils::MakeMaker->VERSION ]}. "
        . "This should never happen - please check whether this is still present "
        . "in the latest version, and then file a bug with the distribution "
        . "containing $pkg."
        );
      }

      $interesting_modules->{$pkg}{version} = $eumm_ver;
    }
    elsif( defined $mod_ver ) {

      $interesting_modules->{$pkg}{version} = $mod_ver;
    }
  }
  elsif ( $known_failed_loads->{$pkg} ) {
    $abs_unix_path = $known_failed_loads->{$pkg};
    $interesting_modules->{$pkg}{version} = '!! LOAD FAIL !!';
  }

  if ($abs_unix_path) {
    my ($marker, $initial_inc_idx);

    my $current_inc_idx = module_found_at_inc_index($pkg, \@INC);
    my $p = subpath_of_known_path( $abs_unix_path );

    if (
      defined $current_inc_idx
        and
      $p->{marker}
        and
      abs_unix_path($INC[$current_inc_idx]) eq $p->{abs_unix_path}
    ) {
      $marker = $p->{marker};
    }
    elsif (defined ( $initial_inc_idx = module_found_at_inc_index($pkg, \@initial_INC) ) ) {
      $marker = "\$INC[$initial_inc_idx]";
    }

    # we are only interested if there was a declared version already above
    # OR if the module came from somewhere other than skip_unversioned_modules
    if (
      $marker
        and
      (
        $interesting_modules->{$pkg}
          or
        !$p->{skip_unversioned_modules}
      )
    ) {
      $interesting_modules->{$pkg}{source_marker} = $marker;
      $seen_markers->{$marker} = 1;
    }

    # at this point only fill in the path (md5 calc) IFF it is interesting
    # in any respect
    $interesting_modules->{$pkg}{abs_unix_path} = $abs_unix_path
      if $interesting_modules->{$pkg};
  }

  1;
});

# compress identical versions sourced from ./blib, ./lib, ./t and ./xt
# as close to the root of a namespace as we can
purge_identically_versioned_submodules_with_markers([ map {
  ( $_->{skip_unversioned_modules} && $_->{marker} ) || ()
} values %$known_paths ]);

ok 1, (scalar keys %$interesting_modules) . " distinctly versioned modules found";

# do not announce anything under ci - we are watching for STDERR silence
exit 0 if DBICTest::RunMode->is_ci;


# diag the result out
my $max_ver_len = max map
  { length "$_" }
  ( 'xxx.yyyzzz_bbb', map { $_->{version} || '' } values %$interesting_modules )
;
my $max_marker_len = max map { length $_ } ( '$INC[999]', keys %$seen_markers );

# Note - must be less than 76 chars wide to account for the diag() prefix
my $discl = <<'EOD';

List of loadable modules within both *OPTIONAL* and core dependency chains
present on this system (modules sourced from ./blib, ./lib, ./t, and ./xt
with versions identical to their parent namespace were omitted for brevity)

    *** Note that *MANY* of these modules will *NEVER* be loaded ***
            *** during normal operation of DBIx::Class ***
EOD

# pre-assemble everything and print it in one shot
# makes it less likely for parallel test execution to insert bogus lines
my $final_out = "\n$discl\n";

$final_out .= "\@INC at startup (does not reflect manipulation at runtime):\n";

my $in_inc_skip;
for (0.. $#initial_INC) {

  my $shortname = shorten_fn( $initial_INC[$_] );

  # when *to* print a line of INC
  if (
    ! $ENV{AUTOMATED_TESTING}
      or
    @initial_INC < 11
      or
    $seen_markers->{"\$INC[$_]"}
      or
    ! -e $shortname
      or
    ! File::Spec->file_name_is_absolute($shortname)
  ) {
    $in_inc_skip = 0;
    $final_out .= sprintf ( "% 3s: %s\n",
      $_,
      $shortname
    );
  }
  elsif(! $in_inc_skip++) {
    $final_out .= "  ...\n";
  }
}

$final_out .= "\n";

if (my @seen_known_paths = grep { $known_paths->{$_} } keys %$seen_markers) {

  $final_out .= join "\n", 'Sourcing markers:', (map
    {
      sprintf "%*s: %s",
        $max_marker_len => $_->{marker},
        ($_->{config_key} ? "\$Config{$_->{config_key}}" : "$_->{rel_path}/" )
    }
    sort
      {
        !!$b->{config_key} cmp !!$a->{config_key}
          or
        ( $a->{marker}||'') cmp ($b->{marker}||'')
      }
      @{$known_paths}{@seen_known_paths}
  ), '', '';

}

$final_out .= "=============================\n";

$final_out .= join "\n", (map
  { sprintf (
    "%*s  %*s  %*s%s",
    $max_marker_len => $interesting_modules->{$_}{source_marker} || '',
    $max_ver_len => ( defined $interesting_modules->{$_}{version}
      ? $interesting_modules->{$_}{version}
      : ''
    ),
    -78 => $_,
    ($interesting_modules->{$_}{abs_unix_path}
      ? "  [ MD5: @{[ get_md5( $interesting_modules->{$_}{abs_unix_path} ) ]} ]"
      : "! -f \$INC{'@{[ module_notional_filename($_) ]}'}"
    ),
  ) }
  sort { lc($a) cmp lc($b) } keys %$interesting_modules
), '';

$final_out .= "=============================\n$discl\n\n";

diag $final_out;

# *very* large printouts may not finish flushing before the test exits
# injecting a <testname> ... ok in the middle of the diag
# http://www.cpantesters.org/cpan/report/fbdac74c-35ca-11e6-ab41-c893a58a4b8c
select( undef, undef, undef, 0.2 );

exit 0;



sub say_err { print STDERR "\n", @_, "\n\n" };

# do !!!NOT!!! use Module::Runtime's require_module - it breaks CORE::require
sub try_module_require {
  # trap deprecation warnings and whatnot
  local $SIG{__WARN__} = sub {};
  local $@;
  eval "require $_[0]";
}

sub abs_unix_path {
  return '' unless (
    defined $_[0]
      and
    ( -e $_[0] or File::Spec->file_name_is_absolute($_[0]) )
  );

  # File::Spec's rel2abs does not resolve symlinks
  # we *need* to look at the filesystem to be sure
  #
  # But looking at the FS for non-existing basenames *may*
  # throw on some OSes so be extra paranoid:
  # http://www.cpantesters.org/cpan/report/26a6e42f-6c23-1014-b7dd-5cd275d8a230
  #
  my $abs_fn = eval { abs_path($_[0]) } || '';

  if ( $abs_fn and $^O eq 'MSWin32' ) {

    # sometimes we can get a short/longname mix, normalize everything to longnames
    $abs_fn = Win32::GetLongPathName($abs_fn)
      if -e $abs_fn;

    # Fixup (native) slashes in Config not matching (unixy) slashes in INC
    $abs_fn =~ s|\\|/|g;
  }

  $abs_fn;
}

sub shorten_fn {
  my $fn = shift;

  my $abs_fn = abs_unix_path($fn);

  if ($abs_fn and my $p = subpath_of_known_path( $fn ) ) {
    $abs_fn =~ s| (?<! / ) $|/|x
      if -d $abs_fn;

    if ($p->{rel_path}) {
      $abs_fn =~ s!\Q$p->{abs_unix_path}!$p->{rel_path}!
        and return $abs_fn;
    }
    elsif ($p->{config_key}) {
      $abs_fn =~ s!\Q$p->{abs_unix_path}!<<$p->{marker}>>!
        and
      $seen_markers->{$p->{marker}} = 1
        and
      return $abs_fn;
    }
  }

  # we got so far - not a known path
  # return the unixified version it if was absolute, leave as-is otherwise
  my $rv = ( $abs_fn and File::Spec->file_name_is_absolute( $fn ) )
    ? $abs_fn
    : $fn
  ;

  $rv = "( ! -e ) $rv" unless -e $rv;

  return $rv;
}

sub subpath_of_known_path {
  my $abs_fn = abs_unix_path( $_[0] )
    or return '';

  for my $p (
    sort {
      length( $b->{abs_unix_path} ) <=> length( $a->{abs_unix_path} )
        or
      ( $a->{match_order} || 0 ) <=> ( $b->{match_order} || 0 )
    }
    values %$known_paths
  ) {
    # run through the matcher twice - first always append a /
    # then try without
    # important to avoid false positives
    for my $suff ( '/', '' ) {
      return { %$p } if 0 == index( $abs_fn, "$p->{abs_unix_path}$suff" );
    }
  }
}

sub module_found_at_inc_index {
  my ($mod, $inc_dirs) = @_;

  return undef unless @$inc_dirs;

  my $fn = module_notional_filename($mod);

  # trust INC if it specifies an existing path
  if( -f ( my $existing_path = abs_unix_path( $INC{$fn} ) ) ) {
    for my $i ( 0 .. $#$inc_dirs ) {

      # searching from here on out won't mean anything
      # FIXME - there is actually a way to interrogate this safely, but
      # that's a fight for another day
      return undef if length ref $inc_dirs->[$i];

      return $i
        if 0 == index( $existing_path, abs_unix_path( $inc_dirs->[$i] ) . '/' );
    }
  }

  for my $i ( 0 .. $#$inc_dirs ) {

    if (
      -d $inc_dirs->[$i]
        and
      -f "$inc_dirs->[$i]/$fn"
        and
      -r "$inc_dirs->[$i]/$fn"
    ) {
      return $i;
    }
  }

  return undef;
}

sub purge_identically_versioned_submodules_with_markers {
  my $markers = shift;

  return unless @$markers;

  for my $mod ( sort { length($b) <=> length($a) } keys %$interesting_modules ) {

    next unless defined $interesting_modules->{$mod}{version};

    my $marker = $interesting_modules->{$mod}{source_marker}
      or next;

    next unless grep { $marker eq $_ } @$markers;

    my $parent = $mod;

    while ( $parent =~ s/ :: (?: . (?! :: ) )+ $ //x ) {
      $interesting_modules->{$parent}
        and
      ($interesting_modules->{$parent}{version}||'') eq $interesting_modules->{$mod}{version}
        and
      ($interesting_modules->{$parent}{source_marker}||'') eq $interesting_modules->{$mod}{source_marker}
        and
    delete $interesting_modules->{$mod}
        and
      last
    }
  }
}

sub module_notional_filename {
  (my $fn = $_[0] . '.pm') =~ s|::|/|g;
  $fn;
}

sub get_md5 {
  # we already checked for -r/-f, just bail if can't open
  open my $fh, '<:raw', $_[0] or return '';
  Digest::MD5->new->addfile($fh)->hexdigest;
}