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