Really make sure xt/ tests run on non-plain installs (fix c26b30de)
[dbsrgits/DBIx-Class.git] / xt / extra / lean_startup.t
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
3
4 my ($initial_inc_contents, $expected_dbic_deps, $require_sites);
5 BEGIN {
6   # these envvars *will* bring in more stuff than the baseline
7   delete @ENV{qw(DBICTEST_SQLT_DEPLOY DBIC_TRACE)};
8
9   # make sure extras do not load even when this is set
10   $ENV{PERL_STRICTURES_EXTRA} = 1;
11
12   unshift @INC, 't/lib';
13   require DBICTest::Util::OverrideRequire;
14
15   DBICTest::Util::OverrideRequire::override_global_require( sub {
16     my $res = $_[0]->();
17
18     my $req = $_[1];
19     $req =~ s/\.pm$//;
20     $req =~ s/\//::/g;
21
22     my $up = 0;
23     my @caller;
24     do { @caller = caller($up++) } while (
25       @caller and (
26         # exclude our test suite, known "module require-rs" and eval frames
27         $caller[1] =~ /^ t [\/\\] /x
28           or
29         $caller[0] =~ /^ (?: base | parent | Class::C3::Componentised | Module::Inspector | Module::Runtime ) $/x
30           or
31         $caller[3] eq '(eval)',
32       )
33     );
34
35     push @{$require_sites->{$req}}, "$caller[1] line $caller[2]"
36       if @caller;
37
38     return $res if $req =~ /^DBIx::Class|^DBICTest::/;
39
40     # exclude everything where the current namespace does not match the called function
41     # (this works around very weird XS-induced require callstack corruption)
42     if (
43       !$initial_inc_contents->{$req}
44         and
45       !$expected_dbic_deps->{$req}
46         and
47       @caller
48         and
49       $caller[0] =~ /^DBIx::Class/
50         and
51       (caller($up))[3] =~ /\Q$caller[0]/
52     ) {
53       CORE::require('Test/More.pm');
54       Test::More::fail ("Unexpected require of '$req' by $caller[0] ($caller[1] line $caller[2])");
55
56       CORE::require('DBICTest/Util.pm');
57       Test::More::diag( 'Require invoked' .  DBICTest::Util::stacktrace() );
58     }
59
60     return $res;
61   });
62 }
63
64 use strict;
65 use warnings;
66 use Test::More;
67
68 BEGIN {
69   plan skip_all => 'A defined PERL5OPT may inject extra deps crashing this test'
70     if $ENV{PERL5OPT};
71
72   plan skip_all => 'Dependency load patterns are radically different before perl 5.10'
73     if "$]" < 5.010;
74
75   # add what we loaded so far
76   for (keys %INC) {
77     my $mod = $_;
78     $mod =~ s/\.pm$//;
79     $mod =~ s!\/!::!g;
80     $initial_inc_contents->{$mod} = 1;
81   }
82 }
83
84 BEGIN {
85   delete $ENV{$_} for qw(
86     DBICTEST_VIA_REPLICATED
87     DBICTEST_DEBUG_CONCURRENCY_LOCKS
88   );
89 }
90
91 #######
92 ### This is where the test starts
93 #######
94
95 # checking base schema load, no storage no connection
96 {
97   register_lazy_loadable_requires(qw(
98     B
99     constant
100     overload
101
102     base
103     Devel::GlobalDestruction
104     mro
105
106     Carp
107     namespace::clean
108     Try::Tiny
109     Sub::Name
110     Sub::Defer
111     Sub::Quote
112
113     Scalar::Util
114     List::Util
115     Storable
116
117     Class::Accessor::Grouped
118     Class::C3::Componentised
119     SQL::Abstract
120   ));
121
122   require DBICTest::Schema;
123   assert_no_missing_expected_requires();
124 }
125
126 # check schema/storage instantiation with no connect
127 {
128   register_lazy_loadable_requires(qw(
129     Moo
130     Moo::Object
131     Method::Generate::Accessor
132     Method::Generate::Constructor
133     Context::Preserve
134   ));
135
136   my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
137   ok (! $s->storage->connected, 'no connection');
138   assert_no_missing_expected_requires();
139 }
140
141 # do something (deploy, insert)
142 {
143   register_lazy_loadable_requires(qw(
144     DBI
145     Hash::Merge
146   ));
147
148   my $s = DBICTest::Schema->connect('dbi:SQLite::memory:');
149   $s->storage->dbh_do(sub {
150     $_[1]->do('CREATE TABLE artist (
151       "artistid" INTEGER PRIMARY KEY NOT NULL,
152       "name" varchar(100),
153       "rank" integer NOT NULL DEFAULT 13,
154       "charfield" char(10)
155     )');
156   });
157
158   my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
159   $art->discard_changes;
160   $art->update({ rank => 69, name => 'foo' });
161   assert_no_missing_expected_requires();
162 }
163
164 # and do full populate() as well, just in case - shouldn't add new stuff
165 {
166   local $ENV{DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER};
167   {
168     # in general we do not want DBICTest to load before sqla, but it is
169     # ok to cheat here
170     local $INC{'SQL/Abstract.pm'};
171     require DBICTest;
172   }
173   my $s = DBICTest->init_schema;
174   is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae');
175   assert_no_missing_expected_requires();
176 }
177
178 done_testing;
179
180 sub register_lazy_loadable_requires {
181   local $Test::Builder::Level = $Test::Builder::Level + 1;
182
183   for my $mod (@_) {
184     (my $modfn = "$mod.pm") =~ s!::!\/!g;
185     fail(join "\n",
186       "Module $mod already loaded by require site(s):",
187       (map { "\t$_" } @{$require_sites->{$mod}}),
188       '',
189     ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
190
191     $expected_dbic_deps->{$mod}++
192   }
193 }
194
195 # check if anything we were expecting didn't actually load
196 sub assert_no_missing_expected_requires {
197   my $nl;
198   for my $mod (keys %$expected_dbic_deps) {
199     (my $modfn = "$mod.pm") =~ s/::/\//g;
200     fail sprintf (
201       "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
202       $mod,
203       __FILE__
204     ) unless $INC{$modfn};
205   }
206   pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
207     __FILE__,
208     (caller(0))[2],
209     join (', ', sort keys %$expected_dbic_deps ),
210   ) unless $nl;
211 }