1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-01 16:51:13 -05:00

chore: Port Python tests to unittest (#2408)

This commit is contained in:
Bartek Iwańczuk 2019-05-27 15:27:55 +02:00 committed by Ryan Dahl
parent 9fd4096235
commit 824542e61e
5 changed files with 317 additions and 357 deletions

View file

@ -7,6 +7,7 @@ import select
import subprocess import subprocess
import sys import sys
import time import time
import unittest
import http_server import http_server
from util import build_path, root_path, executable_suffix, green_ok, red_failed from util import build_path, root_path, executable_suffix, green_ok, red_failed
@ -52,252 +53,214 @@ def tty_capture(cmd, bytes_input, timeout=5):
return p.returncode, res['stdout'], res['stderr'] return p.returncode, res['stdout'], res['stderr']
# Wraps a test in debug printouts class ComplexPermissionTestCase(unittest.TestCase):
# so we have visual indicator of what test failed def __init__(self, method_name, test_type, deno_exe):
def wrap_test(test_name, test_method, *argv): super(ComplexPermissionTestCase, self).__init__(method_name)
sys.stdout.write(test_name + " ... ") self.test_type = test_type
try:
test_method(*argv)
print green_ok()
except AssertionError:
print red_failed()
raise
class Prompt(object):
def __init__(self, deno_exe, test_types):
self.deno_exe = deno_exe self.deno_exe = deno_exe
self.test_types = test_types
def run(self, flags, args, bytes_input): def _run_deno(self, flags, args):
"Returns (return_code, stdout, stderr)." "Returns (return_code, stdout, stderr)."
cmd = [self.deno_exe, "run"] + flags + [PERMISSIONS_PROMPT_TEST_TS cmd = ([self.deno_exe, "run", "--no-prompt"] + flags +
] + args [PERMISSIONS_PROMPT_TEST_TS] + args)
print " ".join(cmd) return tty_capture(cmd, b'')
return tty_capture(cmd, bytes_input)
def warm_up(self):
# ignore the ts compiling message
self.run(["--allow-read"], ["read", "package.json"], b'')
def test(self): class TestReadWritePermissions(ComplexPermissionTestCase):
for test_type in ["read", "write"]: def test_inside_project_dir(self):
test_name_base = "test_" + test_type code, _stdout, stderr = self._run_deno(
wrap_test(test_name_base + "_inside_project_dir", ["--allow-" + self.test_type + "=" + root_path],
self.test_inside_project_dir, test_type) [self.test_type, "package.json", "tests/subdir/config.json"])
wrap_test(test_name_base + "_outside_tests_dir",
self.test_outside_test_dir, test_type)
wrap_test(test_name_base + "_inside_tests_dir",
self.test_inside_test_dir, test_type)
wrap_test(test_name_base + "_outside_tests_and_js_dir",
self.test_outside_test_and_js_dir, test_type)
wrap_test(test_name_base + "_inside_tests_and_js_dir",
self.test_inside_test_and_js_dir, test_type)
wrap_test(test_name_base + "_relative", self.test_relative,
test_type)
wrap_test(test_name_base + "_no_prefix", self.test_no_prefix,
test_type)
test_name = "net_fetch"
test_name_base = "test_" + test_name
wrap_test(test_name_base + "_allow_localhost_4545",
self.test_allow_localhost_4545, test_name,
["http://localhost:4545"])
wrap_test(test_name_base + "_allow_deno_land",
self.test_allow_deno_land, test_name,
["http://localhost:4545"])
wrap_test(test_name_base + "_allow_localhost_4545_fail",
self.test_allow_localhost_4545_fail, test_name,
["http://localhost:4546"])
wrap_test(test_name_base + "_allow_localhost",
self.test_allow_localhost, test_name, [
"http://localhost:4545", "http://localhost:4546",
"http://localhost:4547"
])
test_name = "net_dial"
test_name_base = "test_" + test_name
wrap_test(test_name_base + "_allow_127.0.0.1:4545",
self.test_allow_localhost_ip_4555, test_name,
["127.0.0.1:4545"])
wrap_test(test_name_base + "_allow_deno_land",
self.test_allow_deno_land, test_name, ["127.0.0.1:4545"])
wrap_test(test_name_base + "_allow_127.0.0.1:4545_fail",
self.test_allow_localhost_ip_4545_fail, test_name,
["127.0.0.1:4546"])
wrap_test(test_name_base + "_allow_127.0.0.1",
self.test_allow_localhost_ip, test_name,
["127.0.0.1:4545", "127.0.0.1:4546", "127.0.0.1:4547"])
test_name = "net_listen"
test_name_base = "test_" + test_name
wrap_test(test_name_base + "_allow_localhost_4555",
self.test_allow_localhost_4555, test_name,
["localhost:4555"])
wrap_test(test_name_base + "_allow_deno_land",
self.test_allow_deno_land, test_name, ["localhost:4545"])
wrap_test(test_name_base + "_allow_localhost_4555_fail",
self.test_allow_localhost_4555_fail, test_name,
["localhost:4556"])
wrap_test(test_name_base + "_allow_localhost",
self.test_allow_localhost, test_name,
["localhost:4555", "localhost:4556", "localhost:4557"])
# read/write tests
def test_inside_project_dir(self, test_type):
code, _stdout, stderr = self.run(
["--no-prompt", "--allow-" + test_type + "=" + root_path],
[test_type, "package.json", "tests/subdir/config.json"], b'')
assert code == 0 assert code == 0
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def test_outside_test_dir(self, test_type): def test_outside_test_dir(self):
code, _stdout, stderr = self.run([ code, _stdout, stderr = self._run_deno([
"--no-prompt", "--allow-" + self.test_type + "=" + os.path.join(
"--allow-" + test_type + "=" + os.path.join(root_path, "tests") root_path, "tests")
], [test_type, "package.json"], b'') ], [self.test_type, "package.json"])
assert code == 1 assert code == 1
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr
def test_inside_test_dir(self, test_type): def test_inside_test_dir(self):
code, _stdout, stderr = self.run([ code, _stdout, stderr = self._run_deno([
"--no-prompt", "--allow-" + self.test_type + "=" + os.path.join(
"--allow-" + test_type + "=" + os.path.join(root_path, "tests") root_path, "tests")
], [test_type, "tests/subdir/config.json"], b'') ], [self.test_type, "tests/subdir/config.json"])
assert code == 0 assert code == 0
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def test_outside_test_and_js_dir(self, test_type): def test_outside_test_and_js_dir(self):
code, _stdout, stderr = self.run([ code, _stdout, stderr = self._run_deno([
"--no-prompt", "--allow-" + test_type + "=" + os.path.join( "--allow-" + self.test_type + "=" + os.path.join(
root_path, "tests") + "," + os.path.join(root_path, "js") root_path, "tests") + "," + os.path.join(root_path, "js")
], [test_type, "package.json"], b'') ], [self.test_type, "package.json"])
assert code == 1 assert code == 1
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr
def test_inside_test_and_js_dir(self, test_type): def test_inside_test_and_js_dir(self):
code, _stdout, stderr = self.run([ code, _stdout, stderr = self._run_deno([
"--no-prompt", "--allow-" + test_type + "=" + os.path.join( "--allow-" + self.test_type + "=" + os.path.join(
root_path, "tests") + "," + os.path.join(root_path, "js") root_path, "tests") + "," + os.path.join(root_path, "js")
], [test_type, "js/dir_test.ts", "tests/subdir/config.json"], b'') ], [self.test_type, "js/dir_test.ts", "tests/subdir/config.json"])
assert code == 0 assert code == 0
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def test_relative(self, test_type): def test_relative(self):
# Save and restore curdir # Save and restore curdir
saved_curdir = os.getcwd() saved_curdir = os.getcwd()
os.chdir(root_path) os.chdir(root_path)
code, _stdout, stderr = self.run( code, _stdout, stderr = self._run_deno(
["--no-prompt", "--allow-" + test_type + "=" + "./tests"], ["--allow-" + self.test_type + "=" + "./tests"],
[test_type, "tests/subdir/config.json"], b'') [self.test_type, "tests/subdir/config.json"])
assert code == 0 assert code == 0
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
os.chdir(saved_curdir) os.chdir(saved_curdir)
def test_no_prefix(self, test_type): def test_no_prefix(self):
# Save and restore curdir # Save and restore curdir
saved_curdir = os.getcwd() saved_curdir = os.getcwd()
os.chdir(root_path) os.chdir(root_path)
code, _stdout, stderr = self.run( code, _stdout, stderr = self._run_deno(
["--no-prompt", "--allow-" + test_type + "=" + "tests"], ["--allow-" + self.test_type + "=" + "tests"],
[test_type, "tests/subdir/config.json"], b'') [self.test_type, "tests/subdir/config.json"])
assert code == 0 assert code == 0
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
os.chdir(saved_curdir) os.chdir(saved_curdir)
# net tests
def test_allow_net(self, test_type, allowed_host, hosts): class TestNetFetchPermissions(ComplexPermissionTestCase):
code, _stdout, stderr = self.run( def test_allow_localhost_4545(self):
["--no-prompt", "--allow-net=" + allowed_host], code, _stdout, stderr = self._run_deno(
[test_type] + hosts, b'') ["--allow-net=localhost:4545"],
[self.test_type, "http://localhost:4545"])
assert code == 0 assert code == 0
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_4545(self, test_type, hosts): def test_allow_deno_land(self):
code, _stdout, stderr = self.run( code, _stdout, stderr = self._run_deno(
["--no-prompt", "--allow-net=localhost:4545"], [test_type] + hosts, ["--allow-net=deno.land"],
b'') [self.test_type, "http://localhost:4545"])
assert code == 0
assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_ip_4555(self, test_type, hosts):
code, _stdout, stderr = self.run(
["--no-prompt", "--allow-net=127.0.0.1:4545"], [test_type] + hosts,
b'')
assert code == 0
assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_4555(self, test_type, hosts):
code, _stdout, stderr = self.run(
["--no-prompt", "--allow-net=localhost:4555"], [test_type] + hosts,
b'')
assert code == 0
assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr
def test_allow_deno_land(self, test_type, hosts):
code, _stdout, stderr = self.run(
["--no-prompt", "--allow-net=deno.land"], [test_type] + hosts, b'')
assert code == 1 assert code == 1
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_4545_fail(self, test_type, hosts): def test_allow_localhost_4545_fail(self):
code, _stdout, stderr = self.run( code, _stdout, stderr = self._run_deno(
["--no-prompt", "--allow-net=localhost:4545"], [test_type] + hosts, ["--allow-net=localhost:4545"],
b'') [self.test_type, "http://localhost:4546"])
assert code == 1 assert code == 1
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_ip_4545_fail(self, test_type, hosts): def test_allow_localhost(self):
code, _stdout, stderr = self.run( code, _stdout, stderr = self._run_deno(["--allow-net=localhost"], [
["--no-prompt", "--allow-net=127.0.0.1:4545"], [test_type] + hosts, self.test_type, "http://localhost:4545", "http://localhost:4546",
b'') "http://localhost:4547"
assert code == 1 ])
assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_4555_fail(self, test_type, hosts):
code, _stdout, stderr = self.run(
["--no-prompt", "--allow-net=localhost:4555"], [test_type] + hosts,
b'')
assert code == 1
assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost(self, test_type, hosts):
code, _stdout, stderr = self.run(
["--no-prompt", "--allow-net=localhost"], [test_type] + hosts, b'')
assert code == 0 assert code == 0
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_ip(self, test_type, hosts):
code, _stdout, stderr = self.run( class TestNetDialPermissions(ComplexPermissionTestCase):
["--no-prompt", "--allow-net=127.0.0.1"], [test_type] + hosts, b'') def test_allow_localhost_ip_4555(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=127.0.0.1:4545"], [self.test_type, "127.0.0.1:4545"])
assert code == 0
assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr
def test_allow_deno_land(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=deno.land"], [self.test_type, "127.0.0.1:4545"])
assert code == 1
assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_ip_4545_fail(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=127.0.0.1:4545"], [self.test_type, "127.0.0.1:4546"])
assert code == 1
assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_ip(self):
code, _stdout, stderr = self._run_deno(["--allow-net=127.0.0.1"], [
self.test_type, "127.0.0.1:4545", "127.0.0.1:4546",
"127.0.0.1:4547"
])
assert code == 0
assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr
class TestNetListenPermissions(ComplexPermissionTestCase):
def test_allow_localhost_4555(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=localhost:4555"], [self.test_type, "localhost:4555"])
assert code == 0
assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr
def test_allow_deno_land(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=deno.land"], [self.test_type, "localhost:4545"])
assert code == 1
assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_4555_fail(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=localhost:4555"], [self.test_type, "localhost:4556"])
assert code == 1
assert not PROMPT_PATTERN in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost(self):
code, _stdout, stderr = self._run_deno(["--allow-net=localhost"], [
self.test_type, "localhost:4555", "localhost:4556",
"localhost:4557"
])
assert code == 0 assert code == 0
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def complex_permissions_test(deno_exe): def complex_permissions_test(deno_exe):
p = Prompt(deno_exe, ["read", "write", "net"]) runner = unittest.TextTestRunner(verbosity=2)
p.test() loader = unittest.TestLoader()
tests = (
("read", TestReadWritePermissions),
("write", TestReadWritePermissions),
("net_fetch", TestNetFetchPermissions),
("net_dial", TestNetDialPermissions),
("net_listen", TestNetListenPermissions),
)
for (test_type, test_class) in tests:
print "Complex permissions tests for \"{}\"".format(test_type)
test_names = loader.getTestCaseNames(test_class)
suite = unittest.TestSuite()
for test_name in test_names:
suite.addTest(test_class(test_name, test_type, deno_exe))
result = runner.run(suite)
if not result.wasSuccessful():
sys.exit(1)
def main(): def main():
print "Permissions prompt tests"
deno_exe = os.path.join(build_path(), "deno" + executable_suffix) deno_exe = os.path.join(build_path(), "deno" + executable_suffix)
http_server.spawn() http_server.spawn()
complex_permissions_test(deno_exe) complex_permissions_test(deno_exe)

View file

@ -7,6 +7,7 @@ import select
import subprocess import subprocess
import sys import sys
import time import time
import unittest
from util import build_path, executable_suffix, green_ok, red_failed from util import build_path, executable_suffix, green_ok, red_failed
@ -52,128 +53,104 @@ def tty_capture(cmd, bytes_input, timeout=5):
return p.returncode, res['stdout'], res['stderr'] return p.returncode, res['stdout'], res['stderr']
# Wraps a test in debug printouts class TestPrompt(unittest.TestCase):
# so we have visual indicator of what test failed def __init__(self, method_name, test_type, deno_exe):
def wrap_test(test_name, test_method, *argv): super(TestPrompt, self).__init__(method_name)
sys.stdout.write(test_name + " ... ") self.test_type = test_type
try:
test_method(*argv)
print green_ok()
except AssertionError:
print red_failed()
raise
class Prompt(object):
def __init__(self, deno_exe, test_types):
self.deno_exe = deno_exe self.deno_exe = deno_exe
self.test_types = test_types
def run(self, flags, args, bytes_input): def _run_deno(self, flags, args, bytes_input):
"Returns (return_code, stdout, stderr)." "Returns (return_code, stdout, stderr)."
cmd = [self.deno_exe, "run"] + flags + [PERMISSIONS_PROMPT_TEST_TS cmd = [self.deno_exe, "run"] + flags + [PERMISSIONS_PROMPT_TEST_TS
] + args ] + args
return tty_capture(cmd, bytes_input) return tty_capture(cmd, bytes_input)
def warm_up(self): def test_allow_flag(self):
# ignore the ts compiling message test_type = self.test_type
self.run(["--allow-write"], 'needsWrite', b'') code, stdout, stderr = self._run_deno(
def test(self):
for test_type in self.test_types:
test_name_base = "test_" + test_type
wrap_test(test_name_base + "_allow_flag", self.test_allow_flag,
test_type)
wrap_test(test_name_base + "_yes_yes", self.test_yes_yes,
test_type)
wrap_test(test_name_base + "_yes_no", self.test_yes_no, test_type)
wrap_test(test_name_base + "_no_no", self.test_no_no, test_type)
wrap_test(test_name_base + "_no_yes", self.test_no_yes, test_type)
wrap_test(test_name_base + "_allow", self.test_allow, test_type)
wrap_test(test_name_base + "_deny", self.test_deny, test_type)
wrap_test(test_name_base + "_unrecognized_option",
self.test_unrecognized_option, test_type)
wrap_test(test_name_base + "_no_prompt", self.test_no_prompt,
test_type)
wrap_test(test_name_base + "_no_prompt_allow",
self.test_no_prompt_allow, test_type)
def test_allow_flag(self, test_type):
code, stdout, stderr = self.run(
["--allow-" + test_type], ["needs" + test_type.capitalize()], b'') ["--allow-" + test_type], ["needs" + test_type.capitalize()], b'')
assert code == 0 assert code == 0
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def test_yes_yes(self, test_type): def test_yes_yes(self):
code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], test_type = self.test_type
b'y\ny\n') code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'y\ny\n')
assert code == 0 assert code == 0
assert PROMPT_PATTERN in stderr assert PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def test_yes_no(self, test_type): def test_yes_no(self):
code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], test_type = self.test_type
b'y\nn\n') code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'y\nn\n')
assert code == 1 assert code == 1
assert PROMPT_PATTERN in stderr assert PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert PERMISSION_DENIED_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr
def test_no_no(self, test_type): def test_no_no(self):
code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], test_type = self.test_type
b'n\nn\n') code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'n\nn\n')
assert code == 1 assert code == 1
assert PROMPT_PATTERN in stderr assert PROMPT_PATTERN in stderr
assert FIRST_CHECK_FAILED_PATTERN in stdout assert FIRST_CHECK_FAILED_PATTERN in stdout
assert PERMISSION_DENIED_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr
def test_no_yes(self, test_type): def test_no_yes(self):
code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], test_type = self.test_type
b'n\ny\n') code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'n\ny\n')
assert code == 0 assert code == 0
assert PROMPT_PATTERN in stderr assert PROMPT_PATTERN in stderr
assert FIRST_CHECK_FAILED_PATTERN in stdout assert FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def test_allow(self, test_type): def test_allow(self):
code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], test_type = self.test_type
b'a\n') code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'a\n')
assert code == 0 assert code == 0
assert PROMPT_PATTERN in stderr assert PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
def test_deny(self, test_type): def test_deny(self):
code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], test_type = self.test_type
b'd\n') code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'd\n')
assert code == 1 assert code == 1
assert PROMPT_PATTERN in stderr assert PROMPT_PATTERN in stderr
assert FIRST_CHECK_FAILED_PATTERN in stdout assert FIRST_CHECK_FAILED_PATTERN in stdout
assert PERMISSION_DENIED_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr
def test_unrecognized_option(self, test_type): def test_unrecognized_option(self):
code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], test_type = self.test_type
b'e\na\n') code, stdout, stderr = self._run_deno(
[], ["needs" + test_type.capitalize()], b'e\na\n')
assert code == 0 assert code == 0
assert PROMPT_PATTERN in stderr assert PROMPT_PATTERN in stderr
assert not FIRST_CHECK_FAILED_PATTERN in stdout assert not FIRST_CHECK_FAILED_PATTERN in stdout
assert not PERMISSION_DENIED_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr
assert b'Unrecognized option' in stderr assert b'Unrecognized option' in stderr
def test_no_prompt(self, test_type): def test_no_prompt(self):
code, stdout, stderr = self.run( test_type = self.test_type
code, stdout, stderr = self._run_deno(
["--no-prompt"], ["needs" + test_type.capitalize()], b'') ["--no-prompt"], ["needs" + test_type.capitalize()], b'')
assert code == 1 assert code == 1
assert not PROMPT_PATTERN in stderr assert not PROMPT_PATTERN in stderr
assert FIRST_CHECK_FAILED_PATTERN in stdout assert FIRST_CHECK_FAILED_PATTERN in stdout
assert PERMISSION_DENIED_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr
def test_no_prompt_allow(self, test_type): def test_no_prompt_allow(self):
code, stdout, stderr = self.run( test_type = self.test_type
code, stdout, stderr = self._run_deno(
["--no-prompt", "--allow-" + test_type], ["--no-prompt", "--allow-" + test_type],
["needs" + test_type.capitalize()], b'') ["needs" + test_type.capitalize()], b'')
assert code == 0 assert code == 0
@ -183,12 +160,23 @@ class Prompt(object):
def permission_prompt_test(deno_exe): def permission_prompt_test(deno_exe):
p = Prompt(deno_exe, ["read", "write", "env", "net", "run"]) runner = unittest.TextTestRunner(verbosity=2)
p.test() loader = unittest.TestLoader()
test_types = ["read", "write", "env", "net", "run"]
for test_type in test_types:
print "Permissions prompt tests for \"{}\"".format(test_type)
test_names = loader.getTestCaseNames(TestPrompt)
suite = unittest.TestSuite()
for test_name in test_names:
suite.addTest(TestPrompt(test_name, test_type, deno_exe))
result = runner.run(suite)
if not result.wasSuccessful():
sys.exit(1)
def main(): def main():
print "Permissions prompt tests"
deno_exe = os.path.join(build_path(), "deno" + executable_suffix) deno_exe = os.path.join(build_path(), "deno" + executable_suffix)
permission_prompt_test(deno_exe) permission_prompt_test(deno_exe)

View file

@ -1,67 +1,71 @@
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. # Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import os import os
import sys
import unittest
from setup import gn_string, read_gn_args, write_gn_args from setup import gn_string, read_gn_args, write_gn_args
from shutil import rmtree from shutil import rmtree
from tempfile import mktemp from tempfile import mktemp
def gn_string_test(): class TestSetup(unittest.TestCase):
assert '"abc"' == gn_string('abc') def test_gn_string(self):
assert '"foo\\$bar\\"baz"' == gn_string('foo$bar"baz') assert '"abc"' == gn_string('abc')
assert '"do\\not\\escape"' == gn_string('do\\not\\escape') assert '"foo\\$bar\\"baz"' == gn_string('foo$bar"baz')
assert '"so\\\\\\very\\\\\\"fun\\"' == gn_string('so\\\\very\\"fun\\') assert '"do\\not\\escape"' == gn_string('do\\not\\escape')
assert '"so\\\\\\very\\\\\\"fun\\"' == gn_string('so\\\\very\\"fun\\')
def test_read_gn_args(self):
# Args file doesn't exist.
(args,
hand_edited) = read_gn_args("/baddir/hopefully/nonexistent/args.gn")
assert args is None
assert hand_edited == False
def read_gn_args_test(): # Handwritten empty args file.
# Args file doesn't exist. filename = mktemp()
(args, hand_edited) = read_gn_args("/baddir/hopefully/nonexistent/args.gn") with open(filename, "w"):
assert args is None pass
assert hand_edited == False (args, hand_edited) = read_gn_args(filename)
os.remove(filename)
assert args == []
assert hand_edited == True
# Handwritten empty args file. # Handwritten non-empty args file.
filename = mktemp() expect_args = ['some_number=2', 'another_string="ran/dom#yes"']
with open(filename, "w"): filename = mktemp()
pass with open(filename, "w") as f:
(args, hand_edited) = read_gn_args(filename) f.write("\n".join(expect_args + ["", "# A comment to be ignored"]))
os.remove(filename) (args, hand_edited) = read_gn_args(filename)
assert args == [] os.remove(filename)
assert hand_edited == True assert args == expect_args
assert hand_edited == True
# Handwritten non-empty args file. def test_write_gn_args(self):
expect_args = ['some_number=2', 'another_string="ran/dom#yes"'] # Build a nonexistent path; write_gn_args() should call mkdir as needed.
filename = mktemp() d = mktemp()
with open(filename, "w") as f: filename = os.path.join(d, "args.gn")
f.write("\n".join(expect_args + ["", "# A comment to be ignored"])) assert not os.path.exists(d)
(args, hand_edited) = read_gn_args(filename) assert not os.path.exists(filename)
os.remove(filename) # Write some args.
assert args == expect_args args = ['lalala=42', 'foo_bar_baz="lorem ipsum dolor#amet"']
assert hand_edited == True write_gn_args(filename, args)
# Directory and args file should now be created.
assert os.path.isdir(d)
def write_gn_args_test(): assert os.path.isfile(filename)
# Build a nonexistent path; write_gn_args() should call mkdir as needed. # Validate that the right contents were written.
d = mktemp() (check_args, hand_edited) = read_gn_args(filename)
filename = os.path.join(d, "args.gn") assert check_args == args
assert not os.path.exists(d) assert hand_edited == False
assert not os.path.exists(filename) # Clean up.
# Write some args. rmtree(d)
args = ['lalala=42', 'foo_bar_baz="lorem ipsum dolor#amet"']
write_gn_args(filename, args)
# Directory and args file should now be created.
assert os.path.isdir(d)
assert os.path.isfile(filename)
# Validate that the right contents were written.
(check_args, hand_edited) = read_gn_args(filename)
assert check_args == args
assert hand_edited == False
# Clean up.
rmtree(d)
def setup_test(): def setup_test():
gn_string_test() suite = unittest.TestLoader().loadTestsFromTestCase(TestSetup)
read_gn_args_test() result = unittest.TextTestRunner(verbosity=2).run(suite)
write_gn_args_test() if not result.wasSuccessful():
sys.exit(1)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -4,13 +4,15 @@
# Usage: ./tools/test.py out/Debug # Usage: ./tools/test.py out/Debug
import os import os
import sys import sys
import unittest
from integration_tests import integration_tests from integration_tests import integration_tests
from deno_dir_test import deno_dir_test from deno_dir_test import deno_dir_test
from setup_test import setup_test
from util import build_path, enable_ansi_colors, executable_suffix, run, rmtree from util import build_path, enable_ansi_colors, executable_suffix, run, rmtree
from util import run_output, tests_path, green_ok from util import run_output, tests_path, green_ok
from unit_tests import unit_tests from unit_tests import unit_tests
from util_test import util_test from util_test import util_test
from setup_test import setup_test
from benchmark_test import benchmark_test from benchmark_test import benchmark_test
from repl_test import repl_tests from repl_test import repl_tests
from fetch_test import fetch_test from fetch_test import fetch_test
@ -68,6 +70,7 @@ def main(argv):
# Python/build tools testing # Python/build tools testing
setup_test() setup_test()
util_test() util_test()
run([ run([
"node", "./node_modules/.bin/ts-node", "--project", "node", "./node_modules/.bin/ts-node", "--project",
"tools/ts_library_builder/tsconfig.json", "tools/ts_library_builder/tsconfig.json",

120
tools/util_test.py Normal file → Executable file
View file

@ -1,76 +1,78 @@
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. # Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
from util import pattern_match, parse_exit_code, shell_quote_win
import util
import os
import sys import sys
import unittest
from util import (
pattern_match,
parse_exit_code,
shell_quote_win,
parse_wrk_output,
root_path,
)
import os
def pattern_match_test(): class TestUtil(unittest.TestCase):
print "Testing util.pattern_match()..." def test_pattern_match(self):
# yapf: disable # yapf: disable
fixtures = [("foobarbaz", "foobarbaz", True), fixtures = [("foobarbaz", "foobarbaz", True),
("[WILDCARD]", "foobarbaz", True), ("[WILDCARD]", "foobarbaz", True),
("foobar", "foobarbaz", False), ("foobar", "foobarbaz", False),
("foo[WILDCARD]baz", "foobarbaz", True), ("foo[WILDCARD]baz", "foobarbaz", True),
("foo[WILDCARD]baz", "foobazbar", False), ("foo[WILDCARD]baz", "foobazbar", False),
("foo[WILDCARD]baz[WILDCARD]qux", "foobarbazqatqux", True), ("foo[WILDCARD]baz[WILDCARD]qux", "foobarbazqatqux", True),
("foo[WILDCARD]", "foobar", True), ("foo[WILDCARD]", "foobar", True),
("foo[WILDCARD]baz[WILDCARD]", "foobarbazqat", True)] ("foo[WILDCARD]baz[WILDCARD]", "foobarbazqat", True)]
# yapf: enable # yapf: enable
# Iterate through the fixture lists, testing each one # Iterate through the fixture lists, testing each one
for (pattern, string, expected) in fixtures: for (pattern, string, expected) in fixtures:
actual = pattern_match(pattern, string) actual = pattern_match(pattern, string)
assert expected == actual, \ assert expected == actual, \
"expected %s for\nExpected:\n%s\nTo equal actual:\n%s" % ( "expected %s for\nExpected:\n%s\nTo equal actual:\n%s" % (
expected, pattern, string) expected, pattern, string)
assert pattern_match("foo[BAR]baz", "foobarbaz", assert pattern_match("foo[BAR]baz", "foobarbaz",
"[BAR]") == True, "expected wildcard to be set" "[BAR]") == True, "expected wildcard to be set"
assert pattern_match("foo[BAR]baz", "foobazbar", assert pattern_match("foo[BAR]baz", "foobazbar",
"[BAR]") == False, "expected wildcard to be set" "[BAR]") == False, "expected wildcard to be set"
def test_parse_exit_code(self):
assert 54 == parse_exit_code('hello_error54_world')
assert 1 == parse_exit_code('hello_error_world')
assert 0 == parse_exit_code('hello_world')
def parse_exit_code_test(): def test_shell_quote_win(self):
print "Testing util.parse_exit_code()..." assert 'simple' == shell_quote_win('simple')
assert 54 == parse_exit_code('hello_error54_world') assert 'roof/\\isoprojection' == shell_quote_win(
assert 1 == parse_exit_code('hello_error_world') 'roof/\\isoprojection')
assert 0 == parse_exit_code('hello_world') assert '"with space"' == shell_quote_win('with space')
assert '"embedded""quote"' == shell_quote_win('embedded"quote')
assert '"a""b""""c\\d\\\\""e\\\\\\\\"' == shell_quote_win(
'a"b""c\\d\\"e\\\\')
def test_parse_wrk_output(self):
f = open(os.path.join(root_path, "tools/testdata/wrk1.txt"))
stats = parse_wrk_output(f.read())
assert stats['req_per_sec'] == 1837
assert stats['max_latency'] == 34.96
def shell_quote_win_test(): f2 = open(os.path.join(root_path, "tools/testdata/wrk2.txt"))
print "Testing util.shell_quote_win()..." stats2 = parse_wrk_output(f2.read())
assert 'simple' == shell_quote_win('simple') assert stats2['req_per_sec'] == 53435
assert 'roof/\\isoprojection' == shell_quote_win('roof/\\isoprojection') assert stats2['max_latency'] == 0.00125
assert '"with space"' == shell_quote_win('with space')
assert '"embedded""quote"' == shell_quote_win('embedded"quote')
assert '"a""b""""c\\d\\\\""e\\\\\\\\"' == shell_quote_win(
'a"b""c\\d\\"e\\\\')
f3 = open(os.path.join(root_path, "tools/testdata/wrk3.txt"))
def parse_wrk_output_test(): stats3 = parse_wrk_output(f3.read())
print "Testing util.parse_wrk_output_test()..." assert stats3['req_per_sec'] == 96037
f = open(os.path.join(util.root_path, "tools/testdata/wrk1.txt")) assert stats3['max_latency'] == 1630.0
stats = util.parse_wrk_output(f.read())
assert stats['req_per_sec'] == 1837
assert stats['max_latency'] == 34.96
f2 = open(os.path.join(util.root_path, "tools/testdata/wrk2.txt"))
stats2 = util.parse_wrk_output(f2.read())
assert stats2['req_per_sec'] == 53435
assert stats2['max_latency'] == 0.00125
f3 = open(os.path.join(util.root_path, "tools/testdata/wrk3.txt"))
stats3 = util.parse_wrk_output(f3.read())
assert stats3['req_per_sec'] == 96037
assert stats3['max_latency'] == 1630.0
def util_test(): def util_test():
pattern_match_test() suite = unittest.TestLoader().loadTestsFromTestCase(TestUtil)
parse_exit_code_test() result = unittest.TextTestRunner(verbosity=2).run(suite)
shell_quote_win_test() if not result.wasSuccessful():
parse_wrk_output_test() sys.exit(1)
if __name__ == '__main__': if __name__ == '__main__':