<commit_before>"use strict"
const fs = require('fs')
const assert = require('assert')
const path = require('path')
const join = path.join
const relative = path.relative

const lstat = a => new Promise((s, f) =>
  fs.lstat(a, (e, r) => e ? f(e) : s(r)))

const readdir = a => new Promise((s, f) =>
  fs.readdir(a, (e, r) => e ? f(e) : s(r)))

const eachJsFiles = (dirname, fn) => readdir(dirname)
  .then(files => files.map(f => join(dirname, f)))
  .then(files => Promise.all(files.map(filename => lstat(filename)
    .then(stats => stats.isDirectory()
      ? eachJsFiles(filename, fn)
      : fn(filename, stats)))))

const isFn = fn => typeof fn === 'function'

const mochaDetected = Boolean(process.argv.map(p => p.split(path.sep))
  .map(p => p.slice(p.length - 2).join('').replace('_', ''))
  .filter(p => /binmocha/.test(p)).length)

const mochaGlobals = [
  'after',
  'afterEach',
  'before',
  'beforeEach',
  'context',
  'describe',
  'it',
  'setup',
  'suite',
  'suiteSetup',
  'suiteTeardown',
  'teardown',
  'test',
]

const missingErrorMessage = 'test-mirror: no tests for module';

const noFile = () => { throw Error('no test file !') }
const isStr = str => typeof str === 'string'

const init = opts => {
  opts || (opts = {})
  const rootDir = isStr(opts.rootPath)
    ? opts.rootPath
    : __dirname.slice(0, __dirname.lastIndexOf('node_modules'))

  const inMocha = opts.forceMocha || mochaDetected
  const suffix = isStr(opts.suffix) ? opts.suffix : '.spec'
  const testDir = isStr(opts.testPath) ? opts.testPath : 'test'
  const srcDir = isStr(opts.srcPath) ? opts.srcPath : 'core'
  const testPath = join(rootDir, testDir)
  const srcPath = join(rootDir, srcDir)
  const getTestPath = filename => join(testPath, filename.slice(srcPath.length, -3))
  const fireEvent = (fn, value, fallback) => isFn(fn)
    ? fn(value)
    : isFn(fallback) ? fallback(value) : value

  if (!isFn(opts.match)) {
    opts.match = RegExp.prototype.test.bind(opts.match instanceof RegExp
      ? opts.match
      : /\.js$/)
  }

  opts.args || (opts.args = { assert })

  const addInfo = (src, filename) => {
    src.name = filename.slice(srcPath.length + 1)
    src.srcPath = filename
    src.testPath = getTestPath(filename) + suffix
    return src
  }

  const startTests = () => eachJsFiles(srcPath, filename => {
    if (!opts.match(filename)) return
    const _module = require(filename)

    try {
      let testFilePath
      try {
        testFilePath = require.resolve(getTestPath(filename) + suffix)
      } catch (err) {
        throw new Error(missingErrorMessage)        
      }
      const testForModule = require(testFilePath)
      if (!isFn(opts.wrapper)) return testForModule(opts.args, _module)
      opts.wrapper(addInfo({}, filename), () => testForModule(opts.args, _module))
    }

    catch (err) {
      err.testNotFound = err.message === missingErrorMessage
      fireEvent(opts.onFailingTest, addInfo(err, filename), () => err.testNotFound
        ? opts.ignoreNotFound<commit_msg>allow synchronous build<commit_after>"use strict"
const fs = require('fs')
const assert = require('assert')
const path = require('path')
const join = path.join
const relative = path.relative

const lstat = a => new Promise((s, f) =>
  fs.lstat(a, (e, r) => e ? f(e) : s(r)))

const readdir = a => new Promise((s, f) =>
  fs.readdir(a, (e, r) => e ? f(e) : s(r)))

const eachJsFiles = (dirname, fn) => readdir(dirname)
  .then(files => files.map(f => join(dirname, f)))
  .then(files => Promise.all(files.map(filename => lstat(filename)
    .then(stats => stats.isDirectory()
      ? eachJsFiles(filename, fn)
      : fn(filename, stats)))))

const eachJsFilesSync = (dirname, fn) => readdirSync(dirname)
  .map(f => {
    const filename = join(dirname, f)
    const stats = lstatSync(filename)

    return stats.isDirectory()
      ? eachJsFilesSync(filename, fn)
      : fn(filename, stats)
  })

const isFn = fn => typeof fn === 'function'

const mochaDetected = Boolean(process.argv.map(p => p.split(path.sep))
  .map(p => p.slice(p.length - 2).join('').replace('_', ''))
  .filter(p => /binmocha/.test(p)).length)

const mochaGlobals = [
  'after',
  'afterEach',
  'before',
  'beforeEach',
  'context',
  'describe',
  'it',
  'setup',
  'suite',
  'suiteSetup',
  'suiteTeardown',
  'teardown',
  'test',
]

const missingErrorMessage = 'test-mirror: no tests for module';

const noFile = () => { throw Error('no test file !') }
const isStr = str => typeof str === 'string'

const init = opts => {
  opts || (opts = {})
  const rootDir = isStr(opts.rootPath)
    ? opts.rootPath
    : __dirname.slice(0, __dirname.lastIndexOf('node_modules'))

  const inMocha = opts.forceMocha || mochaDetected
  const suffix = isStr(opts.suffix) ? opts.suffix : '.spec'
  const getFiles = opts.forceSync ? eachJsFilesSync : eachJsFiles
  const testDir = isStr(opts.testPath) ? opts.testPath : 'test'
  const srcDir = isStr(opts.srcPath) ? opts.srcPath : 'core'
  const testPath = join(rootDir, testDir)
  const srcPath = join(rootDir, srcDir)
  const getTestPath = filename => join(testPath, filename.slice(srcPath.length, -3))
  const fireEvent = (fn, value, fallback) => isFn(fn)
    ? fn(value)
    : isFn(fallback) ? fallback(value) : value

  if (!isFn(opts.match)) {
    opts.match = RegExp.prototype.test.bind(opts.match instanceof RegExp
      ? opts.match
      : /\.js$/)
  }

  opts.args || (opts.args = { assert })

  const addInfo = (src, filename) => {
    src.name = filename.slice(srcPath.length + 1)
    src.srcPath = filename
    src.testPath = getTestPath(filename) + suffix
    return src
  }

  const startTests = () => getFiles(srcPath, filename => {
    if (!opts.match(filename)) return
    const _module = require(filename)

    try {
      let testFilePath
      try {
        testFilePath = require.resolve(getTestPath(filename) + suffix)
      } catch (err) {
        throw new Error(missingErrorMessage)        
      }
      const testForModule = require(testFilePath)
      if (!isFn(opts.wrapper)) return testForModule(opts.args, _module)
      opts.wrapper(addInfo({}, filename), () => testForModule(opts.args, _module))
    }

    catch (err) {
      err.testNotFound = err.message === missingErrorMessage
      fireEvent(opts.onFailingTest, addInfo(err, filename), () => err.testNotFound
        ? opts.ignoreNotFound
{'fraction_non_alphanumeric': 0.16029573698285354, 'fraction_numerical': 0.0012584552461853075, 'mean_word_length': 3.458625525946704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 57, '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': '4760710', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2142, 'n_words': 487}