build-tests.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict'
  2. var fs = require('fs')
  3. var path = require('path')
  4. var rimraf = require('rimraf')
  5. var mkdirp = require('mkdirp')
  6. var forkingTap = require('forking-tap')
  7. var zeroFill = require('zero-fill')
  8. var sanitizeFilename = require('sanitize-filename')
  9. // Delete previous files.
  10. process.chdir(__dirname)
  11. rimraf.sync('test/build')
  12. mkdirp.sync(path.join(__dirname, 'test/build'))
  13. var testDir = path.join(__dirname, 'test/src')
  14. var buildDir = path.join(__dirname, 'test/build')
  15. var originalTestsFilename = path.join(testDir, 'nyc-tap.js')
  16. var originalTestSource = fs.readFileSync(originalTestsFilename, 'utf8')
  17. var individualTests = forkingTap(originalTestSource, {
  18. filename: originalTestsFilename,
  19. attachComment: true
  20. })
  21. function writeTest (test, i, enableCache) {
  22. var filename = ['built', zeroFill(3, i)]
  23. .concat(test.nestedName)
  24. .join('-')
  25. if (enableCache) {
  26. filename += '-cache'
  27. }
  28. filename += '.js'
  29. // file names with spaces are legal, but annoying to use w/ CLI commands
  30. filename = filename.replace(/\s/g, '_')
  31. // istanbul freaks out if the there are `'` characters in the file name
  32. filename = filename.replace(/'/g, '')
  33. // remove any illegal chars
  34. filename = sanitizeFilename(filename)
  35. var code = test.code
  36. if (enableCache) {
  37. code = code.replace('var enableCache = false', 'var enableCache = true')
  38. }
  39. fs.writeFileSync(path.join(buildDir, filename), code)
  40. }
  41. individualTests.forEach(function (test, i) {
  42. writeTest(test, i * 2, false)
  43. writeTest(test, i * 2 + 1, true)
  44. })