<?xml version="1.0" encoding="windows-1252"?>
<node id="942886" title="Re: How to retain a CPAN module's unit tests and run them later." created="2011-12-10 19:01:58" updated="2011-12-10 19:01:58">
<type id="11">
note</type>
<author id="757127">
tobyink</author>
<data>
<field name="doctext">
&lt;p&gt;This does the trick. Usage notes at end.&lt;/p&gt;

&lt;code&gt;
use 5.010;
use autodie;
use common::sense;
use MooseX::Declare;

BEGIN
{
	$CPAN::Retest::AUTHORITY = 'cpan:TOBYINK';
	$CPAN::Retest::VERSION   = '0.001';
}

class CPAN::Retest
{
	use App::Prove qw//;
	use File::Basename qw/fileparse/;
	use File::pushd qw/pushd/;
	use File::Path qw/make_path/;
	use File::Spec qw//;
	use File::Temp qw//;
	use LWP::Simple qw/get/;
	use Module::Manifest qw//;
	use Object::AUTHORITY qw/AUTHORITY/;
		
	has author    =&gt; (is =&gt; 'ro', isa =&gt; 'Str', required =&gt; 1);
	has release   =&gt; (is =&gt; 'ro', isa =&gt; 'Str', required =&gt; 1);
	has manifest  =&gt; (is =&gt; 'rw', isa =&gt; 'ArrayRef[Str]', lazy =&gt; 1, builder =&gt; '_build_manifest');
	has testdir   =&gt; (is =&gt; 'ro', isa =&gt; 'File::Temp::Dir', lazy =&gt; 1, builder =&gt; '_build_testdir');
	has verbose   =&gt; (is =&gt; 'ro');
	
	method url_for ($file)
	{
		sprintf(
			'http://api.metacpan.org/source/%s/%s/%s',
			uc $self-&gt;author,
			$self-&gt;release,
			$file
			);
	}

	method test_files
	{
		grep { m{^t/} } @{ $self-&gt;manifest };
	}
	
	method _build_manifest
	{
		my $fh = File::Temp-&gt;new;
		binmode( $fh, ":utf8");
		
		print $fh get($self-&gt;url_for('MANIFEST'));
		close $fh;
		
		my $manifest = Module::Manifest-&gt;new;
		$manifest-&gt;open(manifest =&gt; $fh-&gt;filename);
		return [ $manifest-&gt;files ];
	}
	
	method _build_testdir
	{
		my $testdir = File::Temp-&gt;newdir;
		
		foreach my $file ($self-&gt;test_files)
		{
			my $dest = File::Spec-&gt;catfile($testdir-&gt;dirname, $file);
			
			my (undef, $d, undef) = fileparse($dest);
			make_path($d);
			
			open my $fh, '&gt;', $dest;
			print $fh get($self-&gt;url_for($file));
			close $fh;
		}
		
		return $testdir;
	}
	
	method run
	{
		my $chdir = pushd($self-&gt;testdir-&gt;dirname);
		my $app = App::Prove-&gt;new;
		$app-&gt;process_args('t');
		$app-&gt;verbose(1) if $self-&gt;verbose;
		$app-&gt;run;
		
	}	
}
&lt;/code&gt;

&lt;p&gt;&lt;b&gt;Usage:&lt;/b&gt;&lt;/p&gt;

&lt;code&gt;
my $test = CPAN::Retest-&gt;new(
  author  =&gt; 'TOBYINK',
  release =&gt; 'Object-AUTHORITY-0.003',
  verbose =&gt; 1,
  );
$test-&gt;run;
&lt;/code&gt;

&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;This is now available on CPAN:
&lt;a href="https://metacpan.org/release/TOBYINK/App-Reprove-0.001"&gt;App-Reprove-0.001&lt;/a&gt;.&lt;/p&gt;</field>
<field name="root_node">
942873</field>
<field name="parent_node">
942873</field>
</data>
</node>
