App-MechaCPAN

 view release on metacpan or  search on metacpan

t/07_tmpdata.t  view on Meta::CPAN

use strict;
use FindBin;
use Test::More;
use Cwd qw/cwd/;
use File::Temp qw/tempdir/;

require q[./t/helper.pm];

my $pwd = cwd;
my $tmpdir = tempdir( TEMPLATE => File::Spec->tmpdir . "/mechacpan_t_XXXXXXXX", CLEANUP => 1 );
local $App::MechaCPAN::PROJ_DIR = $tmpdir;
chdir $tmpdir;
my $dir = cwd;

my $test_filename  = 'v0.24.zip';
my $test_url       = "https://github.com/p5sagit/Try-Tiny/archive/$test_filename";
my $type           = join( '', map { ( 'a' .. 'z' )[ int rand 26 ] } ( 1 .. 4 ) );
my $humane_pattern = qr{ mecha_ .*? [.] [\d_]* [.] [^.]* }xms;
my $type_pattern   = qr{ mecha_$type [.] [\d_]* [.] X* }xms;
my $log_pattern    = qr{ mecha_log [.] [\d_]* [.] }xms;

like( App::MechaCPAN::humane_tmpname($type), qr/^$type_pattern$/, 'Humane name is maintained' );

# Test log names
{
  my $log_line;
  local *App::MechaCPAN::info = sub { $log_line = shift };
  App::MechaCPAN::_setup_log($tmpdir);
  isnt( $log_line, undef, 'Logging tells you where it will log to' );
  like( $log_line, $log_pattern, 'Logging to an approriate file name' );
}

# Test temp downloads
SKIP:
{
  skip 'Cannot use https', 6
    if !App::MechaCPAN::can_https();

  my $where  = App::MechaCPAN::fetch_file($test_url);
  my $where2 = "$where";

  is( $where, $where2, 'Download file sanity check' );
  isa_ok( $where, 'File::Temp' );
  is( -e $where, 1, 'Was able to download file correctly' );
  like( $where, qr{^ $tmpdir /local/tmp/ $humane_pattern $}xms, 'Downloaded tmp file is humane' );
  like( $where, qr{ \Q$test_filename\E }xms, 'Downloaded tmp file includes original filename' );

  undef $where;
  is( -e $where2, undef, q{Undef'ing $where removes the file} );
}

# Test relative directory downloads
SKIP:
{
  skip 'Cannot use https', 5
    if !App::MechaCPAN::can_https();

  my $where = App::MechaCPAN::fetch_file( $test_url => 'pkg/' );
  my $where2 = "$where";

  is( $where,     $where2, 'Download file sanity check' );
  is( ref $where, '',      'Downloaded file is not a File::Temp' );
  is( -e $where,  1,       'Was able to download file correctly' );
  like( $where, qr{ $tmpdir /local/pkg/ $test_filename }xms, 'Downloaded tmp file is not humane' );

  undef $where;
  is( -e $where2, 1, q{Undef'ing $where does not removes the file} );
  unlink $where2;
}

# Test relative file downloads
SKIP:
{
  skip 'Cannot use https', 5
    if !App::MechaCPAN::can_https();



( run in 1.105 second using v1.01-cache-2.11-cpan-39bf76dae61 )