Revert c9f4555e - I did not see a mistake I made in the skip-regex
[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
92fbedbc 4BEGIN {
5 if ( $ENV{RELEASE_TESTING} ) {
6 require warnings and warnings->import;
7 require strict and strict->import;
8 }
9}
10
11my ($initial_inc_contents, $expected_dbic_deps, $require_sites, %stack);
3b80fa31 12BEGIN {
45638aed 13 unshift @INC, 't/lib';
14 require DBICTest::Util::OverrideRequire;
15
16 DBICTest::Util::OverrideRequire::override_global_require( sub {
17 my $res = $_[0]->();
8c49cf15 18
92fbedbc 19 return $res if $stack{neutralize_override};
20
8c49cf15 21 my $req = $_[1];
22 $req =~ s/\.pm$//;
23 $req =~ s/\//::/g;
24
25 my $up = 0;
26 my @caller;
92fbedbc 27 do { @caller = CORE::caller($up++) } while (
8c49cf15 28 @caller and (
29 # exclude our test suite, known "module require-rs" and eval frames
92fbedbc 30 $caller[1] =~ / (?: \A | [\/\\] ) x?t [\/\\] /x
8c49cf15 31 or
32 $caller[0] =~ /^ (?: base | parent | Class::C3::Componentised | Module::Inspector | Module::Runtime ) $/x
33 or
34 $caller[3] eq '(eval)',
35 )
36 );
37
38 push @{$require_sites->{$req}}, "$caller[1] line $caller[2]"
39 if @caller;
40
41 return $res if $req =~ /^DBIx::Class|^DBICTest::/;
42
e8452b02 43 # Some modules have a bare 'use $perl_version' as the first statement
44 # Since the use() happens before 'package' had a chance to switch
45 # the namespace, the shim thinks DBIC* tried to require this
397056f9 46 return $res if $req =~ /^v?[0-9.]+$/;
92fbedbc 47
8c49cf15 48 # exclude everything where the current namespace does not match the called function
49 # (this works around very weird XS-induced require callstack corruption)
50 if (
51 !$initial_inc_contents->{$req}
52 and
53 !$expected_dbic_deps->{$req}
54 and
55 @caller
56 and
57 $caller[0] =~ /^DBIx::Class/
58 and
92fbedbc 59 (CORE::caller($up))[3] =~ /\Q$caller[0]/
8c49cf15 60 ) {
92fbedbc 61 local $stack{neutralize_override} = 1;
62
63 do 1 while CORE::caller(++$up);
64
65 require('Test/More.pm');
66 local $Test::Builder::Level = $up + 1;
8c49cf15 67 Test::More::fail ("Unexpected require of '$req' by $caller[0] ($caller[1] line $caller[2])");
68
92fbedbc 69 require('DBICTest/Util.pm');
554484cb 70 Test::More::diag( 'Require invoked' . DBICTest::Util::stacktrace() );
8c49cf15 71 }
72
45638aed 73 return $res;
74 });
3b80fa31 75}
76
77use strict;
78use warnings;
79use Test::More;
80
45638aed 81BEGIN {
8c49cf15 82 plan skip_all => 'A defined PERL5OPT may inject extra deps crashing this test'
83 if $ENV{PERL5OPT};
84
26710bc9 85 plan skip_all => 'Presence of sitecustomize.pl may inject extra deps crashing this test'
86 if grep { $_ =~ m| \/ sitecustomize\.pl $ |x } keys %INC;
87
8c49cf15 88 plan skip_all => 'Dependency load patterns are radically different before perl 5.10'
750a4ad2 89 if "$]" < 5.010;
8c49cf15 90
26710bc9 91 # these envvars *will* bring in more stuff than the baseline
92 delete @ENV{qw(
93 DBIC_TRACE
58b92e31 94 DBIC_SHUFFLE_UNORDERED_RESULTSETS
26710bc9 95 DBICTEST_SQLT_DEPLOY
92fbedbc 96 DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER
26710bc9 97 DBICTEST_VIA_REPLICATED
98 DBICTEST_DEBUG_CONCURRENCY_LOCKS
99 )};
100
101 $ENV{DBICTEST_ANFANG_DEFANG} = 1;
102
103 # make sure extras do not load even when this is set
104 $ENV{PERL_STRICTURES_EXTRA} = 1;
105
8c49cf15 106 # add what we loaded so far
107 for (keys %INC) {
108 my $mod = $_;
109 $mod =~ s/\.pm$//;
110 $mod =~ s!\/!::!g;
111 $initial_inc_contents->{$mod} = 1;
112 }
45638aed 113}
114
8b60b921 115
8c49cf15 116#######
117### This is where the test starts
118#######
3b80fa31 119
8c49cf15 120# checking base schema load, no storage no connection
121{
122 register_lazy_loadable_requires(qw(
123 B
0d8817bc 124 constant
8c49cf15 125 overload
0d8817bc 126
3b80fa31 127 base
8c49cf15 128 Devel::GlobalDestruction
3b80fa31 129 mro
3b80fa31 130
8c49cf15 131 Carp
3b80fa31 132 namespace::clean
133 Try::Tiny
134 Sub::Name
cbd7f87a 135 Sub::Defer
7f9a3f70 136 Sub::Quote
140bcb6a 137 attributes
b46b8537 138 File::Spec
3b80fa31 139
140 Scalar::Util
d7d45bdc 141 Storable
3b80fa31 142
3b80fa31 143 Class::Accessor::Grouped
144 Class::C3::Componentised
8c49cf15 145 ));
3b80fa31 146
92fbedbc 147 require DBIx::Class::Schema;
8c49cf15 148 assert_no_missing_expected_requires();
149}
3b80fa31 150
8c49cf15 151# check schema/storage instantiation with no connect
152{
153 register_lazy_loadable_requires(qw(
154 Moo
cbd7f87a 155 Moo::Object
156 Method::Generate::Accessor
157 Method::Generate::Constructor
8c49cf15 158 Context::Preserve
92fbedbc 159 SQL::Abstract
8c49cf15 160 ));
3b80fa31 161
92fbedbc 162 my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
8c49cf15 163 ok (! $s->storage->connected, 'no connection');
164 assert_no_missing_expected_requires();
165}
3b80fa31 166
8c49cf15 167# do something (deploy, insert)
168{
169 register_lazy_loadable_requires(qw(
170 DBI
8c49cf15 171 Hash::Merge
172 ));
173
92fbedbc 174 {
175 eval <<'EOP' or die $@;
176
177 package DBICTest::Result::Artist;
178
179 use warnings;
180 use strict;
181
182 use base 'DBIx::Class::Core';
183
184 __PACKAGE__->table("artist");
185
186 __PACKAGE__->add_columns(
187 artistid => {
188 data_type => 'integer',
189 is_auto_increment => 1,
190 },
191 name => {
192 data_type => 'varchar',
193 size => 100,
194 is_nullable => 1,
195 },
196 rank => {
197 data_type => 'integer',
198 default_value => 13,
199 },
200 charfield => {
201 data_type => 'char',
202 size => 10,
203 is_nullable => 1,
204 },
205 );
206
207 __PACKAGE__->set_primary_key('artistid');
208 __PACKAGE__->add_unique_constraint(['name']);
209 __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
210
211 1;
212
213EOP
214 }
215
216 my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
217
218 $s->register_class( Artist => 'DBICTest::Result::Artist' );
219
8c49cf15 220 $s->storage->dbh_do(sub {
221 $_[1]->do('CREATE TABLE artist (
222 "artistid" INTEGER PRIMARY KEY NOT NULL,
223 "name" varchar(100),
224 "rank" integer NOT NULL DEFAULT 13,
225 "charfield" char(10)
226 )');
227 });
3b80fa31 228
8c49cf15 229 my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
230 $art->discard_changes;
231 $art->update({ rank => 69, name => 'foo' });
58b92e31 232 $s->resultset('Artist')->all;
8c49cf15 233 assert_no_missing_expected_requires();
234}
6a9e3dd5 235
92fbedbc 236
237# and do full DBICTest based populate() as well, just in case - shouldn't add new stuff
8c49cf15 238{
92fbedbc 239 # DBICTest needs File::Spec, but older versions of Storable load it alread
240 # Instead of adding a contrived conditional, just preempt the testing entirely
241 require File::Spec;
242
243 require DBICTest;
244 DBICTest->import;
245
8c49cf15 246 my $s = DBICTest->init_schema;
92fbedbc 247 is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae', 'Expected find() result');
3b80fa31 248}
249
8c49cf15 250done_testing;
92fbedbc 251# one final quiet guard to run at all times
252END { assert_no_missing_expected_requires('quiet') };
3b80fa31 253
8c49cf15 254sub register_lazy_loadable_requires {
255 local $Test::Builder::Level = $Test::Builder::Level + 1;
3b80fa31 256
8c49cf15 257 for my $mod (@_) {
258 (my $modfn = "$mod.pm") =~ s!::!\/!g;
259 fail(join "\n",
260 "Module $mod already loaded by require site(s):",
261 (map { "\t$_" } @{$require_sites->{$mod}}),
262 '',
263 ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
264
265 $expected_dbic_deps->{$mod}++
266 }
267}
3b80fa31 268
f873b733 269# check if anything we were expecting didn't actually load
8c49cf15 270sub assert_no_missing_expected_requires {
92fbedbc 271 my $quiet = shift;
272
8c49cf15 273 for my $mod (keys %$expected_dbic_deps) {
274 (my $modfn = "$mod.pm") =~ s/::/\//g;
554484cb 275 fail sprintf (
276 "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
277 $mod,
278 __FILE__
279 ) unless $INC{$modfn};
f873b733 280 }
92fbedbc 281
8c49cf15 282 pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
283 __FILE__,
284 (caller(0))[2],
285 join (', ', sort keys %$expected_dbic_deps ),
92fbedbc 286 ) unless $quiet;
f873b733 287}