Bring back _TempExtlib (d0435d75), this time for Sub::Quote
[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 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);
12 BEGIN {
13   unshift @INC, 't/lib';
14   require DBICTest::Util::OverrideRequire;
15
16   DBICTest::Util::OverrideRequire::override_global_require( sub {
17     my $res = $_[0]->();
18
19     return $res if $stack{neutralize_override};
20
21     my $req = $_[1];
22     $req =~ s/\.pm$//;
23     $req =~ s/\//::/g;
24
25     my $up = 0;
26     my @caller;
27     do { @caller = CORE::caller($up++) } while (
28       @caller and (
29         # exclude our test suite, known "module require-rs" and eval frames
30         $caller[1] =~ / (?: \A | [\/\\] ) x?t [\/\\] /x
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
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
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
66       (CORE::caller($up))[3] =~ /\Q$caller[0]/
67     ) {
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;
74       Test::More::fail ("Unexpected require of '$req' by $caller[0] ($caller[1] line $caller[2])");
75
76       require('DBICTest/Util.pm');
77       Test::More::diag( 'Require invoked' .  DBICTest::Util::stacktrace() );
78     }
79
80     return $res;
81   });
82 }
83
84 use strict;
85 use warnings;
86 use Test::More;
87
88 BEGIN {
89   plan skip_all => 'A defined PERL5OPT may inject extra deps crashing this test'
90     if $ENV{PERL5OPT};
91
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
95   plan skip_all => 'Dependency load patterns are radically different before perl 5.10'
96     if "$]" < 5.010;
97
98   # these envvars *will* bring in more stuff than the baseline
99   delete @ENV{qw(
100     DBIC_TRACE
101     DBIC_SHUFFLE_UNORDERED_RESULTSETS
102     DBICTEST_SQLT_DEPLOY
103     DBICTEST_SQLITE_REVERSE_DEFAULT_ORDER
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
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   }
120 }
121
122
123 #######
124 ### This is where the test starts
125 #######
126
127 # checking base schema load, no storage no connection
128 {
129   register_lazy_loadable_requires(qw(
130     B
131     constant
132     overload
133
134     base
135     Devel::GlobalDestruction
136     mro
137
138     Carp
139     namespace::clean
140     Try::Tiny
141     Sub::Name
142     Sub::Defer
143     Sub::Quote
144     File::Spec
145
146     Scalar::Util
147     Storable
148
149     Class::Accessor::Grouped
150     Class::C3::Componentised
151   ));
152
153   require DBIx::Class::Schema;
154   assert_no_missing_expected_requires();
155 }
156
157 # check schema/storage instantiation with no connect
158 {
159   register_lazy_loadable_requires(qw(
160     Moo
161     Moo::Object
162     Method::Generate::Accessor
163     Method::Generate::Constructor
164     Context::Preserve
165     SQL::Abstract
166   ));
167
168   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
169   ok (! $s->storage->connected, 'no connection');
170   assert_no_missing_expected_requires();
171 }
172
173 # do something (deploy, insert)
174 {
175   register_lazy_loadable_requires(qw(
176     DBI
177     Hash::Merge
178   ));
179
180   {
181     eval <<'EOP' or die $@;
182
183   package DBICTest::Result::Artist;
184
185   use warnings;
186   use strict;
187
188   use base 'DBIx::Class::Core';
189
190   __PACKAGE__->table("artist");
191
192   __PACKAGE__->add_columns(
193     artistid => {
194       data_type => 'integer',
195       is_auto_increment => 1,
196     },
197     name => {
198       data_type => 'varchar',
199       size      => 100,
200       is_nullable => 1,
201     },
202     rank => {
203       data_type => 'integer',
204       default_value => 13,
205     },
206     charfield => {
207       data_type => 'char',
208       size => 10,
209       is_nullable => 1,
210     },
211   );
212
213   __PACKAGE__->set_primary_key('artistid');
214   __PACKAGE__->add_unique_constraint(['name']);
215   __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
216
217   1;
218
219 EOP
220   }
221
222   my $s = DBIx::Class::Schema->connect('dbi:SQLite::memory:');
223
224   $s->register_class( Artist => 'DBICTest::Result::Artist' );
225
226   $s->storage->dbh_do(sub {
227     $_[1]->do('CREATE TABLE artist (
228       "artistid" INTEGER PRIMARY KEY NOT NULL,
229       "name" varchar(100),
230       "rank" integer NOT NULL DEFAULT 13,
231       "charfield" char(10)
232     )');
233   });
234
235   my $art = $s->resultset('Artist')->create({ name => \[ '?' => 'foo'], rank => 42 });
236   $art->discard_changes;
237   $art->update({ rank => 69, name => 'foo' });
238   $s->resultset('Artist')->all;
239   assert_no_missing_expected_requires();
240 }
241
242
243 # and do full DBICTest based populate() as well, just in case - shouldn't add new stuff
244 {
245   # DBICTest needs File::Spec, but older versions of Storable load it alread
246   # Instead of adding a contrived conditional, just preempt the testing entirely
247   require File::Spec;
248
249   require DBICTest;
250   DBICTest->import;
251
252   my $s = DBICTest->init_schema;
253   is ($s->resultset('Artist')->find(1)->name, 'Caterwauler McCrae', 'Expected find() result');
254 }
255
256 done_testing;
257 # one final quiet guard to run at all times
258 END { assert_no_missing_expected_requires('quiet') };
259
260 sub register_lazy_loadable_requires {
261   local $Test::Builder::Level = $Test::Builder::Level + 1;
262
263   for my $mod (@_) {
264     (my $modfn = "$mod.pm") =~ s!::!\/!g;
265     fail(join "\n",
266       "Module $mod already loaded by require site(s):",
267       (map { "\t$_" } @{$require_sites->{$mod}}),
268       '',
269     ) if $INC{$modfn} and !$initial_inc_contents->{$mod};
270
271     $expected_dbic_deps->{$mod}++
272   }
273 }
274
275 # check if anything we were expecting didn't actually load
276 sub assert_no_missing_expected_requires {
277   my $quiet = shift;
278
279   for my $mod (keys %$expected_dbic_deps) {
280     (my $modfn = "$mod.pm") =~ s/::/\//g;
281     fail sprintf (
282       "Expected DBIC core dependency '%s' never loaded - %s needs adjustment",
283       $mod,
284       __FILE__
285     ) unless $INC{$modfn};
286   }
287
288   pass(sprintf 'All modules expected at %s line %s loaded by DBIC: %s',
289     __FILE__,
290     (caller(0))[2],
291     join (', ', sort keys %$expected_dbic_deps ),
292   ) unless $quiet;
293 }