|
2 | 2 | # Licensed under the MIT License. |
3 | 3 | import json |
4 | 4 | import os |
5 | | -import pathlib |
6 | 5 | import shutil |
7 | 6 | import sys |
8 | 7 | from typing import Any, Dict, List, Optional |
9 | 8 |
|
10 | 9 | import pytest |
11 | 10 |
|
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 |
14 | 12 |
|
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 |
19 | 14 |
|
20 | 15 |
|
21 | 16 | @pytest.mark.skipif( |
@@ -36,12 +31,14 @@ def test_import_error(tmp_path): |
36 | 31 | # Saving some files as .txt to avoid that file displaying a syntax error for |
37 | 32 | # the extension as a whole. Instead, rename it before running this test |
38 | 33 | # 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" |
40 | 35 | temp_dir = tmp_path / "temp_data" |
41 | 36 | temp_dir.mkdir() |
42 | 37 | p = temp_dir / "error_pytest_import.py" |
43 | 38 | 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 | + ) |
45 | 42 | assert actual |
46 | 43 | actual_list: List[Dict[str, Any]] = actual |
47 | 44 | if actual_list is not None: |
@@ -51,7 +48,7 @@ def test_import_error(tmp_path): |
51 | 48 | item in actual_item.keys() for item in ("status", "cwd", "error") |
52 | 49 | ) |
53 | 50 | 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) |
55 | 52 |
|
56 | 53 | # Ensure that 'error' is a list and then check its length |
57 | 54 | error_content = actual_item.get("error") |
@@ -81,12 +78,12 @@ def test_syntax_error(tmp_path): |
81 | 78 | # Saving some files as .txt to avoid that file displaying a syntax error for |
82 | 79 | # the extension as a whole. Instead, rename it before running this test |
83 | 80 | # 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" |
85 | 82 | temp_dir = tmp_path / "temp_data" |
86 | 83 | temp_dir.mkdir() |
87 | 84 | p = temp_dir / "error_syntax_discovery.py" |
88 | 85 | shutil.copyfile(file_path, p) |
89 | | - actual = runner(["--collect-only", os.fspath(p)]) |
| 86 | + actual = helpers.runner(["--collect-only", os.fspath(p)]) |
90 | 87 | assert actual |
91 | 88 | actual_list: List[Dict[str, Any]] = actual |
92 | 89 | if actual_list is not None: |
@@ -96,7 +93,7 @@ def test_syntax_error(tmp_path): |
96 | 93 | item in actual_item.keys() for item in ("status", "cwd", "error") |
97 | 94 | ) |
98 | 95 | 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) |
100 | 97 |
|
101 | 98 | # Ensure that 'error' is a list and then check its length |
102 | 99 | error_content = actual_item.get("error") |
@@ -114,7 +111,7 @@ def test_parameterized_error_collect(): |
114 | 111 | The json should still be returned but the errors list should be present. |
115 | 112 | """ |
116 | 113 | file_path_str = "error_parametrize_discovery.py" |
117 | | - actual = runner(["--collect-only", file_path_str]) |
| 114 | + actual = helpers.runner(["--collect-only", file_path_str]) |
118 | 115 | assert actual |
119 | 116 | actual_list: List[Dict[str, Any]] = actual |
120 | 117 | if actual_list is not None: |
@@ -124,7 +121,7 @@ def test_parameterized_error_collect(): |
124 | 121 | item in actual_item.keys() for item in ("status", "cwd", "error") |
125 | 122 | ) |
126 | 123 | 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) |
128 | 125 |
|
129 | 126 | # Ensure that 'error' is a list and then check its length |
130 | 127 | error_content = actual_item.get("error") |
@@ -196,10 +193,10 @@ def test_pytest_collect(file, expected_const): |
196 | 193 | file -- a string with the file or folder to run pytest discovery on. |
197 | 194 | expected_const -- the expected output from running pytest discovery on the file. |
198 | 195 | """ |
199 | | - actual = runner( |
| 196 | + actual = helpers.runner( |
200 | 197 | [ |
201 | 198 | "--collect-only", |
202 | | - os.fspath(TEST_DATA_PATH / file), |
| 199 | + os.fspath(helpers.TEST_DATA_PATH / file), |
203 | 200 | ] |
204 | 201 | ) |
205 | 202 |
|
@@ -210,23 +207,25 @@ def test_pytest_collect(file, expected_const): |
210 | 207 | actual_item = actual_list.pop(0) |
211 | 208 | assert all(item in actual_item.keys() for item in ("status", "cwd", "error")) |
212 | 209 | 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)}" |
215 | 214 |
|
216 | 215 |
|
217 | 216 | def test_symlink_root_dir(): |
218 | 217 | """ |
219 | 218 | Test to test pytest discovery with the command line arg --rootdir specified as a symlink path. |
220 | 219 | Discovery should succeed and testids should be relative to the symlinked root directory. |
221 | 220 | """ |
222 | | - with create_symlink(TEST_DATA_PATH, "root", "symlink_folder") as ( |
| 221 | + with helpers.create_symlink(helpers.TEST_DATA_PATH, "root", "symlink_folder") as ( |
223 | 222 | source, |
224 | 223 | destination, |
225 | 224 | ): |
226 | 225 | assert destination.is_symlink() |
227 | 226 |
|
228 | 227 | # 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( |
230 | 229 | ["--collect-only", f"--rootdir={os.fspath(destination)}"], source |
231 | 230 | ) |
232 | 231 | expected = expected_discovery_test_output.symlink_expected_discovery_output |
@@ -258,13 +257,13 @@ def test_pytest_root_dir(): |
258 | 257 | Test to test pytest discovery with the command line arg --rootdir specified to be a subfolder |
259 | 258 | of the workspace root. Discovery should succeed and testids should be relative to workspace root. |
260 | 259 | """ |
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( |
263 | 262 | [ |
264 | 263 | "--collect-only", |
265 | 264 | rd, |
266 | 265 | ], |
267 | | - TEST_DATA_PATH / "root", |
| 266 | + helpers.TEST_DATA_PATH / "root", |
268 | 267 | ) |
269 | 268 | assert actual |
270 | 269 | actual_list: List[Dict[str, Any]] = actual |
@@ -273,24 +272,24 @@ def test_pytest_root_dir(): |
273 | 272 | actual_item = actual_list.pop(0) |
274 | 273 | assert all(item in actual_item.keys() for item in ("status", "cwd", "error")) |
275 | 274 | 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") |
277 | 276 | assert is_same_tree( |
278 | 277 | actual_item.get("tests"), |
279 | 278 | 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)}" |
281 | 280 |
|
282 | 281 |
|
283 | 282 | def test_pytest_config_file(): |
284 | 283 | """ |
285 | 284 | Test to test pytest discovery with the command line arg -c with a specified config file which |
286 | 285 | changes the workspace root. Discovery should succeed and testids should be relative to workspace root. |
287 | 286 | """ |
288 | | - actual = runner_with_cwd( |
| 287 | + actual = helpers.runner_with_cwd( |
289 | 288 | [ |
290 | 289 | "--collect-only", |
291 | 290 | "tests/", |
292 | 291 | ], |
293 | | - TEST_DATA_PATH / "root", |
| 292 | + helpers.TEST_DATA_PATH / "root", |
294 | 293 | ) |
295 | 294 | assert actual |
296 | 295 | actual_list: List[Dict[str, Any]] = actual |
@@ -299,8 +298,8 @@ def test_pytest_config_file(): |
299 | 298 | actual_item = actual_list.pop(0) |
300 | 299 | assert all(item in actual_item.keys() for item in ("status", "cwd", "error")) |
301 | 300 | 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") |
303 | 302 | assert is_same_tree( |
304 | 303 | actual_item.get("tests"), |
305 | 304 | 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)}" |