Skip to content

Commit

Permalink
extract forking-tap module
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Dec 10, 2015
1 parent f0dce89 commit eee14fa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 116 deletions.
134 changes: 22 additions & 112 deletions build-tests.js
Original file line number Diff line number Diff line change
@@ -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)
})
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion test/nyc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down

0 comments on commit eee14fa

Please sign in to comment.