|
| 1 | +""" |
| 2 | +
|
| 3 | +Copyright (c) 2023 Aiven Ltd |
| 4 | +See LICENSE for details |
| 5 | +
|
| 6 | +""" |
| 7 | +from astacus.common.snapshot import SnapshotGroup |
| 8 | +from astacus.node.snapshot_groups import CompiledGroup, CompiledGroups, glob_compile |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +import os |
| 12 | + |
| 13 | +POSITIVE_TEST_CASES: list[tuple[Path, str]] = [ |
| 14 | + (Path("foo"), "foo"), |
| 15 | + (Path("foo"), "*"), |
| 16 | + (Path("foo/bar"), "*/bar"), |
| 17 | + (Path("foo"), "**"), |
| 18 | + (Path("foo/bar"), "**"), |
| 19 | + (Path("foo/bar/baz"), "**/*"), |
| 20 | + (Path("foo/bar"), "**/*"), |
| 21 | + (Path("foo/bar"), "**/**"), |
| 22 | +] |
| 23 | + |
| 24 | +NEGATIVE_TEST_CASES: list[tuple[Path, str]] = [ |
| 25 | + (Path("foo/bar/baz"), "*/*"), |
| 26 | + (Path("foo"), "foobar"), |
| 27 | + (Path("foo"), "*/foo"), |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +def test_compile() -> None: |
| 32 | + for path, glob in POSITIVE_TEST_CASES: |
| 33 | + assert glob_compile(glob).match(str(path)) is not None |
| 34 | + for path, glob in NEGATIVE_TEST_CASES: |
| 35 | + assert glob_compile(glob).match(str(path)) is None |
| 36 | + |
| 37 | + |
| 38 | +def test_CompiledGroup_matches() -> None: |
| 39 | + for path, glob in POSITIVE_TEST_CASES: |
| 40 | + group = SnapshotGroup(root_glob=glob) |
| 41 | + assert CompiledGroup.compile(group).matches(path) |
| 42 | + group = SnapshotGroup(root_glob=glob, excluded_names=[os.path.basename(path)]) |
| 43 | + assert not CompiledGroup.compile(group).matches(path) |
| 44 | + for path, glob in NEGATIVE_TEST_CASES: |
| 45 | + group = SnapshotGroup(root_glob=glob) |
| 46 | + assert not CompiledGroup.compile(group).matches(path) |
| 47 | + |
| 48 | + |
| 49 | +def test_CompiledGroups() -> None: |
| 50 | + for path, glob in POSITIVE_TEST_CASES: |
| 51 | + group1 = SnapshotGroup(root_glob=glob) |
| 52 | + group2 = SnapshotGroup(root_glob=glob, excluded_names=[os.path.basename(path)]) |
| 53 | + group3 = SnapshotGroup(root_glob="doesntmatch") |
| 54 | + compiled = CompiledGroups.compile([group1, group2, group3]) |
| 55 | + assert compiled.any_match(path) |
| 56 | + assert compiled.get_matching(path) == [group1] |
| 57 | + |
| 58 | + |
| 59 | +def test_CompiledGroup_glob(tmp_path: Path) -> None: |
| 60 | + for p, _ in POSITIVE_TEST_CASES + NEGATIVE_TEST_CASES: |
| 61 | + p = tmp_path / p |
| 62 | + p.mkdir(parents=True, exist_ok=True) |
| 63 | + p.touch() |
| 64 | + for p, glob in POSITIVE_TEST_CASES: |
| 65 | + group = SnapshotGroup(root_glob=glob) |
| 66 | + assert str(p) in CompiledGroup.compile(group).glob(tmp_path) |
| 67 | + for p, glob in NEGATIVE_TEST_CASES: |
| 68 | + group = SnapshotGroup(root_glob=glob) |
| 69 | + assert str(p) not in CompiledGroup.compile(group).glob(tmp_path) |
0 commit comments