Skip to content

Commit

Permalink
fix: pop parent node after adding include role tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
haidaraM committed Jan 9, 2022
1 parent 6fe0d28 commit 940c585
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
5 changes: 4 additions & 1 deletion ansibleplaybookgrapher/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _include_tasks_in_blocks(self, current_play: Play, parent_nodes: List[Compos
block_list = []
else:
if self.include_role_tasks:
# If we have an include_role, and we want to include role tasks, the parent node now becomes
# If we have an include_role, and we want to include its tasks, the parent node now becomes
# the role.
parent_nodes.append(role_node)

Expand Down Expand Up @@ -288,6 +288,9 @@ def _include_tasks_in_blocks(self, current_play: Play, parent_nodes: List[Compos
for b in block_list: # loop through the blocks inside the included tasks or role
self._include_tasks_in_blocks(current_play=current_play, parent_nodes=parent_nodes, block=b,
play_vars=task_vars, node_type=node_type)
if self.include_role_tasks and isinstance(task_or_block, IncludeRole):
# We remove the parent node we have added if we included some tasks from a role
parent_nodes.pop()
else:
if (len(parent_nodes) > 1 and # 1
not has_role_parent(task_or_block) and # 2
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/include_role.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
msg: "Debug 1"
when: ansible_os == "ubuntu"

- name: (2) Include role
- name: (2) First Include role
include_role:
name: fake_role

- name: (3) Debug 2
debug:
msg: "Debug 2"

- name: (4) Include role 2
- name: (4) Second Include role
when: x is not defined
include_role:
name: display_some_facts

- name: Include role with loop
- name: (5) Third Include role (with loop)
include_role:
name: '{{ item }}'
loop:
Expand Down
8 changes: 7 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def test_include_role_parsing(grapher_cli: PlaybookGrapherCLI, display: Display)
assert isinstance(include_role_2, RoleNode)
assert len(include_role_2.tasks) == 3

# third include_role
include_role_3 = tasks[4].destination
assert isinstance(include_role_3, RoleNode)
assert len(include_role_3.tasks) == 0, "We don't support adding tasks from include_role with loop"


@pytest.mark.parametrize('grapher_cli', [["with_block.yml"]], indirect=True)
def test_block_parsing(grapher_cli: PlaybookGrapherCLI, display: Display):
Expand All @@ -75,7 +80,8 @@ def test_block_parsing(grapher_cli: PlaybookGrapherCLI, display: Display):
total_pre_tasks = get_all_tasks(pre_tasks)
total_tasks = get_all_tasks(tasks)
total_post_tasks = get_all_tasks(post_tasks)
assert len(total_pre_tasks) == 4, f"The play should contain 4 pre tasks but we found {len(total_pre_tasks)} pre task(s)"
assert len(
total_pre_tasks) == 4, f"The play should contain 4 pre tasks but we found {len(total_pre_tasks)} pre task(s)"
assert len(total_tasks) == 7, f"The play should contain 3 tasks but we found {len(total_tasks)} task(s)"
assert len(
total_post_tasks) == 2, f"The play should contain 2 post tasks but we found {len(total_post_tasks)} post task(s)"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_playbook_grapher.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test_include_role(request, include_role_tasks_option, expected_tasks_number)
additional_args=[include_role_tasks_option])

_common_tests(svg_path=svg_path, playbook_path=playbook_path, plays_number=1, tasks_number=expected_tasks_number,
roles_number=2)
roles_number=3)


def test_with_block(request):
Expand Down

0 comments on commit 940c585

Please sign in to comment.