File Coverage

blib/lib/Test/CPANpm.pm
Criterion Covered Total %
statement 31 55 56.3
branch 0 4 0.0
condition n/a
subroutine 10 12 83.3
pod 2 2 100.0
total 43 73 58.9


line stmt bran cond sub pod time code
1             package Test::CPANpm;
2              
3 1     1   865 use 5.006;
  1         3  
  1         35  
4 1     1   4 use strict;
  1         2  
  1         31  
5 1     1   14 use warnings;
  1         2  
  1         28  
6 1     1   474 use Test::CPANpm::Fake;
  1         5  
  1         178  
7 1     1   9 use Test::More;
  1         1  
  1         26  
8 1     1   1638 use Exporter;
  1         2  
  1         40  
9 1     1   6 use base q(Exporter);
  1         3  
  1         97  
10 1     1   6 use Cwd qw(getcwd);
  1         2  
  1         589  
11              
12             our @EXPORT = qw(cpan_depends_ok cpan_depends_ok_force_missing);
13             our $VERSION = '0.010';
14              
15             return 1;
16              
17             sub cpan_depends_ok {
18 1     1 1 612 my($deps, $test_name) = @_;
19 1         3 my @actual;
20 1         31 my @deps = sort @$deps;
21              
22 1         5 my($out, $in) = change_std();
23             run_with_cpan_config {
24 1     1   4 my $dist_dir = dist_dir('.');
25 1         32 @actual = get_prereqs($dist_dir);
26 0           @actual = sort(@actual);
27 1         10 };
28 0           restore_std($out, $in);
29              
30 0           my $test = Test::More->builder;
31 0 0         if(eq_array(\@actual, \@deps)) {
32 0           $test->ok(1, $test_name);
33             } else {
34 0           diag("Expected dependancies: @deps, Actual: @actual");
35 0           $test->ok(0, $test_name);
36             }
37             }
38              
39             sub cpan_depends_ok_force_missing {
40 0     0 1   my($deps, $missing, $test_name) = @_;
41 0           my @actual;
42 0           my @deps = sort @$deps;
43              
44 0           my($out, $in) = change_std();
45             run_with_cpan_config {
46 0     0     my $dist_dir = dist_dir('.');
47 0           my %missing = map { $_ => 0 } @$missing;
  0            
48 0           @actual = run_with_fake_modules { get_prereqs($dist_dir); } %missing;
  0            
49 0           @actual = sort(@actual);
50 0           };
51 0           restore_std($out, $in);
52              
53 0           my $test = Test::More->builder;
54 0 0         if(eq_array(\@actual, \@deps)) {
55 0           $test->ok(1, $test_name);
56             } else {
57 0           diag("Expected dependancies: @deps, Actual: @actual");
58 0           $test->ok(0, $test_name);
59             }
60             }
61              
62             =pod
63              
64             =head1 NAME
65              
66             Test::CPANpm - Test a distribution's interaction with CPAN before uploading.
67              
68             =head1 SYNOPSIS
69              
70             use Test::CPANpm;
71             use Test::More qw(no_plan);
72            
73             cpan_depends_ok(
74             ['CGI', 'Module::Build', 'Acme::Wabbit'],
75             'got the right dependancies'
76             );
77              
78             cpan_depends_ok_force_missing(
79             ['Some::Module::Build::Subclass', 'CGI', 'Module::Build', 'Acme::Wabbit'],
80             ['Some::Module::Build::Subclass'],
81             'got dependancies even though our Module::Build subclass is missing'
82             );
83            
84             =head1 DESCRIPTION
85              
86             Test::CPANpm fools CPAN.pm into thinking it has downloaded and unpacked your
87             package, then has it attempt to generate a C or C script.
88             After this process completes, it asks your CPAN module what dependancies
89             it thinks exist.
90              
91             If you just want to make sure your distribution is packaged in a way that
92             is good for CPAN, consider using L instead. The main
93             time that C is useful is when you depend on modules inside your
94             C or C script and you want to make sure that you
95             degrade gracefully if those modules are not available.
96              
97             =head1 TESTS
98              
99             =over
100              
101             =item cpan_depends_ok([modules], $test_name)
102              
103             Generate a distribution directory and tell CPAN to process it. The test will
104             pass if your distribution depends on the exact modules listed in [modules].
105              
106             =item cpan_depends_ok_force_missing([modules], [force-missing], $test_name)
107              
108             Create a bunch of modules that will fail to load, named in the [force-missing]
109             array. Preprend this to our C<@INC>, then do the C test
110             above. This is useful if, say, you have a Module::Build subclass, and you want
111             to verify that your C script whines about this subclass missing in
112             a way that CPAN can understand.
113              
114             The reason the fake modules are generated, is to prevent the already-installed
115             modules on your system from interfereing with this test.
116              
117             Example: Given a C that contains the following:
118              
119             my $build;
120              
121             our %opts = (
122             module_name => 'My::Module::Subclass',
123             license => 'perl',
124             requires => {
125             'Test::CPANpm' => '0',
126             # My::Module provides My::Module::Build
127             'My::Module' => '0',
128             },
129             create_makefile_pl => 'passthrough',
130             );
131              
132             eval { require My::Module::Build; };
133              
134             if($@) {
135             warn "My::Module::Build is required to build this module!";
136             $opts{requires}{'My::Module::Build'} = 0;
137             # setting installdirs to an empty hash makes "./Build install" fail,
138             # but we'll still get a "Build" script/Makefile that CPAN can use to
139             # find prereqs
140             $build = Module::Build->new(%opts, installdirs => {});
141             } else {
142             $build = My::Module::Build->new(%opts);
143             }
144              
145             $build->create_build_script;
146              
147             The following tests would be expected to pass:
148              
149             cpan_depends_ok(
150             ['My::Module', 'Test::CPANpm'],
151             'CPAN sees basic dependancies'
152             );
153            
154             cpan_depends_ok_force_missing(
155             [
156             'My::Module', 'Test::CPANpm', 'My::Module::Build'
157             ],
158             [
159             'My::Module::Build'
160             ],
161             'CPAN complains if My::Module::Build is missing'
162             );
163              
164             =back
165              
166             =head1 CAVEAT
167              
168             You must have a C or C script in the current working directory
169             for C to work. It will call the "distdir" command on that script
170             in order to build it's test environment. (This also means that your MANIFEST
171             needs to be up-to-date for the tests to usually pass... but of course your
172             MANIFEST needs to be up-to-date before you can upload to CPAN anyway, right?)
173              
174             =head1 TODO
175              
176             I'm rushing this package out because I have another package whose testability
177             depends on these functions, but there's more I'd like to see in here:
178              
179             =over
180              
181             =item Test that you depend on particular versions of modules
182              
183              
184             =item Test that a "Makefile" is actually created
185              
186             This is tacitly, implicitly done already by cpan_depends_ok, since
187             you won't get any dependancy information out of CPAN without a Makefile,
188             but an explicit test would still be good.
189              
190             =item Only generate distdir once for multiple tests
191              
192              
193             =item Check that the modules you depend on actually are on CPAN
194              
195             Right now we just test that you depend on certain modules, there is no
196             check to see if they are actually available.
197              
198             =item Tests that use CPANPLUS
199              
200             CPANPLUS is supposed to behave more-or-less the same as CPAN given a
201             distribution, but it'd be nice to C it.
202              
203             =back
204              
205             =head1 SEE ALSO
206              
207             L, L, L, L,
208             L
209              
210             =head1 AUTHOR
211              
212             Tyler "Crackerjack" MacDonald
213              
214             =head1 LICENSE
215              
216             Copyright 2006 Tyler MacDonald.
217              
218             This is free software; you may redistribute it under the same terms as perl itself.
219              
220             =cut