jacoco统计新增代码覆盖率踩过的坑


缘起:

比较正在开发的branch和master代码的区别,然后统计新增代码覆盖率。

步骤:

a,代码覆盖率是通过分析exec文件来比较的,exec文件是通过ant从服务器中dump下来的,调用jacoco也是通过ant的build.xml文件来实现的,所以,branch需要从ant文件传给jacoco;

b,传给jacoco之后,通过jgit统计变更的代码;

c,在jacoco代码统计的入口处,将不需要统计的行数和类过滤掉;

踩过的坑:

1,莫名其妙的不能不能clone多个branch,最后莫名其妙的好了,至今没搞明白为什么。

command.setBranchesToClone(Arrays.asList("refs/heads/master","refs/heads/"+branch));

2,运行ant的xml文件的时候出现类检查错误,这个是因为加载类的时候会对类进行类检查,不通过的时候就会报这个错。

Type top (current frame, locals[11]) is not assignable to integer 。

这个是jvm虚拟机的机制,可以通过设置jvm的参数来去掉检查。

对于java7而言,需要添加-XX:-UseSplitVerifier(已实践)。 
对于java8而言,需要添加-noverify

ant如何设置jvm参数?下面是官方文档

The Ant wrapper script for Unix will source (read and evaluate) the file ~/.antrc before it does anything. On Windows, the Ant wrapper batch-file invokes %HOME%\antrc_pre.bat at the start and %HOME%\antrc_post.bat at the end. You can use these files, for example, to set/unset environment variables that should only be visible during the execution of Ant. See the next section for examples.

我用的mac电脑,通过添加~/.antrc文件,文件中添加ANT_OPTS="-noverify",设置jvm参数,ant运行.xml文件的时候,会自动加载这个文件设置jvm的参数。

3,如何把branch和repository传进去,通过给group添加两个参数将它们传进去,不过这个要让ant认识这两个属性attribute,根据ant的官方文档需要更改jacoco的reportTask类支持新属性。
public void setRepository(final String repository) {
this.repository = repository;
}
public void setBranch(final String branch) {
this.branch = branch;
}
分析新增代码覆盖率的思路:
1,将分支和master的代码拉下来,获取到新增加和修改的类addOrModifiedClasses集合,分析代码的时候只分析addOrModifiedClasses,将不是addOrModifiedClasses集合中的代码过滤调。分两个层次来过滤
    a,过滤掉不在addOrModifiedClasses里面的类。
    b,过滤掉没有更改的行,只分析更改的行数。
2,分析新增代码覆盖率的实现方式:
    a,用jgit开源软件,获取到两个分支的代码的区别DiffEntity。
    b,Analyzer中的analyzeAll类文件的分析的入口处过滤掉,红色的部分添加的过滤类文件的代码。
public int analyzeAll(final File file) throws IOException {
int count = 0;
if (file.isDirectory()) {
for (final File f : file.listFiles()) {
count += analyzeAll(f);
}
} else {
String filePath=file.getPath();
if(filePath.endsWith(CLASS_SUFFIX)) {
String className = filePath.substring(CLASS_PREFIX.length(), (file.getPath().length() - CLASS_SUFFIX.length()));
//只分析修改或者添加的class
if (isNewClass(className)) {
final InputStream in = new FileInputStream(file);
try {
System.out.println("正在分析的className:" + className);
count += analyzeAll(in, file.getPath());
} finally {
in.close();
}
}
}else {

}
}
return count;
}
     c,ClassAnalyzer过滤没有修改的method,获取到修改和新增加的的method,统计修改了和新增加的的method,忽略未修改的method。红色部分为新增加的代码。
return new MethodAnalyzer(stringPool.get(name), stringPool.get(desc),
stringPool.get(signature), probes) {
@Override
public void visitEnd() {
super.visitEnd();
final IMethodCoverage methodCoverage = getCoverage();
if (methodCoverage.getInstructionCounter().getTotalCount() > 0) {
// Only consider methods that actually contain code
final int firstLine = methodCoverage.getFirstLine();
final int lastLine = methodCoverage.getLastLine();
for (int i = firstLine; i <= lastLine; i++) {
//对于更改的类:只分析更改的方法,对于没有更改的方法不进行分析diffLines.size()>0&&diffLines.contains(i)
//对于增加的类:分析所有的diffLines.size()==0
if((diffLines.size()>0&&diffLines.contains(i))||diffLines.size()==0){
coverage.addMethod(methodCoverage,diffLines);
break;
}
}

}
}
};
    d,SourceNodeImpl过滤新增加和修改的行,统计这部分代码的覆盖率,忽略未修改和未增加的行。红色部分是新增加的部分
public void increment(final ISourceNode child,List<Integer> diffLines) {
instructionCounter = instructionCounter.increment(child
.getInstructionCounter());
branchCounter = branchCounter.increment(child.getBranchCounter());
complexityCounter = complexityCounter.increment(child
.getComplexityCounter());
methodCounter = methodCounter.increment(child.getMethodCounter());
classCounter = classCounter.increment(child.getClassCounter());
final int firstLine = child.getFirstLine();
if (firstLine != UNKNOWN_LINE) {
final int lastLine = child.getLastLine();
ensureCapacity(firstLine, lastLine);
for (int i = firstLine; i <= lastLine; i++) {
//只分析增加和修改的line的的覆盖率
//对于更改的类的方法:diffLines.contains(i)如果包含修改的行的话,就分析这行
//对于新增加类的方法diffLines.size()==0分析所有的
if(diffLines.contains(i)||diffLines.size()==0){
final ILine line = child.getLine(i);
incrementLine(line.getInstructionCounter(),
line.getBranchCounter(), i);
}
}
}
}

研究 jacoco 的入口:
ReportGenerator
资料:
jgit使用指南:https://github.com/centic9/jgit-cookbook;
ant文档:http://ant.apache.org/manual/running.html;
jgit相关博客:https://blog.csdn.net/chen136428/article/details/8904416?utm_source=copy;
解决类校验的错误的相关博客:https://www.cnblogs.com/zhangcybb/p/4897417.html
jgit document:  http://download.eclipse.org/jgit/site/5.1.1.201809181055-r/apidocs/index.html
有赞的jacoco简介: https://testerhome.com/articles/16981
 
问题:
改了class码之后不会对程序造成什么异常吗?但是改了字节码,真正的程序的运行不是用的字节码吗?
为什么不是侵入式的?因为他没有改源代码,源代码虽然没有改。
如何更改字节码的?通过ClassFileTransformer类的transform防范。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM