Skip to content

Commit 63cf263

Browse files
authored
use abspath for top_level_dir for build_test_tree (#22740)
fixes #22727
1 parent 127457d commit 63cf263

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

pythonFiles/tests/unittestadapter/test_discovery.py

+65
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,71 @@ def test_simple_discovery() -> None:
133133
assert "error" not in actual
134134

135135

136+
def test_simple_discovery_with_top_dir_calculated() -> None:
137+
"""The discover_tests function should return a dictionary with a "success" status, a uuid, no errors, and a test tree
138+
if unittest discovery was performed successfully.
139+
"""
140+
start_dir = "."
141+
pattern = "discovery_simple*"
142+
file_path = os.fsdecode(pathlib.PurePath(TEST_DATA_PATH / "discovery_simple.py"))
143+
144+
expected = {
145+
"path": os.fsdecode(pathlib.PurePath(TEST_DATA_PATH)),
146+
"type_": TestNodeTypeEnum.folder,
147+
"name": ".data",
148+
"children": [
149+
{
150+
"name": "discovery_simple.py",
151+
"type_": TestNodeTypeEnum.file,
152+
"path": file_path,
153+
"children": [
154+
{
155+
"name": "DiscoverySimple",
156+
"path": file_path,
157+
"type_": TestNodeTypeEnum.class_,
158+
"children": [
159+
{
160+
"name": "test_one",
161+
"path": file_path,
162+
"type_": TestNodeTypeEnum.test,
163+
"lineno": "14",
164+
"id_": file_path
165+
+ "\\"
166+
+ "DiscoverySimple"
167+
+ "\\"
168+
+ "test_one",
169+
},
170+
{
171+
"name": "test_two",
172+
"path": file_path,
173+
"type_": TestNodeTypeEnum.test,
174+
"lineno": "17",
175+
"id_": file_path
176+
+ "\\"
177+
+ "DiscoverySimple"
178+
+ "\\"
179+
+ "test_two",
180+
},
181+
],
182+
"id_": file_path + "\\" + "DiscoverySimple",
183+
}
184+
],
185+
"id_": file_path,
186+
}
187+
],
188+
"id_": os.fsdecode(pathlib.PurePath(TEST_DATA_PATH)),
189+
}
190+
191+
uuid = "some-uuid"
192+
# Define the CWD to be the root of the test data folder.
193+
os.chdir(os.fsdecode(pathlib.PurePath(TEST_DATA_PATH)))
194+
actual = discover_tests(start_dir, pattern, None, uuid)
195+
196+
assert actual["status"] == "success"
197+
assert is_same_tree(actual.get("tests"), expected)
198+
assert "error" not in actual
199+
200+
136201
def test_empty_discovery() -> None:
137202
"""The discover_tests function should return a dictionary with a "success" status, a uuid, no errors, and no test tree
138203
if unittest discovery was performed successfully but no tests were found.

pythonFiles/unittestadapter/discovery.py

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ def discover_tests(
9090
if top_level_dir is None:
9191
top_level_dir = start_dir
9292

93+
# Get abspath of top level directory for build_test_tree.
94+
top_level_dir = os.path.abspath(top_level_dir)
95+
9396
tests, error = build_test_tree(
9497
suite, top_level_dir
9598
) # test tree built successfully here.

0 commit comments

Comments
 (0)