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
5 if ( $ENV{RELEASE_TESTING} ) {
6 require warnings and warnings->import;
7 require strict and strict->import;
11 my ($initial_inc_contents, $expected_dbic_deps, $require_sites, %stack);
13 unshift @INC, 't/lib';
14 require DBICTest::Util::OverrideRequire;
16 DBICTest::Util::OverrideRequire::override_global_require( sub {
19 return $res if $stack{neutralize_override};
27 do { @caller = CORE::caller($up++) } while (
29 # exclude our test suite, known "module require-rs" and eval frames
30 $caller[1] =~ / (?: \A | [\/\\] ) x?t [\/\\] /x
32 $caller[0] =~ /^ (?: base | parent | Class::C3::Componentised | Module::Inspector | Module::Runtime ) $/x
34 $caller[3] eq '(eval)',
38 push @{$require_sites->{$req}}, "$caller[1] line $caller[2]"
41 return $res if $req =~ /^DBIx::Class|^DBICTest::/;
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.]+$/;
48 # exclude everything where the current namespace does not match the called function
49 # (this works around very weird XS-induced require callstack corruption)
51 !$initial_inc_contents->{$req}
53 !$expected_dbic_deps->{$req}
57 $caller[0] =~ /^DBIx::Class/
59 (CORE::caller($up))[3] =~ /\Q$caller[0]/
61 local $stack{neutralize_override} = 1;
63 do 1 while CORE::caller(++$up);
65 require('Test/More.pm');
66 local $Test::Builder::Level = $up + 1;
67 Test::More::fail ("Unexpected require of '$req' by $caller[0] ($caller[1] line $caller[2])");
69 require('DBICTest/Util.pm');
70 Test::More::diag( 'Require invoked' . DBICTest::Util::stacktrace() );
82 plan skip_all => 'A defined PERL5OPT may inject extra deps crashing this test'
85 plan skip_all => 'Presence of sitecustomize.pl may inject extra deps crashing this test'
86 if grep { $_ =~ m| \/ sitecustomize\.pl $ |x } keys %INC;
88 plan skip_all => 'Dependency load patterns are radically different before perl 5.10'
91 # these envvars *will* bring in more stuff than the baseline
94 DBIC_SHUFFLE_UNORDERED_RESULTSETS
96 DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER
97 DBICTEST_VIA_REPLICATED
98 DBICTEST_DEBUG_CONCURRENCY_LOCKS
101 $ENV{DBICTEST_ANFANG_DEFANG} = 1;
103 # make sure extras do not load even when this is set
104 $ENV{PERL_STRICTURES_EXTRA} = 1;
106 # add what we loaded so far
111 $initial_inc_contents->{$mod} = 1;
117 ### This is where the test starts
120 # checking base schema load, no storage no connection
122 register_lazy_loadable_requires(qw(
128 Devel::GlobalDestruction
142 Class::Accessor::Grouped
143 Class::C3::Componentised
146 require DBIx::Class::Schema;
147 assert_no_missing_expected_requires();
150 # check schema/storage instantiation with no connect
152 register_lazy_loadable_requires(qw(
155 Method::Generate::Accessor
156 Method::Generate::Constructor
161 my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
162 ok (! $s->storage->connected, 'no connection');
163 assert_no_missing_expected_requires();
166 # do something (deploy, insert)
168 register_lazy_loadable_requires(qw(
174 eval <<'EOP' or die $@;
176 package DBICTest::Result::Artist;
181 use base 'DBIx::Class::Core';
183 __PACKAGE__->table("artist");
185 __PACKAGE__->add_columns(
187 data_type => 'integer',
188 is_auto_increment => 1,
191 data_type => 'varchar',
196 data_type => 'integer',
206 __PACKAGE__->set_primary_key('artistid');
207 __PACKAGE__->add_unique_constraint(['name']);
208 __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
215 my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
217 $s->register_class( Artist => 'DBICTest::Result::Artist' );
219 $s->storage->dbh_do(sub {
220 $_[1]->do('CREATE TABLE artist (
221 "artistid" INTEGER PRIMARY KEY NOT NULL,
223 "rank" integer NOT NULL DEFAULT 13,
228 my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
229 $art->discard_changes;
230 $art->update({ rank => 69, name => 'foo' });
231 $s->resultset('Artist')->all;
232 assert_no_missing_expected_requires();
236 # and do full DBICTest based populate() as well, just in case - shouldn't add new stuff
238 # DBICTest needs File::Spec, but older versions of Storable load it alread
239 # Instead of adding a contrived conditional, just preempt the testing entirely
245 my $s = DBICTest->init_schema;
246 is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae', 'Expected find() result');
250 # one final quiet guard to run at all times
251 END { assert_no_missing_expected_requires('quiet') };
253 sub register_lazy_loadable_requires {
254 local $Test::Builder::Level = $Test::Builder::Level + 1;
257 (my $modfn = "$mod.pm") =~ s!::!\/!g;
259 "Module $mod already loaded by require site(s):",
260 (map { "\t$_" } @{$require_sites->{$mod}}),
262 ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
264 $expected_dbic_deps->{$mod}++
268 # check if anything we were expecting didn't actually load
269 sub assert_no_missing_expected_requires {
272 for my $mod (keys %$expected_dbic_deps) {
273 (my $modfn = "$mod.pm") =~ s/::/\//g;
275 "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
278 ) unless $INC{$modfn};
281 pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
284 join (', ', sort keys %$expected_dbic_deps ),