1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
from pathlib import Path
from typing import List
import pytest
from build_helpers.build_helpers import find, find_version, matches
@pytest.mark.parametrize(
"path_rel,include_files,include_dirs,excludes,scan_exclude,expected",
[
pytest.param("test_files", [], [], [], None, [], id="none"),
pytest.param(
"test_files",
[".*"],
[],
[],
[],
[
"a/b/bad_dir/.gitkeep",
"a/b/file2.txt",
"a/b/file1.txt",
"a/b/junk.txt",
"c/bad_dir/.gitkeep",
"c/file2.txt",
"c/file1.txt",
"c/junk.txt",
],
id="all",
),
pytest.param(
"test_files",
[".*"],
[],
["^a/.*"],
[],
["c/bad_dir/.gitkeep", "c/file2.txt", "c/file1.txt", "c/junk.txt"],
id="filter_a",
),
pytest.param(
"test_files",
[".*"],
[],
[],
["^a/.*"],
["c/bad_dir/.gitkeep", "c/file2.txt", "c/file1.txt", "c/junk.txt"],
id="do_not_scan_a",
),
pytest.param(
"test_files",
["^a/.*"],
[],
[],
[],
["a/b/bad_dir/.gitkeep", "a/b/file2.txt", "a/b/file1.txt", "a/b/junk.txt"],
id="include_a",
),
pytest.param(
"test_files",
["^a/.*"],
[],
[".*/file1\\.txt"],
[],
["a/b/bad_dir/.gitkeep", "a/b/file2.txt", "a/b/junk.txt"],
id="include_a,exclude_file1",
),
pytest.param(
"test_files",
[".*"],
[],
["^.*/junk.txt$"],
[],
[
"a/b/bad_dir/.gitkeep",
"a/b/file2.txt",
"a/b/file1.txt",
"c/bad_dir/.gitkeep",
"c/file2.txt",
"c/file1.txt",
],
id="no_junk",
),
pytest.param(
"test_files",
["^.*/junk.txt"],
[],
[],
[],
["a/b/junk.txt", "c/junk.txt"],
id="junk_only",
),
pytest.param("test_files", [], ["^a$"], [], [], ["a"], id="exact_a"),
pytest.param(
"test_files",
[],
[".*bad_dir$"],
[],
[],
["a/b/bad_dir", "c/bad_dir"],
id="bad_dirs",
),
],
)
def test_find(
path_rel: str,
include_files: List[str],
include_dirs: List[str],
excludes: List[str],
scan_exclude: List[str],
expected: List[str],
) -> None:
basedir = Path(__file__).parent.absolute()
path = basedir / path_rel
ret = find(
root=path,
excludes=excludes,
include_files=include_files,
include_dirs=include_dirs,
scan_exclude=scan_exclude,
)
ret_set = set([str(x) for x in ret])
expected_set = set([str(Path(x)) for x in expected])
assert ret_set == expected_set
@pytest.mark.parametrize(
"patterns,query,expected",
[
(["^a/.*"], Path("a") / "b.txt", True),
(["^/foo/bar/.*"], Path("/foo") / "bar" / "blag", True),
],
)
def test_matches(patterns: List[str], query: Path, expected: bool) -> None:
ret = matches(patterns, query)
assert ret == expected
def test_find_version() -> None:
version = find_version("omegaconf", "version.py")
# Ensure `version` is a string starting with a digit.
assert isinstance(version, str) and version and "0" <= version[0] <= "9"
|