Add comprehensive 'report-prereqs'-like tooling
[dbsrgits/DBIx-Class.git] / t / 00describe_environment.t
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
6
7 BEGIN {
8   if ($] < 5.010) {
9
10     # Pre-5.10 perls pollute %INC on unsuccesfull module
11     # require, making it appear as if the module is already
12     # loaded on subsequent require()s
13     # Can't seem to find the exact RT/perldelta entry
14     #
15     # The reason we can't just use a sane, clean loader, is because
16     # if a Module require()s another module the %INC will still
17     # get filled with crap and we are back to square one. A global
18     # fix is really the only way for this test, as we try to load
19     # each available module separately, and have no control (nor
20     # knowledge) over their common dependencies.
21     #
22     # we want to do this here, in the very beginning, before even
23     # warnings/strict are loaded
24
25     unshift @INC, 't/lib';
26     require DBICTest::Util::OverrideRequire;
27
28     DBICTest::Util::OverrideRequire::override_global_require( sub {
29       my $res = eval { $_[0]->() };
30       if ($@ ne '') {
31         delete $INC{$_[1]};
32         die $@;
33       }
34       return $res;
35     } );
36   }
37 }
38
39 # Explicitly add 'lib' to the front of INC - this way we will
40 # know without ambiguity what was loaded from the local untar
41 # and what came from elsewhere
42 use lib qw(lib t/lib);
43
44 use strict;
45 use warnings;
46
47 use Test::More 'no_plan';
48 use Config;
49 use File::Find 'find';
50 use Module::Runtime 'module_notional_filename';
51 use List::Util 'max';
52 use ExtUtils::MakeMaker;
53 use DBICTest::Util 'visit_namespaces';
54
55 # load these two to pull in the t/lib armada
56 use DBICTest;
57 use DBICTest::Schema;
58
59 # do !!!NOT!!! use Module::Runtime's require_module - it breaks CORE::require
60 sub req_mod ($) {
61   # trap deprecation warnings and whatnot
62   local $SIG{__WARN__} = sub {};
63   local $@;
64   eval "require $_[0]";
65 }
66
67 sub say_err {
68   print STDERR "\n", @_, "\n";
69 }
70
71 my @lib_display_order = qw(
72   sitearch
73   sitelib
74   vendorarch
75   vendorlib
76   archlib
77   privlib
78 );
79 my $lib_paths = {
80   (map
81     { $Config{$_} ? ( $_ => $Config{"${_}exp"} || $Config{$_} ) : () }
82     @lib_display_order
83   ),
84
85   # synthetic, for display
86   './lib' => 'lib',
87 };
88
89 sub describe_fn {
90   my $fn = shift;
91
92   $lib_paths->{$_} and $fn =~ s/^\Q$lib_paths->{$_}/<<$_>>/ and last
93     for @lib_display_order;
94
95   $fn;
96 }
97
98 sub md5_of_fn {
99   # we already checked for -r/-f, just bail if can't open
100   open my $fh, '<:raw', $_[0] or return '';
101   require Digest::MD5;
102   Digest::MD5->new->addfile($fh)->hexdigest;
103 }
104
105 # first run through lib and *try* to load anything we can find
106 # within our own project
107 find({
108   wanted => sub {
109     -f $_ or return;
110
111     # can't just `require $fn`, as we need %INC to be
112     # populated properly
113     my ($mod) = $_ =~ /^ lib [\/\\] (.+) \.pm $/x
114       or return;
115
116     req_mod join ('::', File::Spec->splitdir($mod));
117   },
118   no_chdir => 1,
119 }, 'lib' );
120
121 # now run through OptDeps and attempt loading everything else
122 #
123 # some things needs to be sorted before other things
124 # positive - load first
125 # negative - load last
126 my $load_weights = {
127   # Make sure oracle is tried last - some clients (e.g. 10.2) have symbol
128   # clashes with libssl, and will segfault everything coming after them
129   "DBD::Oracle" => -999,
130 };
131 req_mod $_ for sort
132   { ($load_weights->{$b}||0) <=> ($load_weights->{$a}||0) }
133   keys %{
134     DBIx::Class::Optional::Dependencies->req_list_for([
135       keys %{DBIx::Class::Optional::Dependencies->req_group_list}
136     ])
137   }
138 ;
139
140 my $has_versionpm = eval { require version };
141
142 # at this point we've loaded everything we ever could, let's drill through
143 # the *ENTIRE* symtable and build a map of versions
144 my $version_list = { perl => $] };
145 visit_namespaces( action => sub {
146   my $pkg = shift;
147
148   # keep going, but nothing to see here
149   return 1 if $pkg eq 'main';
150
151   # private - not interested
152   return 0 if $pkg =~ / (?: ^ | :: ) _ /x;
153
154   no strict 'refs';
155   # that would be some synthetic class, or a custom sub VERSION
156   return 1 if (
157     ! defined ${"${pkg}::VERSION"}
158       or
159     ${"${pkg}::VERSION"} =~ /\Qset by base.pm/
160   );
161
162   # make sure a version can be extracted, be noisy when it doesn't work
163   # do this even if we are throwing away the result below in lieu of EUMM
164   my $mod_ver = eval { $pkg->VERSION };
165   if (my $err = $@) {
166     $err =~ s/^/  /mg;
167     say_err
168       "Calling `$pkg->VERSION` resulted in an exception, which should never "
169     . "happen - please file a bug with the distribution containing $pkg. "
170     . "Follows the full text of the exception:\n\n$err\n"
171     ;
172   }
173   elsif( ! defined $mod_ver ) {
174     say_err
175       "Calling `$pkg->VERSION` returned 'undef', which should never "
176     . "happen - please file a bug with the distribution containing $pkg."
177     ;
178
179   }
180   elsif( ! length $mod_ver ) {
181     say_err
182       "Calling `$pkg->VERSION` returned the empty string '', which should never "
183     . "happen - please file a bug with the distribution containing $pkg."
184     ;
185     undef $mod_ver;
186   }
187
188   # if this is a real file - extract the version via EUMM whenever possible
189   my $fn = $INC{module_notional_filename($pkg)};
190
191   my $eumm_ver = eval { MM->parse_version( $fn ) }
192     if $fn and  -f $fn and -r $fn;
193
194   if (
195     $has_versionpm
196       and
197     defined $eumm_ver
198       and
199     defined $mod_ver
200       and
201     $eumm_ver ne $mod_ver
202       and
203     (
204       ( eval { version->parse( do { (my $v = $eumm_ver) =~ s/_//g; $v } ) } || 0 )
205         !=
206       ( eval { version->parse( do { (my $v = $mod_ver) =~ s/_//g; $v } ) } || 0 )
207     )
208   ) {
209     say_err
210       "Mismatch of versions '$mod_ver' and '$eumm_ver', obtained respectively "
211     . "via `$pkg->VERSION` and parsing the version out of @{[ describe_fn $fn ]} "
212     . "with ExtUtils::MakeMaker\@@{[ ExtUtils::MakeMaker->VERSION ]}. "
213     . "This should never happen - please check whether this is still present "
214     . "in the latest version, and then file a bug with the distribution "
215     . "containing $pkg."
216     ;
217   }
218
219   if( defined $eumm_ver ) {
220     $version_list->{$pkg} = $eumm_ver;
221   }
222   elsif( defined $mod_ver ) {
223     $version_list->{$pkg} = $mod_ver;
224   }
225
226   1;
227 });
228
229 # compress identical versions as close to the root as we can
230 # unless we are dealing with a smoker - in which case we want
231 # to see every MD5 there is
232 unless ( $ENV{AUTOMATED_TESTING} ) {
233   for my $mod ( sort { length($b) <=> length($a) } keys %$version_list ) {
234     my $parent = $mod;
235
236     while ( $parent =~ s/ :: (?: . (?! :: ) )+ $ //x ) {
237       $version_list->{$parent}
238         and
239       $version_list->{$parent} eq $version_list->{$mod}
240         and
241       ( ( delete $version_list->{$mod} ) or 1 )
242         and
243       last
244     }
245   }
246 }
247
248 ok 1, (scalar keys %$version_list) . " distinctly versioned modules";
249
250 # do not announce anything under ci - we are watching for STDERR silence
251 exit if DBICTest::RunMode->is_ci;
252
253 # sort stuff into @INC segments
254 my $segments;
255
256 MODULE:
257 for my $mod ( sort { lc($a) cmp lc($b) } keys %$version_list ) {
258   my $fn = $INC{module_notional_filename($mod)};
259
260   my $tuple = [
261     $mod,
262     ( ( $fn && -f $fn && -r $fn ) ? $fn : undef )
263   ];
264
265
266   if ($fn) {
267     for my $lib (@lib_display_order, './lib') {
268       if ( $lib_paths->{$lib} and index($fn, $lib_paths->{$lib}) == 0 ) {
269         push @{$segments->{$lib}}, $tuple;
270         next MODULE;
271       }
272     }
273   }
274
275   # fallthrough for anything without a physical filename, or unknown lib
276   push @{$segments->{''}}, $tuple;
277 }
278
279 # diag the result out
280 my $max_ver_len = max map { length $_ } values %$version_list;
281 my $max_mod_len = max map { length $_ } keys %$version_list;
282
283 my $diag = "\n\nVersions of all loadable modules within the configure/build/test/runtime dependency chains present on this system (both core and optional)\n\n";
284 for my $seg ( '', @lib_display_order, './lib' ) {
285   next unless $segments->{$seg};
286
287   $diag .= sprintf "=== %s ===\n\n",
288     $seg
289       ? "Modules found in " . ( $Config{$seg} ? "\$Config{$seg}" : $seg )
290       : 'Misc'
291   ;
292
293   $diag .= sprintf (
294     "   %*s  %*s%s\n",
295     $max_ver_len => $version_list->{$_->[0]},
296     -$max_mod_len => $_->[0],
297     ($_->[1]
298       ? "  [ MD5: @{[ md5_of_fn( $_->[1] ) ]} ]"
299       : ''
300     ),
301   ) for @{$segments->{$seg}};
302
303   $diag .= "\n\n"
304 }
305
306 diag $diag;