<commit_before>const fs = require('fs')
const path = require('path')
const tmp = require('tmp')
const glob = require('glob')
const mkdirp = require('mkdirp')
const pify = require('pify')
const dummyTestModulesFolder = tmp.dirSync({ unsafeCleanup: true }).name
const NightwatchApi = require('./nightwatch-api')
const CucumberApi = require('./cucumber-api')

tmp.setGracefulCleanup()

module.exports = class Runner {
  constructor () {
    this.originalFeaturePath = {}
  }

  generateDummyTestModules () {
    this.featureFiles.forEach((featureSource) => {
      const featureFiles = glob.sync(path.join(featureSource, '**/*.feature'))

      featureFiles.forEach((featureFile) => {
        const dummyTestModuleFile = this.featurePathToDummyPath(featureFile)
        mkdirp.sync(path.dirname(dummyTestModuleFile))
        fs.writeFileSync(dummyTestModuleFile, '')
      })
    })
  }

  getTags (tags) {
    tags = tags || []

    if (typeof tags === 'string') {
      tags = tags.split(',')
    }

    if (Array.isArray(tags)) {
      return tags.map((tag) => `@${tag}`)
    } else {
      throw new Error(`Expected tags to be Array or String.`)
    }
  }

  featurePathToDummyPath (featureFile) {
    if (!featureFile.match(/\.feature$/)) featureFile += '.feature'

    const dummyPath = path.join(dummyTestModulesFolder, featureFile.replace(/\.feature$/, '.js'))

    this.originalFeaturePath[dummyPath] = featureFile

    return dummyPath
  }

  dummyPathToFeaturePath (dummyPath) {
    if (!dummyPath.match(/\.js$/)) dummyPath += '.js'

    return this.originalFeaturePath[dummyPath] || dummyPath
  }

  addIndexToFileName (fileName, index) {
    const cutIndex = fileName.lastIndexOf('.')
    const ext = fileName.slice(cutIndex)
    const rest = fileName.slice(0, cutIndex)
    return `${rest}-${index}${ext}`
  }

  * executeTestModules (dummyTestModules) {
    const featureFiles = dummyTestModules.map((dummyTestModule) => this.dummyPathToFeaturePath(dummyTestModule))

    if (this.nightwatchApi.isRunningInParallel()) {
      const workerIndex = this.nightwatchApi.getWorkerIndex()
      this.jsonReport = this.addIndexToFileName(this.jsonReport, workerIndex)
    }

    if (this.jsonReport) {
      yield pify(mkdirp)(path.dirname(this.jsonReport))
    }
    const cucumberArgs = this.cucumberApi.getAdjustedArgs({
      args: this.options.cucumberArgs,
      featureFiles,
      jsonReport: this.jsonReport,
      tags: this.getTags(this.nightwatchApi.nightwatchArgv.tag),
      skipTags: this.getTags(this.nightwatchApi.nightwatchArgv.skiptags)
    })
    return yield * this.cucumberApi.run(cucumberArgs)
  }

  run (options) {
    const self = this
    this.options = options
    this.nightwatchApi = new NightwatchApi(options)
    this.cucumberApi = new CucumberApi(options)
    this.jsonReport = this.cucumberApi.getJSONReportName(options.cucumberArgs)
    this.featureFiles = this.cucumberApi.getFeatureFiles(options.cucumberArgs)
    this.featureFiles.forEach((featureSource) => {
      try {
        fs.statSync(featureSource)
      } catch (err) {
        throw new Error(`Feature source ${featureSource} doesn't exists`)
      }
    })
    const dummyPaths = this.featureFiles.map((srcPath) => path.join(dummyTestModulesFolder, srcPath))

    this.generateDummyTestModules()

    this.cucumberApi.injectTimeoutHandler()
    this.cucumberApi.addAttachmentProvider(this.nightwatchApi.takeScreenshot.bind(this.nightwatchApi))

    this.nightwatchApi.addTestModulePaths(dummyPaths)
    this.nightwatchApi.addPathConverter(this.featurePathToDummyPath.bind(this), this.dummyPathToFeaturePath.bind(this))
    this.nightwatchApi.overrideOriginalStartTestWorkers()
    this.nightwatchApi.addTestRunner(this.executeTestModules.bind(this))
    this.nightwatchApi.addHookAfterChildProcesses(function * () {
      const reports = yield pify(glob)(self.addIndexToFileName(self.jsonReport, '*'))
      yield * self.cucumberApi.mergeCucumberJsonReports(reports, self.jsonReport)
    })
  }
}
<commit_msg>Allow for single features to be added to cucumberargs<commit_after>const fs = require('fs')
const path = require('path')
const tmp = require('tmp')
const glob = require('glob')
const mkdirp = require('mkdirp')
const pify = require('pify')
const dummyTestModulesFolder = tmp.dirSync({ unsafeCleanup: true }).name
const NightwatchApi = require('./nightwatch-api')
const CucumberApi = require('./cucumber-api')

tmp.setGracefulCleanup()

module.exports = class Runner {
  constructor () {
    this.originalFeaturePath = {}
  }

  generateDummyTestModules () {
    this.featureFiles.forEach((featureSource) => {
      let featureFiles
      if (fs.statSync(featureSource).isDirectory()) {
        featureFiles = glob.sync(path.join(featureSource, '**/*.feature'))
      } else {
        featureFiles = [featureSource]
      }

      featureFiles.forEach((featureFile) => {
        const dummyTestModuleFile = this.featurePathToDummyPath(featureFile)
        mkdirp.sync(path.dirname(dummyTestModuleFile))
        fs.writeFileSync(dummyTestModuleFile, '')
      })
    })
  }

  getTags (tags) {
    tags = tags || []

    if (typeof tags === 'string') {
      tags = tags.split(',')
    }

    if (Array.isArray(tags)) {
      return tags.map((tag) => `@${tag}`)
    } else {
      throw new Error(`Expected tags to be Array or String.`)
    }
  }

  featurePathToDummyPath (featureFile) {
    if (!featureFile.match(/\.feature$/)) featureFile += '.feature'

    const dummyPath = path.join(dummyTestModulesFolder, featureFile.replace(/\.feature$/, '.js'))

    this.originalFeaturePath[dummyPath] = featureFile

    return dummyPath
  }

  dummyPathToFeaturePath (dummyPath) {
    if (!dummyPath.match(/\.js$/)) dummyPath += '.js'

    return this.originalFeaturePath[dummyPath] || dummyPath
  }

  addIndexToFileName (fileName, index) {
    const cutIndex = fileName.lastIndexOf('.')
    const ext = fileName.slice(cutIndex)
    const rest = fileName.slice(0, cutIndex)
    return `${rest}-${index}${ext}`
  }

  * executeTestModules (dummyTestModules) {
    const featureFiles = dummyTestModules.map((dummyTestModule) => this.dummyPathToFeaturePath(dummyTestModule))

    if (this.nightwatchApi.isRunningInParallel()) {
      const workerIndex = this.nightwatchApi.getWorkerIndex()
      this.jsonReport = this.addIndexToFileName(this.jsonReport, workerIndex)
    }

    if (this.jsonReport) {
      yield pify(mkdirp)(path.dirname(this.jsonReport))
    }
    const cucumberArgs = this.cucumberApi.getAdjustedArgs({
      args: this.options.cucumberArgs,
      featureFiles,
      jsonReport: this.jsonReport,
      tags: this.getTags(this.nightwatchApi.nightwatchArgv.tag),
      skipTags: this.getTags(this.nightwatchApi.nightwatchArgv.skiptags)
    })
    return yield * this.cucumberApi.run(cucumberArgs)
  }

  run (options) {
    const self = this
    this.options = options
    this.nightwatchApi = new NightwatchApi(options)
    this.cucumberApi = new CucumberApi(options)
    this.jsonReport = this.cucumberApi.getJSONReportName(options.cucumberArgs)
    this.featureFiles = this.cucumberApi.getFeatureFiles(options.cucumberArgs)
    this.featureFiles.forEach((featureSource) => {
      try {
        fs.statSync(featureSource)
      } catch (err) {
        throw new Error(`Feature source ${featureSource} doesn't exists`)
      }
    })

    const dummyPaths = this.featureFiles.map((srcPath) => {
      const sourcePath = fs.statSync(srcPath).isDirectory() ? srcPath : path.dirname(srcPath)
      return path.join(dummyTestModulesFolder, sourcePath)
    })

    this.generateDummyTestModules()

    this.cucumberApi.injectTimeoutHandler()
    this.cucumberApi.addAttachmentProvider(this.nightwatchApi.takeScreenshot.bind(this.nightwatchApi))

    this.nightwatchApi.addTestModulePaths(dummyPaths)
    this.nightwatchApi.addPathConverter(this.featurePathToDummyPath.bind(this), this.dummyPathToFeaturePath.bind(this))
    this.nightwatchApi.overrideOriginalStartTestWorkers()
    this.nightwatchApi.addTestRunner(this.executeTestModules.bind(this))
    this.nightwatchApi.addHookAfterChildProcesses(function * () {
      const reports = yield pify(glob)(self.addIndexToFileName(self.jsonReport, '*'))
      yield * self.cucumberApi.mergeCucumberJsonReports(reports, self.jsonReport)
    })
  }
}

{'fraction_non_alphanumeric': 0.12490983409473431, 'fraction_numerical': 0.00024044241404183698, 'mean_word_length': 3.9137625516834023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '6108503', 'n_tokens_mistral': 2573, 'n_tokens_neox': 2550, 'n_words': 410}