Make OptDeps aware of envvars as first-class requirements (test_* groups only)
[dbsrgits/DBIx-Class.git] / xt / optional_deps.t
CommitLineData
05c0614b 1use strict;
2use warnings;
3no warnings qw/once/;
4
c052f3dd 5my ($inc_before, $inc_after);
be68095d 6BEGIN {
c052f3dd 7 require Carp; # Carp is not used in the test, but in OptDeps, load for proper %INC comparison
8
9 $inc_before = [ keys %INC ];
10 require DBIx::Class::Optional::Dependencies;
11 $inc_after = [ keys %INC ];
be68095d 12}
13
c052f3dd 14use Test::More;
15use Test::Exception;
e3a7746c 16
17# load before we break require()
18use Scalar::Util();
19use MRO::Compat();
05c0614b 20
c052f3dd 21ok ( (! grep { $_ =~ m|DBIx/Class| } @$inc_before ), 'Nothing DBIC related was loaded before inc-test')
22 unless $ENV{PERL5OPT}; # a defined PERL5OPT may inject extra deps crashing this test
3c3e76bd 23
24is_deeply (
25 [ sort @$inc_after],
26 [ sort (@$inc_before, 'DBIx/Class/Optional/Dependencies.pm') ],
27 'Nothing loaded other than DBIx::Class::OptDeps',
28);
29
31c31b8d 30
31# check the project-local groups for sanity
32lives_ok {
33 DBIx::Class::Optional::Dependencies->req_group_list
34} "The entire optdep list is well formed";
35
05c0614b 36is_deeply (
31c31b8d 37 [ keys %{ DBIx::Class::Optional::Dependencies->req_list_for ('deploy') } ],
05c0614b 38 [ 'SQL::Translator' ],
39 'Correct deploy() dependency list',
40);
41
31c31b8d 42# scope to break require()
d8799bab 43{
31c31b8d 44
45# make module loading impossible, regardless of actual libpath contents
e3a7746c 46 local @INC = (sub { Carp::confess('Optional Dep Test') } );
d8799bab 47
31c31b8d 48# basic test using the deploy target
49 for ('deploy', ['deploy']) {
50
51 # explicitly blow up cache
52 %DBIx::Class::Optional::Dependencies::req_unavailability_cache = ();
53
54 ok (
55 ! DBIx::Class::Optional::Dependencies->req_ok_for ($_),
56 'deploy() deps missing',
57 );
58
59 like (
e3a7746c 60 DBIx::Class::Optional::Dependencies->modreq_missing_for ($_),
61 qr/
62 \A
63 " SQL::Translator \~ \>\= [\d\.]+ "
64 \z
65 /x,
66 'expected modreq missing string contents',
67 );
68
69 like (
31c31b8d 70 DBIx::Class::Optional::Dependencies->req_missing_for ($_),
71 qr/
e3a7746c 72 \A
31c31b8d 73 " SQL::Translator \~ \>\= [\d\.]+ "
e3a7746c 74 \Q (see DBIx::Class::Optional::Dependencies documentation for details)\E
31c31b8d 75 \z
76 /x,
77 'expected missing string contents',
78 );
79
80 like (
5ffa39c7 81 DBIx::Class::Optional::Dependencies->modreq_errorlist_for ($_)->{'SQL::Translator'},
31c31b8d 82 qr/Optional Dep Test/,
83 'custom exception found in errorlist',
84 );
85
86 #make it so module appears loaded
87 local $INC{'SQL/Translator.pm'} = 1;
88 local $SQL::Translator::VERSION = 999;
89
90 ok (
91 ! DBIx::Class::Optional::Dependencies->req_ok_for ($_),
92 'deploy() deps missing cached properly from previous run',
93 );
94
95 # blow cache again
96 %DBIx::Class::Optional::Dependencies::req_unavailability_cache = ();
97
98 ok (
99 DBIx::Class::Optional::Dependencies->req_ok_for ($_),
100 'deploy() deps present',
101 );
102
103 is (
104 DBIx::Class::Optional::Dependencies->req_missing_for ($_),
105 '',
106 'expected null missing string',
107 );
108
109 is_deeply (
5ffa39c7 110 # use the deprecated method name
31c31b8d 111 DBIx::Class::Optional::Dependencies->req_errorlist_for ($_),
112 undef,
113 'expected empty errorlist',
114 );
115 }
116
e3a7746c 117# test single-db text
118 local $ENV{DBICTEST_MYSQL_DSN};
119 is_deeply(
120 DBIx::Class::Optional::Dependencies->req_list_for('test_rdbms_mysql'),
121 undef,
122 'unknown optional dependencies list for testing MySQL without ENV var',
123 );
31c31b8d 124 is_deeply(
e3a7746c 125 DBIx::Class::Optional::Dependencies->modreq_list_for('test_rdbms_mysql'),
126 { 'DBD::mysql' => 0 },
127 'correct optional module dependencies list for testing MySQL without ENV var',
128 );
129
130 local $ENV{DBICTEST_MYSQL_DSN};
131 local $ENV{DBICTEST_PG_DSN};
132
133 is_deeply(
134 DBIx::Class::Optional::Dependencies->modreq_list_for('test_rdbms_pg'),
135 { 'DBD::Pg' => '2.009002' },
136 'optional dependencies list for testing Postgres without envvar',
137 );
138
139 is_deeply(
140 DBIx::Class::Optional::Dependencies->req_list_for('test_rdbms_pg'),
141 undef,
142 'optional dependencies list for testing Postgres without envvar',
d8799bab 143 );
144
31c31b8d 145 is_deeply(
146 DBIx::Class::Optional::Dependencies->req_list_for('rdbms_pg'),
147 { 'DBD::Pg' => '0', },
148 'optional dependencies list for using Postgres matches',
d8799bab 149 );
150
e3a7746c 151# test combination of different requirements on same module (pg's are relatively stable)
31c31b8d 152 is_deeply (
153 DBIx::Class::Optional::Dependencies->req_list_for([qw( rdbms_pg test_rdbms_pg )]),
e3a7746c 154 { 'DBD::Pg' => '0' },
155 'optional module dependencies list for testing Postgres matches without envvar',
156 );
157
158 is(
159 DBIx::Class::Optional::Dependencies->req_missing_for([qw( rdbms_pg test_rdbms_pg )]),
160 '"DBD::Pg~>=2.009002" as well as the following group(s) of environment variables: DBICTEST_PG_DSN/..._USER/..._PASS',
161 'optional dependencies for testing Postgres without envvar'
162 );
163
164 is(
165 DBIx::Class::Optional::Dependencies->req_missing_for([qw( test_rdbms_mysql test_rdbms_pg )]),
166 'DBD::mysql "DBD::Pg~>=2.009002" as well as the following group(s) of environment variables: DBICTEST_MYSQL_DSN/..._USER/..._PASS and DBICTEST_PG_DSN/..._USER/..._PASS',
167 'optional dependencies for testing Postgres+MySQL without envvars'
168 );
169
170 $ENV{DBICTEST_PG_DSN} = 'boo';
171 is_deeply (
172 DBIx::Class::Optional::Dependencies->modreq_list_for([qw( rdbms_pg test_rdbms_pg )]),
31c31b8d 173 { 'DBD::Pg' => '2.009002' },
e3a7746c 174 'optional module dependencies list for testing Postgres matches with envvar',
d8799bab 175 );
05c0614b 176
31c31b8d 177 is(
178 DBIx::Class::Optional::Dependencies->req_missing_for([qw( rdbms_pg test_rdbms_pg )]),
179 '"DBD::Pg~>=2.009002"',
e3a7746c 180 'optional dependencies error text for testing Postgres matches with evvar',
31c31b8d 181 );
05c0614b 182
31c31b8d 183}
05c0614b 184
be68095d 185# test multiple times to find autovivification bugs
e3a7746c 186for my $meth (qw(req_list_for modreq_list_for)) {
be68095d 187 throws_ok {
e3a7746c 188 DBIx::Class::Optional::Dependencies->$meth();
be68095d 189 } qr/\Qreq_list_for() expects a requirement group name/,
e3a7746c 190 "$meth without groupname throws exception";
be68095d 191
192 throws_ok {
e3a7746c 193 DBIx::Class::Optional::Dependencies->$meth('');
194 } qr/\Q$meth() expects a requirement group name/,
195 "$meth with empty groupname throws exception";
be68095d 196
197 throws_ok {
e3a7746c 198 DBIx::Class::Optional::Dependencies->$meth('invalid_groupname');
31c31b8d 199 } qr/Requirement group 'invalid_groupname' is not defined/,
e3a7746c 200 "$meth with invalid groupname throws exception";
be68095d 201}
202
05c0614b 203done_testing;