Revert TempExtlib ( b46b85376 ) - new Sub::Quote shipped
[dbsrgits/DBIx-Class.git] / xt / extra / lean_startup.t
CommitLineData
3b80fa31 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
3b80fa31 3
92fbedbc 4BEGIN {
5 if ( $ENV{RELEASE_TESTING} ) {
6 require warnings and warnings->import;
7 require strict and strict->import;
8 }
9}
10
11my ($initial_inc_contents, $expected_dbic_deps, $require_sites, %stack);
3b80fa31 12BEGIN {
45638aed 13 unshift @INC, 't/lib';
14 require DBICTest::Util::OverrideRequire;
15
16 DBICTest::Util::OverrideRequire::override_global_require( sub {
17 my $res = $_[0]->();
8c49cf15 18
92fbedbc 19 return $res if $stack{neutralize_override};
20
8c49cf15 21 my $req = $_[1];
22 $req =~ s/\.pm$//;
23 $req =~ s/\//::/g;
24
25 my $up = 0;
26 my @caller;
92fbedbc 27 do { @caller = CORE::caller($up++) } while (
8c49cf15 28 @caller and (
29 # exclude our test suite, known "module require-rs" and eval frames
92fbedbc 30 $caller[1] =~ / (?: \A | [\/\\] ) x?t [\/\\] /x
8c49cf15 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
e8452b02 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
397056f9 46 return $res if $req =~ /^v?[0-9.]+$/;
92fbedbc 47
8c49cf15 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
92fbedbc 59 (CORE::caller($up))[3] =~ /\Q$caller[0]/
8c49cf15 60 ) {
92fbedbc 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;
8c49cf15 67 Test::More::fail ("Unexpected require of '$req' by $caller[0] ($caller[1] line $caller[2])");
68
92fbedbc 69 require('DBICTest/Util.pm');
554484cb 70 Test::More::diag( 'Require invoked' . DBICTest::Util::stacktrace() );
8c49cf15 71 }
72
45638aed 73 return $res;
74 });
3b80fa31 75}
76
77use strict;
78use warnings;
79use Test::More;
80
45638aed 81BEGIN {
8c49cf15 82 plan skip_all => 'A defined PERL5OPT may inject extra deps crashing this test'
83 if $ENV{PERL5OPT};
84
26710bc9 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
8c49cf15 88 plan skip_all => 'Dependency load patterns are radically different before perl 5.10'
750a4ad2 89 if "$]" < 5.010;
8c49cf15 90
26710bc9 91 # these envvars *will* bring in more stuff than the baseline
92 delete @ENV{qw(
93 DBIC_TRACE
58b92e31 94 DBIC_SHUFFLE_UNORDERED_RESULTSETS
26710bc9 95 DBICTEST_SQLT_DEPLOY
92fbedbc 96 DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER
26710bc9 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
8c49cf15 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 }
45638aed 113}
114
8b60b921 115
8c49cf15 116#######
117### This is where the test starts
118#######
3b80fa31 119
8c49cf15 120# checking base schema load, no storage no connection
121{
122 register_lazy_loadable_requires(qw(
123 B
0d8817bc 124 constant
8c49cf15 125 overload
0d8817bc 126
3b80fa31 127 base
8c49cf15 128 Devel::GlobalDestruction
3b80fa31 129 mro
3b80fa31 130
8c49cf15 131 Carp
3b80fa31 132 namespace::clean
133 Try::Tiny
134 Sub::Name
cbd7f87a 135 Sub::Defer
7f9a3f70 136 Sub::Quote
140bcb6a 137 attributes
3b80fa31 138
139 Scalar::Util
d7d45bdc 140 Storable
3b80fa31 141
3b80fa31 142 Class::Accessor::Grouped
143 Class::C3::Componentised
8c49cf15 144 ));
3b80fa31 145
92fbedbc 146 require DBIx::Class::Schema;
8c49cf15 147 assert_no_missing_expected_requires();
148}
3b80fa31 149
8c49cf15 150# check schema/storage instantiation with no connect
151{
152 register_lazy_loadable_requires(qw(
153 Moo
cbd7f87a 154 Moo::Object
155 Method::Generate::Accessor
156 Method::Generate::Constructor
8c49cf15 157 Context::Preserve
92fbedbc 158 SQL::Abstract
8c49cf15 159 ));
3b80fa31 160
92fbedbc 161 my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
8c49cf15 162 ok (! $s->storage->connected, 'no connection');
163 assert_no_missing_expected_requires();
164}
3b80fa31 165
8c49cf15 166# do something (deploy, insert)
167{
168 register_lazy_loadable_requires(qw(
169 DBI
8c49cf15 170 Hash::Merge
171 ));
172
92fbedbc 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
212EOP
213 }
214
215 my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
216
217 $s->register_class( Artist => 'DBICTest::Result::Artist' );
218
8c49cf15 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 });
3b80fa31 227
8c49cf15 228 my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
229 $art->discard_changes;
230 $art->update({ rank => 69, name => 'foo' });
58b92e31 231 $s->resultset('Artist')->all;
8c49cf15 232 assert_no_missing_expected_requires();
233}
6a9e3dd5 234
92fbedbc 235
236# and do full DBICTest based populate() as well, just in case - shouldn't add new stuff
8c49cf15 237{
92fbedbc 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
8c49cf15 245 my $s = DBICTest->init_schema;
92fbedbc 246 is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae', 'Expected find() result');
3b80fa31 247}
248
8c49cf15 249done_testing;
92fbedbc 250# one final quiet guard to run at all times
251END { assert_no_missing_expected_requires('quiet') };
3b80fa31 252
8c49cf15 253sub register_lazy_loadable_requires {
254 local $Test::Builder::Level = $Test::Builder::Level + 1;
3b80fa31 255
8c49cf15 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}
3b80fa31 267
f873b733 268# check if anything we were expecting didn't actually load
8c49cf15 269sub assert_no_missing_expected_requires {
92fbedbc 270 my $quiet = shift;
271
8c49cf15 272 for my $mod (keys %$expected_dbic_deps) {
273 (my $modfn = "$mod.pm") =~ s/::/\//g;
554484cb 274 fail sprintf (
275 "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
276 $mod,
277 __FILE__
278 ) unless $INC{$modfn};
f873b733 279 }
92fbedbc 280
8c49cf15 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 ),
92fbedbc 285 ) unless $quiet;
f873b733 286}