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