Skip to content

Commit 7b76677

Browse files
committed
Add handling of rp_hierarchy_test_file flag
1 parent f73ae02 commit 7b76677

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

pytest_reportportal/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class AgentConfig:
4040
rp_hierarchy_code: bool
4141
rp_dir_level: int
4242
rp_hierarchy_dirs: bool
43-
rp_display_suite_test_file: bool
43+
rp_hierarchy_test_file: bool
4444
rp_dir_path_separator: str
4545
rp_ignore_attributes: set
4646
rp_is_skipped_an_issue: bool
@@ -77,7 +77,7 @@ def __init__(self, pytest_config: Config) -> None:
7777
self.rp_hierarchy_code = self.find_option(pytest_config, 'rp_hierarchy_code')
7878
self.rp_dir_level = int(self.find_option(pytest_config, 'rp_hierarchy_dirs_level'))
7979
self.rp_hierarchy_dirs = self.find_option(pytest_config, 'rp_hierarchy_dirs')
80-
self.rp_display_suite_test_file = self.find_option(pytest_config, 'rp_display_suite_test_file')
80+
self.rp_hierarchy_test_file = self.find_option(pytest_config, 'rp_hierarchy_test_file')
8181
self.rp_dir_path_separator = self.find_option(pytest_config, 'rp_hierarchy_dir_path_separator')
8282
self.rp_ignore_attributes = set(self.find_option(pytest_config, 'rp_ignore_attributes') or [])
8383
self.rp_is_skipped_an_issue = self.find_option(pytest_config, 'rp_is_skipped_an_issue')

pytest_reportportal/plugin.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,15 @@ def add_shared_option(name, help_str, default=None, action='store'):
504504
default=False,
505505
type='bool',
506506
help='Enables hierarchy for directories')
507-
parser.addini(
508-
'rp_display_suite_test_file',
509-
default=True,
510-
type='bool',
511-
help='Show file name in hierarchy. Depends on rp_hierarchy_dirs_level to get deep enough')
512507
parser.addini(
513508
'rp_hierarchy_dir_path_separator',
514509
default=os.path.sep,
515510
help='Path separator to display directories in test hierarchy')
511+
parser.addini(
512+
'rp_hierarchy_test_file',
513+
default=True,
514+
type='bool',
515+
help='Show file name in hierarchy. Depends on rp_hierarchy_dirs_level to get deep enough')
516516
parser.addini(
517517
'rp_issue_system_url',
518518
default='',

pytest_reportportal/service.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def _get_tree_path(self, item: Item) -> List[Item]:
241241
path.reverse()
242242
return path
243243

244-
def _get_leaf(self, leaf_type: LeafType, parent_item: Optional[Dict[str, Any]], item: Optional[Item],
244+
def _get_leaf(self, leaf_type, parent_item: Optional[Dict[str, Any]], item: Optional[Item],
245245
item_id: Optional[str] = None) -> Dict[str, Any]:
246246
"""Construct a leaf for the itest tree.
247247
@@ -283,27 +283,36 @@ def _build_test_tree(self, session: Session) -> Dict[str, Any]:
283283
current_leaf = children_leafs[leaf]
284284
return test_tree
285285

286-
def _remove_node(self, test_tree: Dict[str, Any], max_dir_level, dir_level, recursive):
287-
parent_leaf = test_tree['parent']
288-
current_item = test_tree['item']
289-
del parent_leaf['children'][current_item]
290-
for item, child_leaf in test_tree['children'].items():
291-
parent_leaf['children'][item] = child_leaf
292-
child_leaf['parent'] = parent_leaf
293-
if recursive:
294-
self._remove_root_dirs(child_leaf, max_dir_level, dir_level + 1)
295-
296-
def _remove_root_dirs(self, test_tree: Dict[str, Any], max_dir_level, dir_level=0) -> None:
286+
def _remove_root_dirs(self, test_tree: Dict[str, Any], max_dir_level: int, dir_level: int = 0) -> None:
297287
if test_tree['type'] == LeafType.ROOT:
298-
for child_leaf in list(test_tree['children'].values()):
288+
items = list(test_tree['children'].items())
289+
for item, child_leaf in items:
299290
self._remove_root_dirs(child_leaf, max_dir_level, 1)
300291
return
301292
if test_tree['type'] == LeafType.DIR and dir_level <= max_dir_level:
302-
self._remove_node(test_tree, max_dir_level, dir_level, recursive=True)
303-
return
304-
if test_tree['type'] == LeafType.FILE and not self._config.rp_display_suite_test_file:
305-
self._remove_node(test_tree, max_dir_level, dir_level, recursive=False)
293+
new_level = dir_level + 1
294+
parent_leaf = test_tree['parent']
295+
current_item = test_tree['item']
296+
del parent_leaf['children'][current_item]
297+
for item, child_leaf in test_tree['children'].items():
298+
parent_leaf['children'][item] = child_leaf
299+
child_leaf['parent'] = parent_leaf
300+
self._remove_root_dirs(child_leaf, max_dir_level, new_level)
301+
302+
def _remove_file_names(self, test_tree: Dict[str, Any]) -> None:
303+
if test_tree['type'] != LeafType.FILE:
304+
items = list(test_tree['children'].items())
305+
for item, child_leaf in items:
306+
self._remove_file_names(child_leaf)
306307
return
308+
if not self._config.rp_hierarchy_test_file:
309+
parent_leaf = test_tree['parent']
310+
current_item = test_tree['item']
311+
del parent_leaf['children'][current_item]
312+
for item, child_leaf in test_tree['children'].items():
313+
parent_leaf['children'][item] = child_leaf
314+
child_leaf['parent'] = parent_leaf
315+
self._remove_file_names(child_leaf)
307316

308317
def _generate_names(self, test_tree: Dict[str, Any]) -> None:
309318
if test_tree['type'] == LeafType.ROOT:
@@ -312,7 +321,7 @@ def _generate_names(self, test_tree: Dict[str, Any]) -> None:
312321
if test_tree['type'] == LeafType.DIR:
313322
test_tree['name'] = test_tree['item'].basename
314323

315-
if test_tree['type'] in [LeafType.CODE, LeafType.FILE]:
324+
if test_tree['type'] in {LeafType.CODE, LeafType.FILE}:
316325
item = test_tree['item']
317326
if isinstance(item, Module):
318327
test_tree['name'] = os.path.split(str(item.fspath))[1]
@@ -322,7 +331,7 @@ def _generate_names(self, test_tree: Dict[str, Any]) -> None:
322331
for item, child_leaf in test_tree['children'].items():
323332
self._generate_names(child_leaf)
324333

325-
def _merge_leaf_types(self, test_tree, leaf_types, separator):
334+
def _merge_leaf_types(self, test_tree: Dict[str, Any], leaf_types: Set, separator: str):
326335
child_items = list(test_tree['children'].items())
327336
if test_tree['type'] not in leaf_types:
328337
for item, child_leaf in child_items:
@@ -335,15 +344,14 @@ def _merge_leaf_types(self, test_tree, leaf_types, separator):
335344
for item, child_leaf in child_items:
336345
parent_leaf['children'][item] = child_leaf
337346
child_leaf['parent'] = parent_leaf
338-
child_leaf['name'] = \
339-
current_name + separator + child_leaf['name']
347+
child_leaf['name'] = current_name + separator + child_leaf['name']
340348
self._merge_leaf_types(child_leaf, leaf_types, separator)
341349

342350
def _merge_dirs(self, test_tree: Dict[str, Any]) -> None:
343-
self._merge_leaf_types(test_tree, [LeafType.DIR], self._config.rp_dir_path_separator)
351+
self._merge_leaf_types(test_tree, {LeafType.DIR}, self._config.rp_dir_path_separator)
344352

345353
def _merge_code(self, test_tree: Dict[str, Any]) -> None:
346-
self._merge_leaf_types(test_tree, [LeafType.CODE, LeafType.FILE], '::')
354+
self._merge_leaf_types(test_tree, {LeafType.CODE, LeafType.FILE}, '::')
347355

348356
def _build_item_paths(self, leaf: Dict[str, Any], path: List[Dict[str, Any]]) -> None:
349357
if 'children' in leaf and len(leaf['children']) > 0:
@@ -363,6 +371,7 @@ def collect_tests(self, session: Session) -> None:
363371
# Create a test tree to be able to apply mutations
364372
test_tree = self._build_test_tree(session)
365373
self._remove_root_dirs(test_tree, self._config.rp_dir_level)
374+
self._remove_file_names(test_tree)
366375
self._generate_names(test_tree)
367376
if not self._config.rp_hierarchy_dirs:
368377
self._merge_dirs(test_tree)

0 commit comments

Comments
 (0)