f915819de49fc1dd8057f58529ee1c2696db7438
[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 | DBIx::Class::Optional::Dependencies ) $/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     if (
49       !$initial_inc_contents->{$req}
50         and
51       !$expected_dbic_deps->{$req}
52         and
53       @caller
54         and
55       $caller[0] =~ /^DBIx::Class/
56     ) {
57       local $stack{neutralize_override} = 1;
58
59       # find last-most frame, to feed to T::B below
60       while( CORE::caller(++$up) ) { 1 }
61
62       require('Test/More.pm');
63       local $Test::Builder::Level = $up + 1;
64
65       # work around the trainwreck that is https://github.com/doy/package-stash-xs/pull/4
66       local $::TODO = 'sigh' if (
67         $INC{'Package/Stash/XS.pm'}
68           and
69         $req eq 'utf8'
70       );
71
72       Test::More::fail ("Unexpected require of '$req' by $caller[0] ($caller[1] line $caller[2])");
73
74       unless( $::TODO ) {
75         require('DBICTest/Util.pm');
76         Test::More::diag( 'Require invoked' .  DBICTest::Util::stacktrace() );
77       }
78     }
79
80     return $res;
81   });
82 }
83
84 use strict;
85 use warnings;
86 use Test::More;
87
88 BEGIN {
89   plan skip_all => 'A defined PERL5OPT may inject extra deps crashing this test'
90     if $ENV{PERL5OPT};
91
92   plan skip_all => 'Presence of sitecustomize.pl may inject extra deps crashing this test'
93     if grep { $_ =~ m| \/ sitecustomize\.pl $ |x } keys %INC;
94
95   plan skip_all => 'Dependency load patterns are radically different before perl 5.10'
96     if "$]" < 5.010;
97
98   # these envvars *will* bring in more stuff than the baseline
99   delete @ENV{qw(
100     DBIC_TRACE
101     DBIC_SHUFFLE_UNORDERED_RESULTSETS
102     DBICTEST_SQLT_DEPLOY
103     DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER
104     DBICTEST_VIA_REPLICATED
105     DBICTEST_DEBUG_CONCURRENCY_LOCKS
106   )};
107
108   # ensures the checker won't be disabled in
109   # t/lib/DBICTest/BaseSchema.pm
110   $ENV{DBIC_ASSERT_NO_FAILING_SANITY_CHECKS} = 1;
111
112   $ENV{DBICTEST_ANFANG_DEFANG} = 1;
113
114   # make sure extras do not load even when this is set
115   $ENV{PERL_STRICTURES_EXTRA} = 1;
116
117   # add what we loaded so far
118   for (keys %INC) {
119     my $mod = $_;
120     $mod =~ s/\.pm$//;
121     $mod =~ s!\/!::!g;
122     $initial_inc_contents->{$mod} = 1;
123   }
124 }
125
126
127 #######
128 ### This is where the test starts
129 #######
130
131 # checking base schema load, no storage no connection
132 {
133   register_lazy_loadable_requires(qw(
134     B
135     constant
136     overload
137
138     base
139     Devel::GlobalDestruction
140     mro
141
142     Carp
143     namespace::clean
144     Try::Tiny
145     Sub::Name
146     Sub::Defer
147     Sub::Quote
148     attributes
149
150     Scalar::Util
151     Storable
152
153     Class::Accessor::Grouped
154     Class::C3::Componentised
155   ));
156
157   # load Storable ourselves here - there are too many
158   # variations with DynaLoader and XSLoader making testing
159   # for it rather unstable
160   require Storable;
161
162   require DBIx::Class::Schema;
163   assert_no_missing_expected_requires();
164 }
165
166 # check schema/storage instantiation with no connect
167 {
168   register_lazy_loadable_requires(qw(
169     Moo
170     Moo::Object
171     Method::Generate::Accessor
172     Method::Generate::Constructor
173     Context::Preserve
174     SQL::Abstract
175   ));
176
177   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
178   ok (! $s->storage->connected, 'no connection');
179   assert_no_missing_expected_requires();
180 }
181
182 # do something (deploy, insert)
183 {
184   register_lazy_loadable_requires(qw(
185     DBI
186     Hash::Merge
187     Data::Dumper
188   ));
189
190   {
191     eval <<'EOP' or die $@;
192
193   package DBICTest::Result::Artist;
194
195   use warnings;
196   use strict;
197
198   use base 'DBIx::Class::Core';
199
200   __PACKAGE__->table("artist");
201
202   __PACKAGE__->add_columns(
203     artistid => {
204       data_type => 'integer',
205       is_auto_increment => 1,
206     },
207     name => {
208       data_type => 'varchar',
209       size      => 100,
210       is_nullable => 1,
211     },
212     rank => {
213       data_type => 'integer',
214       default_value => 13,
215     },
216     charfield => {
217       data_type => 'char',
218       size => 10,
219       is_nullable => 1,
220     },
221   );
222
223   __PACKAGE__->set_primary_key('artistid');
224   __PACKAGE__->add_unique_constraint(['name']);
225   __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
226
227   1;
228
229 EOP
230   }
231
232   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
233
234   $s->register_class( Artist => 'DBICTest::Result::Artist' );
235
236   $s->storage->dbh_do(sub {
237     $_[1]->do('CREATE TABLE artist (
238       "artistid" INTEGER PRIMARY KEY NOT NULL,
239       "name" varchar(100),
240       "rank" integer NOT NULL DEFAULT 13,
241       "charfield" char(10)
242     )');
243   });
244
245   my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
246   $art->discard_changes;
247   $art->update({ rank => 69, name => 'foo' });
248   $s->resultset('Artist')->all;
249   assert_no_missing_expected_requires();
250 }
251
252
253 # and do full DBICTest based populate() as well, just in case - shouldn't add new stuff
254 {
255   # DBICTest needs File::Spec, but older versions of Storable load it alread
256   # Instead of adding a contrived conditional, just preempt the testing entirely
257   require File::Spec;
258
259   require DBICTest;
260   DBICTest->import;
261
262   my $s = DBICTest->init_schema;
263   is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae', 'Expected find() result');
264 }
265
266 done_testing;
267 # one final quiet guard to run at all times
268 END { assert_no_missing_expected_requires('quiet') };
269
270 sub register_lazy_loadable_requires {
271   local $Test::Builder::Level = $Test::Builder::Level + 1;
272
273   for my $mod (@_) {
274     (my $modfn = "$mod.pm") =~ s!::!\/!g;
275     fail(join "\n",
276       "Module $mod already loaded by require site(s):",
277       (map { "\t$_" } @{$require_sites->{$mod}}),
278       '',
279     ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
280
281     $expected_dbic_deps->{$mod}++
282   }
283 }
284
285 # check if anything we were expecting didn't actually load
286 sub assert_no_missing_expected_requires {
287   my $quiet = shift;
288
289   for my $mod (keys %$expected_dbic_deps) {
290     (my $modfn = "$mod.pm") =~ s/::/\//g;
291     fail sprintf (
292       "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
293       $mod,
294       __FILE__
295     ) unless $INC{$modfn};
296   }
297
298   pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
299     __FILE__,
300     (caller(0))[2],
301     join (', ', sort keys %$expected_dbic_deps ),
302   ) unless $quiet;
303 }