From ad20f59eaf541cbac674bd3405245da49cddcaa1 Mon Sep 17 00:00:00 2001 From: James Talmage Date: Thu, 10 Dec 2015 14:01:50 -0500 Subject: [PATCH] extract forking-tap module --- build-tests.js | 134 ++++++++--------------------------------------- package.json | 7 +-- test/nyc-test.js | 2 +- 3 files changed, 27 insertions(+), 116 deletions(-) diff --git a/build-tests.js b/build-tests.js index 58d9fccb4..34e9feb24 100644 --- a/build-tests.js +++ b/build-tests.js @@ -1,127 +1,37 @@ 'use strict' var fs = require('fs') -var Path = require('path') +var path = require('path') var del = require('del') -var recast = require('recast') -var makeSourcemapComment = require('inline-source-map-comment') -var types = recast.types -var n = types.namedTypes +var forkingTap = require('forking-tap') +var zeroFill = require('zero-fill') +var sanitizeFilename = require('sanitize-filename') // Delete previous files. process.chdir(__dirname) del.sync(['test/built-*']) -var outputDir = Path.join(__dirname, 'test') -var inputPath = Path.join(outputDir, 'nyc-test.js') -var inputSource = fs.readFileSync(inputPath, 'utf8') - -function parse () { - return recast.parse(inputSource, { - sourceFileName: inputPath - }) -} - -function print (ast) { - var result = recast.print(rootNode(ast), { - sourceMapName: inputPath + '.map' - }) - - return result.code + '\n' + makeSourcemapComment(result.map) -} - -var i = 0 - -types.visit(parse(), { - visitExpressionStatement: function (path) { - var node = path.node - if (isIt(node)) { - fs.writeFileSync( - Path.join(outputDir, name(path, i)), - print(copy(path)) - ) - i++ - return false - } - this.traverse(path) - } +var testDir = path.join(__dirname, 'test') +var originalTestsFilename = path.join(testDir, 'nyc-test.js') +var originalTestSource = fs.readFileSync(originalTestsFilename, 'utf8') +var individualTests = forkingTap(originalTestSource, { + filename: originalTestsFilename, + attachComment: true }) -function copy (path) { - var copied - if (path.parentPath) { - copied = copy(path.parentPath).get(path.name) - } else { - copied = new types.NodePath({root: parse()}) - } - - var parent = copied.parent - var node = copied.value - if (!(n.Node.check(node) && parent && (n.BlockStatement.check(parent.node) || n.Program.check(parent.node)))) { - return copied - } +individualTests.forEach(function (test, i) { + var filename = ['built', zeroFill(3, i)] + .concat(test.nestedName) + .join('-') + '.js' - var body = parent.get('body').value - var keeper = parent.get('body', path.name).node + // file names with spaces are legal, but annoying to use w/ CLI commands + filename = filename.replace(/\s/g, '_') - var statementIdx = 0 + // istanbul freaks out if the there are `'` characters in the file name + filename = filename.replace(/'/g, '') - while (statementIdx < body.length) { - var statement = body[statementIdx] - if ((isDescribe(statement) || isIt(statement)) && statement !== keeper) { - parent.get('body', statementIdx).replace() - } else { - statementIdx++ - } - } + // remove any illegal chars + filename = sanitizeFilename(filename) - return copied -} - -function isDescribe (node) { - if (!n.ExpressionStatement.check(node)) { - return false - } - node = node.expression - return n.CallExpression.check(node) && n.Identifier.check(node.callee) && (node.callee.name === 'describe') -} - -function isIt (node) { - if (!n.ExpressionStatement.check(node)) { - return false - } - node = node.expression - return n.CallExpression.check(node) && n.Identifier.check(node.callee) && (node.callee.name === 'it') -} - -// Walks the path up to the root. -function rootNode (path) { - while (path.parent) { - path = path.parent - } - return path -} - -// Picks a file name for the test, by walking up the tree and looking at describe / require calls. -function name (path, i) { - var arr = [] - _name(path, arr) - var testName = arr.reverse().join(' ') - var filename = i + '-' + testName.replace(/\s/g, '_') + '.js' - if (i < 100) { - filename = (i < 10 ? '00' : '0') + filename - } - return 'built-' + filename -} - -function _name (path, arr) { - if (!path) { - return - } - if (isDescribe(path.node) || isIt(path.node)) { - var firstArg = path.get('expression', 'arguments', 0).node - n.Literal.assert(firstArg) - arr.push(firstArg.value) - } - _name(path.parent, arr) -} + fs.writeFileSync(path.join(testDir, filename), test.code) +}) diff --git a/package.json b/package.json index 7b8eac644..4bc55d2f2 100644 --- a/package.json +++ b/package.json @@ -75,13 +75,14 @@ "chai": "^3.0.0", "coveralls": "^2.11.4", "del": "^2.2.0", - "inline-source-map-comment": "^1.0.5", - "recast": "^0.10.39", + "forking-tap": "^0.1.1", + "sanitize-filename": "^1.5.3", "sinon": "^1.15.3", "source-map-fixtures": "^0.2.0", "source-map-support": "^0.4.0", "standard": "^5.2.1", - "tap": "^1.3.4" + "tap": "^1.3.4", + "zero-fill": "^2.2.1" }, "bundleDependencies": [ "spawn-wrap" diff --git a/test/nyc-test.js b/test/nyc-test.js index a8b6e2f81..0b9b9512d 100644 --- a/test/nyc-test.js +++ b/test/nyc-test.js @@ -46,7 +46,7 @@ describe('nyc', function () { }) describe('config', function () { - it('loads exclude patterns from package.json', function () { + it("loads 'exclude' patterns from package.json", function () { var nyc = new NYC({ cwd: path.resolve(__dirname, './fixtures') })