CodecovをCircleCI導入した時の話

f:id:hawaiiantime:20190207145514p:plain

CircleCIの設定画面でCODECOV_TOKENの環境変数を指定しておく
そうすればカバレッジレポートを送信する際にトークンを指定する必要が無い

このプロジェクトでは、Makefileにテストスクリプトを用意していた

test:
	go test -v -coverprofile=coverage.txt -count=1 ./...

Codecov用の設定ファイルを用意

coverage:
  status:
    project:
      default:
        target: 50%
        threshold: 5%
      logic:
        paths:
          - src/<directory_name>

.circleci/config.yml でテスト実行後にカバレッジレポートを送信する処理を追加

test:
    executor: default
    steps:
    - restore_code
    - run:
        name: test
        command: |
          make test
          bash <(curl -s https://codecov.io/bash)

READMEにバッジもつけておく

[![codecov](https://codecov.io/gh/<team>/<project_name>/branch/develop/graph/badge.svg?token=xxxxxxxxx)](https://codecov.io/gh/<team>/<project_name>)

どえらいハマったのが、当初Codecovの実行をtestとは別ジョブで登録していて、それだとtestの方でsave_cacheしないと、
次のジョブに入る際カバレッジレポートが無くなっていて、アップロードしようとしてもファイルが存在しない。
testジョブの中で、テスト実行後に即レポートを送ることで回避した。

Goを使ったプロジェクトで、もし複数パッケージでテスト実行したい時はひと手間必要

テスト用シェルコマンドを用意して、、

#!/usr/bin/env bash

set -e
echo "" > coverage.txt

for d in $(go list ./... | grep -v vendor); do
    go test -race -coverprofile=profile.out -covermode=atomic $d
    if [ -f profile.out ]; then
        cat profile.out >> coverage.txt
        rm profile.out
    fi
done

それを実行する

test:
    ./test.sh