7c5df0a3e6a40972a8b5b7fdeaf5789871a9ce45
[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   # these envvars *will* bring in more stuff than the baseline
92   delete @ENV{qw(
93     DBIC_TRACE
94     DBIC_SHUFFLE_UNORDERED_RESULTSETS
95     DBICTEST_SQLT_DEPLOY
96     DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER
97     DBICTEST_VIA_REPLICATED
98     DBICTEST_DEBUG_CONCURRENCY_LOCKS
99   )};
100
101   $ENV{DBICTEST_ANFANG_DEFANG} = 1;
102
103   # make sure extras do not load even when this is set
104   $ENV{PERL_STRICTURES_EXTRA} = 1;
105
106   # add what we loaded so far
107   for (keys %INC) {
108     my $mod = $_;
109     $mod =~ s/\.pm$//;
110     $mod =~ s!\/!::!g;
111     $initial_inc_contents->{$mod} = 1;
112   }
113 }
114
115
116 #######
117 ### This is where the test starts
118 #######
119
120 # checking base schema load, no storage no connection
121 {
122   register_lazy_loadable_requires(qw(
123     B
124     constant
125     overload
126
127     base
128     Devel::GlobalDestruction
129     mro
130
131     Carp
132     namespace::clean
133     Try::Tiny
134     Sub::Name
135     Sub::Defer
136     Sub::Quote
137     attributes
138     File::Spec
139
140     Scalar::Util
141     Storable
142
143     Class::Accessor::Grouped
144     Class::C3::Componentised
145   ));
146
147   require DBIx::Class::Schema;
148   assert_no_missing_expected_requires();
149 }
150
151 # check schema/storage instantiation with no connect
152 {
153   register_lazy_loadable_requires(qw(
154     Moo
155     Moo::Object
156     Method::Generate::Accessor
157     Method::Generate::Constructor
158     Context::Preserve
159     SQL::Abstract
160   ));
161
162   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
163   ok (! $s->storage->connected, 'no connection');
164   assert_no_missing_expected_requires();
165 }
166
167 # do something (deploy, insert)
168 {
169   register_lazy_loadable_requires(qw(
170     DBI
171     Hash::Merge
172   ));
173
174   {
175     eval <<'EOP' or die $@;
176
177   package DBICTest::Result::Artist;
178
179   use warnings;
180   use strict;
181
182   use base 'DBIx::Class::Core';
183
184   __PACKAGE__->table("artist");
185
186   __PACKAGE__->add_columns(
187     artistid => {
188       data_type => 'integer',
189       is_auto_increment => 1,
190     },
191     name => {
192       data_type => 'varchar',
193       size      => 100,
194       is_nullable => 1,
195     },
196     rank => {
197       data_type => 'integer',
198       default_value => 13,
199     },
200     charfield => {
201       data_type => 'char',
202       size => 10,
203       is_nullable => 1,
204     },
205   );
206
207   __PACKAGE__->set_primary_key('artistid');
208   __PACKAGE__->add_unique_constraint(['name']);
209   __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
210
211   1;
212
213 EOP
214   }
215
216   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
217
218   $s->register_class( Artist => 'DBICTest::Result::Artist' );
219
220   $s->storage->dbh_do(sub {
221     $_[1]->do('CREATE TABLE artist (
222       "artistid" INTEGER PRIMARY KEY NOT NULL,
223       "name" varchar(100),
224       "rank" integer NOT NULL DEFAULT 13,
225       "charfield" char(10)
226     )');
227   });
228
229   my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
230   $art->discard_changes;
231   $art->update({ rank => 69, name => 'foo' });
232   $s->resultset('Artist')->all;
233   assert_no_missing_expected_requires();
234 }
235
236
237 # and do full DBICTest based populate() as well, just in case - shouldn't add new stuff
238 {
239   # DBICTest needs File::Spec, but older versions of Storable load it alread
240   # Instead of adding a contrived conditional, just preempt the testing entirely
241   require File::Spec;
242
243   require DBICTest;
244   DBICTest->import;
245
246   my $s = DBICTest->init_schema;
247   is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae', 'Expected find() result');
248 }
249
250 done_testing;
251 # one final quiet guard to run at all times
252 END { assert_no_missing_expected_requires('quiet') };
253
254 sub register_lazy_loadable_requires {
255   local $Test::Builder::Level = $Test::Builder::Level + 1;
256
257   for my $mod (@_) {
258     (my $modfn = "$mod.pm") =~ s!::!\/!g;
259     fail(join "\n",
260       "Module $mod already loaded by require site(s):",
261       (map { "\t$_" } @{$require_sites->{$mod}}),
262       '',
263     ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
264
265     $expected_dbic_deps->{$mod}++
266   }
267 }
268
269 # check if anything we were expecting didn't actually load
270 sub assert_no_missing_expected_requires {
271   my $quiet = shift;
272
273   for my $mod (keys %$expected_dbic_deps) {
274     (my $modfn = "$mod.pm") =~ s/::/\//g;
275     fail sprintf (
276       "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
277       $mod,
278       __FILE__
279     ) unless $INC{$modfn};
280   }
281
282   pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
283     __FILE__,
284     (caller(0))[2],
285     join (', ', sort keys %$expected_dbic_deps ),
286   ) unless $quiet;
287 }