Search in sources :

Example 1 with TestRunCoverageStatistics

use of com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics in project saga by timurstrekalov.

the class DefaultCoverageGenerator method runTests.

private void runTests(final List<URI> tests, final int actualThreadCount, final OutputStrategy outputStrategy, final TestRunCoverageStatistics totalStats) throws IOException {
    final ExecutorService executorService = Executors.newFixedThreadPool(actualThreadCount);
    final CompletionService<TestRunCoverageStatistics> completionService = new ExecutorCompletionService<TestRunCoverageStatistics>(executorService);
    for (final URI test : tests) {
        completionService.submit(new TestRunCoverageStatisticsCallable(config, test, outputStrategy));
    }
    final List<TestRunCoverageStatistics> allRunStats = Lists.newLinkedList();
    final int submittedTasks = tests.size();
    try {
        for (int i = 0; i < submittedTasks; i++) {
            try {
                final Future<TestRunCoverageStatistics> future = completionService.take();
                final TestRunCoverageStatistics runStats = future.get();
                allRunStats.add(runStats);
            } catch (final Exception e) {
                logger.debug(e.getMessage(), e);
            }
        }
    } finally {
        executorService.shutdown();
    }
    logger.info("Test run finished");
    if (outputStrategy.contains(OutputStrategy.TOTAL)) {
        for (final TestRunCoverageStatistics runStats : allRunStats) {
            if (runStats != TestRunCoverageStatistics.EMPTY) {
                for (final ScriptCoverageStatistics scriptCoverageStatistics : runStats) {
                    totalStats.add(scriptCoverageStatistics);
                }
            }
        }
        new WritesStatistics().write(config, totalStats);
    }
}
Also used : TestRunCoverageStatistics(com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics) ExecutorService(java.util.concurrent.ExecutorService) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) URI(java.net.URI) IOException(java.io.IOException) ScriptCoverageStatistics(com.github.timurstrekalov.saga.core.model.ScriptCoverageStatistics)

Example 2 with TestRunCoverageStatistics

use of com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics in project saga by timurstrekalov.

the class DefaultCoverageGenerator method instrumentAndGenerateReports.

@Override
public void instrumentAndGenerateReports() throws IOException {
    Preconditions.checkNotNull(config.getBaseDir(), "baseDir cannot be null");
    Preconditions.checkNotNull(config.getOutputDir(), "outputDir cannot be null");
    final URI baseUri = config.getBaseUri();
    final List<URI> tests = fetchTests(baseUri);
    if (tests.isEmpty()) {
        logger.warn("No tests found, exiting");
        return;
    }
    final int actualThreadCount = Math.min(config.getThreadCount(), tests.size());
    logger.info("Using up to {} threads", actualThreadCount);
    final OutputStrategy outputStrategy = config.getOutputStrategy();
    logger.info("Output strategy set to {}", outputStrategy);
    if (!config.isIncludeInlineScripts()) {
        config.getNoInstrumentPatterns().add(INLINE_SCRIPT_RE);
        config.getNoInstrumentPatterns().add(".+JavaScriptStringJob");
        config.getNoInstrumentPatterns().add(".+#\\d+\\(eval\\)\\(\\d+\\)");
        config.getNoInstrumentPatterns().add("injected script");
    }
    if (!config.getNoInstrumentPatterns().isEmpty()) {
        logger.info("Using the following no-instrument patterns:\n\t{}", Joiner.on("\n\t").join(config.getNoInstrumentPatterns()));
    }
    final File outputDir = config.getOutputDir();
    FileUtils.mkdir(outputDir.getAbsolutePath());
    if (config.isOutputInstrumentedFiles()) {
        FileUtils.mkdir(config.getInstrumentedFileDirectory().getAbsolutePath());
    }
    final TestRunCoverageStatistics totalStats = new TestRunCoverageStatistics(baseUri.relativize(URI.create(TOTAL_REPORT_NAME)), "Total coverage report");
    totalStats.setSortBy(config.getSortBy());
    totalStats.setOrder(config.getOrder());
    totalStats.setSourceDirs(config.getSourceDirs());
    maybePreloadSources(totalStats);
    runTests(tests, actualThreadCount, outputStrategy, totalStats);
}
Also used : TestRunCoverageStatistics(com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics) URI(java.net.URI) File(java.io.File)

Example 3 with TestRunCoverageStatistics

use of com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics in project saga by timurstrekalov.

the class TestRunCoverageStatisticsCallable method collectAndWriteRunStats.

private TestRunCoverageStatistics collectAndWriteRunStats(final URI test, final Map<String, Map<String, Long>> coverageDataForAllScripts) throws IOException {
    final TestRunCoverageStatistics runStats = new TestRunCoverageStatistics(test);
    runStats.setSortBy(config.getSortBy());
    runStats.setOrder(config.getOrder());
    final URI baseUri = config.getBaseUri();
    for (final ScriptData data : getLocalBrowser().getScriptDataList()) {
        final String sourceUri = data.getSourceUriAsString();
        @SuppressWarnings("unchecked") final Map<String, Long> coverageDataForScript = coverageDataForAllScripts.get(sourceUri);
        final ScriptCoverageStatistics scriptCoverageStatistics = data.generateScriptCoverageStatistics(baseUri, coverageDataForScript);
        runStats.add(scriptCoverageStatistics);
    }
    return runStats;
}
Also used : TestRunCoverageStatistics(com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics) ScriptData(com.github.timurstrekalov.saga.core.model.ScriptData) URI(java.net.URI) ScriptCoverageStatistics(com.github.timurstrekalov.saga.core.model.ScriptCoverageStatistics)

Example 4 with TestRunCoverageStatistics

use of com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics in project saga by timurstrekalov.

the class TestRunCoverageStatisticsCallable method call.

@Override
public TestRunCoverageStatistics call() {
    try {
        logger.info("Running test at {}", test.toString());
        try {
            final TestRunCoverageStatistics runStats = runTest(test);
            if (runStats == TestRunCoverageStatistics.EMPTY) {
                logger.warn("No actual test run for file: {}", test);
            } else if (outputStrategy.contains(OutputStrategy.PER_TEST)) {
                if (UriUtil.isFileUri(test)) {
                    new WritesStatistics().write(config, runStats);
                } else {
                    logger.warn("Output strategy PER_TEST only makes sense in the context of tests run off the filesystem, ignoring");
                }
            }
            return runStats;
        } catch (final IOException e) {
            return TestRunCoverageStatistics.EMPTY;
        } catch (final RuntimeException e) {
            logger.warn("Error running test {}: {}", test.toString(), e.getMessage());
            throw e;
        }
    } finally {
        // keep browser open so that test results can be read
        if (localBrowser.get() != null && config.getInstrumentingBrowser() == null) {
            logger.info("Quitting browser");
            localBrowser.get().quit();
        }
    }
}
Also used : TestRunCoverageStatistics(com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics) IOException(java.io.IOException)

Example 5 with TestRunCoverageStatistics

use of com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics in project saga by timurstrekalov.

the class ReporterUtilTest method test_getFileOutputDir.

@Test
public void test_getFileOutputDir() throws Exception {
    final URI test = URI.create(FILE_PREFIX + Joiner.on("/").join(PARENT_DIR_PATH, "src", "main", "javascript", "MyTest.html"));
    final TestRunCoverageStatistics stats = new TestRunCoverageStatistics(test);
    final File actual = getFileOutputDir(BASE_URI, OUTPUT_DIR, stats).getAbsoluteFile();
    final File expected = new File(PARENT_DIR_PATH, Joiner.on(File.separatorChar).join("target", "main", "javascript")).getAbsoluteFile();
    assertThat(actual, equalTo(expected));
}
Also used : TestRunCoverageStatistics(com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics) URI(java.net.URI) File(java.io.File) Test(org.junit.Test)

Aggregations

TestRunCoverageStatistics (com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics)5 URI (java.net.URI)4 ScriptCoverageStatistics (com.github.timurstrekalov.saga.core.model.ScriptCoverageStatistics)2 File (java.io.File)2 IOException (java.io.IOException)2 ScriptData (com.github.timurstrekalov.saga.core.model.ScriptData)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 ExecutorService (java.util.concurrent.ExecutorService)1 Test (org.junit.Test)1