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