Revert TempExtlib ( b46b85376 ) - new Sub::Quote shipped
[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
139     Scalar::Util
140     Storable
141
142     Class::Accessor::Grouped
143     Class::C3::Componentised
144   ));
145
146   require DBIx::Class::Schema;
147   assert_no_missing_expected_requires();
148 }
149
150 # check schema/storage instantiation with no connect
151 {
152   register_lazy_loadable_requires(qw(
153     Moo
154     Moo::Object
155     Method::Generate::Accessor
156     Method::Generate::Constructor
157     Context::Preserve
158     SQL::Abstract
159   ));
160
161   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
162   ok (! $s->storage->connected, 'no connection');
163   assert_no_missing_expected_requires();
164 }
165
166 # do something (deploy, insert)
167 {
168   register_lazy_loadable_requires(qw(
169     DBI
170     Hash::Merge
171   ));
172
173   {
174     eval <<'EOP' or die $@;
175
176   package DBICTest::Result::Artist;
177
178   use warnings;
179   use strict;
180
181   use base 'DBIx::Class::Core';
182
183   __PACKAGE__->table("artist");
184
185   __PACKAGE__->add_columns(
186     artistid => {
187       data_type => 'integer',
188       is_auto_increment => 1,
189     },
190     name => {
191       data_type => 'varchar',
192       size      => 100,
193       is_nullable => 1,
194     },
195     rank => {
196       data_type => 'integer',
197       default_value => 13,
198     },
199     charfield => {
200       data_type => 'char',
201       size => 10,
202       is_nullable => 1,
203     },
204   );
205
206   __PACKAGE__->set_primary_key('artistid');
207   __PACKAGE__->add_unique_constraint(['name']);
208   __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
209
210   1;
211
212 EOP
213   }
214
215   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
216
217   $s->register_class( Artist => 'DBICTest::Result::Artist' );
218
219   $s->storage->dbh_do(sub {
220     $_[1]->do('CREATE TABLE artist (
221       "artistid" INTEGER PRIMARY KEY NOT NULL,
222       "name" varchar(100),
223       "rank" integer NOT NULL DEFAULT 13,
224       "charfield" char(10)
225     )');
226   });
227
228   my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
229   $art->discard_changes;
230   $art->update({ rank => 69, name => 'foo' });
231   $s->resultset('Artist')->all;
232   assert_no_missing_expected_requires();
233 }
234
235
236 # and do full DBICTest based populate() as well, just in case - shouldn't add new stuff
237 {
238   # DBICTest needs File::Spec, but older versions of Storable load it alread
239   # Instead of adding a contrived conditional, just preempt the testing entirely
240   require File::Spec;
241
242   require DBICTest;
243   DBICTest->import;
244
245   my $s = DBICTest->init_schema;
246   is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae', 'Expected find() result');
247 }
248
249 done_testing;
250 # one final quiet guard to run at all times
251 END { assert_no_missing_expected_requires('quiet') };
252
253 sub register_lazy_loadable_requires {
254   local $Test::Builder::Level = $Test::Builder::Level + 1;
255
256   for my $mod (@_) {
257     (my $modfn = "$mod.pm") =~ s!::!\/!g;
258     fail(join "\n",
259       "Module $mod already loaded by require site(s):",
260       (map { "\t$_" } @{$require_sites->{$mod}}),
261       '',
262     ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
263
264     $expected_dbic_deps->{$mod}++
265   }
266 }
267
268 # check if anything we were expecting didn't actually load
269 sub assert_no_missing_expected_requires {
270   my $quiet = shift;
271
272   for my $mod (keys %$expected_dbic_deps) {
273     (my $modfn = "$mod.pm") =~ s/::/\//g;
274     fail sprintf (
275       "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
276       $mod,
277       __FILE__
278     ) unless $INC{$modfn};
279   }
280
281   pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
282     __FILE__,
283     (caller(0))[2],
284     join (', ', sort keys %$expected_dbic_deps ),
285   ) unless $quiet;
286 }