Search in sources :

Example 1 with CoverageReport

use of com.google.jstestdriver.idea.rt.coverage.CoverageReport in project intellij-plugins by JetBrains.

the class JstdCoverageEngine method generateReport.

@Override
public void generateReport(@NotNull Project project, @NotNull DataContext dataContext, @NotNull CoverageSuitesBundle currentSuiteBundle) {
    CoverageReport coverageReport = new CoverageReport();
    for (CoverageSuite suite : currentSuiteBundle.getSuites()) {
        ProjectData projectData = suite.getCoverageData(CoverageDataManager.getInstance(project));
        if (projectData != null) {
            @SuppressWarnings("unchecked") Map<String, ClassData> classDataMap = projectData.getClasses();
            for (Map.Entry<String, ClassData> classDataEntry : classDataMap.entrySet()) {
                String fileName = classDataEntry.getKey();
                ClassData classData = classDataEntry.getValue();
                List<CoverageReport.LineHits> lineHitsList = convertClassDataToLineHits(classData);
                coverageReport.mergeFileReport(fileName, lineHitsList);
            }
        }
    }
    final ExportToHTMLSettings settings = ExportToHTMLSettings.getInstance(project);
    final File outputDir = new File(settings.OUTPUT_DIRECTORY);
    FileUtil.createDirectory(outputDir);
    String outputFileName = getOutputFileName(currentSuiteBundle);
    String title = "Coverage Report Generation";
    try {
        File output = new File(outputDir, outputFileName);
        CoverageSerializationUtils.writeLCOV(coverageReport, output);
        refresh(output);
        String url = "http://ltp.sourceforge.net/coverage/lcov.php";
        Messages.showInfoMessage("<html>Coverage report has been successfully saved as '" + outputFileName + "' file.<br>Use <a href='" + url + "'>" + url + "</a>" + " to generate HTML output." + "</html>", title);
    } catch (IOException e) {
        LOG.warn("Can not export coverage data", e);
        Messages.showErrorDialog("Can not generate coverage report: " + e.getMessage(), title);
    }
}
Also used : CoverageReport(com.google.jstestdriver.idea.rt.coverage.CoverageReport) IOException(java.io.IOException) ExportToHTMLSettings(com.intellij.codeEditor.printing.ExportToHTMLSettings) ClassData(com.intellij.rt.coverage.data.ClassData) 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)

Example 2 with CoverageReport

use of com.google.jstestdriver.idea.rt.coverage.CoverageReport in project intellij-plugins by JetBrains.

the class JstdCoverageRunner method readProjectData.

@NotNull
private static ProjectData readProjectData(@NotNull File dataFile) throws IOException {
    CoverageReport report = CoverageSerializationUtils.readLCOV(dataFile);
    ProjectData projectData = new ProjectData();
    for (Map.Entry<String, List<CoverageReport.LineHits>> entry : report.getInfo().entrySet()) {
        String filePath = SimpleCoverageAnnotator.getFilePath(entry.getKey());
        ClassData classData = projectData.getOrCreateClassData(filePath);
        int max = 0;
        List<CoverageReport.LineHits> lineHitsList = entry.getValue();
        if (lineHitsList.size() > 0) {
            CoverageReport.LineHits lastLineHits = lineHitsList.get(lineHitsList.size() - 1);
            max = lastLineHits.getLineNumber();
        }
        LineData[] lines = new LineData[max + 1];
        for (CoverageReport.LineHits lineHits : lineHitsList) {
            LineData lineData = new LineData(lineHits.getLineNumber(), null);
            lineData.setHits(lineHits.getHits());
            lines[lineHits.getLineNumber()] = lineData;
        }
        classData.setLines(lines);
    }
    return projectData;
}
Also used : CoverageReport(com.google.jstestdriver.idea.rt.coverage.CoverageReport) LineData(com.intellij.rt.coverage.data.LineData) ClassData(com.intellij.rt.coverage.data.ClassData) List(java.util.List) Map(java.util.Map) ProjectData(com.intellij.rt.coverage.data.ProjectData) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with CoverageReport

use of com.google.jstestdriver.idea.rt.coverage.CoverageReport in project intellij-plugins by JetBrains.

the class TestRunner method runTests.

@SuppressWarnings("deprecation")
private void runTests(@NotNull final File configFile, @NotNull String[] extraArgs, final boolean dryRun) throws ConfigurationException {
    JsTestDriverBuilder builder = new JsTestDriverBuilder();
    final ParsedConfiguration parsedConfiguration;
    try {
        parsedConfiguration = JstdConfigParsingUtils.parseConfiguration(configFile);
    } catch (Exception e) {
        throw new ConfigurationException("Configuration file parsing failed.\n" + "See http://code.google.com/p/js-test-driver/wiki/ConfigurationFile for clarification.\n\n" + "Details:", e);
    }
    final File singleBasePath = JstdConfigParsingUtils.getSingleBasePath(parsedConfiguration.getBasePaths(), configFile);
    myTreeManager.setCurrentBasePath(singleBasePath.getAbsolutePath());
    JstdConfigParsingUtils.wipeCoveragePlugin(parsedConfiguration);
    builder.setDefaultConfiguration(parsedConfiguration);
    builder.withPluginInitializer(new PluginInitializer() {

        @Override
        public Module initializeModule(Flags flags, Configuration config) {
            return new AbstractModule() {

                @Override
                public void configure() {
                    Multibinder<TestListener> testListeners = Multibinder.newSetBinder(binder(), TestListener.class);
                    testListeners.addBinding().to(TestResultHolder.class);
                    testListeners.addBinding().toInstance(new IdeaTestListener(myTreeManager, configFile, singleBasePath, dryRun, mySettings.getTestFileScope()));
                }
            };
        }
    });
    builder.setRunnerMode(RunnerMode.QUIET);
    builder.setServer(mySettings.getServerUrl());
    List<String> flagArgs = Lists.newArrayList("--captureConsole", "--server", mySettings.getServerUrl());
    ResolvedConfiguration resolvedConfiguration = JstdConfigParsingUtils.resolveConfiguration(parsedConfiguration);
    if (dryRun && JstdUtils.isJasmineTests(resolvedConfiguration)) {
        // https://github.com/ibolmo/jasmine-jstd-adapter/pull/21
        flagArgs.add("--reset");
    }
    flagArgs.addAll(Arrays.asList(extraArgs));
    List<String> coverageExcludedFiles = null;
    File emptyOutputDir = null;
    boolean runCoverage = false;
    if (myCoverageSession != null && !dryRun) {
        emptyOutputDir = createTempDir();
        if (emptyOutputDir != null) {
            flagArgs.add("--testOutput");
            flagArgs.add(emptyOutputDir.getAbsolutePath());
            List<String> testPaths = getTestFilePaths(resolvedConfiguration);
            coverageExcludedFiles = Lists.newArrayList(testPaths);
            coverageExcludedFiles.addAll(mySettings.getFilesExcludedFromCoverageRec());
            PluginInitializer coverageInitializer = getCoverageInitializer(coverageExcludedFiles);
            if (coverageInitializer != null) {
                builder.withPluginInitializer(coverageInitializer);
                builder.withPluginInitializer(new DependenciesTouchFix());
                runCoverage = true;
            }
        }
    }
    builder.setFlags(toStringArray(flagArgs));
    builder.setFlagsParser(new IntelliJFlagParser(mySettings, dryRun));
    JsTestDriver jstd = builder.build();
    jstd.runConfiguration();
    if (runCoverage) {
        File[] coverageReportFiles = emptyOutputDir.listFiles((dir, name) -> name.endsWith("-coverage.dat"));
        if (coverageReportFiles != null && coverageReportFiles.length == 1) {
            try {
                CoverageReport coverageReport = CoverageSerializationUtils.readLCOV(coverageReportFiles[0]);
                for (String excludedPath : coverageExcludedFiles) {
                    coverageReport.clearReportByFilePath(excludedPath);
                }
                myCoverageSession.mergeReport(coverageReport);
            } catch (Exception e) {
                myTreeManager.printThrowable(e);
            }
        }
    }
}
Also used : PluginInitializer(com.google.jstestdriver.hooks.PluginInitializer) Configuration(com.google.jstestdriver.config.Configuration) ResolvedConfiguration(com.google.jstestdriver.config.ResolvedConfiguration) ParsedConfiguration(com.google.jstestdriver.config.ParsedConfiguration) Multibinder(com.google.inject.multibindings.Multibinder) CoverageReport(com.google.jstestdriver.idea.rt.coverage.CoverageReport) TestResultHolder(com.google.jstestdriver.output.TestResultHolder) JsTestDriverBuilder(com.google.jstestdriver.embedded.JsTestDriverBuilder) ConfigurationException(com.google.jstestdriver.config.ConfigurationException) AbstractModule(com.google.inject.AbstractModule) ResolvedConfiguration(com.google.jstestdriver.config.ResolvedConfiguration) ParsedConfiguration(com.google.jstestdriver.config.ParsedConfiguration) ConfigurationException(com.google.jstestdriver.config.ConfigurationException) TestListener(com.google.jstestdriver.hooks.TestListener) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule)

Example 4 with CoverageReport

use of com.google.jstestdriver.idea.rt.coverage.CoverageReport in project intellij-plugins by JetBrains.

the class CoverageSerializationUtils method readLCOV.

public static CoverageReport readLCOV(@NotNull File file) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    try {
        String currentFileName = null;
        String line;
        List<CoverageReport.LineHits> lineDataList = null;
        CoverageReport report = new CoverageReport();
        while ((line = reader.readLine()) != null) {
            if (line.startsWith(SOURCE_FILE_PREFIX)) {
                currentFileName = line.substring(SOURCE_FILE_PREFIX.length());
                lineDataList = Lists.newArrayList();
            } else if (line.startsWith(LINE_HIT_PREFIX)) {
                if (lineDataList == null) {
                    throw new RuntimeException("lineDataList is null!");
                }
                String[] values = line.substring(LINE_HIT_PREFIX.length()).split(",");
                Preconditions.checkState(values.length == 2);
                int lineNum = Integer.parseInt(values[0]);
                int hitCount = Integer.parseInt(values[1]);
                CoverageReport.LineHits lineHits = new CoverageReport.LineHits(lineNum, hitCount);
                lineDataList.add(lineHits);
            } else if (END_OF_RECORD.equals(line)) {
                if (lineDataList == null) {
                    throw new RuntimeException("lineDataList is null!");
                }
                Preconditions.checkNotNull(currentFileName);
                report.mergeFileReport(currentFileName, lineDataList);
                currentFileName = null;
                lineDataList = null;
            }
        }
        Preconditions.checkState(lineDataList == null && currentFileName == null);
        return report;
    } finally {
        reader.close();
    }
}
Also used : CoverageReport(com.google.jstestdriver.idea.rt.coverage.CoverageReport)

Aggregations

CoverageReport (com.google.jstestdriver.idea.rt.coverage.CoverageReport)4 ClassData (com.intellij.rt.coverage.data.ClassData)2 ProjectData (com.intellij.rt.coverage.data.ProjectData)2 AbstractModule (com.google.inject.AbstractModule)1 Module (com.google.inject.Module)1 Multibinder (com.google.inject.multibindings.Multibinder)1 Configuration (com.google.jstestdriver.config.Configuration)1 ConfigurationException (com.google.jstestdriver.config.ConfigurationException)1 ParsedConfiguration (com.google.jstestdriver.config.ParsedConfiguration)1 ResolvedConfiguration (com.google.jstestdriver.config.ResolvedConfiguration)1 JsTestDriverBuilder (com.google.jstestdriver.embedded.JsTestDriverBuilder)1 PluginInitializer (com.google.jstestdriver.hooks.PluginInitializer)1 TestListener (com.google.jstestdriver.hooks.TestListener)1 TestResultHolder (com.google.jstestdriver.output.TestResultHolder)1 ExportToHTMLSettings (com.intellij.codeEditor.printing.ExportToHTMLSettings)1 JSFile (com.intellij.lang.javascript.psi.JSFile)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 LineData (com.intellij.rt.coverage.data.LineData)1 File (java.io.File)1