Giter Site home page Giter Site logo

whlbest / jacoco Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tb0819/jacoco

0.0 1.0 0.0 900 KB

新增增量覆盖率

License: Other

Shell 0.12% Python 0.03% Java 78.16% HTML 14.31% Groovy 0.15% Kotlin 0.60% CSS 0.51% XSLT 0.92% JavaScript 5.20%

jacoco's Introduction

JaCoCo Java Code Coverage Library

Build Status Build status Maven Central

JaCoCo is a free Java code coverage library distributed under the Eclipse Public License. Check the project homepage for downloads, documentation and feedback.

Please use our mailing list for questions regarding JaCoCo which are not already covered by the extensive documentation.

Note: We do not answer general questions in the project's issue tracker. Please use our mailing list for this.

实现方式

现大多代码管理居于git控制,项目迭代的差异在于git分支版本之间的代码差异。主要实现方法在于修改jacoco报告生成的源码,其中涉及git代码diff,jdt的AST获取源码。

  • ClassProbesAdapter类中的 “visitMethod” 方法的作用为覆盖率计算,被测应用的每个类的方法都需调用该方法来统计代码的覆盖率。改造原理:通过Jgit.jar进行代码diff,统计改动的方法以及改动的行数。只对方法级进行统计。
@Override
   public final MethodVisitor visitMethod(final int access, final String name,
         final String desc, final String signature, final String[] exceptions) {
      final MethodProbesVisitor methodProbes;
      final MethodProbesVisitor mv = cv.visitMethod(access, name, desc,
            signature, exceptions);
      //jacoco源码
//    if (mv !=null) {
//       methodProbes = mv;
//    } else {
//       // We need to visit the method in any case, otherwise probe ids
//       // are not reproducible
//       methodProbes = EMPTY_METHOD_PROBES_VISITOR;
//    }
      // 改造后的代码:判断是否计算覆盖率
      if (mv !=null && isContainsMethod(name, CoverageBuilder.classInfos)) {
         methodProbes = mv;
      } else {
         // We need to visit the method in any case, otherwise probe ids
         // are not reproducible
         methodProbes = EMPTY_METHOD_PROBES_VISITOR;
      }

      return new MethodSanitizer(null, access, name, desc, signature,
            exceptions) {

         @Override
         public void visitEnd() {
            super.visitEnd();
            LabelFlowAnalyzer.markLabels(this);
            final MethodProbesAdapter probesAdapter = new MethodProbesAdapter(
                  methodProbes, ClassProbesAdapter.this);
            if (trackFrames) {
               final AnalyzerAdapter analyzer = new AnalyzerAdapter(
                     ClassProbesAdapter.this.name, access, name, desc,
                     probesAdapter);
               probesAdapter.setAnalyzer(analyzer);
               methodProbes.accept(this, analyzer);
            } else {
               methodProbes.accept(this, probesAdapter);
            }
         }
      };
   }
   
   private boolean isContainsMethod(String currentMethod, List<ClassInfo> classInfos) {
   if (classInfos== null || classInfos.isEmpty()) {
      return true;
   }
   String currentClassName = name.replaceAll("/",".");
   for (ClassInfo classInfo : classInfos) {
      String className = classInfo.getPackages() + "." + classInfo.getClassName();
      if (currentClassName.equals(className)) {
         for (MethodInfo methodInfo: classInfo.getMethodInfos()) {
            String methodName = methodInfo.getMethodName();
            if (currentMethod.equals(methodName)) {
               return true;
            }
         }
      }
   }
   return false;
}
  • 报告的生成主要体现在源码展示,修改org.jacoco.report下的SourceHighlighter.java
//  修改每行代码的报告样式
private void renderCodeLine(final HTMLElement pre, final String linesrc,
      final ILine line, final int lineNr, final String classPath) throws IOException {
   if (CoverageBuilder.classInfos == null || CoverageBuilder.classInfos.isEmpty()) {
      // 全量覆盖
      highlight(pre, line, lineNr).text(linesrc);
      pre.text("\n");
   } else {
      // 增量覆盖
      boolean existFlag = true;
      for (ClassInfo classInfo : CoverageBuilder.classInfos) {
          String tClassPath = classInfo.getPackages() + "." + classInfo.getClassName();
          if (classPath.equals(tClassPath)) {
            // 新增的类
            if (classInfo.getType().equals("ADD")) {
               highlight(pre, line, lineNr).text("+ " + linesrc);
               pre.text("\n");
            } else {
               // 修改的类
               boolean flag = false;
               List<int[]> addLines = classInfo.getAddLines();
               for (int[] ints: addLines) {
                  if (ints[0] <= lineNr &&  lineNr <= ints[1]){
                     flag = true;
                     break;
                  }
               }
               if (flag) {
                  highlight(pre, line, lineNr).text("+ " + linesrc);
                  pre.text("\n");
               } else {
                  highlight(pre, line, lineNr).text(" " + linesrc);
                  pre.text("\n");
               }
            }
            existFlag = false;
            break;
          }
      }
      if (existFlag) {
         highlight(pre, line, lineNr).text(" " + linesrc);
         pre.text("\n");
      }
   }
}
  • 实例

git操作需要有权限,操作之前需先赋予权限:GitAdapter.setCredentialsProvider("cangzhu", "test1234");

private IBundleCoverage analyzeStructure() throws IOException {
        //定义本地git路径和分支名称,将会自动与master代码diff
        final CoverageBuilder coverageBuilder = new CoverageBuilder("C:\\workspace\\companywork\\demo","jacoco-cov");
//        final CoverageBuilder coverageBuilder = new CoverageBuilder();
        final Analyzer analyzer = new Analyzer(
                execFileLoader.getExecutionDataStore(), coverageBuilder);

        analyzer.analyzeAll(classesDirectory);

        return coverageBuilder.getBundle(title);

jacoco's People

Contributors

tb0819 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.