This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
947ea84b3bf07e9cf334f518ae7ee90c0d00f62f
[perl5.git] / lib / CPANPLUS / t / 08_CPANPLUS-Backend.t
1 ### make sure we can find our conf.pl file
2 BEGIN { 
3     use FindBin; 
4     require "$FindBin::Bin/inc/conf.pl";
5 }
6
7 use strict;
8 use Test::More      'no_plan';
9 use File::Basename  'dirname';
10
11 use Data::Dumper;
12 use CPANPLUS::Error;
13 use CPANPLUS::Internals::Constants;
14
15 my $conf = gimme_conf();
16
17 my $Class = 'CPANPLUS::Backend';
18 ### D::C has troubles with the 'use_ok' -- it finds the wrong paths.
19 ### for now, do a 'use' instead
20 #use_ok( $Class ) or diag "$Class not found";
21 use CPANPLUS::Backend;
22
23 my $cb = $Class->new( $conf );
24 isa_ok( $cb, $Class );
25
26 my $mt = $cb->module_tree;
27 my $at = $cb->author_tree;
28 ok( scalar keys %$mt,       "Module tree has entries" ); 
29 ok( scalar keys %$at,       "Author tree has entries" ); 
30
31 ### module_tree tests ###
32 my $Name = TEST_CONF_MODULE;
33 my $mod  = $cb->module_tree($Name);
34
35 ### XXX SOURCEFILES FIX
36 {   my @mods = $cb->module_tree($Name,$Name);
37     my $none = $cb->module_tree( TEST_CONF_INVALID_MODULE );
38     
39     ok( IS_MODOBJ->(mod => $mod),           "Module object found" );
40     is( scalar(@mods), 2,                   "   Module list found" );
41     ok( IS_MODOBJ->(mod => $mods[0]),       "   ISA module object" );
42     ok( !IS_MODOBJ->(mod => $none),         "   Bogus module detected");
43 }
44
45 ### author_tree tests ###
46 {   my @auths = $cb->author_tree( $mod->author->cpanid,
47                                   $mod->author->cpanid );
48     my $none  = $cb->author_tree( 'fnurk' );
49     
50     ok( IS_AUTHOBJ->(auth => $mod->author), "Author object found" );
51     is( scalar(@auths), 2,                  "   Author list found" );
52     ok( IS_AUTHOBJ->( author => $auths[0] ),"   ISA author object" );
53     is( $mod->author, $auths[0],            "   Objects are identical" );
54     ok( !IS_AUTHOBJ->( author => $none ),   "   Bogus author detected" );
55 }
56
57 my $conf_obj = $cb->configure_object;
58 ok( IS_CONFOBJ->(conf => $conf_obj),    "Configure object found" );
59
60
61 ### parse_module tests ###
62 {   my @map = (     # author                package             version
63         $Name   => [ $mod->author->cpanid,  $mod->package_name, $mod->version ],
64         $mod    => [ $mod->author->cpanid,  $mod->package_name, $mod->version ],
65         'Foo-Bar-EU-NOXS'
66                 => [ $mod->author->cpanid,  $mod->package_name, $mod->version ],
67         'Foo-Bar-EU-NOXS-0.01'
68                 => [ $mod->author->cpanid,  $mod->package_name, '0.01' ],
69         'EUNOXS/Foo-Bar-EU-NOXS'
70                 => [ 'EUNOXS',              $mod->package_name, $mod->version ],
71         'EUNOXS/Foo-Bar-EU-NOXS-0.01'
72                 => [ 'EUNOXS',              $mod->package_name, '0.01' ],
73         'Foo-Bar-EU-NOXS-0.09'
74                 => [ $mod->author->cpanid,  $mod->package_name, '0.09' ],
75         'MBXS/Foo-Bar-EU-NOXS-0.01'
76                 => [ 'MBXS',                $mod->package_name, '0.01' ],
77         'EUNOXS/Foo-Bar-EU-NOXS-0.09'
78                 => [ 'EUNOXS',              $mod->package_name, '0.09' ],
79         'EUNOXS/Foo-Bar-EU-NOXS-0.09.zip'
80                 => [ 'EUNOXS',              $mod->package_name, '0.09' ],
81         'FROO/Flub-Flob-1.1.zip'
82                 => [ 'FROO',                'Flub-Flob',        '1.1' ],
83         'G/GO/GOYALI/SMS_API_3_01.tar.gz'
84                 => [ 'GOYALI',              'SMS_API',          '3_01' ],
85         'E/EY/EYCK/Net/Lite/Net-Lite-FTP-0.091'
86                 => [ 'EYCK',                'Net-Lite-FTP',     '0.091' ],
87         'EYCK/Net/Lite/Net-Lite-FTP-0.091'
88                 => [ 'EYCK',                'Net-Lite-FTP',     '0.091' ],
89         'M/MA/MAXDB/DBD-MaxDB-7.5.00.24a'
90                 => [ 'MAXDB',               'DBD-MaxDB',        '7.5.00.24a' ],
91         'EUNOXS/perl5.005_03.tar.gz'
92                 => [ 'EUNOXS',              'perl',             '5.005_03' ],
93         'FROO/Flub-Flob-v1.1.0.tbz'
94                 => [ 'FROO',                'Flub-Flob',        'v1.1.0' ],
95         'FROO/Flub-Flob-1.1_2.tbz'
96                 => [ 'FROO',                'Flub-Flob',        '1.1_2' ],   
97         'LDS/CGI.pm-3.27.tar.gz'
98                 => [ 'LDS',                 'CGI',              '3.27' ],
99         'FROO/Text-Tabs+Wrap-2006.1117.tar.gz'
100                 => [ 'FROO',                'Text-Tabs+Wrap',   '2006.1117' ],   
101         'JETTERO/Crypt-PBC-0.7.20.0-0.4.9',
102                 => [ 'JETTERO',             'Crypt-PBC',    '0.7.20.0-0.4.9' ],   
103                 
104     );       
105
106     while ( my($guess, $attr) = splice @map, 0, 2 ) {
107         my( $author, $pkg, $version ) = @$attr;
108
109         ok( $guess,             "Attempting to parse $guess" );
110
111         my $obj = $cb->parse_module( module => $guess );
112         
113         ok( $obj,               "   Result returned" );
114         ok( IS_MODOBJ->( mod => $obj ), 
115                                 "   parse_module success by '$guess'" );     
116         
117         is( $obj->version, $version,
118                                 "   Proper version found: $version" );
119         is( $obj->package_version, $version,
120                                 "       Found in package_version as well" );
121         is( $obj->package_name, $pkg,
122                                 "   Proper package found: $pkg" );
123         unlike( $obj->package_name, qr/\d/,
124                                 "       No digits in package name" );
125         like( $obj->author->cpanid, "/$author/i", 
126                                 "   Proper author found: $author");
127         like( $obj->path,           "/$author/i", 
128                                 "   Proper path found: " . $obj->path );
129     }
130
131
132     ### test for things that look like real modules, but aren't ###
133     {   my @map = (
134             [  $Name . $$ => [
135                 [qr/does not contain an author/,"Missing author part detected"],
136                 [qr/Cannot find .+? in the module tree/,"Unable to find module"]
137             ] ],
138             [ {}, => [
139                 [ qr/module string from reference/,"Unable to parse ref"] 
140             ] ],
141         );
142
143         for my $entry ( @map ) {
144             my($mod,$aref) = @$entry;
145             
146             my $none = $cb->parse_module( module => $mod );
147             ok( !IS_MODOBJ->(mod => $none),     
148                                 "Non-existant module detected" );
149             ok( !IS_FAKE_MODOBJ->(mod => $none),
150                                 "Non-existant fake module detected" );
151         
152             my $str = CPANPLUS::Error->stack_as_string;
153             for my $pair (@$aref) {
154                 my($re,$diag) = @$pair;
155                 like( $str, $re,"   $diag" );
156             }
157         }    
158     }
159     
160     ### test parsing of arbitrary URI
161     for my $guess ( qw[ http://foo/bar.gz
162                         http://a/b/c/d/e/f/g/h/i/j
163                         flub://floo ]
164     ) {
165         my $obj = $cb->parse_module( module => $guess );
166         ok( IS_FAKE_MODOBJ->(mod => $obj), "parse_module success by '$guess'" );
167         is( $obj->status->_fetch_from, $guess,
168                                             "   Fetch from set ok" );
169     }                                       
170 }         
171
172 ### RV tests ###
173 {   my $method = 'readme';
174     my %args   = ( modules => [$Name] );  
175     
176     my $rv = $cb->$method( %args );
177     ok( IS_RVOBJ->( $rv ),              "Got an RV object" );
178     ok( $rv->ok,                        "   Overall OK" );
179     cmp_ok( $rv, '==', 1,               "   Overload OK" );
180     is( $rv->function, $method,         "   Function stored OK" );     
181     is_deeply( $rv->args, \%args,       "   Arguments stored OK" );
182     is( $rv->rv->{$Name}, $mod->readme, "   RV as expected" );
183 }
184
185 ### reload_indices tests ###
186 {
187     my $file = File::Spec->catfile( $conf->get_conf('base'),
188                                     $conf->_get_source('mod'),
189                                 );
190   
191     ok( $cb->reload_indices( update_source => 0 ),  "Rebuilding trees" );                              
192     my $age = -M $file;
193     
194     ### make sure we are 'newer' on faster machines with a sleep..
195     ### apparently Win32's FAT isn't granual enough on intervals
196     ### < 2 seconds, so it may give the same answer before and after
197     ### the sleep, causing the test to fail. so sleep atleast 2 seconds.
198     sleep 2;
199     ok( $cb->reload_indices( update_source => 1 ),  
200                                     "Rebuilding and refetching trees" );
201     cmp_ok( $age, '>', -M $file,    "    Source file '$file' updated" );                                      
202 }
203
204 ### flush tests ###
205 {
206     for my $cache( qw[methods hosts modules lib all] ) {
207         ok( $cb->flush($cache), "Cache $cache flushed ok" );
208     }
209 }
210
211 ### installed tests ###
212 {   
213     ok( scalar $cb->installed,    "Found list of installed modules" );
214 }    
215                 
216 ### autobudle tests ###
217 {
218     my $where = $cb->autobundle;
219     ok( $where,     "Autobundle written" );
220     ok( -s $where,  "   File has size" );
221 }
222
223 ### local_mirror tests ###
224 {   ### turn off md5 checks for the 'fake' packages we have 
225     my $old_md5 = $conf->get_conf('md5');
226     $conf->set_conf( md5 => 0 );
227
228     ### otherwise 'status->fetch' might be undef! ###
229     my $rv = $cb->local_mirror( path => 'dummy-localmirror' );
230     ok( $rv,                        "Local mirror created" );
231     
232     for my $mod ( values %{ $cb->module_tree } ) {
233         my $name    = $mod->module;
234         
235         my $cksum   = File::Spec->catfile(
236                         dirname($mod->status->fetch),
237                         CHECKSUMS );
238         ok( -e $mod->status->fetch, "   Module '$name' fetched" );
239         ok( -s _,                   "       Module '$name' has size" );
240         ok( -e $cksum,              "   Checksum fetched for '$name'" );
241         ok( -s _,                   "       Checksum for '$name' has size" );
242     }      
243
244     $conf->set_conf( md5 => $old_md5 );
245 }    
246
247 ### check ENV variable
248 {   ### process id
249     {   my $name = 'PERL5_CPANPLUS_IS_RUNNING';
250         ok( $ENV{$name},            "Env var '$name' set" );
251         is( $ENV{$name}, $$,        "   Set to current process id" );
252     }
253
254     ### Version    
255     {   my $name = 'PERL5_CPANPLUS_IS_VERSION';
256         ok( $ENV{$name},            "Env var '$name' set" );
257
258         ### version.pm formats ->VERSION output... *sigh*
259         is( $ENV{$name}, $Class->VERSION,        
260                                     "   Set to current process version" );
261     }
262     
263 }
264
265 __END__    
266                                           
267 # Local variables:
268 # c-indentation-style: bsd
269 # c-basic-offset: 4
270 # indent-tabs-mode: nil
271 # End:
272 # vim: expandtab shiftwidth=4:                    
273