Skip to content

Commit e259b7c

Browse files
authored
Helper function for locating assets (#43127)
* offer up a helper function for usage by the ai-voicelive team
1 parent e9396e1 commit e259b7c

File tree

1 file changed

+57
-1
lines changed
  • eng/tools/azure-sdk-tools/devtools_testutils

1 file changed

+57
-1
lines changed

eng/tools/azure-sdk-tools/devtools_testutils/helpers.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import random
99
import string
1010
import sys
11-
from urllib3 import PoolManager, Retry
11+
from pathlib import Path
1212

13+
from urllib3 import PoolManager, Retry
1314
from .config import TestConfig
1415

1516

@@ -18,6 +19,61 @@
1819
this = sys.modules[__name__]
1920
this.recording_ids = {}
2021

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}")
2177

2278
def get_http_client(**kwargs):
2379
"""Returns a `urllib3` client that provides the test proxy's self-signed certificate if it's available.

0 commit comments

Comments
 (0)