summaryrefslogtreecommitdiff
path: root/tools/repl_test.py
diff options
context:
space:
mode:
authorAndy Hayden <andyhayden1@gmail.com>2018-11-28 16:08:18 -0800
committerRyan Dahl <ry@tinyclouds.org>2018-11-29 11:53:17 -0800
commite6309b78b0ef56fe5adbbfae511434c75acc10ca (patch)
tree96b5fa06223a34cae529c4ec602fe46552907438 /tools/repl_test.py
parent09aa9b9698dd7646029551b8e7cd4f5b67a81b31 (diff)
Cleanup setTimeout repl test and alphabetize tests
Diffstat (limited to 'tools/repl_test.py')
-rw-r--r--tools/repl_test.py103
1 files changed, 52 insertions, 51 deletions
diff --git a/tools/repl_test.py b/tools/repl_test.py
index 9969e9804..ce8ef917c 100644
--- a/tools/repl_test.py
+++ b/tools/repl_test.py
@@ -1,8 +1,8 @@
# Copyright 2018 the Deno authors. All rights reserved. MIT license.
import os
-from subprocess import PIPE, Popen
+from subprocess import CalledProcessError, PIPE, Popen
import sys
-from time import sleep
+import time
from util import build_path, executable_suffix, green_ok
@@ -10,20 +10,29 @@ from util import build_path, executable_suffix, green_ok
class Repl(object):
def __init__(self, deno_exe):
self.deno_exe = deno_exe
- self.warm_up()
+ self._warm_up()
+
+ def _warm_up(self):
+ # This may output an error message about the history file (ignore it).
+ self.input("")
def input(self, *lines, **kwargs):
exit_ = kwargs.pop("exit", True)
+ sleep_ = kwargs.pop("sleep", 0)
p = Popen([self.deno_exe], stdout=PIPE, stderr=PIPE, stdin=PIPE)
try:
+ # Note: The repl takes a >100ms until it's ready.
+ time.sleep(sleep_)
for line in lines:
p.stdin.write(line.encode("utf-8") + b'\n')
+ p.stdin.flush()
+ time.sleep(sleep_)
if exit_:
p.stdin.write(b'deno.exit(0)\n')
else:
- sleep(1) # wait to be killed by js
+ time.sleep(1) # wait to be killed by js
out, err = p.communicate()
- except Exception as e: # Should this be CalledProcessError?
+ except CalledProcessError as e:
p.kill()
p.wait()
raise
@@ -31,9 +40,26 @@ class Repl(object):
# Ignore Windows CRLF (\r\n).
return out.replace('\r\n', '\n'), err.replace('\r\n', '\n'), retcode
- def warm_up(self):
- # This may output an error message about the history file (ignore it).
- self.input("")
+ def run(self):
+ print('repl_test.py')
+ test_names = [name for name in dir(self) if name.startswith("test_")]
+ for t in test_names:
+ self.__getattribute__(t)()
+ sys.stdout.write(".")
+ sys.stdout.flush()
+ print(' {}\n'.format(green_ok()))
+
+ def test_console_log(self):
+ out, err, code = self.input("console.log('hello')", "'world'")
+ assertEqual(out, 'hello\nundefined\nworld\n')
+ assertEqual(err, '')
+ assertEqual(code, 0)
+
+ def test_exit_command(self):
+ out, err, code = self.input(".exit", "'ignored'", exit=False)
+ assertEqual(out, '')
+ assertEqual(err, '')
+ assertEqual(code, 0)
def test_function(self):
out, err, code = self.input("deno.writeFileSync")
@@ -41,19 +67,19 @@ class Repl(object):
assertEqual(err, '')
assertEqual(code, 0)
- def test_console_log(self):
- out, err, code = self.input("console.log('hello')", "'world'")
- assertEqual(out, 'hello\nundefined\nworld\n')
+ def test_multiline(self):
+ out, err, code = self.input("(\n1 + 2\n)")
+ assertEqual(out, '3\n')
assertEqual(err, '')
assertEqual(code, 0)
- def test_variable(self):
- out, err, code = self.input("var a = 123;", "a")
- assertEqual(out, 'undefined\n123\n')
- assertEqual(err, '')
+ def test_reference_error(self):
+ out, err, code = self.input("not_a_variable")
+ assertEqual(out, '')
+ assertEqual(err, 'ReferenceError: not_a_variable is not defined\n')
assertEqual(code, 0)
- def test_settimeout(self):
+ def test_set_timeout(self):
out, err, code = self.input(
"setTimeout(() => { console.log('b'); deno.exit(0); }, 10)",
"'a'",
@@ -62,10 +88,13 @@ class Repl(object):
assertEqual(err, '')
assertEqual(code, 0)
- def test_reference_error(self):
- out, err, code = self.input("not_a_variable")
- assertEqual(out, '')
- assertEqual(err, 'ReferenceError: not_a_variable is not defined\n')
+ def test_set_timeout_interlaced(self):
+ out, err, code = self.input(
+ "setTimeout(() => console.log('a'), 250)",
+ "setTimeout(() => console.log('b'), 150)",
+ sleep=0.2)
+ assertEqual(out, '1\n2\na\nb\n')
+ assertEqual(err, '')
assertEqual(code, 0)
def test_syntax_error(self):
@@ -80,40 +109,12 @@ class Repl(object):
assertEqual(err, 'TypeError: console is not a function\n')
assertEqual(code, 0)
- def test_multiline(self):
- out, err, code = self.input("(\n1 + 2\n)")
- assertEqual(out, '3\n')
- assertEqual(err, '')
- assertEqual(code, 0)
-
- def test_set_timeout(self):
- # Special treatment
- p = Popen([self.deno_exe], stdout=PIPE, stderr=PIPE, stdin=PIPE)
- # Print after 0.1 second
- p.stdin.write(
- "setTimeout(() => console.log('HI'), 100)\n".encode("utf-8"))
- sleep(0.2) # Wait 0.2 second before proceed
- out, err = p.communicate()
- code = p.poll()
- assertEqual(out.replace('\r\n', '\n'), '1\nHI\n')
- assertEqual(err.replace('\r\n', '\n'), '')
- assertEqual(code, 0)
-
- def test_exit_command(self):
- out, err, code = self.input(".exit", "'ignored'", exit=False)
- assertEqual(out, '')
+ def test_variable(self):
+ out, err, code = self.input("var a = 123;", "a")
+ assertEqual(out, 'undefined\n123\n')
assertEqual(err, '')
assertEqual(code, 0)
- def run(self):
- print('repl_test.py')
- test_names = [name for name in dir(self) if name.startswith("test_")]
- for t in test_names:
- self.__getattribute__(t)()
- sys.stdout.write(".")
- sys.stdout.flush()
- print(' {}\n'.format(green_ok()))
-
def assertEqual(left, right):
if left != right: