Test-ModuleVersion
view release on metacpan or search on metacpan
lib/Test/ModuleVersion.pm view on Meta::CPAN
my $libs = ref $self->lib ? $self->lib : [$self->lib];
$code .= "use FindBin;\n";
$code .= qq|use lib "\$FindBin::Bin/$_";\n| for @$libs;
# Before
$code .= $self->before . "\n";
# Reffer this module
$code .= "# Created by Test::ModuleVersion $Test::ModuleVersion::VERSION\n";
# Test code
$code .= <<'EOS';
use Test::More;
use strict;
use warnings;
use ExtUtils::Installed;
EOS
# Main
$code .= <<'EOS';
sub main {
my $command = shift;
my @options = @_;
die qq/command "$command" is unkonwn command/
if defined $command && $command ne 'list';
my $list_failed;
my $lwp = 'auto';
for my $option (@options) {
if ($option eq '--fail') { $list_failed = 1 }
elsif ($option eq '--lwp') { $lwp = 'use' }
elsif ($option eq '--no-lwp') { $lwp = 'no' }
else { die qq/list $option is unknown option/ }
}
if (defined $command) {
my $builder = Test::More->builder;
open my $out_fh, '>', undef;
$builder->output($out_fh);
$builder->failure_output($out_fh);
$builder->todo_output($out_fh);
}
my $modules = [];
my $failed = [];
my $require_ok;
my $version_ok;
my $version;
plan tests => <%%%%%% test_count %%%%%%>;
EOS
# Module and version check
my $test_count = 0;
for my $m (@{$self->modules}) {
my ($module, $version) = @$m;
$code .= " # $module\n"
. " \$require_ok = require_ok('$module');\n"
. " \$version_ok = is(\$${module}::VERSION, '$version', '$module version: $version');\n"
. " push \@\$modules, ['$module' => '$version'];\n"
. " push \@\$failed, ['$module' => '$version'] unless \$require_ok && \$version_ok;\n\n";
$test_count += 2;
}
# Print module URLs
$code .= <<'EOS';
# Print module URLs
if (defined $command) {
my $distnames = <%%%%%% distnames %%%%%%>
;
my $privates = <%%%%%% privates %%%%%%>
;
my $tm = Test::ModuleVersion->new;
my @ms = $command eq 'list' && $list_failed ? @$failed
: $command eq 'list' ? @$modules
: [];
for my $m (@ms) {
my ($module, $version) = @$m;
my $mu = Test::ModuleVersion::ModuleURL->new;
$mu->distnames($distnames);
$mu->privates($privates);
$mu->lwp($lwp);
my $url = $mu->get($module, $version);
if (defined $url) { print "$url\n" }
else { print STDERR $mu->error . "\n" }
}
}
}
EOS
# Embbed Test::ModuleVersion
$code .= $self->_source . "\n";
# Run
$code .= "package main;\n"
. "main(\@ARGV);\n";
# Test count
$code =~ s/<%%%%%% test_count %%%%%%>/$test_count/e;
# Distribution names
my $distnames_code = Data::Dumper->new([$self->distnames])->Terse(1)->Indent(2)->Dump;
$code =~ s/<%%%%%% distnames %%%%%%>/$distnames_code/e;
# Private repositories
my $privates_code = Data::Dumper->new([$self->privates])->Terse(1)->Indent(2)->Dump;
$code =~ s/<%%%%%% privates %%%%%%>/$privates_code/e;
if (my $file = $opts{output}) {
open my $fh, '>', $file
or die qq/Can't open file "$file": $!/;
print $fh $code;
}
return $code;
}
sub _source {
lib/Test/ModuleVersion.pm view on Meta::CPAN
=head1 NAME
Test::ModuleVersion - Module version test generator
=head1 CAUTION
(2013/3/20)
Sorry. This module is DEPRECATED because L<cpanm> and L<cpanfile> is much better.
If you want to install moudles, use L<cpanm> and L<cpanfile> instead.
See L<https://github.com/kraih/mojo/wiki/Installation-of-cpan-modules-by-cpanm-and-cpanfile>
If you want to test module version, you write test by yourself.
is($DBIx::Custom::VERSION, '0.2108');
This module will be removed from CPAN on 2018/3/1
=head1 SYNOPSIS
use Test::ModuleVersion;
my $tm = Test::ModuleVersion->new;
$tm->modules([
['DBIx::Custom' => '0.2108'],
['Validator::Custom' => '0.1426']
]);
$tm->test_script(output => 't/module.t');
=head1 DESCRIPTION
L<Test::ModuleVersion> is test generator for module version check.
If you run the test generated by L<Test::ModuleVersion>,
you can check the module version.
If module version test is failed, you can list module URLs.
=head2 Create version test
Let's create version test.
# mvt.pl
my $tm = Test::ModuleVersion->new;
$tm->modules([
['DBIx::Custom' => '0.2108'],
['Validator::Custom' => '0.1426']
]);
$tm->test_script(output => 't/module.t');
C<modules> attribute is set to the pairs of module and version.
C<test_script> method print version test into C<t/module.t> file.
Run C<mvt.pl>
$ perl mvt.pl
Test script C<t/module.t> is created.
...
$require_ok = require_ok('DBIx::Custom');
$version_ok = is($DBIx::Custom::VERSION, '0.2108', 'DBIx::Custom version: 0.2108');
$require_ok = require_ok('Validator::Custom');
$version_ok = is($Validator::Custom::VERSION, '0.1426', 'DBIx::Custom version: 0.1426');
...
=head2 Run version test
Run version test.
$ perl t/module.t
If module is not installed or version is different,
test fail.
ok 1 - require DBIx::Custom;
not ok 2 - DBIx::Custom version: 0.2108
# Failed test 'DBIx::Custom version: 0.2108'
# at t/module.t.pl line 13.
# got: '0.2106'
# expected: '0.2108'
ok 2 - require Validator::Custom;
ok 3 - Validator::Custom version: 0.1426
=head2 List module URLs
You can list moudle URLs by C<list> command
$ perl t/module.t list
All module URLs in version test is output to C<STDOUT>.
http://cpan.metacpan.org/authors/id/K/KI/KIMOTO/DBIx-Custom-0.2108.tar.gz
...
You can list only test failed module URLs by C<--fail> option
$ perl t/module.t list --fail
=head1 Advanced
=head2 Module installation by L<cpanm>
$ perl t/module.t list --fail | perl cpanm -L extlib
Module installation is very easy. Test failed module
is installed into C<extlib> directory by L<cpanm>.
=head2 HTTP client
L<Test::Module> version switch two HTTP client as necessary.
=over 2
=item 1. LWP::UserAgent
=item 2. HTTP::Tiny
=back
These module is used to get module URLs from metaCPAN.
( run in 0.676 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )