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