<commit_before>// @flow
import path from 'path';
import EventEmitter from 'events';
import _ from 'lodash';
import chokidar from 'chokidar';

import { glob } from '../util/glob';
import { ensureAbsolutePath } from '../util/paths';
import createCompiler from '../webpack/compiler/createCompiler';
import createWatchCompiler from '../webpack/compiler/createWatchCompiler';
import registerInMemoryCompiler from '../webpack/compiler/registerInMemoryCompiler';
import registerReadyCallback from '../webpack/compiler/registerReadyCallback';
// $FlowFixMe
import { EntryConfig, KEY as ENTRY_CONFIG_KEY } from '../webpack/loader/entryLoader';
import configureMocha from './configureMocha';
import getBuildStats from '../webpack/util/getBuildStats';
import buildProgressPlugin from '../webpack/plugin/buildProgressPlugin';

import type { MochaWebpackOptions } from '../MochaWebpack';
import type { BuildStats } from '../webpack/util/getBuildStats';
import type { WatchCompiler } from '../webpack/compiler/createWatchCompiler';
import type { Compiler, Stats } from '../webpack/types';

const entryPath = path.resolve(__dirname, '../entry.js');
const entryLoaderPath = path.resolve(__dirname, '../webpack/loader/entryLoader.js');
const includeLoaderPath = path.resolve(__dirname, '../webpack/loader/includeFilesLoader.js');
const noop = () => void 0;


type MochaRunner = {
  abort: () => void,
};
type Mocha = {
  run: (cb: (failures: number) => void) => MochaRunner,
};

export default class TestRunner extends EventEmitter {

  entries: Array<string>;
  includes: Array<string>;
  tmpPath: string;
  options: MochaWebpackOptions;
  outputFilePath: string;

  constructor(entries: Array<string>, includes: Array<string>, options: MochaWebpackOptions) {
    super();
    this.entries = entries;
    this.includes = includes;

    this.options = options;
    this.tmpPath = path.join(this.options.cwd, '.tmp', 'mocha-webpack');
    this.outputFilePath = path.join(this.tmpPath, 'bundle.js');
  }

  prepareMocha(webpackConfig: Object, stats: Stats): Mocha {
    const mocha: Mocha = configureMocha(this.options);
    const outputPath = webpackConfig.output.path;
    const buildStats: BuildStats = getBuildStats(stats, outputPath);

    global.__webpackManifest__ = buildStats.affectedModules; // eslint-disable-line

    // clear up require cache for changed files to make sure that we get the latest changes
    buildStats.affectedFiles.forEach((filePath) => {
      delete require.cache[filePath];
    });
    // pass webpack's entry files to mocha<commit_msg>use "unique" output path for bundle (#165)<commit_after>// @flow
import path from 'path';
import EventEmitter from 'events';
import _ from 'lodash';
import chokidar from 'chokidar';

import { glob } from '../util/glob';
import { ensureAbsolutePath } from '../util/paths';
import createCompiler from '../webpack/compiler/createCompiler';
import createWatchCompiler from '../webpack/compiler/createWatchCompiler';
import registerInMemoryCompiler from '../webpack/compiler/registerInMemoryCompiler';
import registerReadyCallback from '../webpack/compiler/registerReadyCallback';
// $FlowFixMe
import { EntryConfig, KEY as ENTRY_CONFIG_KEY } from '../webpack/loader/entryLoader';
import configureMocha from './configureMocha';
import getBuildStats from '../webpack/util/getBuildStats';
import buildProgressPlugin from '../webpack/plugin/buildProgressPlugin';

import type { MochaWebpackOptions } from '../MochaWebpack';
import type { BuildStats } from '../webpack/util/getBuildStats';
import type { WatchCompiler } from '../webpack/compiler/createWatchCompiler';
import type { Compiler, Stats } from '../webpack/types';

const entryPath = path.resolve(__dirname, '../entry.js');
const entryLoaderPath = path.resolve(__dirname, '../webpack/loader/entryLoader.js');
const includeLoaderPath = path.resolve(__dirname, '../webpack/loader/includeFilesLoader.js');
const noop = () => void 0;


type MochaRunner = {
  abort: () => void,
};
type Mocha = {
  run: (cb: (failures: number) => void) => MochaRunner,
};

export default class TestRunner extends EventEmitter {

  entries: Array<string>;
  includes: Array<string>;
  tmpPath: string;
  options: MochaWebpackOptions;
  outputFilePath: string;

  constructor(entries: Array<string>, includes: Array<string>, options: MochaWebpackOptions) {
    super();
    this.entries = entries;
    this.includes = includes;

    this.options = options;
    this.tmpPath = path.join(this.options.cwd, '.tmp', 'mocha-webpack', Date.now().toString());
    this.outputFilePath = path.join(this.tmpPath, 'bundle.js');
  }

  prepareMocha(webpackConfig: Object, stats: Stats): Mocha {
    const mocha: Mocha = configureMocha(this.options);
    const outputPath = webpackConfig.output.path;
    const buildStats: BuildStats = getBuildStats(stats, outputPath);

    global.__webpackManifest__ = buildStats.affectedModules; // eslint-disable-line

    // clear up require cache for changed files to make sure that we get the latest changes
    buildStats.affectedFiles.forEach((filePath) => {
      delete require.cache[filePath];
    });
    // pass webpack's entry files to mocha
{'fraction_non_alphanumeric': 0.13072148952676493, 'fraction_numerical': 0.000969743987587277, 'mean_word_length': 4.679515418502203, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '629406', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1407, 'n_words': 388}