Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 1 | /* eslint-disable max-len, strict */ |
Ryan Dahl | a0159b4 | 2010-12-04 23:20:34 | [diff] [blame] | 2 | var common = require('../common'); |
| 3 | var assert = require('assert'); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 4 | |
Ryan Dahl | 02cc39f | 2010-12-04 21:40:39 | [diff] [blame] | 5 | common.globalCheck = false; |
| 6 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 7 | var net = require('net'), |
| 8 | repl = require('repl'), |
| 9 | message = 'Read, Eval, Print Loop', |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 10 | prompt_unix = 'node via Unix socket> ', |
| 11 | prompt_tcp = 'node via TCP socket> ', |
| 12 | prompt_multiline = '... ', |
isaacs | 28e851c | 2012-06-05 19:02:37 | [diff] [blame] | 13 | prompt_npm = 'npm should be run outside of the ' + |
| 14 | 'node repl, in your normal shell.\n' + |
| 15 | '(Press Control-D to exit.)\n', |
| 16 | expect_npm = prompt_npm + prompt_unix, |
Fedor Indutny | 1a52d6a | 2014-07-31 08:12:18 | [diff] [blame] | 17 | server_tcp, server_unix, client_tcp, client_unix, timer; |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 18 | |
Ryan Dahl | 03a119e | 2011-08-09 23:06:32 | [diff] [blame] | 19 | |
Ryan Dahl | 4962702 | 2010-09-19 18:20:25 | [diff] [blame] | 20 | // absolute path to test/fixtures/a.js |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 21 | var moduleFilename = require('path').join(common.fixturesDir, 'a'); |
Ryan Dahl | 4962702 | 2010-09-19 18:20:25 | [diff] [blame] | 22 | |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 23 | console.error('repl test'); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 24 | |
| 25 | // function for REPL to run |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 26 | invoke_me = function(arg) { |
| 27 | return 'invoked ' + arg; |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | function send_expect(list) { |
| 31 | if (list.length > 0) { |
| 32 | var cur = list.shift(); |
| 33 | |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 34 | console.error('sending ' + JSON.stringify(cur.send)); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 35 | |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 36 | cur.client.expect = cur.expect; |
| 37 | cur.client.list = list; |
| 38 | if (cur.send.length > 0) { |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 39 | cur.client.write(cur.send + '\n'); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 44 | function clean_up() { |
| 45 | client_tcp.end(); |
| 46 | client_unix.end(); |
| 47 | clearTimeout(timer); |
| 48 | } |
| 49 | |
| 50 | function error_test() { |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 51 | // The other stuff is done so reuse unix socket |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 52 | var read_buffer = ''; |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 53 | client_unix.removeAllListeners('data'); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 54 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 55 | client_unix.on('data', function(data) { |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 56 | read_buffer += data.toString('ascii', 0, data.length); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 57 | console.error('Unix data: ' + JSON.stringify(read_buffer) + ', expecting ' + |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 58 | (client_unix.expect.exec ? |
| 59 | client_unix.expect : |
| 60 | JSON.stringify(client_unix.expect))); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 61 | |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 62 | if (read_buffer.indexOf(prompt_unix) !== -1) { |
isaacs | 28e851c | 2012-06-05 19:02:37 | [diff] [blame] | 63 | // if it's an exact match, then don't do the regexp |
| 64 | if (read_buffer !== client_unix.expect) { |
Yazhong Liu | 613654e | 2014-07-01 13:35:35 | [diff] [blame] | 65 | var expect = client_unix.expect; |
| 66 | if (expect === prompt_multiline) |
| 67 | expect = /[\.]{3} /; |
| 68 | assert.ok(read_buffer.match(expect)); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 69 | console.error('match'); |
isaacs | 28e851c | 2012-06-05 19:02:37 | [diff] [blame] | 70 | } |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 71 | read_buffer = ''; |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 72 | if (client_unix.list && client_unix.list.length > 0) { |
| 73 | send_expect(client_unix.list); |
| 74 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 75 | console.error('End of Error test, running TCP test.'); |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 76 | tcp_test(); |
| 77 | } |
| 78 | |
seebees | 3421f43 | 2011-11-12 01:44:39 | [diff] [blame] | 79 | } else if (read_buffer.indexOf(prompt_multiline) !== -1) { |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 80 | // Check that you meant to send a multiline test |
| 81 | assert.strictEqual(prompt_multiline, client_unix.expect); |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 82 | read_buffer = ''; |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 83 | if (client_unix.list && client_unix.list.length > 0) { |
| 84 | send_expect(client_unix.list); |
| 85 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 86 | console.error('End of Error test, running TCP test.\n'); |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 87 | tcp_test(); |
| 88 | } |
| 89 | |
| 90 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 91 | console.error('didn\'t see prompt yet, buffering.'); |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 92 | } |
| 93 | }); |
| 94 | |
| 95 | send_expect([ |
| 96 | // Uncaught error throws and prints out |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 97 | { client: client_unix, send: 'throw new Error(\'test error\');', |
| 98 | expect: /^Error: test error/ }, |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 99 | // Common syntax error is treated as multiline command |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 100 | { client: client_unix, send: 'function test_func() {', |
| 101 | expect: prompt_multiline }, |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 102 | // You can recover with the .break command |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 103 | { client: client_unix, send: '.break', |
| 104 | expect: prompt_unix }, |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 105 | // But passing the same string to eval() should throw |
| 106 | { client: client_unix, send: 'eval("function test_func() {")', |
| 107 | expect: /^SyntaxError: Unexpected end of input/ }, |
Xiaowei Li | b7365c1 | 2015-01-19 05:42:05 | [diff] [blame] | 108 | // Can handle multiline template literals |
| 109 | { client: client_unix, send: '`io.js', |
| 110 | expect: prompt_multiline }, |
| 111 | // Special REPL commands still available |
| 112 | { client: client_unix, send: '.break', |
| 113 | expect: prompt_unix }, |
| 114 | // Template expressions can cross lines |
| 115 | { client: client_unix, send: '`io.js ${"1.0"', |
| 116 | expect: prompt_multiline }, |
| 117 | { client: client_unix, send: '+ ".2"}`', |
| 118 | expect: `'io.js 1.0.2'\n${prompt_unix}` }, |
Ben Noordhuis | b6e9897 | 2012-11-10 17:21:13 | [diff] [blame] | 119 | // Floating point numbers are not interpreted as REPL commands. |
| 120 | { client: client_unix, send: '.1234', |
| 121 | expect: '0.1234' }, |
Nirk Niggler | 0459a23 | 2013-01-03 14:27:55 | [diff] [blame] | 122 | // Floating point expressions are not interpreted as REPL commands |
Ben Noordhuis | 8a65df9 | 2013-07-10 07:48:55 | [diff] [blame] | 123 | { client: client_unix, send: '.1+.1', |
Nirk Niggler | 0459a23 | 2013-01-03 14:27:55 | [diff] [blame] | 124 | expect: '0.2' }, |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 125 | // Can parse valid JSON |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 126 | { client: client_unix, send: 'JSON.parse(\'{"valid": "json"}\');', |
| 127 | expect: '{ valid: \'json\' }'}, |
| 128 | // invalid input to JSON.parse error is special case of syntax error, |
| 129 | // should throw |
| 130 | { client: client_unix, send: 'JSON.parse(\'{invalid: \\\'json\\\'}\');', |
koichik | 8faf941 | 2011-07-04 19:10:14 | [diff] [blame] | 131 | expect: /^SyntaxError: Unexpected token i/ }, |
Nathan Rajlich | 3b7312d | 2012-10-01 18:36:06 | [diff] [blame] | 132 | // end of input to JSON.parse error is special case of syntax error, |
| 133 | // should throw |
Brian White | 774b28f | 2013-05-26 16:26:39 | [diff] [blame] | 134 | { client: client_unix, send: 'JSON.parse(\'066\');', |
| 135 | expect: /^SyntaxError: Unexpected number/ }, |
| 136 | // should throw |
Nathan Rajlich | 3b7312d | 2012-10-01 18:36:06 | [diff] [blame] | 137 | { client: client_unix, send: 'JSON.parse(\'{\');', |
| 138 | expect: /^SyntaxError: Unexpected end of input/ }, |
Nathan Rajlich | 4a26707 | 2012-09-22 01:46:16 | [diff] [blame] | 139 | // invalid RegExps are a special case of syntax error, |
| 140 | // should throw |
| 141 | { client: client_unix, send: '/(/;', |
| 142 | expect: /^SyntaxError: Invalid regular expression\:/ }, |
Nathan Rajlich | f1722a2 | 2012-10-01 05:43:35 | [diff] [blame] | 143 | // invalid RegExp modifiers are a special case of syntax error, |
| 144 | // should throw (GH-4012) |
| 145 | { client: client_unix, send: 'new RegExp("foo", "wrong modifier");', |
| 146 | expect: /^SyntaxError: Invalid flags supplied to RegExp constructor/ }, |
Nathan Rajlich | 085f9d6 | 2013-03-30 20:10:30 | [diff] [blame] | 147 | // strict mode syntax errors should be caught (GH-5178) |
| 148 | { client: client_unix, send: '(function() { "use strict"; return 0755; })()', |
| 149 | expect: /^SyntaxError: Octal literals are not allowed in strict mode/ }, |
Nathan Rajlich | 085f9d6 | 2013-03-30 20:10:30 | [diff] [blame] | 150 | { client: client_unix, send: '(function(a, a, b) { "use strict"; return a + b + c; })()', |
| 151 | expect: /^SyntaxError: Strict mode function may not have duplicate parameter names/ }, |
| 152 | { client: client_unix, send: '(function() { "use strict"; with (this) {} })()', |
| 153 | expect: /^SyntaxError: Strict mode code may not include a with statement/ }, |
| 154 | { client: client_unix, send: '(function() { "use strict"; var x; delete x; })()', |
| 155 | expect: /^SyntaxError: Delete of an unqualified identifier in strict mode/ }, |
| 156 | { client: client_unix, send: '(function() { "use strict"; eval = 17; })()', |
Fedor Indutny | ce04c72 | 2014-03-13 16:38:14 | [diff] [blame] | 157 | expect: /^SyntaxError: Unexpected eval or arguments in strict mode/ }, |
Ben Noordhuis | 6e9d1c8 | 2015-01-07 18:16:21 | [diff] [blame] | 158 | { client: client_unix, send: '(function() { "use strict"; if (true) function f() { } })()', |
Nathan Rajlich | 085f9d6 | 2013-03-30 20:10:30 | [diff] [blame] | 159 | expect: /^SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function/ }, |
Ryan Dahl | feb77ea | 2011-01-03 02:08:08 | [diff] [blame] | 160 | // Named functions can be used: |
| 161 | { client: client_unix, send: 'function blah() { return 1; }', |
| 162 | expect: prompt_unix }, |
| 163 | { client: client_unix, send: 'blah()', |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 164 | expect: '1\n' + prompt_unix }, |
Fedor Indutny | ae5e233 | 2012-02-17 18:18:11 | [diff] [blame] | 165 | // Functions should not evaluate twice (#2773) |
| 166 | { client: client_unix, send: 'var I = [1,2,3,function() {}]; I.pop()', |
| 167 | expect: '[Function]' }, |
Ryan Dahl | feb77ea | 2011-01-03 02:08:08 | [diff] [blame] | 168 | // Multiline object |
| 169 | { client: client_unix, send: '{ a: ', |
| 170 | expect: prompt_multiline }, |
| 171 | { client: client_unix, send: '1 }', |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 172 | expect: '{ a: 1 }' }, |
Ryan Dahl | feb77ea | 2011-01-03 02:08:08 | [diff] [blame] | 173 | // Multiline anonymous function with comment |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 174 | { client: client_unix, send: '(function() {', |
Ryan Dahl | feb77ea | 2011-01-03 02:08:08 | [diff] [blame] | 175 | expect: prompt_multiline }, |
| 176 | { client: client_unix, send: '// blah', |
| 177 | expect: prompt_multiline }, |
| 178 | { client: client_unix, send: 'return 1;', |
| 179 | expect: prompt_multiline }, |
| 180 | { client: client_unix, send: '})()', |
isaacs | 28e851c | 2012-06-05 19:02:37 | [diff] [blame] | 181 | expect: '1' }, |
| 182 | // npm prompt error message |
| 183 | { client: client_unix, send: 'npm install foobar', |
Maciej Małecki | 6a11f3e | 2012-07-03 02:13:24 | [diff] [blame] | 184 | expect: expect_npm }, |
Roman Reiss | f29762f | 2015-05-19 11:00:06 | [diff] [blame] | 185 | { client: client_unix, send: '(function() {\n\nreturn 1;\n})()', |
Nathan Rajlich | 9126dd2 | 2012-07-04 18:51:24 | [diff] [blame] | 186 | expect: '1' }, |
| 187 | { client: client_unix, send: '{\n\na: 1\n}', |
Felix Böhm | 9bce5e8 | 2013-01-12 20:07:06 | [diff] [blame] | 188 | expect: '{ a: 1 }' }, |
| 189 | { client: client_unix, send: 'url.format("http://google.com")', |
| 190 | expect: 'http://google.com/' }, |
| 191 | { client: client_unix, send: 'var path = 42; path', |
Sakthipriyan Vairamani | 77fa385 | 2015-07-12 00:53:39 | [diff] [blame] | 192 | expect: '42' }, |
| 193 | // this makes sure that we don't print `undefined` when we actually print |
| 194 | // the error message |
| 195 | { client: client_unix, send: '.invalid_repl_command', |
| 196 | expect: 'Invalid REPL keyword\n' + prompt_unix }, |
Sakthipriyan Vairamani | 30edb5a | 2015-07-12 00:58:20 | [diff] [blame] | 197 | // this makes sure that we don't crash when we use an inherited property as |
| 198 | // a REPL command |
| 199 | { client: client_unix, send: '.toString', |
| 200 | expect: 'Invalid REPL keyword\n' + prompt_unix }, |
Sakthipriyan Vairamani | 81ea52a | 2015-07-12 01:22:33 | [diff] [blame] | 201 | // fail when we are not inside a String and a line continuation is used |
| 202 | { client: client_unix, send: '[] \\', |
| 203 | expect: /^SyntaxError: Unexpected token ILLEGAL/ }, |
| 204 | // do not fail when a String is created with line continuation |
| 205 | { client: client_unix, send: '\'the\\\nfourth\\\neye\'', |
| 206 | expect: prompt_multiline + prompt_multiline + |
| 207 | '\'thefourtheye\'\n' + prompt_unix }, |
| 208 | // Don't fail when a partial String is created and line continuation is used |
| 209 | // with whitespace characters at the end of the string. We are to ignore it. |
| 210 | // This test is to make sure that we properly remove the whitespace |
| 211 | // characters at the end of line, unlike the buggy `trimWhitespace` function |
| 212 | { client: client_unix, send: ' \t .break \t ', |
| 213 | expect: prompt_unix }, |
| 214 | // multiline strings preserve whitespace characters in them |
| 215 | { client: client_unix, send: '\'the \\\n fourth\t\t\\\n eye \'', |
| 216 | expect: prompt_multiline + prompt_multiline + |
| 217 | '\'the fourth\\t\\t eye \'\n' + prompt_unix }, |
| 218 | // more than one multiline strings also should preserve whitespace chars |
| 219 | { client: client_unix, send: '\'the \\\n fourth\' + \'\t\t\\\n eye \'', |
| 220 | expect: prompt_multiline + prompt_multiline + |
| 221 | '\'the fourth\\t\\t eye \'\n' + prompt_unix }, |
| 222 | // using REPL commands within a string literal should still work |
| 223 | { client: client_unix, send: '\'\\\n.break', |
| 224 | expect: prompt_unix }, |
| 225 | // using REPL command "help" within a string literal should still work |
| 226 | { client: client_unix, send: '\'thefourth\\\n.help\neye\'', |
| 227 | expect: /'thefourtheye'/ }, |
Sakthipriyan Vairamani | afd7e37 | 2015-07-12 21:48:50 | [diff] [blame] | 228 | // empty lines in the REPL should be allowed |
| 229 | { client: client_unix, send: '\n\r\n\r\n', |
| 230 | expect: prompt_unix + prompt_unix + prompt_unix }, |
| 231 | // empty lines in the string literals should not affect the string |
| 232 | { client: client_unix, send: '\'the\\\n\\\nfourtheye\'\n', |
| 233 | expect: prompt_multiline + prompt_multiline + |
| 234 | '\'thefourtheye\'\n' + prompt_unix }, |
cjihrig | a69ab27 | 2015-08-13 16:14:34 | [diff] [blame] | 235 | // Regression test for https://github.com/nodejs/node/issues/597 |
Sakthipriyan Vairamani | ea05e76 | 2015-07-08 21:53:48 | [diff] [blame] | 236 | { client: client_unix, |
| 237 | send: '/(.)(.)(.)(.)(.)(.)(.)(.)(.)/.test(\'123456789\')\n', |
| 238 | expect: `true\n${prompt_unix}` }, |
| 239 | // the following test's result depends on the RegEx's match from the above |
| 240 | { client: client_unix, |
| 241 | send: 'RegExp.$1\nRegExp.$2\nRegExp.$3\nRegExp.$4\nRegExp.$5\n' + |
| 242 | 'RegExp.$6\nRegExp.$7\nRegExp.$8\nRegExp.$9\n', |
| 243 | expect: ['\'1\'\n', '\'2\'\n', '\'3\'\n', '\'4\'\n', '\'5\'\n', '\'6\'\n', |
| 244 | '\'7\'\n', '\'8\'\n', '\'9\'\n'].join(`${prompt_unix}`) }, |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 245 | ]); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 246 | } |
| 247 | |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 248 | function tcp_test() { |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 249 | server_tcp = net.createServer(function(socket) { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 250 | assert.strictEqual(server_tcp, socket.server); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 251 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 252 | socket.on('end', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 253 | socket.end(); |
| 254 | }); |
| 255 | |
| 256 | repl.start(prompt_tcp, socket); |
| 257 | }); |
| 258 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 259 | server_tcp.listen(common.PORT, function() { |
| 260 | var read_buffer = ''; |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 261 | |
Ryan Dahl | 9fd5e3c | 2010-07-15 18:47:25 | [diff] [blame] | 262 | client_tcp = net.createConnection(common.PORT); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 263 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 264 | client_tcp.on('connect', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 265 | assert.equal(true, client_tcp.readable); |
| 266 | assert.equal(true, client_tcp.writable); |
| 267 | |
| 268 | send_expect([ |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 269 | { client: client_tcp, send: '', |
| 270 | expect: prompt_tcp }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 271 | { client: client_tcp, send: 'invoke_me(333)', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 272 | expect: ('\'' + 'invoked 333' + '\'\n' + prompt_tcp) }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 273 | { client: client_tcp, send: 'a += 1', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 274 | expect: ('12346' + '\n' + prompt_tcp) }, |
| 275 | { client: client_tcp, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 276 | send: 'require(' + JSON.stringify(moduleFilename) + ').number', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 277 | expect: ('42' + '\n' + prompt_tcp) } |
| 278 | ]); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 279 | }); |
| 280 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 281 | client_tcp.on('data', function(data) { |
Ryan Dahl | 4fe5e86 | 2010-09-06 19:12:36 | [diff] [blame] | 282 | read_buffer += data.toString('ascii', 0, data.length); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 283 | console.error('TCP data: ' + JSON.stringify(read_buffer) + |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 284 | ', expecting ' + JSON.stringify(client_tcp.expect)); |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 285 | if (read_buffer.indexOf(prompt_tcp) !== -1) { |
| 286 | assert.strictEqual(client_tcp.expect, read_buffer); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 287 | console.error('match'); |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 288 | read_buffer = ''; |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 289 | if (client_tcp.list && client_tcp.list.length > 0) { |
| 290 | send_expect(client_tcp.list); |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 291 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 292 | console.error('End of TCP test.\n'); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 293 | clean_up(); |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 294 | } |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 295 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 296 | console.error('didn\'t see prompt yet, buffering'); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 297 | } |
| 298 | }); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 299 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 300 | client_tcp.on('error', function(e) { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 301 | throw e; |
| 302 | }); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 303 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 304 | client_tcp.on('close', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 305 | server_tcp.close(); |
| 306 | }); |
| 307 | }); |
| 308 | |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | function unix_test() { |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 312 | server_unix = net.createServer(function(socket) { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 313 | assert.strictEqual(server_unix, socket.server); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 314 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 315 | socket.on('end', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 316 | socket.end(); |
| 317 | }); |
| 318 | |
Fedor Indutny | 1a52d6a | 2014-07-31 08:12:18 | [diff] [blame] | 319 | repl.start({ |
Nathan Rajlich | b1e78ce | 2012-10-12 23:34:36 | [diff] [blame] | 320 | prompt: prompt_unix, |
| 321 | input: socket, |
| 322 | output: socket, |
| 323 | useGlobal: true |
Fedor Indutny | 1a52d6a | 2014-07-31 08:12:18 | [diff] [blame] | 324 | }).context.message = message; |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 325 | }); |
| 326 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 327 | server_unix.on('listening', function() { |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 328 | var read_buffer = ''; |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 329 | |
Ryan Dahl | 03a119e | 2011-08-09 23:06:32 | [diff] [blame] | 330 | client_unix = net.createConnection(common.PIPE); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 331 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 332 | client_unix.on('connect', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 333 | assert.equal(true, client_unix.readable); |
| 334 | assert.equal(true, client_unix.writable); |
| 335 | |
| 336 | send_expect([ |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 337 | { client: client_unix, send: '', |
| 338 | expect: prompt_unix }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 339 | { client: client_unix, send: 'message', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 340 | expect: ('\'' + message + '\'\n' + prompt_unix) }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 341 | { client: client_unix, send: 'invoke_me(987)', |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 342 | expect: ('\'' + 'invoked 987' + '\'\n' + prompt_unix) }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 343 | { client: client_unix, send: 'a = 12345', |
Ryan Dahl | 8b352bd | 2011-01-02 05:14:06 | [diff] [blame] | 344 | expect: ('12345' + '\n' + prompt_unix) }, |
Nathan Rajlich | aab7cb7 | 2012-04-06 19:20:01 | [diff] [blame] | 345 | { client: client_unix, send: '{a:1}', |
Colton Baker | 87286cc | 2011-10-04 22:08:18 | [diff] [blame] | 346 | expect: ('{ a: 1 }' + '\n' + prompt_unix) } |
Oleg Efimov | 0665f02 | 2010-12-05 19:15:30 | [diff] [blame] | 347 | ]); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 348 | }); |
| 349 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 350 | client_unix.on('data', function(data) { |
Ryan Dahl | 4fe5e86 | 2010-09-06 19:12:36 | [diff] [blame] | 351 | read_buffer += data.toString('ascii', 0, data.length); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 352 | console.error('Unix data: ' + JSON.stringify(read_buffer) + |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 353 | ', expecting ' + JSON.stringify(client_unix.expect)); |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 354 | if (read_buffer.indexOf(prompt_unix) !== -1) { |
| 355 | assert.strictEqual(client_unix.expect, read_buffer); |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 356 | console.error('match'); |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 357 | read_buffer = ''; |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 358 | if (client_unix.list && client_unix.list.length > 0) { |
| 359 | send_expect(client_unix.list); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 360 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 361 | console.error('End of Unix test, running Error test.\n'); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 362 | process.nextTick(error_test); |
Matt Ranney | afe3c1c | 2010-04-12 21:29:49 | [diff] [blame] | 363 | } |
Ryan Dahl | 08d8116 | 2010-12-01 21:43:05 | [diff] [blame] | 364 | } else { |
isaacs | 4631c50 | 2013-08-28 01:53:39 | [diff] [blame] | 365 | console.error('didn\'t see prompt yet, buffering.'); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 366 | } |
| 367 | }); |
Ryan Dahl | a6942b3 | 2010-06-08 00:39:24 | [diff] [blame] | 368 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 369 | client_unix.on('error', function(e) { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 370 | throw e; |
| 371 | }); |
| 372 | |
Ben Noordhuis | 018e110 | 2011-10-14 23:08:36 | [diff] [blame] | 373 | client_unix.on('close', function() { |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 374 | server_unix.close(); |
| 375 | }); |
| 376 | }); |
| 377 | |
Ryan Dahl | 03a119e | 2011-08-09 23:06:32 | [diff] [blame] | 378 | server_unix.listen(common.PIPE); |
Matt Ranney | b744104 | 2010-04-11 23:13:32 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | unix_test(); |
Marco Rogers | 118b88e | 2010-11-20 23:44:58 | [diff] [blame] | 382 | |
Oleg Efimov | 0ce9cba | 2010-12-04 22:45:52 | [diff] [blame] | 383 | timer = setTimeout(function() { |
| 384 | assert.fail('Timeout'); |
Ryan Dahl | 129217a | 2011-01-26 01:35:06 | [diff] [blame] | 385 | }, 5000); |