Move a number of tests to xt, restructure extra lists
[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
ba7892a8 56 if ( $ENV{TEST_VERBOSE} or ! DBICTest::RunMode->is_plain ) {
8c49cf15 57 CORE::require('DBICTest/Util.pm');
58 Test::More::diag( 'Require invoked' . DBICTest::Util::stacktrace() );
59 }
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'
75 if $] < 5.010;
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
8c49cf15 86#######
87### This is where the test starts
88#######
3b80fa31 89
8c49cf15 90# checking base schema load, no storage no connection
91{
92 register_lazy_loadable_requires(qw(
93 B
0d8817bc 94 constant
8c49cf15 95 overload
0d8817bc 96
cb551b07 97 if
3b80fa31 98 base
8c49cf15 99 Devel::GlobalDestruction
3b80fa31 100 mro
3b80fa31 101
8c49cf15 102 Carp
3b80fa31 103 namespace::clean
104 Try::Tiny
105 Sub::Name
ba0e8d1c 106 strictures
cbd7f87a 107 Sub::Defer
7f9a3f70 108 Sub::Quote
3b80fa31 109
110 Scalar::Util
111 List::Util
d7d45bdc 112 Storable
3b80fa31 113
3b80fa31 114 Class::Accessor::Grouped
115 Class::C3::Componentised
b5ce6748 116 SQL::Abstract
8c49cf15 117 ));
3b80fa31 118
8c49cf15 119 require DBICTest::Schema;
120 assert_no_missing_expected_requires();
121}
3b80fa31 122
8c49cf15 123# check schema/storage instantiation with no connect
124{
125 register_lazy_loadable_requires(qw(
126 Moo
cbd7f87a 127 Moo::Object
128 Method::Generate::Accessor
129 Method::Generate::Constructor
8c49cf15 130 Context::Preserve
131 ));
3b80fa31 132
8c49cf15 133 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
134 ok (! $s->storage->connected, 'no connection');
135 assert_no_missing_expected_requires();
136}
3b80fa31 137
8c49cf15 138# do something (deploy, insert)
139{
140 register_lazy_loadable_requires(qw(
141 DBI
8c49cf15 142 Hash::Merge
143 ));
144
145 my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
146 $s->storage->dbh_do(sub {
147 $_[1]->do('CREATE TABLE artist (
148 "artistid" INTEGER PRIMARY KEY NOT NULL,
149 "name" varchar(100),
150 "rank" integer NOT NULL DEFAULT 13,
151 "charfield" char(10)
152 )');
153 });
3b80fa31 154
8c49cf15 155 my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
156 $art->discard_changes;
157 $art->update({ rank => 69, name => 'foo' });
158 assert_no_missing_expected_requires();
159}
6a9e3dd5 160
8c49cf15 161# and do full populate() as well, just in case - shouldn't add new stuff
162{
4a24dba9 163 local $ENV{DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER};
5e724964 164 {
165 # in general we do not want DBICTest to load before sqla, but it is
166 # ok to cheat here
167 local $INC{'SQL/Abstract.pm'};
168 require DBICTest;
169 }
8c49cf15 170 my $s = DBICTest->init_schema;
fb88ca2c 171 is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae');
8c49cf15 172 assert_no_missing_expected_requires();
3b80fa31 173}
174
cbd7f87a 175# make sure we never loaded any of the strictures XS bullshit
176{
177 ok( ! exists $INC{ Module::Runtime::module_notional_filename($_) }, "$_ load never attempted" )
178 for qw(indirect multidimensional bareword::filehandles);
179}
180
8c49cf15 181done_testing;
3b80fa31 182
8c49cf15 183sub register_lazy_loadable_requires {
184 local $Test::Builder::Level = $Test::Builder::Level + 1;
3b80fa31 185
8c49cf15 186 for my $mod (@_) {
187 (my $modfn = "$mod.pm") =~ s!::!\/!g;
188 fail(join "\n",
189 "Module $mod already loaded by require site(s):",
190 (map { "\t$_" } @{$require_sites->{$mod}}),
191 '',
192 ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
193
194 $expected_dbic_deps->{$mod}++
195 }
196}
3b80fa31 197
f873b733 198# check if anything we were expecting didn't actually load
8c49cf15 199sub assert_no_missing_expected_requires {
200 my $nl;
201 for my $mod (keys %$expected_dbic_deps) {
202 (my $modfn = "$mod.pm") =~ s/::/\//g;
203 unless ($INC{$modfn}) {
204 my $err = sprintf "Expected DBIC core dependency '%s' never loaded - %s needs adjustment", $mod, __FILE__;
205 if (DBICTest::RunMode->is_smoker or DBICTest::RunMode->is_author) {
206 fail ($err)
207 }
208 else {
209 diag "\n" unless $nl->{$mod}++;
210 diag $err;
211 }
f873b733 212 }
213 }
8c49cf15 214 pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
215 __FILE__,
216 (caller(0))[2],
217 join (', ', sort keys %$expected_dbic_deps ),
218 ) unless $nl;
f873b733 219}