| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,8 @@ | |||
| 1 | 1 | const assert = require('assert'); | |
| 2 | + const debug = require('util').debuglog('test'); | ||
| 2 | 3 | ||
| 3 | 4 | function onmessage(m) { | |
| 4 | - console.log('CHILD got message:', m); | ||
| 5 | + debug('CHILD got message:', m); | ||
| 5 | 6 | assert.ok(m.hello); | |
| 6 | 7 | process.removeListener('message', onmessage); | |
| 7 | 8 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,14 +2,15 @@ | |||
| 2 | 2 | require('../common'); | |
| 3 | 3 | const assert = require('assert'); | |
| 4 | 4 | const child_process = require('child_process'); | |
| 5 | - const { inspect } = require('util'); | ||
| 5 | + const { debuglog, inspect } = require('util'); | ||
| 6 | + const debug = debuglog('test'); | ||
| 6 | 7 | ||
| 7 | 8 | const p = child_process.spawnSync( | |
| 8 | 9 | process.execPath, [ '--completion-bash' ]); | |
| 9 | 10 | assert.ifError(p.error); | |
| 10 | 11 | ||
| 11 | 12 | const output = p.stdout.toString().trim().replace(/\r/g, ''); | |
| 12 | - console.log(output); | ||
| 13 | + debug(output); | ||
| 13 | 14 | ||
| 14 | 15 | const prefix = `_node_complete() { | |
| 15 | 16 | local cur_word options | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,15 +20,16 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | 22 | 'use strict'; | |
| 23 | - const common = require('../common'); | ||
| 23 | + const { isWindows } = require('../common'); | ||
| 24 | 24 | const assert = require('assert'); | |
| 25 | 25 | ||
| 26 | 26 | const spawn = require('child_process').spawn; | |
| 27 | + const debug = require('util').debuglog('test'); | ||
| 27 | 28 | ||
| 28 | 29 | process.env.HELLO = 'WORLD'; | |
| 29 | 30 | ||
| 30 | 31 | let child; | |
| 31 | - if (common.isWindows) { | ||
| 32 | + if (isWindows) { | ||
| 32 | 33 | child = spawn('cmd.exe', ['/c', 'set'], {}); | |
| 33 | 34 | } else { | |
| 34 | 35 | child = spawn('/usr/bin/env', [], {}); | |
@@ -39,7 +40,7 @@ let response = ''; | |||
| 39 | 40 | child.stdout.setEncoding('utf8'); | |
| 40 | 41 | ||
| 41 | 42 | child.stdout.on('data', function(chunk) { | |
| 42 | - console.log(`stdout: ${chunk}`); | ||
| 43 | + debug(`stdout: ${chunk}`); | ||
| 43 | 44 | response += chunk; | |
| 44 | 45 | }); | |
| 45 | 46 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,17 +20,22 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | 22 | 'use strict'; | |
| 23 | - const common = require('../common'); | ||
| 23 | + const { | ||
| 24 | + isWindows, | ||
| 25 | + mustCall, | ||
| 26 | + mustCallAtLeast, | ||
| 27 | + } = require('../common'); | ||
| 24 | 28 | const assert = require('assert'); | |
| 25 | 29 | const os = require('os'); | |
| 26 | 30 | const spawn = require('child_process').spawn; | |
| 31 | + const debug = require('util').debuglog('test'); | ||
| 27 | 32 | ||
| 28 | 33 | // We're trying to reproduce: | |
| 29 | 34 | // $ echo "hello\nnode\nand\nworld" | grep o | sed s/o/a/ | |
| 30 | 35 | ||
| 31 | 36 | let grep, sed, echo; | |
| 32 | 37 | ||
| 33 | - if (common.isWindows) { | ||
| 38 | + if (isWindows) { | ||
| 34 | 39 | grep = spawn('grep', ['--binary', 'o']); | |
| 35 | 40 | sed = spawn('sed', ['--binary', 's/o/O/']); | |
| 36 | 41 | echo = spawn('cmd.exe', | |
@@ -54,62 +59,66 @@ if (common.isWindows) { | |||
| 54 | 59 | ||
| 55 | 60 | ||
| 56 | 61 | // pipe echo | grep | |
| 57 | - echo.stdout.on('data', function(data) { | ||
| 58 | - console.error(`grep stdin write ${data.length}`); | ||
| 62 | + echo.stdout.on('data', mustCallAtLeast((data) => { | ||
| 63 | + debug(`grep stdin write ${data.length}`); | ||
| 59 | 64 | if (!grep.stdin.write(data)) { | |
| 60 | 65 | echo.stdout.pause(); | |
| 61 | 66 | } | |
| 62 | - }); | ||
| 67 | + })); | ||
| 63 | 68 | ||
| 64 | - grep.stdin.on('drain', function(data) { | ||
| 69 | + // TODO(@jasnell): This does not appear to ever be | ||
| 70 | + // emitted. It's not clear if it is necessary. | ||
| 71 | + grep.stdin.on('drain', (data) => { | ||
| 65 | 72 | echo.stdout.resume(); | |
| 66 | 73 | }); | |
| 67 | 74 | ||
| 68 | 75 | // Propagate end from echo to grep | |
| 69 | - echo.stdout.on('end', function(code) { | ||
| 76 | + echo.stdout.on('end', mustCall((code) => { | ||
| 70 | 77 | grep.stdin.end(); | |
| 71 | - }); | ||
| 78 | + })); | ||
| 72 | 79 | ||
| 73 | - echo.on('exit', function() { | ||
| 74 | - console.error('echo exit'); | ||
| 75 | - }); | ||
| 80 | + echo.on('exit', mustCall(() => { | ||
| 81 | + debug('echo exit'); | ||
| 82 | + })); | ||
| 76 | 83 | ||
| 77 | - grep.on('exit', function() { | ||
| 78 | - console.error('grep exit'); | ||
| 79 | - }); | ||
| 84 | + grep.on('exit', mustCall(() => { | ||
| 85 | + debug('grep exit'); | ||
| 86 | + })); | ||
| 80 | 87 | ||
| 81 | - sed.on('exit', function() { | ||
| 82 | - console.error('sed exit'); | ||
| 83 | - }); | ||
| 88 | + sed.on('exit', mustCall(() => { | ||
| 89 | + debug('sed exit'); | ||
| 90 | + })); | ||
| 84 | 91 | ||
| 85 | 92 | ||
| 86 | 93 | // pipe grep | sed | |
| 87 | - grep.stdout.on('data', function(data) { | ||
| 88 | - console.error(`grep stdout ${data.length}`); | ||
| 94 | + grep.stdout.on('data', mustCallAtLeast((data) => { | ||
| 95 | + debug(`grep stdout ${data.length}`); | ||
| 89 | 96 | if (!sed.stdin.write(data)) { | |
| 90 | 97 | grep.stdout.pause(); | |
| 91 | 98 | } | |
| 92 | - }); | ||
| 99 | + })); | ||
| 93 | 100 | ||
| 94 | - sed.stdin.on('drain', function(data) { | ||
| 101 | + // TODO(@jasnell): This does not appear to ever be | ||
| 102 | + // emitted. It's not clear if it is necessary. | ||
| 103 | + sed.stdin.on('drain', (data) => { | ||
| 95 | 104 | grep.stdout.resume(); | |
| 96 | 105 | }); | |
| 97 | 106 | ||
| 98 | 107 | // Propagate end from grep to sed | |
| 99 | - grep.stdout.on('end', function(code) { | ||
| 100 | - console.error('grep stdout end'); | ||
| 108 | + grep.stdout.on('end', mustCall((code) => { | ||
| 109 | + debug('grep stdout end'); | ||
| 101 | 110 | sed.stdin.end(); | |
| 102 | - }); | ||
| 111 | + })); | ||
| 103 | 112 | ||
| 104 | 113 | ||
| 105 | 114 | let result = ''; | |
| 106 | 115 | ||
| 107 | 116 | // print sed's output | |
| 108 | - sed.stdout.on('data', function(data) { | ||
| 117 | + sed.stdout.on('data', mustCallAtLeast((data) => { | ||
| 109 | 118 | result += data.toString('utf8', 0, data.length); | |
| 110 | - console.log(data); | ||
| 111 | - }); | ||
| 119 | + debug(data); | ||
| 120 | + })); | ||
| 112 | 121 | ||
| 113 | - sed.stdout.on('end', function(code) { | ||
| 122 | + sed.stdout.on('end', mustCall((code) => { | ||
| 114 | 123 | assert.strictEqual(result, `hellO${os.EOL}nOde${os.EOL}wOrld${os.EOL}`); | |
| 115 | - }); | ||
| 124 | + })); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,9 +20,14 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | 22 | 'use strict'; | |
| 23 | - const common = require('../common'); | ||
| 23 | + const { | ||
| 24 | + isWindows, | ||
| 25 | + mustCall, | ||
| 26 | + mustCallAtLeast, | ||
| 27 | + } = require('../common'); | ||
| 24 | 28 | const assert = require('assert'); | |
| 25 | 29 | const os = require('os'); | |
| 30 | + const debug = require('util').debuglog('test'); | ||
| 26 | 31 | ||
| 27 | 32 | const spawn = require('child_process').spawn; | |
| 28 | 33 | ||
@@ -38,7 +43,7 @@ Object.setPrototypeOf(env, { | |||
| 38 | 43 | }); | |
| 39 | 44 | ||
| 40 | 45 | let child; | |
| 41 | - if (common.isWindows) { | ||
| 46 | + if (isWindows) { | ||
| 42 | 47 | child = spawn('cmd.exe', ['/c', 'set'], { env }); | |
| 43 | 48 | } else { | |
| 44 | 49 | child = spawn('/usr/bin/env', [], { env }); | |
@@ -49,15 +54,15 @@ let response = ''; | |||
| 49 | 54 | ||
| 50 | 55 | child.stdout.setEncoding('utf8'); | |
| 51 | 56 | ||
| 52 | - child.stdout.on('data', (chunk) => { | ||
| 53 | - console.log(`stdout: ${chunk}`); | ||
| 57 | + child.stdout.on('data', mustCallAtLeast((chunk) => { | ||
| 58 | + debug(`stdout: ${chunk}`); | ||
| 54 | 59 | response += chunk; | |
| 55 | - }); | ||
| 60 | + })); | ||
| 56 | 61 | ||
| 57 | - process.on('exit', () => { | ||
| 62 | + child.stdout.on('end', mustCall(() => { | ||
| 58 | 63 | assert.ok(response.includes('HELLO=WORLD')); | |
| 59 | 64 | assert.ok(response.includes('FOO=BAR')); | |
| 60 | 65 | assert.ok(!response.includes('UNDEFINED=undefined')); | |
| 61 | 66 | assert.ok(response.includes('NULL=null')); | |
| 62 | 67 | assert.ok(response.includes(`EMPTY=${os.EOL}`)); | |
| 63 | - }); | ||
| 68 | + })); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,9 +20,11 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | 22 | 'use strict'; | |
| 23 | - const common = require('../common'); | ||
| 23 | + const { isWindows } = require('../common'); | ||
| 24 | 24 | const assert = require('assert'); | |
| 25 | 25 | const exec = require('child_process').exec; | |
| 26 | + const debug = require('util').debuglog('test'); | ||
| 27 | + | ||
| 26 | 28 | let success_count = 0; | |
| 27 | 29 | let error_count = 0; | |
| 28 | 30 | let response = ''; | |
@@ -31,17 +33,17 @@ let child; | |||
| 31 | 33 | function after(err, stdout, stderr) { | |
| 32 | 34 | if (err) { | |
| 33 | 35 | error_count++; | |
| 34 | - console.log(`error!: ${err.code}`); | ||
| 35 | - console.log(`stdout: ${JSON.stringify(stdout)}`); | ||
| 36 | - console.log(`stderr: ${JSON.stringify(stderr)}`); | ||
| 36 | + debug(`error!: ${err.code}`); | ||
| 37 | + debug(`stdout: ${JSON.stringify(stdout)}`); | ||
| 38 | + debug(`stderr: ${JSON.stringify(stderr)}`); | ||
| 37 | 39 | assert.strictEqual(err.killed, false); | |
| 38 | 40 | } else { | |
| 39 | 41 | success_count++; | |
| 40 | 42 | assert.notStrictEqual(stdout, ''); | |
| 41 | 43 | } | |
| 42 | 44 | } | |
| 43 | 45 | ||
| 44 | - if (!common.isWindows) { | ||
| 46 | + if (!isWindows) { | ||
| 45 | 47 | child = exec('/usr/bin/env', { env: { 'HELLO': 'WORLD' } }, after); | |
| 46 | 48 | } else { | |
| 47 | 49 | child = exec('set', | |
@@ -55,7 +57,7 @@ child.stdout.on('data', function(chunk) { | |||
| 55 | 57 | }); | |
| 56 | 58 | ||
| 57 | 59 | process.on('exit', function() { | |
| 58 | - console.log('response: ', response); | ||
| 60 | + debug('response: ', response); | ||
| 59 | 61 | assert.strictEqual(success_count, 1); | |
| 60 | 62 | assert.strictEqual(error_count, 0); | |
| 61 | 63 | assert.ok(response.includes('HELLO=WORLD')); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,7 @@ | |||
| 20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | 21 | ||
| 22 | 22 | 'use strict'; | |
| 23 | - require('../common'); | ||
| 23 | + const common = require('../common'); | ||
| 24 | 24 | const assert = require('assert'); | |
| 25 | 25 | const fork = require('child_process').fork; | |
| 26 | 26 | const net = require('net'); | |
@@ -29,7 +29,7 @@ const count = 12; | |||
| 29 | 29 | if (process.argv[2] === 'child') { | |
| 30 | 30 | const sockets = []; | |
| 31 | 31 | ||
| 32 | - process.on('message', function(m, socket) { | ||
| 32 | + process.on('message', common.mustCall((m, socket) => { | ||
| 33 | 33 | function sendClosed(id) { | |
| 34 | 34 | process.send({ id: id, status: 'closed' }); | |
| 35 | 35 | } | |
@@ -52,41 +52,39 @@ if (process.argv[2] === 'child') { | |||
| 52 | 52 | sockets[m.id].destroy(); | |
| 53 | 53 | } | |
| 54 | 54 | } | |
| 55 | - }); | ||
| 55 | + })); | ||
| 56 | 56 | ||
| 57 | 57 | } else { | |
| 58 | 58 | const child = fork(process.argv[1], ['child']); | |
| 59 | 59 | ||
| 60 | - child.on('exit', function(code, signal) { | ||
| 60 | + child.on('exit', common.mustCall((code, signal) => { | ||
| 61 | 61 | if (!subprocessKilled) { | |
| 62 | 62 | assert.fail('subprocess died unexpectedly! ' + | |
| 63 | 63 | `code: ${code} signal: ${signal}`); | |
| 64 | 64 | } | |
| 65 | - }); | ||
| 65 | + })); | ||
| 66 | 66 | ||
| 67 | 67 | const server = net.createServer(); | |
| 68 | 68 | const sockets = []; | |
| 69 | - let sent = 0; | ||
| 70 | 69 | ||
| 71 | - server.on('connection', function(socket) { | ||
| 70 | + server.on('connection', common.mustCall((socket) => { | ||
| 72 | 71 | child.send({ cmd: 'new' }, socket); | |
| 73 | 72 | sockets.push(socket); | |
| 74 | 73 | ||
| 75 | 74 | if (sockets.length === count) { | |
| 76 | 75 | closeSockets(0); | |
| 77 | 76 | } | |
| 78 | - }); | ||
| 77 | + }, count)); | ||
| 79 | 78 | ||
| 80 | - let disconnected = 0; | ||
| 81 | - server.on('listening', function() { | ||
| 79 | + const onClose = common.mustCall(count); | ||
| 80 | + | ||
| 81 | + server.on('listening', common.mustCall(() => { | ||
| 82 | 82 | let j = count; | |
| 83 | 83 | while (j--) { | |
| 84 | 84 | const client = net.connect(server.address().port, '127.0.0.1'); | |
| 85 | - client.on('close', function() { | ||
| 86 | - disconnected += 1; | ||
| 87 | - }); | ||
| 85 | + client.on('close', onClose); | ||
| 88 | 86 | } | |
| 89 | - }); | ||
| 87 | + })); | ||
| 90 | 88 | ||
| 91 | 89 | let subprocessKilled = false; | |
| 92 | 90 | function closeSockets(i) { | |
@@ -97,29 +95,18 @@ if (process.argv[2] === 'child') { | |||
| 97 | 95 | return; | |
| 98 | 96 | } | |
| 99 | 97 | ||
| 100 | - child.once('message', function(m) { | ||
| 98 | + child.once('message', common.mustCall((m) => { | ||
| 101 | 99 | assert.strictEqual(m.status, 'closed'); | |
| 102 | - server.getConnections(function(err, num) { | ||
| 100 | + server.getConnections(common.mustCall((err, num) => { | ||
| 103 | 101 | assert.ifError(err); | |
| 104 | 102 | assert.strictEqual(num, count - (i + 1)); | |
| 105 | 103 | closeSockets(i + 1); | |
| 106 | - }); | ||
| 107 | - }); | ||
| 108 | - sent++; | ||
| 104 | + })); | ||
| 105 | + })); | ||
| 109 | 106 | child.send({ id: i, cmd: 'close' }); | |
| 110 | 107 | } | |
| 111 | 108 | ||
| 112 | - let closeEmitted = false; | ||
| 113 | - server.on('close', function() { | ||
| 114 | - closeEmitted = true; | ||
| 115 | - }); | ||
| 109 | + server.on('close', common.mustCall()); | ||
| 116 | 110 | ||
| 117 | 111 | server.listen(0, '127.0.0.1'); | |
| 118 | - | ||
| 119 | - process.on('exit', function() { | ||
| 120 | - assert.strictEqual(sent, count); | ||
| 121 | - assert.strictEqual(disconnected, count); | ||
| 122 | - assert.ok(closeEmitted); | ||
| 123 | - console.log('ok'); | ||
| 124 | - }); | ||
| 125 | 112 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments