Search in sources :

Example 1 with ScriptData

use of com.github.timurstrekalov.saga.core.model.ScriptData 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 2 with ScriptData

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

the class HtmlUnitBasedScriptInstrumenter method instrument.

@Override
public String instrument(final String sourceCode, final String sourceName, final int lineNumber) {
    try {
        final String normalizedSourceName = handleEvals(handleInvalidUriChars(handleInlineScripts(sourceName)));
        if (shouldIgnore(normalizedSourceName)) {
            return sourceCode;
        }
        final boolean separateFile = isSeparateFile(sourceName, normalizedSourceName);
        final URI sourceUri = URI.create(normalizedSourceName).normalize();
        if (config.isCacheInstrumentedCode() && instrumentedScriptCache.containsKey(sourceUri)) {
            final ScriptData data = instrumentedScriptCache.get(sourceUri);
            scriptDataList.add(data);
            return data.getInstrumentedSourceCode();
        }
        final ScriptData data = addNewScriptData(sourceCode, separateFile, sourceUri);
        final String instrumentedCode = instrument(lineNumber, data);
        data.setInstrumentedSourceCode(instrumentedCode);
        maybeCache(sourceUri, data);
        maybeWriteInstrumentedCodeToDisk(separateFile, sourceUri, instrumentedCode);
        return instrumentedCode;
    } catch (final RuntimeException e) {
        logger.error("Exception caught while instrumenting code", e);
        return sourceCode;
    }
}
Also used : ScriptData(com.github.timurstrekalov.saga.core.model.ScriptData) URI(java.net.URI)

Example 3 with ScriptData

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

the class HtmlUnitBasedScriptInstrumenterTest method verifyClassJsData.

private void verifyClassJsData(String sourceName, String expectedSourceName) {
    final ScriptInstrumenter instrumenter = new HtmlUnitBasedScriptInstrumenter(new InstanceFieldPerPropertyConfig());
    instrumenter.instrument(Data.getClassJsSourceCode(), sourceName, 1);
    assertEquals(1, instrumenter.getScriptDataList().size());
    final ScriptData classJsData = instrumenter.getScriptDataList().get(0);
    assertEquals(expectedSourceName, classJsData.getSourceUriAsString());
    assertEquals(5, classJsData.getLineNumberOfFirstStatement());
    assertEquals(114, classJsData.getNumberOfStatements());
    assertEqualsIgnoreCRLF(Data.getClassJsInstrumented(expectedSourceName), classJsData.getInstrumentedSourceCode());
}
Also used : ScriptData(com.github.timurstrekalov.saga.core.model.ScriptData) InstanceFieldPerPropertyConfig(com.github.timurstrekalov.saga.core.cfg.InstanceFieldPerPropertyConfig)

Example 4 with ScriptData

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

the class FileSystemSourcePreloader method preloadSources.

@Override
public void preloadSources(final Config config, final TestRunCoverageStatistics totalStats) throws IOException {
    final ScriptInstrumenter instrumenter = new HtmlUnitBasedScriptInstrumenter(config);
    final String sourcesToPreload = config.getSourcesToPreload();
    final URI baseUri = config.getBaseUri();
    if (sourcesToPreload == null || !config.getOutputStrategy().contains(OutputStrategy.TOTAL) || !UriUtil.isFileUri(baseUri)) {
        return;
    }
    final String sourcesToPreloadEncoding = config.getSourcesToPreloadEncoding();
    logger.info("Using {} to preload sources", sourcesToPreloadEncoding);
    final List<File> filesToPreload = FileUtils.getFiles(new File(baseUri), sourcesToPreload, null);
    logger.info("Preloading {} files", filesToPreload.size());
    for (final File file : filesToPreload) {
        logger.debug("Preloading {}", file);
        final String source = CharStreams.toString(Files.newReaderSupplier(file, Charset.forName(sourcesToPreloadEncoding)));
        instrumenter.instrument(source, file.toURI().toString(), 0);
    }
    for (final ScriptData data : instrumenter.getScriptDataList()) {
        final Map<String, Long> coverageData = Maps.newHashMap();
        for (final Integer lineNumber : data.getLineNumbersOfAllStatements()) {
            coverageData.put(String.valueOf(lineNumber), 0L);
        }
        totalStats.add(data.generateScriptCoverageStatistics(baseUri, coverageData));
    }
}
Also used : HtmlUnitBasedScriptInstrumenter(com.github.timurstrekalov.saga.core.instrumentation.HtmlUnitBasedScriptInstrumenter) ScriptInstrumenter(com.github.timurstrekalov.saga.core.instrumentation.ScriptInstrumenter) HtmlUnitBasedScriptInstrumenter(com.github.timurstrekalov.saga.core.instrumentation.HtmlUnitBasedScriptInstrumenter) ScriptData(com.github.timurstrekalov.saga.core.model.ScriptData) URI(java.net.URI) File(java.io.File)

Example 5 with ScriptData

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

the class HtmlUnitBasedScriptInstrumenter method addNewScriptData.

private ScriptData addNewScriptData(final String sourceCode, final boolean separateFile, final URI sourceUri) {
    final ScriptData data = new ScriptData(sourceUri, sourceCode, separateFile);
    scriptDataList.add(data);
    return data;
}
Also used : ScriptData(com.github.timurstrekalov.saga.core.model.ScriptData)

Aggregations

ScriptData (com.github.timurstrekalov.saga.core.model.ScriptData)5 URI (java.net.URI)3 InstanceFieldPerPropertyConfig (com.github.timurstrekalov.saga.core.cfg.InstanceFieldPerPropertyConfig)1 HtmlUnitBasedScriptInstrumenter (com.github.timurstrekalov.saga.core.instrumentation.HtmlUnitBasedScriptInstrumenter)1 ScriptInstrumenter (com.github.timurstrekalov.saga.core.instrumentation.ScriptInstrumenter)1 ScriptCoverageStatistics (com.github.timurstrekalov.saga.core.model.ScriptCoverageStatistics)1 TestRunCoverageStatistics (com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics)1 File (java.io.File)1