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