Do not load DBIC::Optional::Dependencies at runtime unless we need to
[dbsrgits/DBIx-Class.git] / xt / extra / lean_startup.t
CommitLineData
c0329273 1BEGIN { $ENV{DBICTEST_ANFANG_DEFANG} = 1 }
2
3b80fa31 3# Use a require override instead of @INC munging (less common)
4# Do the override as early as possible so that CORE::require doesn't get compiled away
3b80fa31 5
8c49cf15 6my ($initial_inc_contents, $expected_dbic_deps, $require_sites);
3b80fa31 7BEGIN {
8c49cf15 8 # these envvars *will* bring in more stuff than the baseline
9 delete @ENV{qw(DBICTEST_SQLT_DEPLOY DBIC_TRACE)};
10
cbd7f87a 11 # make sure extras do not load even when this is set
12 $ENV{PERL_STRICTURES_EXTRA} = 1;
13
45638aed 14 unshift @INC, 't/lib';
15 require DBICTest::Util::OverrideRequire;
16
17 DBICTest::Util::OverrideRequire::override_global_require( sub {
18 my $res = $_[0]->();
8c49cf15 19
20 my $req = $_[1];
21 $req =~ s/\.pm$//;
22 $req =~ s/\//::/g;
23
24 my $up = 0;
25 my @caller;
26 do { @caller = caller($up++) } while (
27 @caller and (
28 # exclude our test suite, known "module require-rs" and eval frames
29 $caller[1] =~ /^ t [\/\\] /x
30 or
31 $caller[0] =~ /^ (?: base | parent | Class::C3::Componentised | Module::Inspector | Module::Runtime ) $/x
32 or
33 $caller[3] eq '(eval)',
34 )
35 );
36
37 push @{$require_sites->{$req}}, "$caller[1] line $caller[2]"
38 if @caller;
39
40 return $res if $req =~ /^DBIx::Class|^DBICTest::/;
41
42 # exclude everything where the current namespace does not match the called function
43 # (this works around very weird XS-induced require callstack corruption)
44 if (
45 !$initial_inc_contents->{$req}
46 and
47 !$expected_dbic_deps->{$req}
48 and
49 @caller
50 and
51 $caller[0] =~ /^DBIx::Class/
52 and
53 (caller($up))[3] =~ /\Q$caller[0]/
54 ) {
55 CORE::require('Test/More.pm');
56 Test::More::fail ("Unexpected require of '$req' by $caller[0] ($caller[1] line $caller[2])");
57
554484cb 58 CORE::require('DBICTest/Util.pm');
59 Test::More::diag( 'Require invoked' . DBICTest::Util::stacktrace() );
8c49cf15 60 }
61
45638aed 62 return $res;
63 });
3b80fa31 64}
65
66use strict;
67use warnings;
68use Test::More;
69
45638aed 70BEGIN {
8c49cf15 71 plan skip_all => 'A defined PERL5OPT may inject extra deps crashing this test'
72 if $ENV{PERL5OPT};
73
74 plan skip_all => 'Dependency load patterns are radically different before perl 5.10'
750a4ad2 75 if "$]" < 5.010;
8c49cf15 76
77 # add what we loaded so far
78 for (keys %INC) {
79 my $mod = $_;
80 $mod =~ s/\.pm$//;
81 $mod =~ s!\/!::!g;
82 $initial_inc_contents->{$mod} = 1;
83 }
45638aed 84}
85
072b62c4 86BEGIN {
87 delete $ENV{$_} for qw(
88 DBICTEST_VIA_REPLICATED
89 DBICTEST_DEBUG_CONCURRENCY_LOCKS
90 );
91}
8b60b921 92
8c49cf15 93#######
94### This is where the test starts
95#######
3b80fa31 96
8c49cf15 97# checking base schema load, no storage no connection
98{
99 register_lazy_loadable_requires(qw(
100 B
0d8817bc 101 constant
8c49cf15 102 overload
0d8817bc 103
3b80fa31 104 base
8c49cf15 105 Devel::GlobalDestruction
3b80fa31 106 mro
3b80fa31 107
8c49cf15 108 Carp
3b80fa31 109 namespace::clean
110 Try::Tiny
111 Sub::Name
cbd7f87a 112 Sub::Defer
7f9a3f70 113 Sub::Quote
3b80fa31 114
115 Scalar::Util
116 List::Util
d7d45bdc 117 Storable
3b80fa31 118
3b80fa31 119 Class::Accessor::Grouped
120 Class::C3::Componentised
b5ce6748 121 SQL::Abstract
8c49cf15 122 ));
3b80fa31 123
8c49cf15 124 require DBICTest::Schema;
125 assert_no_missing_expected_requires();
126}
3b80fa31 127
8c49cf15 128# check schema/storage instantiation with no connect
129{
130 register_lazy_loadable_requires(qw(
131 Moo
cbd7f87a 132 Moo::Object
133 Method::Generate::Accessor
134 Method::Generate::Constructor
8c49cf15 135 Context::Preserve
136 ));
3b80fa31 137
8c49cf15 138 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
139 ok (! $s->storage->connected, 'no connection');
140 assert_no_missing_expected_requires();
141}
3b80fa31 142
8c49cf15 143# do something (deploy, insert)
144{
145 register_lazy_loadable_requires(qw(
146 DBI
8c49cf15 147 Hash::Merge
148 ));
149
150 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
151 $s->storage->dbh_do(sub {
152 $_[1]->do('CREATE TABLE artist (
153 "artistid" INTEGER PRIMARY KEY NOT NULL,
154 "name" varchar(100),
155 "rank" integer NOT NULL DEFAULT 13,
156 "charfield" char(10)
157 )');
158 });
3b80fa31 159
8c49cf15 160 my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
161 $art->discard_changes;
162 $art->update({ rank => 69, name => 'foo' });
163 assert_no_missing_expected_requires();
164}
6a9e3dd5 165
8c49cf15 166# and do full populate() as well, just in case - shouldn't add new stuff
167{
4a24dba9 168 local $ENV{DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER};
5e724964 169 {
170 # in general we do not want DBICTest to load before sqla, but it is
171 # ok to cheat here
172 local $INC{'SQL/Abstract.pm'};
173 require DBICTest;
174 }
8c49cf15 175 my $s = DBICTest->init_schema;
fb88ca2c 176 is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae');
8c49cf15 177 assert_no_missing_expected_requires();
3b80fa31 178}
179
8c49cf15 180done_testing;
3b80fa31 181
8c49cf15 182sub register_lazy_loadable_requires {
183 local $Test::Builder::Level = $Test::Builder::Level + 1;
3b80fa31 184
8c49cf15 185 for my $mod (@_) {
186 (my $modfn = "$mod.pm") =~ s!::!\/!g;
187 fail(join "\n",
188 "Module $mod already loaded by require site(s):",
189 (map { "\t$_" } @{$require_sites->{$mod}}),
190 '',
191 ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
192
193 $expected_dbic_deps->{$mod}++
194 }
195}
3b80fa31 196
f873b733 197# check if anything we were expecting didn't actually load
8c49cf15 198sub assert_no_missing_expected_requires {
199 my $nl;
200 for my $mod (keys %$expected_dbic_deps) {
201 (my $modfn = "$mod.pm") =~ s/::/\//g;
554484cb 202 fail sprintf (
203 "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
204 $mod,
205 __FILE__
206 ) unless $INC{$modfn};
f873b733 207 }
8c49cf15 208 pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
209 __FILE__,
210 (caller(0))[2],
211 join (', ', sort keys %$expected_dbic_deps ),
212 ) unless $nl;
f873b733 213}