2019-01-21 14:03:30 -05:00
|
|
|
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-11-05 12:55:59 -05:00
|
|
|
import os
|
2018-11-28 19:08:18 -05:00
|
|
|
from subprocess import CalledProcessError, PIPE, Popen
|
2018-11-05 12:55:59 -05:00
|
|
|
import sys
|
2018-11-28 19:08:18 -05:00
|
|
|
import time
|
2018-11-05 12:55:59 -05:00
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
from util import DenoTestCase, test_main
|
2018-11-05 12:55:59 -05:00
|
|
|
|
|
|
|
|
2019-05-30 16:40:40 -04:00
|
|
|
class TestRepl(DenoTestCase):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(TestRepl, self).__init__(*args, **kwargs)
|
2018-11-28 19:08:18 -05:00
|
|
|
self._warm_up()
|
|
|
|
|
|
|
|
def _warm_up(self):
|
|
|
|
# This may output an error message about the history file (ignore it).
|
|
|
|
self.input("")
|
2018-11-05 12:55:59 -05:00
|
|
|
|
|
|
|
def input(self, *lines, **kwargs):
|
|
|
|
exit_ = kwargs.pop("exit", True)
|
2018-11-28 19:08:18 -05:00
|
|
|
sleep_ = kwargs.pop("sleep", 0)
|
2019-05-03 17:15:16 -04:00
|
|
|
p = Popen([self.deno_exe], stdout=PIPE, stderr=PIPE, stdin=PIPE)
|
2018-11-05 12:55:59 -05:00
|
|
|
try:
|
2018-11-28 19:08:18 -05:00
|
|
|
# Note: The repl takes a >100ms until it's ready.
|
|
|
|
time.sleep(sleep_)
|
2018-11-05 12:55:59 -05:00
|
|
|
for line in lines:
|
|
|
|
p.stdin.write(line.encode("utf-8") + b'\n')
|
2018-11-28 19:08:18 -05:00
|
|
|
p.stdin.flush()
|
|
|
|
time.sleep(sleep_)
|
2018-11-05 12:55:59 -05:00
|
|
|
if exit_:
|
2019-02-12 10:08:56 -05:00
|
|
|
p.stdin.write(b'Deno.exit(0)\n')
|
2018-11-05 12:55:59 -05:00
|
|
|
else:
|
2018-11-28 19:08:18 -05:00
|
|
|
time.sleep(1) # wait to be killed by js
|
2018-11-05 12:55:59 -05:00
|
|
|
out, err = p.communicate()
|
2018-11-28 19:08:18 -05:00
|
|
|
except CalledProcessError as e:
|
2018-11-05 12:55:59 -05:00
|
|
|
p.kill()
|
|
|
|
p.wait()
|
2018-11-30 03:27:41 -05:00
|
|
|
raise e
|
2018-11-05 12:55:59 -05:00
|
|
|
retcode = p.poll()
|
|
|
|
# Ignore Windows CRLF (\r\n).
|
|
|
|
return out.replace('\r\n', '\n'), err.replace('\r\n', '\n'), retcode
|
|
|
|
|
2018-11-28 19:08:18 -05:00
|
|
|
def test_console_log(self):
|
|
|
|
out, err, code = self.input("console.log('hello')", "'world'")
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, 'hello\nundefined\nworld\n')
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
self.assertEqual(code, 0)
|
2018-11-28 19:08:18 -05:00
|
|
|
|
|
|
|
def test_exit_command(self):
|
2019-01-29 14:41:12 -05:00
|
|
|
out, err, code = self.input("exit", "'ignored'", exit=False)
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, '')
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
self.assertEqual(code, 0)
|
2018-11-05 12:55:59 -05:00
|
|
|
|
2019-01-29 14:41:12 -05:00
|
|
|
def test_help_command(self):
|
|
|
|
out, err, code = self.input("help")
|
|
|
|
expectedOut = '\n'.join([
|
|
|
|
"exit Exit the REPL",
|
|
|
|
"help Print this help message",
|
|
|
|
"",
|
|
|
|
])
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, expectedOut)
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
self.assertEqual(code, 0)
|
2019-01-29 14:41:12 -05:00
|
|
|
|
2018-11-05 12:55:59 -05:00
|
|
|
def test_function(self):
|
2019-02-12 10:08:56 -05:00
|
|
|
out, err, code = self.input("Deno.writeFileSync")
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, '[Function: writeFileSync]\n')
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
self.assertEqual(code, 0)
|
2018-11-05 12:55:59 -05:00
|
|
|
|
2018-11-28 19:08:18 -05:00
|
|
|
def test_multiline(self):
|
|
|
|
out, err, code = self.input("(\n1 + 2\n)")
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, '3\n')
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
self.assertEqual(code, 0)
|
2018-11-05 12:55:59 -05:00
|
|
|
|
2019-02-11 14:01:28 -05:00
|
|
|
# This should print error instead of wait for input
|
|
|
|
def test_eval_unterminated(self):
|
|
|
|
out, err, code = self.input("eval('{')")
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, '')
|
2019-02-11 14:01:28 -05:00
|
|
|
assert "Unexpected end of input" in err
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(code, 0)
|
2019-02-11 14:01:28 -05:00
|
|
|
|
2018-11-28 19:08:18 -05:00
|
|
|
def test_reference_error(self):
|
|
|
|
out, err, code = self.input("not_a_variable")
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, '')
|
2019-02-09 16:55:40 -05:00
|
|
|
assert "not_a_variable is not defined" in err
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(code, 0)
|
2018-11-05 12:55:59 -05:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
# def test_set_timeout(self):
|
|
|
|
# out, err, code = self.input(
|
|
|
|
# "setTimeout(() => { console.log('b'); Deno.exit(0); }, 1)",
|
|
|
|
# "'a'",
|
|
|
|
# exit=False)
|
2019-05-30 16:40:40 -04:00
|
|
|
# self.assertEqual(out, '1\na\nb\n')
|
|
|
|
# self.assertEqual(err, '')
|
|
|
|
# self.assertEqual(code, 0)
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
# def test_set_timeout_interlaced(self):
|
|
|
|
# out, err, code = self.input(
|
|
|
|
# "setTimeout(() => console.log('a'), 1)",
|
|
|
|
# "setTimeout(() => console.log('b'), 6)",
|
|
|
|
# sleep=0.8)
|
2019-05-30 16:40:40 -04:00
|
|
|
# self.assertEqual(out, '1\n2\na\nb\n')
|
|
|
|
# self.assertEqual(err, '')
|
|
|
|
# self.assertEqual(code, 0)
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
# def test_async_op(self):
|
|
|
|
# out, err, code = self.input(
|
|
|
|
# "fetch('http://localhost:4545/tests/001_hello.js')" +
|
|
|
|
# ".then(res => res.text()).then(console.log)",
|
|
|
|
# sleep=1)
|
2019-05-30 16:40:40 -04:00
|
|
|
# self.assertEqual(out, 'Promise {}\nconsole.log("Hello World");\n\n')
|
|
|
|
# self.assertEqual(err, '')
|
|
|
|
# self.assertEqual(code, 0)
|
2019-02-09 16:55:40 -05:00
|
|
|
|
2018-11-05 12:55:59 -05:00
|
|
|
def test_syntax_error(self):
|
|
|
|
out, err, code = self.input("syntax error")
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, '')
|
2019-02-09 16:55:40 -05:00
|
|
|
assert "Unexpected identifier" in err
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(code, 0)
|
2018-11-05 12:55:59 -05:00
|
|
|
|
|
|
|
def test_type_error(self):
|
|
|
|
out, err, code = self.input("console()")
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, '')
|
2019-02-09 16:55:40 -05:00
|
|
|
assert "console is not a function" in err
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(code, 0)
|
2018-11-05 12:55:59 -05:00
|
|
|
|
2018-11-28 19:08:18 -05:00
|
|
|
def test_variable(self):
|
|
|
|
out, err, code = self.input("var a = 123;", "a")
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, 'undefined\n123\n')
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
self.assertEqual(code, 0)
|
2018-11-05 12:55:59 -05:00
|
|
|
|
2019-02-09 16:55:40 -05:00
|
|
|
def test_lexical_scoped_variable(self):
|
|
|
|
out, err, code = self.input("let a = 123;", "a")
|
2019-05-30 16:40:40 -04:00
|
|
|
self.assertEqual(out, 'undefined\n123\n')
|
|
|
|
self.assertEqual(err, '')
|
|
|
|
self.assertEqual(code, 0)
|
2018-11-30 03:27:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-05-30 16:40:40 -04:00
|
|
|
test_main()
|