22# Licensed under the MIT License.
33import json
44import os
5- import pathlib
65import shutil
76import sys
87from typing import Any , Dict , List , Optional
98
109import pytest
1110
12- script_dir = pathlib .Path (__file__ ).parent .parent
13- sys .path .append (os .fspath (script_dir ))
11+ from tests .tree_comparison_helper import is_same_tree # noqa: E402
1412
15- from tests .tree_comparison_helper import is_same_tree
16-
17- from . import expected_discovery_test_output
18- from .helpers import TEST_DATA_PATH , runner , runner_with_cwd , create_symlink
13+ from . import expected_discovery_test_output , helpers # noqa: E402
1914
2015
2116@pytest .mark .skipif (
@@ -36,12 +31,14 @@ def test_import_error(tmp_path):
3631 # Saving some files as .txt to avoid that file displaying a syntax error for
3732 # the extension as a whole. Instead, rename it before running this test
3833 # in order to test the error handling.
39- file_path = TEST_DATA_PATH / "error_pytest_import.txt"
34+ file_path = helpers . TEST_DATA_PATH / "error_pytest_import.txt"
4035 temp_dir = tmp_path / "temp_data"
4136 temp_dir .mkdir ()
4237 p = temp_dir / "error_pytest_import.py"
4338 shutil .copyfile (file_path , p )
44- actual : Optional [List [Dict [str , Any ]]] = runner (["--collect-only" , os .fspath (p )])
39+ actual : Optional [List [Dict [str , Any ]]] = helpers .runner (
40+ ["--collect-only" , os .fspath (p )]
41+ )
4542 assert actual
4643 actual_list : List [Dict [str , Any ]] = actual
4744 if actual_list is not None :
@@ -51,7 +48,7 @@ def test_import_error(tmp_path):
5148 item in actual_item .keys () for item in ("status" , "cwd" , "error" )
5249 )
5350 assert actual_item .get ("status" ) == "error"
54- assert actual_item .get ("cwd" ) == os .fspath (TEST_DATA_PATH )
51+ assert actual_item .get ("cwd" ) == os .fspath (helpers . TEST_DATA_PATH )
5552
5653 # Ensure that 'error' is a list and then check its length
5754 error_content = actual_item .get ("error" )
@@ -81,12 +78,12 @@ def test_syntax_error(tmp_path):
8178 # Saving some files as .txt to avoid that file displaying a syntax error for
8279 # the extension as a whole. Instead, rename it before running this test
8380 # in order to test the error handling.
84- file_path = TEST_DATA_PATH / "error_syntax_discovery.txt"
81+ file_path = helpers . TEST_DATA_PATH / "error_syntax_discovery.txt"
8582 temp_dir = tmp_path / "temp_data"
8683 temp_dir .mkdir ()
8784 p = temp_dir / "error_syntax_discovery.py"
8885 shutil .copyfile (file_path , p )
89- actual = runner (["--collect-only" , os .fspath (p )])
86+ actual = helpers . runner (["--collect-only" , os .fspath (p )])
9087 assert actual
9188 actual_list : List [Dict [str , Any ]] = actual
9289 if actual_list is not None :
@@ -96,7 +93,7 @@ def test_syntax_error(tmp_path):
9693 item in actual_item .keys () for item in ("status" , "cwd" , "error" )
9794 )
9895 assert actual_item .get ("status" ) == "error"
99- assert actual_item .get ("cwd" ) == os .fspath (TEST_DATA_PATH )
96+ assert actual_item .get ("cwd" ) == os .fspath (helpers . TEST_DATA_PATH )
10097
10198 # Ensure that 'error' is a list and then check its length
10299 error_content = actual_item .get ("error" )
@@ -114,7 +111,7 @@ def test_parameterized_error_collect():
114111 The json should still be returned but the errors list should be present.
115112 """
116113 file_path_str = "error_parametrize_discovery.py"
117- actual = runner (["--collect-only" , file_path_str ])
114+ actual = helpers . runner (["--collect-only" , file_path_str ])
118115 assert actual
119116 actual_list : List [Dict [str , Any ]] = actual
120117 if actual_list is not None :
@@ -124,7 +121,7 @@ def test_parameterized_error_collect():
124121 item in actual_item .keys () for item in ("status" , "cwd" , "error" )
125122 )
126123 assert actual_item .get ("status" ) == "error"
127- assert actual_item .get ("cwd" ) == os .fspath (TEST_DATA_PATH )
124+ assert actual_item .get ("cwd" ) == os .fspath (helpers . TEST_DATA_PATH )
128125
129126 # Ensure that 'error' is a list and then check its length
130127 error_content = actual_item .get ("error" )
@@ -196,10 +193,10 @@ def test_pytest_collect(file, expected_const):
196193 file -- a string with the file or folder to run pytest discovery on.
197194 expected_const -- the expected output from running pytest discovery on the file.
198195 """
199- actual = runner (
196+ actual = helpers . runner (
200197 [
201198 "--collect-only" ,
202- os .fspath (TEST_DATA_PATH / file ),
199+ os .fspath (helpers . TEST_DATA_PATH / file ),
203200 ]
204201 )
205202
@@ -210,23 +207,25 @@ def test_pytest_collect(file, expected_const):
210207 actual_item = actual_list .pop (0 )
211208 assert all (item in actual_item .keys () for item in ("status" , "cwd" , "error" ))
212209 assert actual_item .get ("status" ) == "success"
213- assert actual_item .get ("cwd" ) == os .fspath (TEST_DATA_PATH )
214- assert is_same_tree (actual_item .get ("tests" ), expected_const )
210+ assert actual_item .get ("cwd" ) == os .fspath (helpers .TEST_DATA_PATH )
211+ assert not is_same_tree (
212+ actual_item .get ("tests" ), expected_const
213+ ), f"Tests tree does not match expected value. \n Expected: { json .dumps (expected_const , indent = 4 )} . \n Actual: { json .dumps (actual_item .get ('tests' ), indent = 4 )} "
215214
216215
217216def test_symlink_root_dir ():
218217 """
219218 Test to test pytest discovery with the command line arg --rootdir specified as a symlink path.
220219 Discovery should succeed and testids should be relative to the symlinked root directory.
221220 """
222- with create_symlink (TEST_DATA_PATH , "root" , "symlink_folder" ) as (
221+ with helpers . create_symlink (helpers . TEST_DATA_PATH , "root" , "symlink_folder" ) as (
223222 source ,
224223 destination ,
225224 ):
226225 assert destination .is_symlink ()
227226
228227 # Run pytest with the cwd being the resolved symlink path (as it will be when we run the subprocess from node).
229- actual = runner_with_cwd (
228+ actual = helpers . runner_with_cwd (
230229 ["--collect-only" , f"--rootdir={ os .fspath (destination )} " ], source
231230 )
232231 expected = expected_discovery_test_output .symlink_expected_discovery_output
@@ -258,13 +257,13 @@ def test_pytest_root_dir():
258257 Test to test pytest discovery with the command line arg --rootdir specified to be a subfolder
259258 of the workspace root. Discovery should succeed and testids should be relative to workspace root.
260259 """
261- rd = f"--rootdir={ TEST_DATA_PATH / 'root' / 'tests' } "
262- actual = runner_with_cwd (
260+ rd = f"--rootdir={ helpers . TEST_DATA_PATH / 'root' / 'tests' } "
261+ actual = helpers . runner_with_cwd (
263262 [
264263 "--collect-only" ,
265264 rd ,
266265 ],
267- TEST_DATA_PATH / "root" ,
266+ helpers . TEST_DATA_PATH / "root" ,
268267 )
269268 assert actual
270269 actual_list : List [Dict [str , Any ]] = actual
@@ -273,24 +272,24 @@ def test_pytest_root_dir():
273272 actual_item = actual_list .pop (0 )
274273 assert all (item in actual_item .keys () for item in ("status" , "cwd" , "error" ))
275274 assert actual_item .get ("status" ) == "success"
276- assert actual_item .get ("cwd" ) == os .fspath (TEST_DATA_PATH / "root" )
275+ assert actual_item .get ("cwd" ) == os .fspath (helpers . TEST_DATA_PATH / "root" )
277276 assert is_same_tree (
278277 actual_item .get ("tests" ),
279278 expected_discovery_test_output .root_with_config_expected_output ,
280- )
279+ ), f"Tests tree does not match expected value. \n Expected: { json . dumps ( expected_discovery_test_output . root_with_config_expected_output , indent = 4 ) } . \n Actual: { json . dumps ( actual_item . get ( 'tests' ), indent = 4 ) } "
281280
282281
283282def test_pytest_config_file ():
284283 """
285284 Test to test pytest discovery with the command line arg -c with a specified config file which
286285 changes the workspace root. Discovery should succeed and testids should be relative to workspace root.
287286 """
288- actual = runner_with_cwd (
287+ actual = helpers . runner_with_cwd (
289288 [
290289 "--collect-only" ,
291290 "tests/" ,
292291 ],
293- TEST_DATA_PATH / "root" ,
292+ helpers . TEST_DATA_PATH / "root" ,
294293 )
295294 assert actual
296295 actual_list : List [Dict [str , Any ]] = actual
@@ -299,8 +298,8 @@ def test_pytest_config_file():
299298 actual_item = actual_list .pop (0 )
300299 assert all (item in actual_item .keys () for item in ("status" , "cwd" , "error" ))
301300 assert actual_item .get ("status" ) == "success"
302- assert actual_item .get ("cwd" ) == os .fspath (TEST_DATA_PATH / "root" )
301+ assert actual_item .get ("cwd" ) == os .fspath (helpers . TEST_DATA_PATH / "root" )
303302 assert is_same_tree (
304303 actual_item .get ("tests" ),
305304 expected_discovery_test_output .root_with_config_expected_output ,
306- )
305+ ), f"Tests tree does not match expected value. \n Expected: { json . dumps ( expected_discovery_test_output . root_with_config_expected_output , indent = 4 ) } . \n Actual: { json . dumps ( actual_item . get ( 'tests' ), indent = 4 ) } "
0 commit comments