Search in sources :

Example 1 with ProjectData

use of com.intellij.rt.coverage.data.ProjectData in project intellij-community by JetBrains.

the class CoverageDataManagerImpl method selectSubCoverage.

public void selectSubCoverage(@NotNull final CoverageSuitesBundle suite, final List<String> testNames) {
    suite.restoreCoverageData();
    final ProjectData data = suite.getCoverageData();
    if (data == null)
        return;
    mySubCoverageIsActive = true;
    final Map<String, Set<Integer>> executionTrace = new HashMap<>();
    for (CoverageSuite coverageSuite : suite.getSuites()) {
        final String fileName = coverageSuite.getCoverageDataFileName();
        final File tracesDir = getTracesDirectory(fileName);
        for (String testName : testNames) {
            final File file = new File(tracesDir, FileUtil.sanitizeFileName(testName) + ".tr");
            if (file.exists()) {
                DataInputStream in = null;
                try {
                    in = new DataInputStream(new FileInputStream(file));
                    int traceSize = in.readInt();
                    for (int i = 0; i < traceSize; i++) {
                        final String className = in.readUTF();
                        final int linesSize = in.readInt();
                        Set<Integer> lines = executionTrace.get(className);
                        if (lines == null) {
                            lines = new HashSet<>();
                            executionTrace.put(className, lines);
                        }
                        for (int l = 0; l < linesSize; l++) {
                            lines.add(in.readInt());
                        }
                    }
                } catch (Exception e) {
                    LOG.error(e);
                } finally {
                    try {
                        in.close();
                    } catch (IOException e) {
                        LOG.error(e);
                    }
                }
            }
        }
    }
    final ProjectData projectData = new ProjectData();
    for (String className : executionTrace.keySet()) {
        ClassData loadedClassData = projectData.getClassData(className);
        if (loadedClassData == null) {
            loadedClassData = projectData.getOrCreateClassData(className);
        }
        final Set<Integer> lineNumbers = executionTrace.get(className);
        final ClassData oldData = data.getClassData(className);
        LOG.assertTrue(oldData != null, "missed className: \"" + className + "\"");
        final Object[] oldLines = oldData.getLines();
        LOG.assertTrue(oldLines != null);
        int maxNumber = oldLines.length;
        for (Integer lineNumber : lineNumbers) {
            if (lineNumber >= maxNumber) {
                maxNumber = lineNumber + 1;
            }
        }
        final LineData[] lines = new LineData[maxNumber];
        for (Integer line : lineNumbers) {
            final int lineIdx = line.intValue() - 1;
            String methodSig = null;
            if (lineIdx < oldData.getLines().length) {
                final LineData oldLineData = oldData.getLineData(lineIdx);
                if (oldLineData != null) {
                    methodSig = oldLineData.getMethodSignature();
                }
            }
            final LineData lineData = new LineData(lineIdx, methodSig);
            if (methodSig != null) {
                loadedClassData.registerMethodSignature(lineData);
            }
            lineData.setStatus(LineCoverage.FULL);
            lines[lineIdx] = lineData;
        }
        loadedClassData.setLines(lines);
    }
    suite.setCoverageData(projectData);
    renewCoverageData(suite);
}
Also used : HashSet(com.intellij.util.containers.HashSet) Set(java.util.Set) HashMap(com.intellij.util.containers.HashMap) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) WriteExternalException(com.intellij.openapi.util.WriteExternalException) InvalidDataException(com.intellij.openapi.util.InvalidDataException) IOException(java.io.IOException) LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File) ProjectData(com.intellij.rt.coverage.data.ProjectData)

Example 2 with ProjectData

use of com.intellij.rt.coverage.data.ProjectData in project intellij-community by JetBrains.

the class JavaCoverageSuite method getCoverageData.

@Nullable
public ProjectData getCoverageData(final CoverageDataManager coverageDataManager) {
    final ProjectData data = getCoverageData();
    if (data != null)
        return data;
    ProjectData map = loadProjectInfo();
    if (mySuiteToMerge != null) {
        JavaCoverageSuite toMerge = null;
        final CoverageSuite[] suites = coverageDataManager.getSuites();
        for (CoverageSuite suite : suites) {
            if (Comparing.strEqual(suite.getPresentableName(), mySuiteToMerge)) {
                if (!Comparing.strEqual(((JavaCoverageSuite) suite).getSuiteToMerge(), getPresentableName())) {
                    toMerge = (JavaCoverageSuite) suite;
                }
                break;
            }
        }
        if (toMerge != null) {
            final ProjectData projectInfo = toMerge.getCoverageData(coverageDataManager);
            if (map != null) {
                map.merge(projectInfo);
            } else {
                map = projectInfo;
            }
        }
    }
    setCoverageData(map);
    return map;
}
Also used : ProjectData(com.intellij.rt.coverage.data.ProjectData) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ProjectData

use of com.intellij.rt.coverage.data.ProjectData in project intellij-community by JetBrains.

the class CoberturaLoaderUtil method load.

public static ProjectData load(final File sessionDataFile) {
    ProjectData projectInfo = new ProjectData();
    DataInputStream dataFile = null;
    try {
        dataFile = new DataInputStream(new FileInputStream(sessionDataFile));
        int classesCount = dataFile.read();
        for (int i = 0; i < classesCount; i++) {
            final String classFQName = dataFile.readUTF();
            //sourcefilename
            dataFile.readUTF();
            final ClassData classData = projectInfo.getOrCreateClassData(classFQName);
            final int numberOfLines = dataFile.read();
            for (int l = 0; l < numberOfLines; l++) {
                final int lineNumber = dataFile.read();
                //todo classData.getOrCreateLine(lineNumber, dataFile.readUTF() + dataFile.readUTF());
                final LineData lineData = null;
                long hits = dataFile.readLong();
                final int jumpsNumber = dataFile.read();
                int trueHits = 0;
                int falseHits = 0;
                int totalHits = 0;
                for (int j = 0; j < jumpsNumber; j++) {
                    //jump number
                    dataFile.read();
                    totalHits++;
                    if (dataFile.readLong() > 0)
                        trueHits++;
                    totalHits++;
                    if (dataFile.readLong() > 0)
                        falseHits++;
                }
                int defaultHitsNumber = 0;
                int branchHitNumber = 0;
                final int switchNumber = dataFile.read();
                for (int s = 0; s < switchNumber; s++) {
                    //switch number
                    dataFile.read();
                    //number of keys
                    dataFile.read();
                    long defaultHits = dataFile.readLong();
                    if (defaultHits > 0)
                        defaultHitsNumber++;
                    int coveredSwitchBranches = 0;
                    final int switchBranchesNumber = dataFile.read();
                    for (int b = 0; b < switchBranchesNumber; b++) {
                        final long branchHit = dataFile.readLong();
                        if (branchHit > 0)
                            coveredSwitchBranches++;
                    }
                    if (coveredSwitchBranches == switchBranchesNumber)
                        branchHitNumber++;
                }
                if (hits > 0) {
                    if (totalHits == trueHits + falseHits) {
                        if (defaultHitsNumber == switchNumber && branchHitNumber == switchNumber) {
                            lineData.setStatus(LineCoverage.FULL);
                            continue;
                        }
                    }
                    lineData.setStatus(LineCoverage.PARTIAL);
                } else {
                    lineData.setStatus(LineCoverage.NONE);
                }
            }
        }
    } catch (IOException e) {
        LOG.info(e);
    } finally {
        if (dataFile != null) {
            try {
                dataFile.close();
            } catch (IOException e) {
                LOG.error(e);
            }
        }
    }
    return projectInfo;
}
Also used : LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ProjectData(com.intellij.rt.coverage.data.ProjectData) FileInputStream(java.io.FileInputStream)

Example 4 with ProjectData

use of com.intellij.rt.coverage.data.ProjectData in project intellij-plugins by JetBrains.

the class DartCoverageRunner method doLoadCoverageData.

@Nullable
private static ProjectData doLoadCoverageData(@NotNull final File sessionDataFile, @NotNull final DartCoverageSuite coverageSuite) {
    final ProcessHandler coverageProcess = coverageSuite.getCoverageProcess();
    // coverageProcess == null means that we are switching to data gathered earlier
    if (coverageProcess != null) {
        for (int i = 0; i < 100; ++i) {
            ProgressManager.checkCanceled();
            if (coverageProcess.waitFor(100)) {
                break;
            }
        }
        if (!coverageProcess.isProcessTerminated()) {
            coverageProcess.destroyProcess();
            return null;
        }
    }
    final Project project = coverageSuite.getProject();
    final String contextFilePath = coverageSuite.getContextFilePath();
    if (project == null || contextFilePath == null) {
        return null;
    }
    final String contextId = DartAnalysisServerService.getInstance(project).execution_createContext(contextFilePath);
    if (contextId == null) {
        return null;
    }
    final ProjectData projectData = new ProjectData();
    try {
        DartCoverageData data = new Gson().fromJson(new BufferedReader(new FileReader(sessionDataFile)), DartCoverageData.class);
        if (data == null) {
            LOG.warn("Coverage file does not contain valid data.");
            return null;
        }
        for (Map.Entry<String, SortedMap<Integer, Integer>> entry : data.getMergedDartFileCoverageData().entrySet()) {
            ProgressManager.checkCanceled();
            String filePath = getFileForUri(project, contextId, entry.getKey());
            if (filePath == null) {
                // File is not found.
                continue;
            }
            SortedMap<Integer, Integer> lineHits = entry.getValue();
            ClassData classData = projectData.getOrCreateClassData(filePath);
            if (lineHits.size() == 0) {
                classData.setLines(new LineData[1]);
                continue;
            }
            LineData[] lines = new LineData[lineHits.lastKey() + 1];
            for (Map.Entry<Integer, Integer> hit : lineHits.entrySet()) {
                LineData lineData = new LineData(hit.getKey(), null);
                lineData.setHits(hit.getValue());
                lines[hit.getKey()] = lineData;
            }
            classData.setLines(lines);
        }
    } catch (FileNotFoundException | JsonSyntaxException e) {
        LOG.warn(e);
    } finally {
        DartAnalysisServerService.getInstance(project).execution_deleteContext(contextId);
    }
    return projectData;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) Project(com.intellij.openapi.project.Project) LineData(com.intellij.rt.coverage.data.LineData) JsonSyntaxException(com.google.gson.JsonSyntaxException) ClassData(com.intellij.rt.coverage.data.ClassData) SortedMap(java.util.SortedMap) BufferedReader(java.io.BufferedReader) ProcessHandler(com.intellij.execution.process.ProcessHandler) FileReader(java.io.FileReader) Map(java.util.Map) SortedMap(java.util.SortedMap) ProjectData(com.intellij.rt.coverage.data.ProjectData) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ProjectData

use of com.intellij.rt.coverage.data.ProjectData in project intellij-plugins by JetBrains.

the class KarmaCoverageEngine method findRootDir.

/**
   * Finds a root directory for Coverage toolwindow view.
   * Returns a content root containing at least one covered file.
   */
@Nullable
private static VirtualFile findRootDir(@NotNull final Project project, @NotNull final CoverageSuitesBundle suitesBundle) {
    return ReadAction.compute(() -> {
        CoverageDataManager coverageDataManager = CoverageDataManager.getInstance(project);
        for (CoverageSuite suite : suitesBundle.getSuites()) {
            ProjectData data = suite.getCoverageData(coverageDataManager);
            if (data != null) {
                for (Object key : data.getClasses().keySet()) {
                    if (key instanceof String) {
                        String path = (String) key;
                        VirtualFile file = VfsUtil.findFileByIoFile(new File(path), false);
                        if (file != null && file.isValid()) {
                            ProjectFileIndex projectFileIndex = ProjectFileIndex.SERVICE.getInstance(project);
                            VirtualFile contentRoot = projectFileIndex.getContentRootForFile(file);
                            if (contentRoot != null && contentRoot.isDirectory() && contentRoot.isValid()) {
                                return contentRoot;
                            }
                        }
                    }
                }
            }
        }
        return null;
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) JSFile(com.intellij.lang.javascript.psi.JSFile) File(java.io.File) ProjectData(com.intellij.rt.coverage.data.ProjectData) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ProjectData (com.intellij.rt.coverage.data.ProjectData)15 File (java.io.File)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 ClassData (com.intellij.rt.coverage.data.ClassData)7 LineData (com.intellij.rt.coverage.data.LineData)6 Nullable (org.jetbrains.annotations.Nullable)6 IOException (java.io.IOException)5 PsiFile (com.intellij.psi.PsiFile)4 Map (java.util.Map)4 Module (com.intellij.openapi.module.Module)3 HashMap (com.intellij.util.containers.HashMap)3 CoverageReport (com.google.jstestdriver.idea.rt.coverage.CoverageReport)2 ExportToHTMLSettings (com.intellij.codeEditor.printing.ExportToHTMLSettings)2 JSFile (com.intellij.lang.javascript.psi.JSFile)2 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 DataInputStream (java.io.DataInputStream)2 FileInputStream (java.io.FileInputStream)2 List (java.util.List)2 NotNull (org.jetbrains.annotations.NotNull)2