67a9b26467c1df3334a6d0833275b0b0c5015d79
[dbsrgits/DBIx-Class.git] / xt / extra / lean_startup.t
1 # Use a require override instead of @INC munging (less common)
2 # Do the override as early as possible so that CORE::require doesn't get compiled away
3
4 BEGIN {
5   if ( $ENV{RELEASE_TESTING} ) {
6     require warnings and warnings->import;
7     require strict and strict->import;
8   }
9 }
10
11 my ($initial_inc_contents, $expected_dbic_deps, $require_sites, %stack);
12 BEGIN {
13   unshift @INC, 't/lib';
14   require DBICTest::Util::OverrideRequire;
15
16   DBICTest::Util::OverrideRequire::override_global_require( sub {
17     my $res = $_[0]->();
18
19     return $res if $stack{neutralize_override};
20
21     my $req = $_[1];
22     $req =~ s/\.pm$//;
23     $req =~ s/\//::/g;
24
25     my $up = 0;
26     my @caller;
27     do { @caller = CORE::caller($up++) } while (
28       @caller and (
29         # exclude our test suite, known "module require-rs" and eval frames
30         $caller[1] =~ / (?: \A | [\/\\] ) x?t [\/\\] /x
31           or
32         $caller[0] =~ /^ (?: base | parent | Class::C3::Componentised | Module::Inspector | Module::Runtime ) $/x
33           or
34         $caller[3] eq '(eval)',
35       )
36     );
37
38     push @{$require_sites->{$req}}, "$caller[1] line $caller[2]"
39       if @caller;
40
41     return $res if $req =~ /^DBIx::Class|^DBICTest::/;
42
43     # Some modules have a bare 'use $perl_version' as the first statement
44     # Since the use() happens before 'package' had a chance to switch
45     # the namespace, the shim thinks DBIC* tried to require this
46     return $res if $req =~ /^v?[0-9.]$/;
47
48     # exclude everything where the current namespace does not match the called function
49     # (this works around very weird XS-induced require callstack corruption)
50     if (
51       !$initial_inc_contents->{$req}
52         and
53       !$expected_dbic_deps->{$req}
54         and
55       @caller
56         and
57       $caller[0] =~ /^DBIx::Class/
58         and
59       (CORE::caller($up))[3] =~ /\Q$caller[0]/
60     ) {
61       local $stack{neutralize_override} = 1;
62
63       do 1 while CORE::caller(++$up);
64
65       require('Test/More.pm');
66       local $Test::Builder::Level = $up + 1;
67       Test::More::fail ("Unexpected require of '$req' by $caller[0] ($caller[1] line $caller[2])");
68
69       require('DBICTest/Util.pm');
70       Test::More::diag( 'Require invoked' .  DBICTest::Util::stacktrace() );
71     }
72
73     return $res;
74   });
75 }
76
77 use strict;
78 use warnings;
79 use Test::More;
80
81 BEGIN {
82   plan skip_all => 'A defined PERL5OPT may inject extra deps crashing this test'
83     if $ENV{PERL5OPT};
84
85   plan skip_all => 'Presence of sitecustomize.pl may inject extra deps crashing this test'
86     if grep { $_ =~ m| \/ sitecustomize\.pl $ |x } keys %INC;
87
88   plan skip_all => 'Dependency load patterns are radically different before perl 5.10'
89     if "$]" < 5.010;
90
91   plan skip_all => 'Dependency load patterns may be different on cperl - skip for now'
92     if $^V =~ /\d+c$/;
93
94   # these envvars *will* bring in more stuff than the baseline
95   delete @ENV{qw(
96     DBIC_TRACE
97     DBIC_SHUFFLE_UNORDERED_RESULTSETS
98     DBICTEST_SQLT_DEPLOY
99     DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER
100     DBICTEST_VIA_REPLICATED
101     DBICTEST_DEBUG_CONCURRENCY_LOCKS
102   )};
103
104   $ENV{DBICTEST_ANFANG_DEFANG} = 1;
105
106   # make sure extras do not load even when this is set
107   $ENV{PERL_STRICTURES_EXTRA} = 1;
108
109   # add what we loaded so far
110   for (keys %INC) {
111     my $mod = $_;
112     $mod =~ s/\.pm$//;
113     $mod =~ s!\/!::!g;
114     $initial_inc_contents->{$mod} = 1;
115   }
116 }
117
118
119 #######
120 ### This is where the test starts
121 #######
122
123 # checking base schema load, no storage no connection
124 {
125   register_lazy_loadable_requires(qw(
126     B
127     constant
128     overload
129
130     base
131     Devel::GlobalDestruction
132     mro
133
134     Carp
135     namespace::clean
136     Try::Tiny
137     Sub::Name
138     Sub::Defer
139     Sub::Quote
140     attributes
141     File::Spec
142
143     Scalar::Util
144     Storable
145
146     Class::Accessor::Grouped
147     Class::C3::Componentised
148   ));
149
150   require DBIx::Class::Schema;
151   assert_no_missing_expected_requires();
152 }
153
154 # check schema/storage instantiation with no connect
155 {
156   register_lazy_loadable_requires(qw(
157     Moo
158     Moo::Object
159     Method::Generate::Accessor
160     Method::Generate::Constructor
161     Context::Preserve
162     SQL::Abstract
163   ));
164
165   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
166   ok (! $s->storage->connected, 'no connection');
167   assert_no_missing_expected_requires();
168 }
169
170 # do something (deploy, insert)
171 {
172   register_lazy_loadable_requires(qw(
173     DBI
174     Hash::Merge
175   ));
176
177   {
178     eval <<'EOP' or die $@;
179
180   package DBICTest::Result::Artist;
181
182   use warnings;
183   use strict;
184
185   use base 'DBIx::Class::Core';
186
187   __PACKAGE__->table("artist");
188
189   __PACKAGE__->add_columns(
190     artistid => {
191       data_type => 'integer',
192       is_auto_increment => 1,
193     },
194     name => {
195       data_type => 'varchar',
196       size      => 100,
197       is_nullable => 1,
198     },
199     rank => {
200       data_type => 'integer',
201       default_value => 13,
202     },
203     charfield => {
204       data_type => 'char',
205       size => 10,
206       is_nullable => 1,
207     },
208   );
209
210   __PACKAGE__->set_primary_key('artistid');
211   __PACKAGE__->add_unique_constraint(['name']);
212   __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
213
214   1;
215
216 EOP
217   }
218
219   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
220
221   $s->register_class( Artist => 'DBICTest::Result::Artist' );
222
223   $s->storage->dbh_do(sub {
224     $_[1]->do('CREATE TABLE artist (
225       "artistid" INTEGER PRIMARY KEY NOT NULL,
226       "name" varchar(100),
227       "rank" integer NOT NULL DEFAULT 13,
228       "charfield" char(10)
229     )');
230   });
231
232   my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
233   $art->discard_changes;
234   $art->update({ rank => 69, name => 'foo' });
235   $s->resultset('Artist')->all;
236   assert_no_missing_expected_requires();
237 }
238
239
240 # and do full DBICTest based populate() as well, just in case - shouldn't add new stuff
241 {
242   # DBICTest needs File::Spec, but older versions of Storable load it alread
243   # Instead of adding a contrived conditional, just preempt the testing entirely
244   require File::Spec;
245
246   require DBICTest;
247   DBICTest->import;
248
249   my $s = DBICTest->init_schema;
250   is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae', 'Expected find() result');
251 }
252
253 done_testing;
254 # one final quiet guard to run at all times
255 END { assert_no_missing_expected_requires('quiet') };
256
257 sub register_lazy_loadable_requires {
258   local $Test::Builder::Level = $Test::Builder::Level + 1;
259
260   for my $mod (@_) {
261     (my $modfn = "$mod.pm") =~ s!::!\/!g;
262     fail(join "\n",
263       "Module $mod already loaded by require site(s):",
264       (map { "\t$_" } @{$require_sites->{$mod}}),
265       '',
266     ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
267
268     $expected_dbic_deps->{$mod}++
269   }
270 }
271
272 # check if anything we were expecting didn't actually load
273 sub assert_no_missing_expected_requires {
274   my $quiet = shift;
275
276   for my $mod (keys %$expected_dbic_deps) {
277     (my $modfn = "$mod.pm") =~ s/::/\//g;
278     fail sprintf (
279       "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
280       $mod,
281       __FILE__
282     ) unless $INC{$modfn};
283   }
284
285   pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
286     __FILE__,
287     (caller(0))[2],
288     join (', ', sort keys %$expected_dbic_deps ),
289   ) unless $quiet;
290 }