Devel-Cover-Report-Coveralls

 view release on metacpan or  search on metacpan

AGENTS.md  view on Meta::CPAN

2. `get_config` が環境変数と `.coveralls.yml` から、送信する JSON の CI 関連の項目とエンドポイントを決める
3. `get_git_info` が `git log -1` / `git remote -v` / `git branch` を外部コマンドで呼んで git 情報を作る
   (失敗しても `eval` で空ハッシュになり、送信は続く)。git の作業ツリーの外(`in_git_work_tree` が偽)
   では git を呼ばず、`GIT_ID` / `GIT_BRANCH` / `GIT_AUTHOR_NAME` などの環境変数だけを使う。作業ツリーの
   中では git の値が先で、環境変数は git が返さなかった項目だけを埋める。分からない値は空文字列にせず、
   項目ごと送らない
4. JSON は `json_encoder` が作る。`@JSON_ENCODERS`(`Cpanel::JSON::XS`。`cpanfile` では `recommends`)が
   入っていればそれを使い、無ければ JSON::PP を使う。大きなディストリビューションでは JSON の生成が `report` の
   時間のほとんどを占めるため。JSON::PP と同じ JSON になること(`ascii`)を `t/01_basic.t` の「json_encoder」で
   確かめている。候補を足すときは `cpanfile` にも宣言し、CI で入るようにする(入っていない候補は比べられない)。
   `HTTP::Tiny` で `<endpoint>/api/v1/jobs` へ、JSON を multipart の `json_file` に入れて POST し、
   結果を標準出力に出す(`json_file_request`)。これは API のドキュメントに書かれた形。1.0.0 まで使って
   いた form の `json=` も送れてはいたが、ドキュメントには無い。各ファイルには、ファイルのバイト列の
   MD5 を `source_digest` として付ける(`blib/` の下のファイルはコピー元のもの)。`source` も残す
   (手動のリポジトリの Coveralls Enterprise が要求するため)。
   **送信に失敗したら `die` する**(`parse_response` が判定する)。`cover` は捕まえないので、終了コードが
   0 以外になり CI のジョブが落ちる。`COVERALLS_NO_FAIL` が有効なら `die` せずに戻る。
   `get_config` の `repo_token` が無いときの `die` も、`report` が捕まえて同じように扱う

### `get_config` の CI 判定

lib/Devel/Cover/Report/Coveralls.pm  view on Meta::CPAN

    for my $class (@JSON_ENCODERS) {
        (my $file = "$class.pm") =~ s!::!/!g;
        return $class->new->ascii if eval { require $file; 1 };
        # Installed but not loadable, e.g. built for another version of Perl.
        warn "$class is not used: $@" if $@ !~ /^Can't locate \Q$file\E in \@INC/;
    }
    return JSON::PP->new->ascii;
}

# The body of a request to the jobs API: the job as a JSON file in the multipart field json_file,
# the form the API documents (https://docs.coveralls.io/api-jobs-endpoint). Returns the content
# type and the body. The boundary is not quoted in the content type, as the official
# coverage-reporter does for the nginx of coveralls.io, and is made so that it does not occur in
# the JSON (which holds the source files, this module's own among them).
sub json_file_request {
    my $json = shift;

    my $boundary;
    do {
        $boundary = 'CoverallsPerl' . join '', map { sprintf '%04x', int rand 0x10000 } 1 .. 8;
    } while index($json, $boundary) >= 0;

lib/Devel/Cover/Report/Coveralls.pm  view on Meta::CPAN

    if (-f $CONFIG_FILE) {
        $config = YAML::LoadFile($CONFIG_FILE);
    }

    my $json = {};
    $json->{repo_token} = $config->{repo_token} if $config->{repo_token};
    $json->{repo_token} = $ENV{COVERALLS_REPO_TOKEN} if $ENV{COVERALLS_REPO_TOKEN};
    $json->{flag_name} = $ENV{COVERALLS_FLAG_NAME} if $ENV{COVERALLS_FLAG_NAME};
    $json->{parallel} = \1 if is_enabled($ENV{COVERALLS_PARALLEL});

    my $endpoint = ($ENV{COVERALLS_ENDPOINT} || $API_ENDPOINT) . $API_ENDPOINT_STEM;
    if ($ENV{TRAVIS}) {
        $json->{service_name} = $config->{service_name} || 'travis-ci';
        $json->{service_job_id} = $ENV{TRAVIS_JOB_ID};
        if($ENV{TRAVIS_PULL_REQUEST} && $ENV{TRAVIS_PULL_REQUEST} ne 'false'){
            $json->{service_pull_request} = $ENV{TRAVIS_PULL_REQUEST};
        }
    } elsif ($ENV{CIRCLECI}) {
        $json->{service_name} = 'circleci';
        $json->{service_number} = $ENV{CIRCLE_WORKFLOW_ID};
        # The URL of the pull request; its number is at the end. CI_PULL_REQUEST is the older name.

lib/Devel/Cover/Report/Coveralls.pm  view on Meta::CPAN

    );
    for my $override (@overrides) {
        my ($key, $name) = @$override;
        my $value = env_value($name);
        $json->{$key} = $value if defined $value;
    }
    # "false" is what Travis CI gives on a push, which a CI template may pass on as it is.
    delete $json->{service_pull_request}
        if defined $ENV{COVERALLS_PULL_REQUEST} && $ENV{COVERALLS_PULL_REQUEST} eq 'false';

    return ($json, $endpoint);
}

sub _parse_line ($) {
    my $c = shift;

    return sub {
        my $l = $c->location(shift);

        return $l unless $l;

lib/Devel/Cover/Report/Coveralls.pm  view on Meta::CPAN

    for my $file (@{$options->{file}}) {
        my $f = $cover->file($file);
        my $c = $f->statement();

        my $sf = get_source( $file, _parse_line $c ) or next;
        my $branches = get_branches($f->branch, scalar @{ $sf->{coverage} });
        $sf->{branches} = $branches if @$branches;
        push @sfs, $sf;
    }

    my ($json, $endpoint) = eval { get_config() };
    if (!$json) {
        # No repo token, which happens on pull requests from forks: they get no secrets.
        die $@ unless is_enabled($ENV{COVERALLS_NO_FAIL});
        print "- Report::Coveralls --------\nerror: $@----------------------------\n";
        return;
    }
    $json->{git} = apply_ci_git_info($json, eval { get_git_info() } || {});
    $json->{source_files} = \@sfs;

    my $coder = json_encoder();

    my ($content_type, $body) = json_file_request($coder->encode($json));
    my $response = HTTP::Tiny->new( verify_SSL => 1 )->request('POST', $endpoint, {
        headers => { 'Content-Type' => $content_type },
        content => $body,
    });

    my ($url, $error) = parse_response($response);

    print("- Report::Coveralls --------\n");
    if(exists($json->{service_number})){
        print "My build number: ". $json->{service_number} ."\n";
    }

t/01_basic.t  view on Meta::CPAN

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

t/01_basic.t  view on Meta::CPAN

    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};

t/01_basic.t  view on Meta::CPAN

    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";
    }
    {

t/01_basic.t  view on Meta::CPAN


    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' };



( run in 1.629 second using v1.01-cache-2.11-cpan-eb134014566 )