From 2d418efe2f472c1d246da3f8f9acdefac3764174 Mon Sep 17 00:00:00 2001 From: hirokuni-kitahara Date: Thu, 10 Oct 2024 15:24:56 +0900 Subject: [PATCH] fix line number detection bug with empty lines Signed-off-by: hirokuni-kitahara --- ansible_risk_insight/finder.py | 5 ----- test/test_scanner.py | 11 ++++++++--- test/testdata/files/test_line_number2.yml | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 test/testdata/files/test_line_number2.yml diff --git a/ansible_risk_insight/finder.py b/ansible_risk_insight/finder.py index d25fb070..53b50279 100644 --- a/ansible_risk_insight/finder.py +++ b/ansible_risk_insight/finder.py @@ -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 @@ -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 diff --git a/test/test_scanner.py b/test/test_scanner.py index 161053de..1d229c25 100644 --- a/test/test_scanner.py +++ b/test/test_scanner.py @@ -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 diff --git a/test/testdata/files/test_line_number2.yml b/test/testdata/files/test_line_number2.yml new file mode 100644 index 00000000..10ec51d4 --- /dev/null +++ b/test/testdata/files/test_line_number2.yml @@ -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