Deprecate req_errorlist_for, proxy to to modreq_errorlist_for
[dbsrgits/DBIx-Class-Historic.git] / xt / optional_deps.t
CommitLineData
05c0614b 1use strict;
2use warnings;
3no warnings qw/once/;
4
c052f3dd 5my ($inc_before, $inc_after);
be68095d 6# DBIx::Class::Optional::Dependencies queries $ENV at compile time
7# to build the optional requirements
8BEGIN {
9 $ENV{DBICTEST_PG_DSN} = '1';
efee51b7 10 delete $ENV{DBICTEST_ORA_DSN};
c052f3dd 11
12 require Carp; # Carp is not used in the test, but in OptDeps, load for proper %INC comparison
13
14 $inc_before = [ keys %INC ];
15 require DBIx::Class::Optional::Dependencies;
16 $inc_after = [ keys %INC ];
be68095d 17}
18
c052f3dd 19use Test::More;
20use Test::Exception;
21use Scalar::Util; # load before we break require()
05c0614b 22
c052f3dd 23ok ( (! grep { $_ =~ m|DBIx/Class| } @$inc_before ), 'Nothing DBIC related was loaded before inc-test')
24 unless $ENV{PERL5OPT}; # a defined PERL5OPT may inject extra deps crashing this test
3c3e76bd 25
26is_deeply (
27 [ sort @$inc_after],
28 [ sort (@$inc_before, 'DBIx/Class/Optional/Dependencies.pm') ],
29 'Nothing loaded other than DBIx::Class::OptDeps',
30);
31
31c31b8d 32
33# check the project-local groups for sanity
34lives_ok {
35 DBIx::Class::Optional::Dependencies->req_group_list
36} "The entire optdep list is well formed";
37
05c0614b 38is_deeply (
31c31b8d 39 [ keys %{ DBIx::Class::Optional::Dependencies->req_list_for ('deploy') } ],
05c0614b 40 [ 'SQL::Translator' ],
41 'Correct deploy() dependency list',
42);
43
31c31b8d 44# scope to break require()
d8799bab 45{
31c31b8d 46
47# make module loading impossible, regardless of actual libpath contents
d8799bab 48 local @INC = (sub { die('Optional Dep Test') } );
49
31c31b8d 50# basic test using the deploy target
51 for ('deploy', ['deploy']) {
52
53 # explicitly blow up cache
54 %DBIx::Class::Optional::Dependencies::req_unavailability_cache = ();
55
56 ok (
57 ! DBIx::Class::Optional::Dependencies->req_ok_for ($_),
58 'deploy() deps missing',
59 );
60
61 like (
62 DBIx::Class::Optional::Dependencies->req_missing_for ($_),
63 qr/
64 (?: \A|\s )
65 " SQL::Translator \~ \>\= [\d\.]+ "
66 \s
67 .*?
68 \Q(see DBIx::Class::Optional::Dependencies documentation for details)\E
69 \z
70 /x,
71 'expected missing string contents',
72 );
73
74 like (
5ffa39c7 75 DBIx::Class::Optional::Dependencies->modreq_errorlist_for ($_)->{'SQL::Translator'},
31c31b8d 76 qr/Optional Dep Test/,
77 'custom exception found in errorlist',
78 );
79
80 #make it so module appears loaded
81 local $INC{'SQL/Translator.pm'} = 1;
82 local $SQL::Translator::VERSION = 999;
83
84 ok (
85 ! DBIx::Class::Optional::Dependencies->req_ok_for ($_),
86 'deploy() deps missing cached properly from previous run',
87 );
88
89 # blow cache again
90 %DBIx::Class::Optional::Dependencies::req_unavailability_cache = ();
91
92 ok (
93 DBIx::Class::Optional::Dependencies->req_ok_for ($_),
94 'deploy() deps present',
95 );
96
97 is (
98 DBIx::Class::Optional::Dependencies->req_missing_for ($_),
99 '',
100 'expected null missing string',
101 );
102
103 is_deeply (
5ffa39c7 104 # use the deprecated method name
31c31b8d 105 DBIx::Class::Optional::Dependencies->req_errorlist_for ($_),
106 undef,
107 'expected empty errorlist',
108 );
109 }
110
111# test lack of deps for oracle test (envvar deleted higher up)
112 is_deeply(
113 DBIx::Class::Optional::Dependencies->req_list_for('test_rdbms_oracle'),
114 {},
115 'empty optional dependencies list for testing Oracle without ENV var',
d8799bab 116 );
117
31c31b8d 118# test combination of different requirements on same module (pg's are relatively stable)
119 is_deeply(
120 DBIx::Class::Optional::Dependencies->req_list_for('rdbms_pg'),
121 { 'DBD::Pg' => '0', },
122 'optional dependencies list for using Postgres matches',
d8799bab 123 );
124
31c31b8d 125 is_deeply (
126 DBIx::Class::Optional::Dependencies->req_list_for([qw( rdbms_pg test_rdbms_pg )]),
127 { 'DBD::Pg' => '2.009002' },
128 'optional dependencies list for testing Postgres matches',
d8799bab 129 );
05c0614b 130
31c31b8d 131 is(
132 DBIx::Class::Optional::Dependencies->req_missing_for([qw( rdbms_pg test_rdbms_pg )]),
133 '"DBD::Pg~>=2.009002"',
134 'optional dependencies error text for testing Postgres matches',
135 );
05c0614b 136
31c31b8d 137}
05c0614b 138
be68095d 139# test multiple times to find autovivification bugs
140for (1..2) {
141 throws_ok {
142 DBIx::Class::Optional::Dependencies->req_list_for();
143 } qr/\Qreq_list_for() expects a requirement group name/,
144 "req_list_for without groupname throws exception on run $_";
145
146 throws_ok {
147 DBIx::Class::Optional::Dependencies->req_list_for('');
148 } qr/\Qreq_list_for() expects a requirement group name/,
149 "req_list_for with empty groupname throws exception on run $_";
150
151 throws_ok {
152 DBIx::Class::Optional::Dependencies->req_list_for('invalid_groupname');
31c31b8d 153 } qr/Requirement group 'invalid_groupname' is not defined/,
be68095d 154 "req_list_for with invalid groupname throws exception on run $_";
155}
156
05c0614b 157done_testing;