From 9a5363ce0e1a64275ac7fdc8e1cf0ac95038ec61 Mon Sep 17 00:00:00 2001 From: Shane Saravia Date: Mon, 27 Nov 2023 14:21:17 -0500 Subject: [PATCH] Resolve test suite discovery import errors due to path ordering (#22454) [Issue #22453](https://github.com/microsoft/vscode-python/issues/22453) - Once starting to run discovery, add the projects root path to PATH at index 0 so that any further imports will use the projects root directory and not reference the incorrect directory. - Since the test suite only allows the start_dir to be one directory deep, we can conclude that if the start_dir is not "." or contains a "/", that we need to add that start_dir's parent to PATH. Otherwise, we simply add the start_dir to PATH. --- pythonFiles/unittestadapter/discovery.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pythonFiles/unittestadapter/discovery.py b/pythonFiles/unittestadapter/discovery.py index 274fb5e5e663..e8f602a22fb3 100644 --- a/pythonFiles/unittestadapter/discovery.py +++ b/pythonFiles/unittestadapter/discovery.py @@ -73,6 +73,11 @@ def discover_tests( } """ cwd = os.path.abspath(start_dir) + if "/" in start_dir: # is a subdir + parent_dir = os.path.dirname(start_dir) + sys.path.insert(0, parent_dir) + else: + sys.path.insert(0, cwd) payload: PayloadDict = {"cwd": cwd, "status": "success", "tests": None} tests = None error: List[str] = []