|
18 | 18 | expand_path_vars, |
19 | 19 | expand_paths_vars, |
20 | 20 | find_project_root, |
| 21 | + kind_from_path, |
21 | 22 | normpath, |
22 | 23 | normpath_path, |
23 | 24 | ) |
@@ -543,3 +544,55 @@ def test_bug_2513( |
543 | 544 | results = Runner(filename, rules=default_rules_collection).run() |
544 | 545 | assert len(results) == 1 |
545 | 546 | assert results[0].rule.id == "name" |
| 547 | + |
| 548 | + |
| 549 | +def test_kind_from_path_parent_collision(tmp_path: Path) -> None: |
| 550 | + """Verify that a parent directory named 'tasks' doesn't cause false positives. |
| 551 | +
|
| 552 | + See https://github.com/ansible/ansible-lint/issues/4763 |
| 553 | + """ |
| 554 | + tasks_parent = tmp_path / "tasks" |
| 555 | + project_dir = tasks_parent / "my_project" |
| 556 | + project_dir.mkdir(parents=True) |
| 557 | + |
| 558 | + (project_dir / ".git").mkdir() |
| 559 | + |
| 560 | + playbook_path = project_dir / "site.yml" |
| 561 | + playbook_path.touch() |
| 562 | + |
| 563 | + kind = kind_from_path(playbook_path) |
| 564 | + |
| 565 | + assert kind != "tasks", f"File {playbook_path} should not be identified as 'tasks'" |
| 566 | + |
| 567 | + |
| 568 | +def test_kind_from_path_valid_tasks(tmp_path: Path) -> None: |
| 569 | + """Verify that legitimate tasks directories are still correctly identified.""" |
| 570 | + project_dir = tmp_path / "my_project" |
| 571 | + project_dir.mkdir() |
| 572 | + (project_dir / ".git").mkdir() |
| 573 | + |
| 574 | + task_dir = project_dir / "tasks" |
| 575 | + task_dir.mkdir() |
| 576 | + task_path = task_dir / "main.yml" |
| 577 | + task_path.touch() |
| 578 | + |
| 579 | + kind = kind_from_path(task_path) |
| 580 | + |
| 581 | + assert kind == "tasks", f"File {task_path} should be identified as 'tasks'" |
| 582 | + |
| 583 | + |
| 584 | +def test_kind_from_path_outside_project_root(tmp_path: Path) -> None: |
| 585 | + """Verify fallback to absolute path when file is outside the project root. |
| 586 | +
|
| 587 | + This triggers the except block in kind_from_path |
| 588 | + """ |
| 589 | + project_dir = tmp_path / "actual_project" |
| 590 | + project_dir.mkdir() |
| 591 | + (project_dir / ".git").mkdir() |
| 592 | + |
| 593 | + outside_file = tmp_path / "external_file.yml" |
| 594 | + outside_file.touch() |
| 595 | + |
| 596 | + kind = kind_from_path(outside_file) |
| 597 | + |
| 598 | + assert kind == "yaml" |
0 commit comments