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