|
8 | 8 | import random
|
9 | 9 | import string
|
10 | 10 | import sys
|
11 |
| -from urllib3 import PoolManager, Retry |
| 11 | +from pathlib import Path |
12 | 12 |
|
| 13 | +from urllib3 import PoolManager, Retry |
13 | 14 | from .config import TestConfig
|
14 | 15 |
|
15 | 16 |
|
|
18 | 19 | this = sys.modules[__name__]
|
19 | 20 | this.recording_ids = {}
|
20 | 21 |
|
| 22 | +def locate_assets(current_test_file: str) -> str: |
| 23 | + """Locate the test assets directory for the targeted testfile. |
| 24 | +
|
| 25 | + :returns: Absolute path to the test assets directory + assets prefix path |
| 26 | + :rtype: str |
| 27 | + :raises FileNotFoundError: If assets.json, breadcrumb files, or assets files cannot be found. |
| 28 | + """ |
| 29 | + # Get the directory of the current test file |
| 30 | + test_file_path = Path(current_test_file) |
| 31 | + current_dir = test_file_path.parent |
| 32 | + |
| 33 | + # Search upward for assets.json and repo root |
| 34 | + assets_json_path = None |
| 35 | + repo_root = None |
| 36 | + search_dir = current_dir |
| 37 | + while search_dir != search_dir.parent: # Stop at filesystem root |
| 38 | + # early stop at repo root |
| 39 | + if any((search_dir / indicator).exists() for indicator in [".git", "eng"]): |
| 40 | + repo_root = search_dir |
| 41 | + break |
| 42 | + |
| 43 | + candidate = search_dir / "assets.json" |
| 44 | + if candidate.exists(): |
| 45 | + assets_json_path = candidate |
| 46 | + search_dir = search_dir.parent |
| 47 | + |
| 48 | + if not assets_json_path: |
| 49 | + raise FileNotFoundError(f"Could not find assets.json starting from {current_dir}") |
| 50 | + |
| 51 | + if not repo_root: |
| 52 | + raise FileNotFoundError(f"Could not find repository root starting from {assets_json_path.parent}") |
| 53 | + |
| 54 | + # Look for breadcrumb files in .assets/breadcrumb/ |
| 55 | + breadcrumb_dir = repo_root / ".assets" / "breadcrumb" |
| 56 | + if not breadcrumb_dir.exists(): |
| 57 | + raise FileNotFoundError(f"Breadcrumb directory not found at {breadcrumb_dir}") |
| 58 | + |
| 59 | + # Search for breadcrumb file that matches our asset path |
| 60 | + relative_asset_path = str(assets_json_path.parent.relative_to(repo_root)).replace(os.sep, "/") |
| 61 | + |
| 62 | + for breadcrumb_file in breadcrumb_dir.iterdir(): |
| 63 | + if breadcrumb_file.is_file(): |
| 64 | + try: |
| 65 | + content = breadcrumb_file.read_text().strip() |
| 66 | + # Check if this breadcrumb file contains our asset path |
| 67 | + if relative_asset_path in content: |
| 68 | + # The breadcrumb file name should correspond to the local asset directory |
| 69 | + asset_dir_name = breadcrumb_file.stem |
| 70 | + local_assets_path = repo_root / ".assets" / asset_dir_name |
| 71 | + if local_assets_path.exists(): |
| 72 | + return str(local_assets_path.resolve()) |
| 73 | + except (IOError, UnicodeDecodeError): |
| 74 | + continue # Skip files that can't be read |
| 75 | + |
| 76 | + raise FileNotFoundError(f"No matching breadcrumb file found for asset path {relative_asset_path}") |
21 | 77 |
|
22 | 78 | def get_http_client(**kwargs):
|
23 | 79 | """Returns a `urllib3` client that provides the test proxy's self-signed certificate if it's available.
|
|
0 commit comments