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