Skip to content

Commit 44a2a40

Browse files
Added a builtin function for setting a CLI test's basename using ancestor directories.
1 parent 8d2d937 commit 44a2a40

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

edq/testing/cli.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
If a test class implements a class method with the signature `get_test_basename(cls, path: str) -> str`,
1616
then this method will be called to create the base name for the test case at the given path.
17+
Otherwise, the file's basename will be used.
1718
1819
The expected output or any argument can reference the test's current temp or data dirs with `__TEMP_DIR__()` or `__DATA_DIR__()`, respectively.
1920
An optional slash-separated path can be used as an argument to reference a path within those base directories.
@@ -282,6 +283,32 @@ def replace_path_pattern(text: str, key: str, target_dir: str, normalize_path: b
282283

283284
return text
284285

286+
def compute_ancestor_basename(path: str, cli_tests_dir: str) -> str:
287+
"""
288+
Get the test's name based off of its filename and location.
289+
A useful fuction to use in get_test_basename().
290+
"""
291+
292+
path = os.path.abspath(path)
293+
294+
name = os.path.splitext(os.path.basename(path))[0]
295+
296+
# Clean drive identifiers (for Windows).
297+
cli_tests_dir_path = os.path.splitdrive(os.path.abspath(cli_tests_dir))[1]
298+
path = os.path.splitdrive(path)[1]
299+
300+
ancestors = os.path.dirname(path).replace(cli_tests_dir_path, '')
301+
prefix = ancestors.replace(os.sep, '_')
302+
303+
if (prefix.startswith('_')):
304+
prefix = prefix.replace('_', '', 1)
305+
306+
if (len(prefix) > 0):
307+
name = f"{prefix}_{name}"
308+
309+
return name
310+
311+
285312
def _get_test_method(test_name: str, path: str, data_dir: str) -> typing.Callable:
286313
""" Get a test method that represents the test case at the given path. """
287314

0 commit comments

Comments
 (0)