|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from dev_tools.check_rule_has_tag import find_invalid_files, find_rule_calls, rule_has_tag |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from pyfakefs.fake_filesystem import FakeFilesystem |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + ("content", "expected_rule_count"), |
| 16 | + [ |
| 17 | + ('py_venv(name = "venv")', 1), |
| 18 | + ('other_rule(name = "x")', 0), |
| 19 | + ( |
| 20 | + """ |
| 21 | +py_venv(name = "venv") |
| 22 | +pkg_tar(name = "archive") |
| 23 | +py_venv( |
| 24 | + name = "second", |
| 25 | +) |
| 26 | +""", |
| 27 | + 2, |
| 28 | + ), |
| 29 | + ( |
| 30 | + """ |
| 31 | +py_venv( |
| 32 | + name = name + "_venv", |
| 33 | + deps = [":{}_foo".format(name)], |
| 34 | + package_collisions = "ignore", |
| 35 | + tags = ["manual"], |
| 36 | +) |
| 37 | +""", |
| 38 | + 1, |
| 39 | + ), |
| 40 | + ], |
| 41 | +) |
| 42 | +def test_find_rule_calls_for_content_should_return_expected_rule_count(content: str, expected_rule_count: int) -> None: |
| 43 | + assert len(find_rule_calls(content, "py_venv")) == expected_rule_count |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize( |
| 47 | + ("rule_body", "tag"), |
| 48 | + [ |
| 49 | + ('name = "venv", tags = ["manual"]', "manual"), |
| 50 | + ('name = "venv", tags = ["manual", "no-remote"]', "manual"), |
| 51 | + ('name = "venv", tags = [\n "manual",\n]', "manual"), |
| 52 | + ('name = "archive", tags = ["no-remote"]', "no-remote"), |
| 53 | + ], |
| 54 | +) |
| 55 | +def test_rule_has_tag_for_matching_tag_should_return_true(rule_body: str, tag: str) -> None: |
| 56 | + assert rule_has_tag(rule_body, tag) is True |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize( |
| 60 | + ("rule_body", "tag"), |
| 61 | + [ |
| 62 | + ('name = "venv"', "manual"), |
| 63 | + ('name = "venv", tags = ["not-manual"]', "manual"), |
| 64 | + ('name = "venv", tags = [] # manual', "manual"), |
| 65 | + ('name = "archive", tags = ["remote"]', "no-remote"), |
| 66 | + ('name = "thing", package_collisions = "ignore"', "manual"), |
| 67 | + ], |
| 68 | +) |
| 69 | +def test_rule_has_tag_for_non_matching_tag_should_return_false(rule_body: str, tag: str) -> None: |
| 70 | + assert rule_has_tag(rule_body, tag) is False |
| 71 | + |
| 72 | + |
| 73 | +def test_find_invalid_files_for_rule_without_tag_should_return_file(fs: FakeFilesystem) -> None: |
| 74 | + fs.create_file( |
| 75 | + Path("repo/BUILD.bazel"), |
| 76 | + contents=""" |
| 77 | +py_venv( |
| 78 | + name = "bad", |
| 79 | +) |
| 80 | +py_venv( |
| 81 | + name = "good", |
| 82 | + tags = ["manual"], |
| 83 | +) |
| 84 | +""", |
| 85 | + ) |
| 86 | + |
| 87 | + assert find_invalid_files([Path("repo/BUILD.bazel")], "py_venv", "manual") == [Path("repo/BUILD.bazel")] |
| 88 | + |
| 89 | + |
| 90 | +def test_find_invalid_files_for_file_without_target_rule_should_return_empty_list(fs: FakeFilesystem) -> None: |
| 91 | + fs.create_file( |
| 92 | + Path("repo/BUILD.bazel"), |
| 93 | + contents=""" |
| 94 | +pkg_tar( |
| 95 | + name = "archive", |
| 96 | + tags = ["no-remote"], |
| 97 | +) |
| 98 | +""", |
| 99 | + ) |
| 100 | + |
| 101 | + assert find_invalid_files([Path("repo/BUILD.bazel")], "py_venv", "manual") == [] |
| 102 | + |
| 103 | + |
| 104 | +def test_find_invalid_files_for_rule_with_nested_parentheses_should_return_empty_list(fs: FakeFilesystem) -> None: |
| 105 | + fs.create_file( |
| 106 | + Path("repo/BUILD.bazel"), |
| 107 | + contents=""" |
| 108 | +py_venv( |
| 109 | + name = name + "_venv", |
| 110 | + deps = [":{}_foo".format(name)], |
| 111 | + package_collisions = "ignore", |
| 112 | + tags = ["manual"], |
| 113 | +) |
| 114 | +""", |
| 115 | + ) |
| 116 | + |
| 117 | + assert find_invalid_files([Path("repo/BUILD.bazel")], "py_venv", "manual") == [] |
0 commit comments