Devel-Cover-Report-Coveralls
view release on metacpan or search on metacpan
t/01_basic.t view on Meta::CPAN
use strict;
use warnings;
use utf8;
use Test::More;
use FindBin;
use Cwd ();
use Digest::MD5 ();
use JSON::PP qw(decode_json);
use File::Basename ();
use File::Temp ();
use Devel::Cover::Report::Coveralls;
my $normal_endpoint = 'https://coveralls.io/api/v1/jobs';
my $endpoint_stem = '/api/v1/jobs';
# The variables that get_config and get_git_info read to pick the CI service, or the values for it.
# Every get_config subtest clears them all with `local @ENV{@CI_VARS}`, so that the CI running the
# tests does not leak in.
my @CI_VARS = qw(
TRAVIS TRAVIS_JOB_ID TRAVIS_PULL_REQUEST
CIRCLECI CIRCLE_WORKFLOW_ID CIRCLE_PULL_REQUEST CI_PULL_REQUEST CIRCLE_BRANCH CIRCLE_SHA1
DRONE DRONE_BUILD_NUMBER DRONE_PULL_REQUEST
SEMAPHORE SEMAPHORE_BUILD_NUMBER SEMAPHORE_WORKFLOW_ID SEMAPHORE_GIT_PR_NUMBER
JENKINS_URL BUILD_NUMBER BUILD_ID BRANCH_NAME CHANGE_BRANCH CHANGE_ID
GITLAB_CI CI_PIPELINE_IID CI_JOB_NAME CI_MERGE_REQUEST_IID CI_COMMIT_REF_NAME CI_COMMIT_SHA
TF_BUILD SYSTEM_TEAMFOUNDATIONSERVERURI BUILD_BUILDID SYSTEM_PULLREQUEST_PULLREQUESTNUMBER
BUILD_SOURCEVERSION BUILD_SOURCEBRANCHNAME
GITHUB_ACTIONS GITHUB_TOKEN GITHUB_RUN_ID GITHUB_RUN_ATTEMPT GITHUB_JOB GITHUB_SHA
GITHUB_REF GITHUB_REF_NAME GITHUB_HEAD_REF GITHUB_EVENT_PATH
COVERALLS_REPO_TOKEN COVERALLS_ENDPOINT COVERALLS_FLAG_NAME COVERALLS_PARALLEL
COVERALLS_PERL_SERVICE_NAME COVERALLS_NO_FAIL
COVERALLS_SERVICE_NAME COVERALLS_SERVICE_NUMBER COVERALLS_SERVICE_JOB_ID
COVERALLS_SERVICE_JOB_NUMBER COVERALLS_PULL_REQUEST COVERALLS_GIT_BRANCH COVERALLS_GIT_COMMIT
COVERALLS_EVENT_TYPE
GIT_ID GIT_BRANCH GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
GIT_MESSAGE
);
subtest 'get_config (travis)' => sub {
local @ENV{@CI_VARS};
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{TRAVIS} = 'true';
local $ENV{TRAVIS_JOB_ID} = 100000;
local $ENV{TRAVIS_PULL_REQUEST} = 'false';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_job_id}, 100000, 'config service_job_id';
is $got->{service_name}, 'travis-ci', 'config service_name';
ok !exists $got->{service_pull_request},'not a pull request';
local $ENV{TRAVIS_PULL_REQUEST} = '456';
($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_pull_request},'456','config pull request';
is $endpoint, $normal_endpoint, 'config endpoint';
};
subtest 'get_config extra env (travis)' => sub {
local @ENV{@CI_VARS};
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{TRAVIS} = 'true';
local $ENV{TRAVIS_JOB_ID} = 100000;
my $diff_endpoint = 'http://localhost';
local $ENV{COVERALLS_ENDPOINT} = $diff_endpoint;
local $ENV{COVERALLS_FLAG_NAME} = 'Unit';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_job_id}, 100000, 'config service_job_id';
is $got->{service_name}, 'travis-ci', 'config service_name';
is $got->{flag_name}, 'Unit', 'config flag_name';
is $endpoint, $diff_endpoint . $endpoint_stem, 'new endpoint';
};
subtest 'get_config github actions (push)' => sub {
local @ENV{@CI_VARS};
local $ENV{GITHUB_ACTIONS} = 'true';
local $ENV{GITHUB_TOKEN} = 'from-github';
local $ENV{GITHUB_RUN_ID} = '123456789';
local $ENV{GITHUB_RUN_ATTEMPT} = '2';
local $ENV{GITHUB_JOB} = 'test';
local $ENV{GITHUB_SHA} = 'abc123';
local $ENV{GITHUB_REF} = 'refs/heads/master';
local $ENV{GITHUB_REF_NAME} = 'master';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'github', 'service_name';
is $got->{repo_token}, 'from-github', 'GITHUB_TOKEN without COVERALLS_REPO_TOKEN';
is $got->{service_number}, '123456789', 'service_number is the run';
is $got->{service_attempt}, '2', 'service_attempt';
is $got->{service_job_id}, 'test', 'service_job_id is the job';
is $got->{service_branch}, 'master', 'service_branch';
is $got->{commit_sha}, 'abc123', 'commit_sha';
ok !exists $got->{service_pull_request}, 'not a pull request';
is $endpoint, $normal_endpoint;
local $ENV{COVERALLS_REPO_TOKEN} = 'from-coveralls';
local $ENV{COVERALLS_FLAG_NAME} = 'perl-5.20';
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{repo_token}, 'from-coveralls', 'COVERALLS_REPO_TOKEN first';
is $got->{service_job_id}, 'test-perl-5.20', 'the flag name tells the legs of a matrix apart';
};
subtest 'get_config github actions (pull request)' => sub {
local @ENV{@CI_VARS};
local $ENV{GITHUB_ACTIONS} = 'true';
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{GITHUB_REF} = 'refs/pull/49/merge';
local $ENV{GITHUB_REF_NAME} = '49/merge';
local $ENV{GITHUB_HEAD_REF} = 'feature';
my ($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_pull_request}, '49', 'service_pull_request from GITHUB_REF';
is $got->{service_branch}, 'feature', 'the source branch, not 49/merge';
ok !exists $got->{service_job_id}, 'no service_job_id without GITHUB_JOB';
local $ENV{COVERALLS_FLAG_NAME} = 'unit';
($got) = Devel::Cover::Report::Coveralls::get_config();
ok !exists $got->{service_job_id}, 'nor with only the flag name';
};
subtest 'get_config github actions (pull_request_target)' => sub {
local @ENV{@CI_VARS};
local $ENV{GITHUB_ACTIONS} = 'true';
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{GITHUB_REF} = 'refs/heads/master';
local $ENV{GITHUB_HEAD_REF} = 'feature';
local $ENV{GITHUB_EVENT_PATH} = "$FindBin::Bin/no-such-event.json";
my ($got) = Devel::Cover::Report::Coveralls::get_config();
ok !exists $got->{service_pull_request}, 'no number without an event file';
my $dir = File::Temp::tempdir(CLEANUP => 1);
my $event = "$dir/event.json";
open my $fh, '>', $event or die $!;
print $fh '{"pull_request":{"number":12}}';
close $fh;
local $ENV{GITHUB_EVENT_PATH} = $event;
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_pull_request}, '12', 'the number from the event payload';
is $got->{service_branch}, 'feature', 'service_branch';
};
subtest 'get_config github actions (older runner)' => sub {
local @ENV{@CI_VARS};
local $ENV{GITHUB_ACTIONS} = 'true';
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{GITHUB_REF} = 'refs/heads/release/1.0';
my ($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_branch}, 'release/1.0', 'the branch from GITHUB_REF without GITHUB_REF_NAME';
};
subtest 'GITHUB_TOKEN alone is not GitHub Actions' => sub {
local @ENV{@CI_VARS};
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{GITHUB_TOKEN} = 'set for another tool';
my ($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'coveralls-perl', 'service_name';
is $got->{repo_token}, 'abcdef', 'the token is not GITHUB_TOKEN';
};
subtest 'get_config azure' => sub {
local @ENV{@CI_VARS};
local $ENV{SYSTEM_TEAMFOUNDATIONSERVERURI} = 1;
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{BUILD_SOURCEBRANCHNAME} = 'feature';
local $ENV{BUILD_BUILDID} = '123456789';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'azure-pipelines', 'config service_name';
is $got->{service_number}, '123456789', 'config service_number';
is $got->{service_branch}, 'feature', 'config service_branch';
is $endpoint, $normal_endpoint;
my $git = Devel::Cover::Report::Coveralls::apply_ci_git_info($got,
Devel::Cover::Report::Coveralls::get_git_info());
is $git->{branch}, 'feature', 'the branch of Azure Pipelines over the one of git';
local $ENV{COVERALLS_GIT_BRANCH} = 'from-env';
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_branch}, 'from-env', 'COVERALLS_GIT_BRANCH over BUILD_SOURCEBRANCHNAME';
};
subtest 'get_config drone' => sub {
local @ENV{@CI_VARS};
local $ENV{DRONE_PULL_REQUEST} = '666';
local $ENV{DRONE_BUILD_NUMBER} = '123';
local $ENV{DRONE} = "drone";
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'drone', 'config service_name';
is $got->{service_number}, '123', 'config service_number';
is $got->{service_pull_request}, '666', 'config service_pull_request';
is $endpoint, $normal_endpoint;
};
subtest 'get_config local' => sub {
local @ENV{@CI_VARS};
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'coveralls-perl', 'config service_name';
is $got->{service_event_type}, 'manual', 'config service_event_type';
is $endpoint, $normal_endpoint;
for my $name (qw(
COVERALLS_SERVICE_NAME COVERALLS_PERL_SERVICE_NAME COVERALLS_SERVICE_NUMBER
COVERALLS_SERVICE_JOB_ID COVERALLS_SERVICE_JOB_NUMBER
)) {
local $ENV{$name} = 'buildkite';
($got) = Devel::Cover::Report::Coveralls::get_config();
ok !exists $got->{service_event_type}, "not manual with $name";
}
{
local $ENV{COVERALLS_SERVICE_NAME} = '';
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_event_type}, 'manual', 'still manual with an empty COVERALLS_SERVICE_NAME';
}
{
local $ENV{COVERALLS_EVENT_TYPE} = '';
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_event_type}, 'manual', 'an empty COVERALLS_EVENT_TYPE is not an override';
}
{
local $ENV{GITHUB_ACTIONS} = 'true';
local $ENV{COVERALLS_EVENT_TYPE} = 'push';
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'github', 'a detected CI service';
is $got->{service_event_type}, 'push', 'COVERALLS_EVENT_TYPE on a detected CI service';
}
local $ENV{COVERALLS_EVENT_TYPE} = 'push';
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_event_type}, 'push', 'COVERALLS_EVENT_TYPE';
local $ENV{COVERALLS_SERVICE_NAME} = 'buildkite';
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_event_type}, 'push', 'COVERALLS_EVENT_TYPE with a service name';
};
subtest 'get_config overrides from the environment' => sub {
local @ENV{@CI_VARS};
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{GITHUB_ACTIONS} = 'true';
local $ENV{GITHUB_RUN_ID} = '111';
local $ENV{GITHUB_JOB} = 'test';
local $ENV{GITHUB_SHA} = 'from-github';
local $ENV{COVERALLS_SERVICE_NAME} = 'my-ci';
local $ENV{COVERALLS_SERVICE_NUMBER} = '42';
local $ENV{COVERALLS_SERVICE_JOB_ID} = 'job-1';
local $ENV{COVERALLS_SERVICE_JOB_NUMBER} = '42.1';
local $ENV{COVERALLS_PULL_REQUEST} = '7';
local $ENV{COVERALLS_GIT_BRANCH} = 'topic';
local $ENV{COVERALLS_GIT_COMMIT} = 'from-env';
my ($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'my-ci', 'COVERALLS_SERVICE_NAME';
is $got->{service_number}, '42', 'COVERALLS_SERVICE_NUMBER over GITHUB_RUN_ID';
is $got->{service_job_id}, 'job-1', 'COVERALLS_SERVICE_JOB_ID over GITHUB_JOB';
is $got->{service_job_number}, '42.1', 'COVERALLS_SERVICE_JOB_NUMBER';
is $got->{service_pull_request}, '7', 'COVERALLS_PULL_REQUEST';
is $got->{service_branch}, 'topic', 'COVERALLS_GIT_BRANCH';
is $got->{commit_sha}, 'from-env', 'COVERALLS_GIT_COMMIT over GITHUB_SHA';
local $ENV{COVERALLS_PERL_SERVICE_NAME} = 'perl-ci';
t/01_basic.t view on Meta::CPAN
};
subtest 'report exits non-zero when the upload fails' => sub {
local @ENV{@CI_VARS};
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
my $db = bless {}, 'Local::FakeDB';
no warnings 'once', 'redefine';
local *Local::FakeDB::cover = sub { bless {}, 'Local::FakeCover' };
# Never reach Coveralls from the tests.
my ($response, @request);
local *HTTP::Tiny::request = sub { (undef, @request) = @_; $response };
my $run = sub {
my $out = '';
open my $fh, '>', \$out or die $!;
my $old = select $fh;
my $ok = eval { Devel::Cover::Report::Coveralls->report($db, { file => [] }); 1 };
my $err = $@;
select $old;
return ($ok, $err, $out);
};
$response = { success => '', status => 422, reason => 'Unprocessable Entity',
content => '{"message":"Couldn\'t find a repository matching this job."}' };
my ($ok, $err, $out) = $run->();
ok !$ok, 'dies on an API error';
like $err, qr/COVERALLS_NO_FAIL/, 'says how to exit 0';
like $out, qr/^error: Couldn't find a repository/m, 'prints the error';
$response = { success => '', status => 599, reason => 'Internal Exception',
content => "Could not connect to 'coveralls.io:443'\n" };
($ok, $err, $out) = $run->();
ok !$ok, 'dies on a failed connection';
like $out, qr/^error: 599 Internal Exception: Could not connect/m, 'prints the error';
{
local $ENV{COVERALLS_REPO_TOKEN};
($ok, $err, $out) = $run->();
ok !$ok, 'dies without a repo token';
like $err, qr/COVERALLS_REPO_TOKEN/, 'says which variable to set';
local $ENV{COVERALLS_NO_FAIL} = '1';
($ok, $err, $out) = $run->();
ok $ok, 'returns without a repo token with COVERALLS_NO_FAIL';
like $out, qr/^error: required repo_token/m, 'prints the error';
}
local $ENV{COVERALLS_NO_FAIL} = 'true';
($ok, $err, $out) = $run->();
ok $ok, 'returns with COVERALLS_NO_FAIL';
like $out, qr/^error: /m, 'still prints the error';
local $ENV{COVERALLS_NO_FAIL} = 'false';
$response = { success => 1, status => 200, reason => 'OK',
content => '{"url":"https://coveralls.io/jobs/1"}' };
($ok, $err, $out) = $run->();
ok $ok, 'returns on success';
like $out, qr{^register: https://coveralls.io/jobs/1$}m, 'prints the job';
my ($method, $url, $args) = @request;
is $method, 'POST', 'POST';
is $url, $normal_endpoint, 'to the jobs API';
my ($boundary) = $args->{headers}{'Content-Type'} =~ m{^multipart/form-data; boundary=(\S+)$};
ok defined $boundary, 'as multipart/form-data, with an unquoted boundary';
my ($json) = $args->{content}
=~ m{\A--\Q$boundary\E\r\nContent-Disposition: form-data; name="json_file"; filename="json_file"\r\nContent-Type: application/json\r\n\r\n(.*)\r\n--\Q$boundary\E--\r\n\z}s;
ok defined $json, 'the job is the one field json_file';
is decode_json($json)->{repo_token}, 'abcdef', 'which holds the job';
# With a source file: a statement on line 6 that ran twice.
local *Local::FakeCover::file = sub { bless {}, 'Local::FakeFile' };
local *Local::FakeFile::statement = sub { bless {}, 'Local::FakeStatements' };
local *Local::FakeStatements::location = sub {
$_[1] == 6 ? [ bless {}, 'Local::FakeStatement' ] : undef;
};
local *Local::FakeStatement::uncoverable = sub { 0 };
local *Local::FakeStatement::covered = sub { 2 };
# One decision on line 6, whose true side ran twice and false side never.
local *Local::FakeFile::branch = sub { bless {}, 'Local::FakeBranches' };
local *Local::FakeBranches::location = sub {
$_[1] == 6 ? [ bless { values => [2, 0] }, 'Local::FakeBranch' ] : undef;
};
local *Local::FakeBranch::total = sub { scalar @{ $_[0]{values} } };
local *Local::FakeBranch::value = sub { $_[0]{values}[ $_[1] ] };
local *Local::FakeBranch::uncoverable = sub { 0 };
my $file = "$FindBin::Bin/example.pl";
{
my $out = '';
open my $fh, '>', \$out or die $!;
my $old = select $fh;
Devel::Cover::Report::Coveralls->report($db, { file => [$file] });
select $old;
}
($boundary) = $request[2]{headers}{'Content-Type'} =~ /boundary=(\S+)$/;
($json) = $request[2]{content} =~ m{\r\n\r\n(.*)\r\n--\Q$boundary\E--\r\n\z}s;
my ($sent) = @{ decode_json($json)->{source_files} };
is $sent->{name}, $file, 'the source file is sent';
is $sent->{source_digest}, do {
open my $fh, '<:raw', $file or die $!;
Digest::MD5->new->addfile($fh)->hexdigest;
}, 'with its digest';
is_deeply $sent->{coverage}, [undef, undef, undef, undef, undef, 2], 'and its coverage';
is_deeply $sent->{branches}, [6, 0, 0, 2, 6, 0, 1, 0], 'and its branches';
# The branch coverage was not collected.
local *Local::FakeFile::branch = sub { undef };
{
my $out = '';
open my $fh, '>', \$out or die $!;
my $old = select $fh;
Devel::Cover::Report::Coveralls->report($db, { file => [$file] });
select $old;
}
($boundary) = $request[2]{headers}{'Content-Type'} =~ /boundary=(\S+)$/;
($json) = $request[2]{content} =~ m{\r\n\r\n(.*)\r\n--\Q$boundary\E--\r\n\z}s;
($sent) = @{ decode_json($json)->{source_files} };
ok !exists $sent->{branches}, 'no branches without the branch coverage';
};
subtest 'get_branches' => sub {
my %lines = (
# Two decisions on one line; the false side of the second never ran.
( run in 1.988 second using v1.01-cache-2.11-cpan-eb134014566 )