Skip to content

Commit

Permalink
Merge 1962e46 into 8fb35df
Browse files Browse the repository at this point in the history
  • Loading branch information
gugod committed Jul 27, 2022
2 parents 8fb35df + 1962e46 commit f634aad
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/App/Perlbrew/HTTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ sub http_download {
}
unless ($status == 0) {
$path->unlink;
return "ERROR: Failed to execute the command\n\n\t$download_command\n\nReason:\n\n\t$?";
if ($? == -1) {
return "ERROR: Failed to execute the command\n\n\t$download_command\n\nReason:\n\n\t$!";
}
elsif ($? & 127) {
return "ERROR: The command died with signal " . ($? & 127) . "\n\n\t$download_command\n\n";
}
else {
return "ERROR: The command finished with error\n\n\t$download_command\n\nReason:\n\n\t" . ($? >> 8);
}
}
return 0;
}
Expand Down
26 changes: 26 additions & 0 deletions t/error-http_download-exec-error.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env perl
use strict;
use warnings;

use Test::More import => [ qw( done_testing like subtest ) ];
use File::Temp qw( tempdir );

BEGIN {
*CORE::GLOBAL::system = sub {
return $? = -1
};
}

use App::Perlbrew::Path;
use App::Perlbrew::HTTP qw(http_download);

local $ENV{PERLBREW_ROOT} = $App::perlbrew::PERLBREW_ROOT = tempdir( CLEANUP => 1 );

subtest "The exit status code of curl", sub {
my $error = http_download( "https://example.com/whatever.tar.gz",
App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT)->child("whatever.tar.gz") );

like $error, qr/^ERROR: Failed to execute the command/;
};

done_testing;
28 changes: 28 additions & 0 deletions t/error-http_download-exit-nonzero.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env perl
use strict;
use warnings;

use Test::More import => [ qw( done_testing like subtest ) ];
use File::Temp qw( tempdir );

my $actual_status_code = 42;

BEGIN {
*CORE::GLOBAL::system = sub {
return $? = $actual_status_code << 8;
};
}

use App::Perlbrew::Path;
use App::Perlbrew::HTTP qw(http_download);

local $ENV{PERLBREW_ROOT} = $App::perlbrew::PERLBREW_ROOT = tempdir( CLEANUP => 1 );

subtest "The exit status code of curl", sub {
my $error = http_download( "https://example.com/whatever.tar.gz",
App::Perlbrew::Path->new($App::perlbrew::PERLBREW_ROOT)->child("whatever.tar.gz") );

like $error, qr/^ERROR .+ Reason .+ ${actual_status_code}/xs;
};

done_testing;
22 changes: 22 additions & 0 deletions t/error-http_download-param-validation.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use strict;
use warnings;
use Test::More;
use Test::Exception;
use File::Temp qw(tempdir);
use IO::All;
use App::Perlbrew::HTTP qw(http_download);

subtest "http_download: dies when when the download target already exists" => sub {
my $dir = tempdir( CLEANUP => 1 );
my $output = "$dir/whatever";

io($output)->print("so");

my $error;
throws_ok {
$error = http_download( "https://install.perlbrew.pl", $output );
}
qr(^ERROR: The download target < \Q$output\E > already exists\.$);
};

done_testing;

0 comments on commit f634aad

Please sign in to comment.