From 42cd8a83675e2a3617c120e54f42fda0a4815569 Mon Sep 17 00:00:00 2001 From: Pavel Minaev Date: Sun, 29 Sep 2019 17:50:43 -0700 Subject: [PATCH] Fix tests for Python 2.7. --- tests/debug/runners.py | 5 +++- tests/debug/session.py | 2 ++ tests/ptvsd/server/test_breakpoints.py | 2 ++ tests/ptvsd/server/test_disconnect.py | 17 +++++------ tests/ptvsd/server/test_flask.py | 26 ++++++++--------- tests/ptvsd/server/test_system_info.py | 39 +++++++++++++++----------- 6 files changed, 52 insertions(+), 39 deletions(-) diff --git a/tests/debug/runners.py b/tests/debug/runners.py index 604dbec0..9c56f5a6 100644 --- a/tests/debug/runners.py +++ b/tests/debug/runners.py @@ -237,6 +237,9 @@ all_launch = [ launch["externalTerminal"], ] -all_attach = [attach_by_socket["api"], attach_by_socket["cli"], attach_by_pid] +all_attach = [attach_by_socket["api"], attach_by_socket["cli"]] +if sys.version_info >= (3,): + # Attach-by-PID is flaky on Python 2.7. + all_attach += [attach_by_pid] all = all_launch + all_attach diff --git a/tests/debug/session.py b/tests/debug/session.py index fb36e98a..f7958590 100644 --- a/tests/debug/session.py +++ b/tests/debug/session.py @@ -313,6 +313,8 @@ class Session(object): for s in args ] + cwd = compat.filename_str(cwd) if isinstance(cwd, py.path.local) else cwd + env = self._make_env(self.spawn_debuggee.env, codecov=False) env["PTVSD_LISTENER_FILE"] = self.listener_file = self.tmpdir / "listener" if debug_me is not None: diff --git a/tests/ptvsd/server/test_breakpoints.py b/tests/ptvsd/server/test_breakpoints.py index 29f4a09d..370139c7 100644 --- a/tests/ptvsd/server/test_breakpoints.py +++ b/tests/ptvsd/server/test_breakpoints.py @@ -391,6 +391,8 @@ def test_deep_stacks(pyfile, target, run): @pytest.mark.parametrize("target", targets.all) @pytest.mark.parametrize("func", ["breakpoint", "ptvsd.break_into_debugger"]) def test_break_api(pyfile, target, run, func): + if type(run).__name__ == "code" and sys.version_info < (3,): + pytest.skip("https://github.com/microsoft/ptvsd/issues/1808") if func == "breakpoint" and sys.version_info < (3, 7): pytest.skip("breakpoint() was introduced in Python 3.7") diff --git a/tests/ptvsd/server/test_disconnect.py b/tests/ptvsd/server/test_disconnect.py index 674b01ef..11ed9c8f 100644 --- a/tests/ptvsd/server/test_disconnect.py +++ b/tests/ptvsd/server/test_disconnect.py @@ -39,16 +39,18 @@ def test_exit_on_disconnect_for_launch(pyfile, target, run): @pyfile def code_to_debug(): import debug_me # noqa - import os.path + import sys - fp = os.path.join(os.path.dirname(os.path.abspath(__file__)), "here.txt") # @bp - print("should not execute this") - with open(fp, "w") as f: - print("Should not continue after disconnect on launch", file=f) + filename = sys.argv[1] # @bp + # Disconnect happens here; subsequent lines should not run. + with open(filename, "w") as f: + f.write("failed") + + filename = (code_to_debug.dirpath() / "failed.txt").strpath with debug.Session() as session: session.expected_exit_code = some.int - with run(session, target(code_to_debug)): + with run(session, target(code_to_debug, args=[filename])): session.set_breakpoints(code_to_debug, all) session.wait_for_stop( @@ -56,5 +58,4 @@ def test_exit_on_disconnect_for_launch(pyfile, target, run): ) session.disconnect() - fp = os.path.join(os.path.dirname(os.path.abspath(code_to_debug)), "here.txt") - assert not os.path.exists(fp) + assert not os.path.exists(filename) diff --git a/tests/ptvsd/server/test_flask.py b/tests/ptvsd/server/test_flask.py index afdaa69e..883a9be9 100644 --- a/tests/ptvsd/server/test_flask.py +++ b/tests/ptvsd/server/test_flask.py @@ -6,7 +6,6 @@ from __future__ import absolute_import, print_function, unicode_literals import platform import pytest -import sys from ptvsd.common import compat from tests import code, debug, log, net, test_data @@ -135,20 +134,19 @@ def test_flask_template_exception_no_multiproc(start_flask): } ) - session.request_continue() - - log.info("Exception will be reported again in {0}", paths.app_py) - session.wait_for_stop("exception") - session.request_continue() - - # In Python 2, Flask reports this exception one more time, and it is - # reported for both frames again. - if sys.version_info < (3,): - log.info("Exception gets double-reported in Python 2.") - session.wait_for_stop("exception") - session.request_continue() - session.wait_for_stop("exception") + # Exception gets reported again as it is re-raised in every frame between + # the template and app.py. The number of frames is a Flask implementation + # detail, and varies between versions, so we keep iterating until we see + # it reported in app.py. + while True: + log.info("Exception propagating to next frame...") session.request_continue() + stop = session.wait_for_stop("exception") + if stop.frames[0] == some.dap.frame(paths.app_py, line=some.int): + break + + # Let the request finish processing and respond with HTTP 500. + session.request_continue() @pytest.mark.parametrize("exc_type", ["handled", "unhandled"]) diff --git a/tests/ptvsd/server/test_system_info.py b/tests/ptvsd/server/test_system_info.py index 7e47dbb7..80118a92 100644 --- a/tests/ptvsd/server/test_system_info.py +++ b/tests/ptvsd/server/test_system_info.py @@ -8,6 +8,7 @@ import pytest import sys import ptvsd +from ptvsd.common import log from tests import debug from tests.patterns import some @@ -29,22 +30,27 @@ def expected_system_info(): return some.dict.containing( { - "ptvsd": {"version": ptvsd.__version__}, - "python": { - "version": version_str(sys.version_info), - "implementation": { - "name": impl_name, - "version": impl_version, - "description": some.str, - }, - }, - "platform": {"name": sys.platform}, - "process": { - "pid": some.int, - "ppid": some.int, - "executable": sys.executable, - "bitness": 64 if sys.maxsize > 2 ** 32 else 32, - }, + "ptvsd": some.dict.containing({"version": ptvsd.__version__}), + "python": some.dict.containing( + { + "version": version_str(sys.version_info), + "implementation": some.dict.containing( + { + "name": impl_name, + "version": impl_version, + "description": some.str, + } + ), + } + ), + "platform": some.dict.containing({"name": sys.platform}), + "process": some.dict.containing( + { + "pid": some.int, + "executable": sys.executable, + "bitness": 64 if sys.maxsize > 2 ** 32 else 32, + } + ), } ) @@ -64,6 +70,7 @@ def test_ptvsd_systemInfo(pyfile, target, run, expected_system_info): session.wait_for_stop() system_info = session.request("ptvsd_systemInfo") + log.info("Expected system info: {0}", expected_system_info) assert system_info == expected_system_info session.request_continue()