<commit_before>import _ from 'lodash';
import Gulp from 'gulp';
import GUtil, {PluginError, colors} from 'gulp-util';
import istanbul from 'gulp-istanbul';

import {SourceMapConsumer} from 'source-map';

import {instrumenterConfig} from '../src/cover';
import plumber from '../src/plumber';
import {runTask} from '../src/watch';

import {SOURCE_FILES, COVERAGE_TARGET, WATCHING, COMPLETE_COVERAGE} from '../index';

import {utils, Collector, Report} from 'istanbul';
import through from 'through2';

function coverSourceFiles() {
  return Gulp.src(SOURCE_FILES)
      .pipe(plumber())
      .pipe(istanbul(instrumenterConfig()));
}

// Bit of a hack, but lets us ensure that all source files are included
// in the coverage report.
Gulp.task('cover:untested', function() {
  return coverSourceFiles()
      .pipe(istanbul.writeReports({
        dir: COVERAGE_TARGET,
        reporters: [ 'json' ],
        reportOpts: { dir: `${COVERAGE_TARGET}/untested` }
      }));
});

Gulp.task('cover:report', function() {
  let collector = new Collector();

  return Gulp.src(`${COVERAGE_TARGET}/**/coverage-final.json`)
    .pipe(through.obj(function(file, enc, cb) {
      try {
        collector.add(JSON.parse(file.contents.toString()));
      } catch (err) {
        if (WATCHING) {
          // We can get into an out of sync state where only one set of coverage
          // files are updated and the state doesn't match up, cauing istanbul to
          // blow up. We are ignoring this case and assuming that once the other
          // coverage process completes we will return to a consistent state in
          // the report.
          return;
        } else {
          throw err;
        }
      }

      cb();
    }))
    .on('finish', function() {
      let report = Report.create('lcov', {dir: COVERAGE_TARGET});
      try {
        report.writeReport(collector, true);
      } catch (err) {
        if (WATCHING) {
          // We can get into an out of sync state where only one set of coverage
          // files are updated and the state doesn't match up, cauing istanbul to
          // blow up. We are ignoring this case and assuming that once the other
          // coverage process completes we will return to a consistent state in
          // the report.
          return;
        } else {
          throw err;
        }
      }

      report = Report.create('text-summary');
      report.writeReport(collector, true);

      if (!WATCHING) {
        let errors = _.compact(_.map(collector.getFinalCoverage(), (file, path) => {
          let summary = utils.summarizeFileCoverage(file),
              sourceMap = extractSourceMap(file.code),
              errors = [];

          function mapLine(line, column = 1) {
            if (sourceMap) {
              line = sourceMap.originalPositionFor({line, column}).line || line;<commit_msg>Fix partial match sourcemap lookup in coverage<commit_after>import _ from 'lodash';
import Gulp from 'gulp';
import GUtil, {PluginError, colors} from 'gulp-util';
import istanbul from 'gulp-istanbul';

import {SourceMapConsumer} from 'source-map';

import {instrumenterConfig} from '../src/cover';
import plumber from '../src/plumber';
import {runTask} from '../src/watch';

import {SOURCE_FILES, COVERAGE_TARGET, WATCHING, COMPLETE_COVERAGE} from '../index';

import {utils, Collector, Report} from 'istanbul';
import through from 'through2';

function coverSourceFiles() {
  return Gulp.src(SOURCE_FILES)
      .pipe(plumber())
      .pipe(istanbul(instrumenterConfig()));
}

// Bit of a hack, but lets us ensure that all source files are included
// in the coverage report.
Gulp.task('cover:untested', function() {
  return coverSourceFiles()
      .pipe(istanbul.writeReports({
        dir: COVERAGE_TARGET,
        reporters: [ 'json' ],
        reportOpts: { dir: `${COVERAGE_TARGET}/untested` }
      }));
});

Gulp.task('cover:report', function() {
  let collector = new Collector();

  return Gulp.src(`${COVERAGE_TARGET}/**/coverage-final.json`)
    .pipe(through.obj(function(file, enc, cb) {
      try {
        collector.add(JSON.parse(file.contents.toString()));
      } catch (err) {
        if (WATCHING) {
          // We can get into an out of sync state where only one set of coverage
          // files are updated and the state doesn't match up, cauing istanbul to
          // blow up. We are ignoring this case and assuming that once the other
          // coverage process completes we will return to a consistent state in
          // the report.
          return;
        } else {
          throw err;
        }
      }

      cb();
    }))
    .on('finish', function() {
      let report = Report.create('lcov', {dir: COVERAGE_TARGET});
      try {
        report.writeReport(collector, true);
      } catch (err) {
        if (WATCHING) {
          // We can get into an out of sync state where only one set of coverage
          // files are updated and the state doesn't match up, cauing istanbul to
          // blow up. We are ignoring this case and assuming that once the other
          // coverage process completes we will return to a consistent state in
          // the report.
          return;
        } else {
          throw err;
        }
      }

      report = Report.create('text-summary');
      report.writeReport(collector, true);

      if (!WATCHING) {
        let errors = _.compact(_.map(collector.getFinalCoverage(), (file, path) => {
          let summary = utils.summarizeFileCoverage(file),
              sourceMap = extractSourceMap(file.code),
              errors = [];

          function mapLine(line, column = 0) {
            if (sourceMap) {
              line = sourceMap.originalPositionFor({line, column, bias: SourceMapConsumer.LEAST_UPPER_BOUND}).line || line;
{'fraction_non_alphanumeric': 0.11739280774550484, 'fraction_numerical': 0.0006915629322268327, 'mean_word_length': 2.4765625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2704599', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1640, 'n_words': 525}