Skip to content

Commit

Permalink
unittest: switch to using top dir since test ids are relative to it
Browse files Browse the repository at this point in the history
  • Loading branch information
eleanorjboyd committed Dec 7, 2023
1 parent 8a8c00d commit a40e6f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 11 additions & 1 deletion pythonFiles/unittestadapter/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ def discover_tests(
loader = unittest.TestLoader()
suite = loader.discover(start_dir, pattern, top_level_dir)

tests, error = build_test_tree(suite, cwd) # test tree built succesfully here.
# If the top level directory is not provided, then use the start directory.
if top_level_dir is None:
top_level_dir = start_dir

tests, error = build_test_tree(
suite, top_level_dir
) # test tree built successfully here.

except Exception:
error.append(traceback.format_exc())
Expand Down Expand Up @@ -146,6 +152,10 @@ def post_response(
)
if testUuid is not None:
# Perform test discovery.
print("payload = discover_tests(start_dir, pattern, top_level_dir, testUuid)")
print(
"EJFB star", start_dir, "pat", pattern, "tl", top_level_dir, "tu", testUuid
)
payload = discover_tests(start_dir, pattern, top_level_dir, testUuid)
# Post this discovery payload.
post_response(payload, testPort, testUuid)
Expand Down
15 changes: 9 additions & 6 deletions pythonFiles/unittestadapter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ def build_test_node(path: str, name: str, type_: TestNodeTypeEnum) -> TestNode:
def get_child_node(
name: str, path: str, type_: TestNodeTypeEnum, root: TestNode
) -> TestNode:
"""Find a child node in a test tree given its name and type. If the node doesn't exist, create it."""
"""Find a child node in a test tree given its name, type and path. If the node doesn't exist, create it.
Path is required to distinguish between nodes with the same name and type."""
try:
result = next(
node
for node in root["children"]
if node["name"] == name and node["type_"] == type_
if node["name"] == name and node["type_"] == type_ and node["path"] == path
)
except StopIteration:
result = build_test_node(path, name, type_)
Expand All @@ -109,7 +110,7 @@ def get_child_node(


def build_test_tree(
suite: unittest.TestSuite, test_directory: str
suite: unittest.TestSuite, top_level_directory: str
) -> Tuple[Union[TestNode, None], List[str]]:
"""Build a test tree from a unittest test suite.
Expand Down Expand Up @@ -152,8 +153,10 @@ def build_test_tree(
}
"""
error = []
directory_path = pathlib.PurePath(test_directory)
root = build_test_node(test_directory, directory_path.name, TestNodeTypeEnum.folder)
directory_path = pathlib.PurePath(top_level_directory)
root = build_test_node(
top_level_directory, directory_path.name, TestNodeTypeEnum.folder
)

for test_case in get_test_case(suite):
test_id = test_case.id()
Expand Down Expand Up @@ -185,7 +188,7 @@ def build_test_tree(
)

# Find/build file node.
path_components = [test_directory] + folders + [py_filename]
path_components = [top_level_directory] + folders + [py_filename]
file_path = os.fsdecode(pathlib.PurePath("/".join(path_components)))
current_node = get_child_node(
py_filename, file_path, TestNodeTypeEnum.file, current_node
Expand Down

0 comments on commit a40e6f6

Please sign in to comment.