Golang的Codeclimate测试覆盖格式化程序

时间:2017-08-19 20:34:33

标签: go continuous-integration code-coverage code-climate

Codeclimate docs中没有写过如何指定coverage格式化程序。但是,当我试图向Codeclimate发送报道时:

./cc-test-reporter before-build
./cc-test-reporter after-build

失败了:

  

错误:找不到任何可行的格式化程序。可用的格式化程序:simplecov,lcov,coverage.py,clover,gocov,gcov,cobertura,jacoco

我安装了gocov。我还使用goconv生成了一份报告:

gocov test -coverprofile=out

我尝试以各种方式将报告文件指定给Codeclimate:

./cc-test-reporter after-build out
./cc-test-reporter after-build < out

但没有运气......

我还没找到.codeclimate.yml文件的任何与格式化程序相关的指令。该文档是用超级编写的,你知道&#34;风格所以它没有帮助。如何使用Codeclimate启用/发送测试覆盖率?

3 个答案:

答案 0 :(得分:3)

导出var:

CC_TEST_REPORTER_ID=...

执行命令

for pkg in $(go list ./... | grep -v vendor); do
    go test -coverprofile=$(echo $pkg | tr / -).cover $pkg
done
echo "mode: set" > c.out
grep -h -v "^mode:" ./*.cover >> c.out
rm -f *.cover

./cc-test-reporter after-build

答案 1 :(得分:2)

来自Code Climate Support的Abby。目前,test-reporter尝试从一组默认路径推断覆盖数据的位置。对于遵循coverage工具的默认设置的用户来说,这通常就足够了。

但是,如果输出不在其中一个路径上,您可以使用test-reporter低级命令来指示 覆盖数据的类型及其位置。在这种特殊情况下,您可以执行以下操作:

export CC_TEST_REPORTER_ID=<repo-test-reporter-id>
./cc-test-reporter before-build
gocov test -coverprofile=out 
./cc-test-reporter format-coverage --input-type gocov out 
./cc-test-reporter upload-coverage

您可以使用标记test-reporter查看有关如何使用--help命令的详细信息。例如,./cc-test-reporter format-coverage --help

这应该让你在一个好地方。如果没有,请通过hello@codeclimate.com或此处告诉我们,我可以获得更多见解。 :)

答案 2 :(得分:0)

对于那些仍然有这个问题的人: 对我来说 - 问题是我错误地将输出覆盖文件命名为“cp.out”而不是“c.out”,这是 cc-test-reporter 所期望的。

这是我的工作脚本:

#!/bin/sh
inputFile=$1
while IFS="=" read -r repo reporterID
do 
    cd "$HOME""/go/src/github.com/""$repo" || exit
    echo "performing code coverage upload for ""$repo"
    git checkout master && git pull ##don't know if main is used
    cp -f "$HOME"/shellScripts/test-reporter-latest-darwin-amd64 .
    chmod 755 test-reporter-latest-darwin-amd64
    ./test-reporter-latest-darwin-amd64 before-build
    export CC_TEST_REPORTER_ID="$reporterID"
    go test -coverprofile=c.out ./...

    ./test-reporter-latest-darwin-amd64 after-build --prefix "github.com/""$repo"
    echo
    echo "======================="
    echo
done < "$inputFile"