UNPKG

1.98 kBJavaScriptView Raw
1var test = require('tape')
2var spawn = require('pty.js').spawn
3var path = require('path')
4
5var epbCmdPath = path.join(__dirname, '../bin/cmd.js')
6var code = "setInterval(function() { console.log('tick') })"
7
8function verifyTick(t, data) {
9 var result = /(tick\s+){10}/.test(data)
10 t.true(result, '"tick" was output at least 10 times')
11 if (!result) { console.log(data) }
12}
13
14test('node complains when piped to a stream that ends early', function(t) {
15 t.plan(2)
16 var term = spawn('bash')
17 var allData = ''
18 term.on('data', function(data) { allData += data })
19 term.on('close', function() {
20 verifyTick(t, allData)
21 if (/EPIPE/g.test(allData)) {
22 t.pass('node threw EPIPE when `head` ended')
23 }
24 })
25 term.write('node -e "' + code + '" | head; exit\r')
26})
27
28test('"node -r epipebomb/register" suppresses EPIPE', function(t) {
29 t.plan(2)
30 var term = spawn('bash')
31 var allData = ''
32 term.on('data', function(data) { allData += data })
33 term.on('close', function() {
34 verifyTick(t, allData)
35 if (/EPIPE/g.test(allData)) {
36 t.fail('node threw EPIPE when `head` ended')
37 } else {
38 t.pass('EPIPE error was suppressed')
39 }
40 })
41 term.write('node -e "' + code + '" -r ../register | head; exit\r')
42})
43
44test('cmd epipebomb runs given command', function(t) {
45 t.plan(1)
46 var term = spawn('bash')
47 var allData = ''
48 term.on('data', function(data) { allData += data })
49 term.on('close', function() {
50 verifyTick(t, allData)
51 })
52 term.write(epbCmdPath + ' -e "' + code + '" | head; exit\r')
53})
54
55test('cmd epipebomb suppresses EPIPE', function(t) {
56 t.plan(2)
57 var term = spawn('bash')
58 var allData = ''
59 term.on('data', function(data) { allData += data })
60 term.on('close', function() {
61 verifyTick(t, allData)
62 if (/EPIPE/g.test(allData)) {
63 t.fail('node threw EPIPE when `head` ended')
64 } else {
65 t.pass('EPIPE error was suppressed')
66 }
67 })
68 term.write(epbCmdPath + ' -e "' + code + '" | head; exit\r')
69})