Skip to content

Commit

Permalink
fix line number detection bug with empty lines
Browse files Browse the repository at this point in the history
Signed-off-by: hirokuni-kitahara <[email protected]>
  • Loading branch information
hirokuni-kitahara committed Oct 10, 2024
1 parent 16bb888 commit 2d418ef
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
5 changes: 0 additions & 5 deletions ansible_risk_insight/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,6 @@ def get_indent_level(x):
if new_block:
if line_buffer:
block_str = ""
if isolated_line_buffer:
block_str += "\n".join(isolated_line_buffer) + "\n"
buffer_begin = 1
block_str += "\n".join(line_buffer)
begin = buffer_begin
end = line_num - 1
Expand All @@ -375,8 +372,6 @@ def get_indent_level(x):
line_buffer.append(line)
if line_buffer:
block_str = ""
if isolated_line_buffer:
block_str += "\n".join(isolated_line_buffer) + "\n"
block_str += "\n".join(line_buffer)
begin = buffer_begin
end = line_num
Expand Down
11 changes: 8 additions & 3 deletions test/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,19 @@ def test_scanner_with_role(type, name):
assert result.detail["executed_file"][0] == "/etc/install.sh"


@pytest.mark.parametrize("type, name", [("playbook", "test/testdata/files/test_line_number.yml")])
def test_scanner_line_number_detection(type, name):
@pytest.mark.parametrize(
"type, name, expected_line_numbers",
[
("playbook", "test/testdata/files/test_line_number.yml", [[6, 13], [14, 18], [20, 23], [29, 33]]),
("playbook", "test/testdata/files/test_line_number2.yml", [[12, 15], [16, 17]]),
],
)
def test_scanner_line_number_detection(type, name, expected_line_numbers):
ari_result, _ = _scan(type=type, name=name, playbook_only=True)
assert ari_result
playbook_result = ari_result.playbook(path=name)
assert playbook_result
task_results = playbook_result.tasks()
expected_line_numbers = [[6, 13], [14, 18], [20, 23], [29, 33]]
for i, task_result in enumerate(task_results.nodes):
assert task_result.node.spec.line_num_in_file
detected = task_result.node.spec.line_num_in_file
Expand Down
17 changes: 17 additions & 0 deletions test/testdata/files/test_line_number2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- hosts: localhost
gather_facts: no

tags:
- test

collections:
- community.general

tasks:
- name: Task name here
import_role:
name: abc

- import_role:
name: def

0 comments on commit 2d418ef

Please sign in to comment.