-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: fix test-sync-io-option #1840
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const spawn = require('child_process').spawn; | ||
|
||
const execFile = require('child_process').execFile; | ||
|
||
if (process.argv[2] === 'child') { | ||
setImmediate(function() { | ||
|
@@ -14,34 +13,23 @@ if (process.argv[2] === 'child') { | |
(function runTest(flags) { | ||
var execArgv = [flags.pop()]; | ||
var args = [__filename, 'child']; | ||
var child = spawn(process.execPath, execArgv.concat(args)); | ||
var stderr = ''; | ||
|
||
child.stdout.on('data', function(chunk) { | ||
throw new Error('UNREACHABLE'); | ||
}); | ||
|
||
child.stderr.on('data', function(chunk) { | ||
stderr += chunk.toString(); | ||
}); | ||
|
||
child.on('close', function() { | ||
var cntr1 = (stderr.match(/WARNING/g) || []).length; | ||
var cntr2 = (stderr.match(/fs\.readFileSync/g) || []).length; | ||
assert.equal(cntr1, cntr2); | ||
if (execArgv[0] === '--trace-sync-io') { | ||
// Prints 4 WARNINGS for --trace-sync-io. 1 for each sync call | ||
// inside readFileSync | ||
assert.equal(cntr1, 4); | ||
} else if (execArgv[0] === ' ') { | ||
assert.equal(cntr1, 0); | ||
var cntr = 0; | ||
args = execArgv.concat(args); | ||
if (!args[0]) args.shift(); | ||
execFile(process.execPath, args, function(err, stdout, stderr) { | ||
assert.equal(err, null); | ||
assert.equal(stdout, ''); | ||
if (/^WARNING[\s\S]*fs\.readFileSync/.test(stderr)) | ||
cntr++; | ||
if (args[0] === '--trace-sync-io') { | ||
assert.equal(cntr, 1); | ||
} else if (args[0] === __filename) { | ||
assert.equal(cntr, 0); | ||
} else { | ||
throw new Error('UNREACHABLE'); | ||
} | ||
|
||
if (flags.length > 0) | ||
setImmediate(runTest, flags); | ||
}); | ||
}(['--trace-sync-io', ' '])); | ||
}(['--trace-sync-io', ''])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this flags game and IIFE required? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I wrote it I wanted each run to have it's own running context. So I kept most variables local to the IIFE so there wouldn't be any accidental interference. Just a technique to help write more reliable tests. |
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this change from 4 to 1 warning?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It changed because we only get stderr in the callback one time for each call. Previously it was in a
data
event which was emit more than once.