From 824542e61e1dbef09ca9388deca0c595a3e3e1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 27 May 2019 15:27:55 +0200 Subject: [PATCH] chore: Port Python tests to unittest (#2408) --- tools/complex_permissions_test.py | 295 +++++++++++++----------------- tools/permission_prompt_test.py | 126 ++++++------- tools/setup_test.py | 104 ++++++----- tools/test.py | 5 +- tools/util_test.py | 134 +++++++------- 5 files changed, 312 insertions(+), 352 deletions(-) mode change 100644 => 100755 tools/util_test.py diff --git a/tools/complex_permissions_test.py b/tools/complex_permissions_test.py index 0b826984..39ccda33 100755 --- a/tools/complex_permissions_test.py +++ b/tools/complex_permissions_test.py @@ -7,6 +7,7 @@ import select import subprocess import sys import time +import unittest import http_server 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'] -# Wraps a test in debug printouts -# so we have visual indicator of what test failed -def wrap_test(test_name, test_method, *argv): - sys.stdout.write(test_name + " ... ") - try: - test_method(*argv) - print green_ok() - except AssertionError: - print red_failed() - raise - - -class Prompt(object): - def __init__(self, deno_exe, test_types): +class ComplexPermissionTestCase(unittest.TestCase): + def __init__(self, method_name, test_type, deno_exe): + super(ComplexPermissionTestCase, self).__init__(method_name) + self.test_type = test_type 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)." - cmd = [self.deno_exe, "run"] + flags + [PERMISSIONS_PROMPT_TEST_TS - ] + args - print " ".join(cmd) - 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): - for test_type in ["read", "write"]: - test_name_base = "test_" + test_type - wrap_test(test_name_base + "_inside_project_dir", - self.test_inside_project_dir, test_type) - 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'') + cmd = ([self.deno_exe, "run", "--no-prompt"] + flags + + [PERMISSIONS_PROMPT_TEST_TS] + args) + return tty_capture(cmd, b'') + + +class TestReadWritePermissions(ComplexPermissionTestCase): + def test_inside_project_dir(self): + code, _stdout, stderr = self._run_deno( + ["--allow-" + self.test_type + "=" + root_path], + [self.test_type, "package.json", "tests/subdir/config.json"]) assert code == 0 assert not PROMPT_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr - def test_outside_test_dir(self, test_type): - code, _stdout, stderr = self.run([ - "--no-prompt", - "--allow-" + test_type + "=" + os.path.join(root_path, "tests") - ], [test_type, "package.json"], b'') + def test_outside_test_dir(self): + code, _stdout, stderr = self._run_deno([ + "--allow-" + self.test_type + "=" + os.path.join( + root_path, "tests") + ], [self.test_type, "package.json"]) assert code == 1 assert not PROMPT_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr - def test_inside_test_dir(self, test_type): - code, _stdout, stderr = self.run([ - "--no-prompt", - "--allow-" + test_type + "=" + os.path.join(root_path, "tests") - ], [test_type, "tests/subdir/config.json"], b'') + def test_inside_test_dir(self): + code, _stdout, stderr = self._run_deno([ + "--allow-" + self.test_type + "=" + os.path.join( + root_path, "tests") + ], [self.test_type, "tests/subdir/config.json"]) assert code == 0 assert not PROMPT_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr - def test_outside_test_and_js_dir(self, test_type): - code, _stdout, stderr = self.run([ - "--no-prompt", "--allow-" + test_type + "=" + os.path.join( + def test_outside_test_and_js_dir(self): + code, _stdout, stderr = self._run_deno([ + "--allow-" + self.test_type + "=" + os.path.join( root_path, "tests") + "," + os.path.join(root_path, "js") - ], [test_type, "package.json"], b'') + ], [self.test_type, "package.json"]) assert code == 1 assert not PROMPT_PATTERN in stderr assert PERMISSION_DENIED_PATTERN in stderr - def test_inside_test_and_js_dir(self, test_type): - code, _stdout, stderr = self.run([ - "--no-prompt", "--allow-" + test_type + "=" + os.path.join( + def test_inside_test_and_js_dir(self): + code, _stdout, stderr = self._run_deno([ + "--allow-" + self.test_type + "=" + os.path.join( 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 not PROMPT_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr - def test_relative(self, test_type): + def test_relative(self): # Save and restore curdir saved_curdir = os.getcwd() os.chdir(root_path) - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-" + test_type + "=" + "./tests"], - [test_type, "tests/subdir/config.json"], b'') + code, _stdout, stderr = self._run_deno( + ["--allow-" + self.test_type + "=" + "./tests"], + [self.test_type, "tests/subdir/config.json"]) assert code == 0 assert not PROMPT_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr os.chdir(saved_curdir) - def test_no_prefix(self, test_type): + def test_no_prefix(self): # Save and restore curdir saved_curdir = os.getcwd() os.chdir(root_path) - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-" + test_type + "=" + "tests"], - [test_type, "tests/subdir/config.json"], b'') + code, _stdout, stderr = self._run_deno( + ["--allow-" + self.test_type + "=" + "tests"], + [self.test_type, "tests/subdir/config.json"]) assert code == 0 assert not PROMPT_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr os.chdir(saved_curdir) - # net tests - def test_allow_net(self, test_type, allowed_host, hosts): - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-net=" + allowed_host], - [test_type] + hosts, b'') + +class TestNetFetchPermissions(ComplexPermissionTestCase): + def test_allow_localhost_4545(self): + code, _stdout, stderr = self._run_deno( + ["--allow-net=localhost:4545"], + [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_4545(self, test_type, hosts): - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-net=localhost:4545"], [test_type] + hosts, - b'') - assert code == 0 + def test_allow_deno_land(self): + code, _stdout, stderr = self._run_deno( + ["--allow-net=deno.land"], + [self.test_type, "http://localhost:4545"]) + assert code == 1 assert not PROMPT_PATTERN in stderr - assert not PERMISSION_DENIED_PATTERN in stderr + assert PERMISSION_DENIED_PATTERN in stderr + + def test_allow_localhost_4545_fail(self): + code, _stdout, stderr = self._run_deno( + ["--allow-net=localhost:4545"], + [self.test_type, "http://localhost:4546"]) + assert code == 1 + assert not PROMPT_PATTERN in stderr + assert 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'') + def test_allow_localhost(self): + code, _stdout, stderr = self._run_deno(["--allow-net=localhost"], [ + self.test_type, "http://localhost:4545", "http://localhost:4546", + "http://localhost:4547" + ]) 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'') + +class TestNetDialPermissions(ComplexPermissionTestCase): + 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, test_type, hosts): - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-net=deno.land"], [test_type] + hosts, b'') + 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_4545_fail(self, test_type, hosts): - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-net=localhost:4545"], [test_type] + hosts, - b'') + 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_4545_fail(self, test_type, hosts): - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-net=127.0.0.1:4545"], [test_type] + hosts, - b'') - assert code == 1 + 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 PERMISSION_DENIED_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_localhost_4555_fail(self, test_type, hosts): - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-net=localhost:4555"], [test_type] + hosts, - b'') + 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(self, test_type, hosts): - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-net=localhost"], [test_type] + hosts, b'') - assert code == 0 + 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 not PERMISSION_DENIED_PATTERN in stderr + assert PERMISSION_DENIED_PATTERN in stderr - def test_allow_localhost_ip(self, test_type, hosts): - code, _stdout, stderr = self.run( - ["--no-prompt", "--allow-net=127.0.0.1"], [test_type] + hosts, b'') + 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 not PROMPT_PATTERN in stderr assert not PERMISSION_DENIED_PATTERN in stderr def complex_permissions_test(deno_exe): - p = Prompt(deno_exe, ["read", "write", "net"]) - p.test() + runner = unittest.TextTestRunner(verbosity=2) + 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(): - print "Permissions prompt tests" deno_exe = os.path.join(build_path(), "deno" + executable_suffix) http_server.spawn() complex_permissions_test(deno_exe) diff --git a/tools/permission_prompt_test.py b/tools/permission_prompt_test.py index e3ee7d9c..aef2bdf3 100755 --- a/tools/permission_prompt_test.py +++ b/tools/permission_prompt_test.py @@ -7,6 +7,7 @@ import select import subprocess import sys import time +import unittest 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'] -# Wraps a test in debug printouts -# so we have visual indicator of what test failed -def wrap_test(test_name, test_method, *argv): - sys.stdout.write(test_name + " ... ") - try: - test_method(*argv) - print green_ok() - except AssertionError: - print red_failed() - raise - - -class Prompt(object): - def __init__(self, deno_exe, test_types): +class TestPrompt(unittest.TestCase): + def __init__(self, method_name, test_type, deno_exe): + super(TestPrompt, self).__init__(method_name) + self.test_type = test_type 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)." cmd = [self.deno_exe, "run"] + flags + [PERMISSIONS_PROMPT_TEST_TS ] + args return tty_capture(cmd, bytes_input) - def warm_up(self): - # ignore the ts compiling message - self.run(["--allow-write"], 'needsWrite', b'') - - 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( + def test_allow_flag(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( ["--allow-" + test_type], ["needs" + test_type.capitalize()], b'') assert code == 0 assert not PROMPT_PATTERN in stderr assert not FIRST_CHECK_FAILED_PATTERN in stdout assert not PERMISSION_DENIED_PATTERN in stderr - def test_yes_yes(self, test_type): - code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], - b'y\ny\n') + def test_yes_yes(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( + [], ["needs" + test_type.capitalize()], b'y\ny\n') assert code == 0 assert PROMPT_PATTERN in stderr assert not FIRST_CHECK_FAILED_PATTERN in stdout assert not PERMISSION_DENIED_PATTERN in stderr - def test_yes_no(self, test_type): - code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], - b'y\nn\n') + def test_yes_no(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( + [], ["needs" + test_type.capitalize()], b'y\nn\n') assert code == 1 assert PROMPT_PATTERN in stderr assert not FIRST_CHECK_FAILED_PATTERN in stdout assert PERMISSION_DENIED_PATTERN in stderr - def test_no_no(self, test_type): - code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], - b'n\nn\n') + def test_no_no(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( + [], ["needs" + test_type.capitalize()], b'n\nn\n') assert code == 1 assert PROMPT_PATTERN in stderr assert FIRST_CHECK_FAILED_PATTERN in stdout assert PERMISSION_DENIED_PATTERN in stderr - def test_no_yes(self, test_type): - code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], - b'n\ny\n') + def test_no_yes(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( + [], ["needs" + test_type.capitalize()], b'n\ny\n') assert code == 0 assert PROMPT_PATTERN in stderr assert FIRST_CHECK_FAILED_PATTERN in stdout assert not PERMISSION_DENIED_PATTERN in stderr - def test_allow(self, test_type): - code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], - b'a\n') + def test_allow(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( + [], ["needs" + test_type.capitalize()], b'a\n') assert code == 0 assert PROMPT_PATTERN in stderr assert not FIRST_CHECK_FAILED_PATTERN in stdout assert not PERMISSION_DENIED_PATTERN in stderr - def test_deny(self, test_type): - code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], - b'd\n') + def test_deny(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( + [], ["needs" + test_type.capitalize()], b'd\n') assert code == 1 assert PROMPT_PATTERN in stderr assert FIRST_CHECK_FAILED_PATTERN in stdout assert PERMISSION_DENIED_PATTERN in stderr - def test_unrecognized_option(self, test_type): - code, stdout, stderr = self.run([], ["needs" + test_type.capitalize()], - b'e\na\n') + def test_unrecognized_option(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( + [], ["needs" + test_type.capitalize()], b'e\na\n') assert code == 0 assert PROMPT_PATTERN in stderr assert not FIRST_CHECK_FAILED_PATTERN in stdout assert not PERMISSION_DENIED_PATTERN in stderr assert b'Unrecognized option' in stderr - def test_no_prompt(self, test_type): - code, stdout, stderr = self.run( + def test_no_prompt(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( ["--no-prompt"], ["needs" + test_type.capitalize()], b'') assert code == 1 assert not PROMPT_PATTERN in stderr assert FIRST_CHECK_FAILED_PATTERN in stdout assert PERMISSION_DENIED_PATTERN in stderr - def test_no_prompt_allow(self, test_type): - code, stdout, stderr = self.run( + def test_no_prompt_allow(self): + test_type = self.test_type + code, stdout, stderr = self._run_deno( ["--no-prompt", "--allow-" + test_type], ["needs" + test_type.capitalize()], b'') assert code == 0 @@ -183,12 +160,23 @@ class Prompt(object): def permission_prompt_test(deno_exe): - p = Prompt(deno_exe, ["read", "write", "env", "net", "run"]) - p.test() + runner = unittest.TextTestRunner(verbosity=2) + 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(): - print "Permissions prompt tests" deno_exe = os.path.join(build_path(), "deno" + executable_suffix) permission_prompt_test(deno_exe) diff --git a/tools/setup_test.py b/tools/setup_test.py index 63ddfb41..5434b42a 100644 --- a/tools/setup_test.py +++ b/tools/setup_test.py @@ -1,67 +1,71 @@ # Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import os +import sys +import unittest + from setup import gn_string, read_gn_args, write_gn_args from shutil import rmtree from tempfile import mktemp -def gn_string_test(): - assert '"abc"' == gn_string('abc') - assert '"foo\\$bar\\"baz"' == gn_string('foo$bar"baz') - assert '"do\\not\\escape"' == gn_string('do\\not\\escape') - assert '"so\\\\\\very\\\\\\"fun\\"' == gn_string('so\\\\very\\"fun\\') - - -def read_gn_args_test(): - # Args file doesn't exist. - (args, hand_edited) = read_gn_args("/baddir/hopefully/nonexistent/args.gn") - assert args is None - assert hand_edited == False +class TestSetup(unittest.TestCase): + def test_gn_string(self): + assert '"abc"' == gn_string('abc') + assert '"foo\\$bar\\"baz"' == gn_string('foo$bar"baz') + assert '"do\\not\\escape"' == gn_string('do\\not\\escape') + assert '"so\\\\\\very\\\\\\"fun\\"' == gn_string('so\\\\very\\"fun\\') - # Handwritten empty args file. - filename = mktemp() - with open(filename, "w"): - pass - (args, hand_edited) = read_gn_args(filename) - os.remove(filename) - assert args == [] - assert hand_edited == True + 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 - # Handwritten non-empty args file. - expect_args = ['some_number=2', 'another_string="ran/dom#yes"'] - filename = mktemp() - with open(filename, "w") as f: - f.write("\n".join(expect_args + ["", "# A comment to be ignored"])) - (args, hand_edited) = read_gn_args(filename) - os.remove(filename) - assert args == expect_args - assert hand_edited == True + # Handwritten empty args file. + filename = mktemp() + with open(filename, "w"): + pass + (args, hand_edited) = read_gn_args(filename) + os.remove(filename) + assert args == [] + assert hand_edited == True + # Handwritten non-empty args file. + expect_args = ['some_number=2', 'another_string="ran/dom#yes"'] + filename = mktemp() + with open(filename, "w") as f: + f.write("\n".join(expect_args + ["", "# A comment to be ignored"])) + (args, hand_edited) = read_gn_args(filename) + os.remove(filename) + assert args == expect_args + assert hand_edited == True -def write_gn_args_test(): - # Build a nonexistent path; write_gn_args() should call mkdir as needed. - d = mktemp() - filename = os.path.join(d, "args.gn") - assert not os.path.exists(d) - assert not os.path.exists(filename) - # Write some args. - 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 test_write_gn_args(self): + # Build a nonexistent path; write_gn_args() should call mkdir as needed. + d = mktemp() + filename = os.path.join(d, "args.gn") + assert not os.path.exists(d) + assert not os.path.exists(filename) + # Write some args. + 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(): - gn_string_test() - read_gn_args_test() - write_gn_args_test() + suite = unittest.TestLoader().loadTestsFromTestCase(TestSetup) + result = unittest.TextTestRunner(verbosity=2).run(suite) + if not result.wasSuccessful(): + sys.exit(1) if __name__ == '__main__': diff --git a/tools/test.py b/tools/test.py index 0b913ea5..4769b169 100755 --- a/tools/test.py +++ b/tools/test.py @@ -4,13 +4,15 @@ # Usage: ./tools/test.py out/Debug import os import sys +import unittest + from integration_tests import integration_tests 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 run_output, tests_path, green_ok from unit_tests import unit_tests from util_test import util_test +from setup_test import setup_test from benchmark_test import benchmark_test from repl_test import repl_tests from fetch_test import fetch_test @@ -68,6 +70,7 @@ def main(argv): # Python/build tools testing setup_test() util_test() + run([ "node", "./node_modules/.bin/ts-node", "--project", "tools/ts_library_builder/tsconfig.json", diff --git a/tools/util_test.py b/tools/util_test.py old mode 100644 new mode 100755 index b5d5dcc9..7d189797 --- a/tools/util_test.py +++ b/tools/util_test.py @@ -1,76 +1,78 @@ # 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 unittest + +from util import ( + pattern_match, + parse_exit_code, + shell_quote_win, + parse_wrk_output, + root_path, +) +import os -def pattern_match_test(): - print "Testing util.pattern_match()..." - # yapf: disable - fixtures = [("foobarbaz", "foobarbaz", True), - ("[WILDCARD]", "foobarbaz", True), - ("foobar", "foobarbaz", False), - ("foo[WILDCARD]baz", "foobarbaz", True), - ("foo[WILDCARD]baz", "foobazbar", False), - ("foo[WILDCARD]baz[WILDCARD]qux", "foobarbazqatqux", True), - ("foo[WILDCARD]", "foobar", True), - ("foo[WILDCARD]baz[WILDCARD]", "foobarbazqat", True)] - # yapf: enable - - # Iterate through the fixture lists, testing each one - for (pattern, string, expected) in fixtures: - actual = pattern_match(pattern, string) - assert expected == actual, \ - "expected %s for\nExpected:\n%s\nTo equal actual:\n%s" % ( - expected, pattern, string) - - assert pattern_match("foo[BAR]baz", "foobarbaz", - "[BAR]") == True, "expected wildcard to be set" - assert pattern_match("foo[BAR]baz", "foobazbar", - "[BAR]") == False, "expected wildcard to be set" - - -def parse_exit_code_test(): - print "Testing util.parse_exit_code()..." - assert 54 == parse_exit_code('hello_error54_world') - assert 1 == parse_exit_code('hello_error_world') - assert 0 == parse_exit_code('hello_world') - - -def shell_quote_win_test(): - print "Testing util.shell_quote_win()..." - assert 'simple' == shell_quote_win('simple') - assert 'roof/\\isoprojection' == shell_quote_win('roof/\\isoprojection') - 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 parse_wrk_output_test(): - print "Testing util.parse_wrk_output_test()..." - f = open(os.path.join(util.root_path, "tools/testdata/wrk1.txt")) - 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 +class TestUtil(unittest.TestCase): + def test_pattern_match(self): + # yapf: disable + fixtures = [("foobarbaz", "foobarbaz", True), + ("[WILDCARD]", "foobarbaz", True), + ("foobar", "foobarbaz", False), + ("foo[WILDCARD]baz", "foobarbaz", True), + ("foo[WILDCARD]baz", "foobazbar", False), + ("foo[WILDCARD]baz[WILDCARD]qux", "foobarbazqatqux", True), + ("foo[WILDCARD]", "foobar", True), + ("foo[WILDCARD]baz[WILDCARD]", "foobarbazqat", True)] + # yapf: enable + + # Iterate through the fixture lists, testing each one + for (pattern, string, expected) in fixtures: + actual = pattern_match(pattern, string) + assert expected == actual, \ + "expected %s for\nExpected:\n%s\nTo equal actual:\n%s" % ( + expected, pattern, string) + + assert pattern_match("foo[BAR]baz", "foobarbaz", + "[BAR]") == True, "expected wildcard to be set" + assert pattern_match("foo[BAR]baz", "foobazbar", + "[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 test_shell_quote_win(self): + assert 'simple' == shell_quote_win('simple') + assert 'roof/\\isoprojection' == shell_quote_win( + 'roof/\\isoprojection') + 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 + + f2 = open(os.path.join(root_path, "tools/testdata/wrk2.txt")) + stats2 = parse_wrk_output(f2.read()) + assert stats2['req_per_sec'] == 53435 + assert stats2['max_latency'] == 0.00125 + + f3 = open(os.path.join(root_path, "tools/testdata/wrk3.txt")) + stats3 = parse_wrk_output(f3.read()) + assert stats3['req_per_sec'] == 96037 + assert stats3['max_latency'] == 1630.0 def util_test(): - pattern_match_test() - parse_exit_code_test() - shell_quote_win_test() - parse_wrk_output_test() + suite = unittest.TestLoader().loadTestsFromTestCase(TestUtil) + result = unittest.TextTestRunner(verbosity=2).run(suite) + if not result.wasSuccessful(): + sys.exit(1) if __name__ == '__main__': -- GitLab