Expand/fortify the handling of attributes
[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
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
c9f4555e 91 plan skip_all => 'Dependency load patterns may be different on cperl - skip for now'
92 if $^V =~ /\d+c$/;
93
26710bc9 94 # these envvars *will* bring in more stuff than the baseline
95 delete @ENV{qw(
96 DBIC_TRACE
58b92e31 97 DBIC_SHUFFLE_UNORDERED_RESULTSETS
26710bc9 98 DBICTEST_SQLT_DEPLOY
92fbedbc 99 DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER
26710bc9 100 DBICTEST_VIA_REPLICATED
101 DBICTEST_DEBUG_CONCURRENCY_LOCKS
102 )};
103
104 $ENV{DBICTEST_ANFANG_DEFANG} = 1;
105
106 # make sure extras do not load even when this is set
107 $ENV{PERL_STRICTURES_EXTRA} = 1;
108
8c49cf15 109 # add what we loaded so far
110 for (keys %INC) {
111 my $mod = $_;
112 $mod =~ s/\.pm$//;
113 $mod =~ s!\/!::!g;
114 $initial_inc_contents->{$mod} = 1;
115 }
45638aed 116}
117
8b60b921 118
8c49cf15 119#######
120### This is where the test starts
121#######
3b80fa31 122
8c49cf15 123# checking base schema load, no storage no connection
124{
125 register_lazy_loadable_requires(qw(
126 B
0d8817bc 127 constant
8c49cf15 128 overload
0d8817bc 129
3b80fa31 130 base
8c49cf15 131 Devel::GlobalDestruction
3b80fa31 132 mro
3b80fa31 133
8c49cf15 134 Carp
3b80fa31 135 namespace::clean
136 Try::Tiny
137 Sub::Name
cbd7f87a 138 Sub::Defer
7f9a3f70 139 Sub::Quote
140bcb6a 140 attributes
b46b8537 141 File::Spec
3b80fa31 142
143 Scalar::Util
d7d45bdc 144 Storable
3b80fa31 145
3b80fa31 146 Class::Accessor::Grouped
147 Class::C3::Componentised
8c49cf15 148 ));
3b80fa31 149
92fbedbc 150 require DBIx::Class::Schema;
8c49cf15 151 assert_no_missing_expected_requires();
152}
3b80fa31 153
8c49cf15 154# check schema/storage instantiation with no connect
155{
156 register_lazy_loadable_requires(qw(
157 Moo
cbd7f87a 158 Moo::Object
159 Method::Generate::Accessor
160 Method::Generate::Constructor
8c49cf15 161 Context::Preserve
92fbedbc 162 SQL::Abstract
8c49cf15 163 ));
3b80fa31 164
92fbedbc 165 my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
8c49cf15 166 ok (! $s->storage->connected, 'no connection');
167 assert_no_missing_expected_requires();
168}
3b80fa31 169
8c49cf15 170# do something (deploy, insert)
171{
172 register_lazy_loadable_requires(qw(
173 DBI
8c49cf15 174 Hash::Merge
175 ));
176
92fbedbc 177 {
178 eval <<'EOP' or die $@;
179
180 package DBICTest::Result::Artist;
181
182 use warnings;
183 use strict;
184
185 use base 'DBIx::Class::Core';
186
187 __PACKAGE__->table("artist");
188
189 __PACKAGE__->add_columns(
190 artistid => {
191 data_type => 'integer',
192 is_auto_increment => 1,
193 },
194 name => {
195 data_type => 'varchar',
196 size => 100,
197 is_nullable => 1,
198 },
199 rank => {
200 data_type => 'integer',
201 default_value => 13,
202 },
203 charfield => {
204 data_type => 'char',
205 size => 10,
206 is_nullable => 1,
207 },
208 );
209
210 __PACKAGE__->set_primary_key('artistid');
211 __PACKAGE__->add_unique_constraint(['name']);
212 __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
213
214 1;
215
216EOP
217 }
218
219 my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
220
221 $s->register_class( Artist => 'DBICTest::Result::Artist' );
222
8c49cf15 223 $s->storage->dbh_do(sub {
224 $_[1]->do('CREATE TABLE artist (
225 "artistid" INTEGER PRIMARY KEY NOT NULL,
226 "name" varchar(100),
227 "rank" integer NOT NULL DEFAULT 13,
228 "charfield" char(10)
229 )');
230 });
3b80fa31 231
8c49cf15 232 my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
233 $art->discard_changes;
234 $art->update({ rank => 69, name => 'foo' });
58b92e31 235 $s->resultset('Artist')->all;
8c49cf15 236 assert_no_missing_expected_requires();
237}
6a9e3dd5 238
92fbedbc 239
240# and do full DBICTest based populate() as well, just in case - shouldn't add new stuff
8c49cf15 241{
92fbedbc 242 # DBICTest needs File::Spec, but older versions of Storable load it alread
243 # Instead of adding a contrived conditional, just preempt the testing entirely
244 require File::Spec;
245
246 require DBICTest;
247 DBICTest->import;
248
8c49cf15 249 my $s = DBICTest->init_schema;
92fbedbc 250 is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae', 'Expected find() result');
3b80fa31 251}
252
8c49cf15 253done_testing;
92fbedbc 254# one final quiet guard to run at all times
255END { assert_no_missing_expected_requires('quiet') };
3b80fa31 256
8c49cf15 257sub register_lazy_loadable_requires {
258 local $Test::Builder::Level = $Test::Builder::Level + 1;
3b80fa31 259
8c49cf15 260 for my $mod (@_) {
261 (my $modfn = "$mod.pm") =~ s!::!\/!g;
262 fail(join "\n",
263 "Module $mod already loaded by require site(s):",
264 (map { "\t$_" } @{$require_sites->{$mod}}),
265 '',
266 ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
267
268 $expected_dbic_deps->{$mod}++
269 }
270}
3b80fa31 271
f873b733 272# check if anything we were expecting didn't actually load
8c49cf15 273sub assert_no_missing_expected_requires {
92fbedbc 274 my $quiet = shift;
275
8c49cf15 276 for my $mod (keys %$expected_dbic_deps) {
277 (my $modfn = "$mod.pm") =~ s/::/\//g;
554484cb 278 fail sprintf (
279 "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
280 $mod,
281 __FILE__
282 ) unless $INC{$modfn};
f873b733 283 }
92fbedbc 284
8c49cf15 285 pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
286 __FILE__,
287 (caller(0))[2],
288 join (', ', sort keys %$expected_dbic_deps ),
92fbedbc 289 ) unless $quiet;
f873b733 290}