|
| 1 | +"""Regression tests for the path-pattern matchers in autobuild. |
| 2 | +
|
| 3 | +Both ``env_config._pattern_matches`` and ``build_util.should_skip`` substring- |
| 4 | +match against the file's full path including extension. Patterns can therefore |
| 5 | +end in ``.py`` to anchor against the script form and avoid clobbering sibling |
| 6 | +``_jax`` / ``_jit`` variants. These tests lock in that convention — without |
| 7 | +them, the previous bug (substring match against the path with ``.py`` stripped) |
| 8 | +silently disabled overrides ending in ``.py``. |
| 9 | +""" |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +from autobuild.build_util import should_skip |
| 14 | +from autobuild.env_config import _pattern_matches |
| 15 | + |
| 16 | + |
| 17 | +def test_dot_py_pattern_matches_only_the_py_script(): |
| 18 | + """Pattern ending in `.py` must NOT also match sibling files whose stem |
| 19 | + starts with the same prefix (e.g. `_jax`, `_jit`).""" |
| 20 | + py_file = Path("scripts/imaging/visualization.py") |
| 21 | + jax_file = Path("scripts/imaging/visualization_jax.py") |
| 22 | + jit_file = Path("scripts/imaging/visualization_jit.py") |
| 23 | + |
| 24 | + assert _pattern_matches(py_file, "imaging/visualization.py") is True |
| 25 | + assert _pattern_matches(jax_file, "imaging/visualization.py") is False |
| 26 | + assert _pattern_matches(jit_file, "imaging/visualization.py") is False |
| 27 | + |
| 28 | + |
| 29 | +def test_prefix_pattern_substring_matches_siblings(): |
| 30 | + """A pattern without `.py` still substring-matches both the `.py` script |
| 31 | + and `_jax` / `_jit` siblings, because the prefix is shared. Authors who |
| 32 | + want to target only one must anchor with `.py`.""" |
| 33 | + py_file = Path("scripts/imaging/visualization.py") |
| 34 | + jax_file = Path("scripts/imaging/visualization_jax.py") |
| 35 | + |
| 36 | + assert _pattern_matches(py_file, "imaging/visualization") is True |
| 37 | + assert _pattern_matches(jax_file, "imaging/visualization") is True |
| 38 | + # _jax-specific pattern only matches the _jax file. |
| 39 | + assert _pattern_matches(py_file, "imaging/visualization_jax") is False |
| 40 | + assert _pattern_matches(jax_file, "imaging/visualization_jax") is True |
| 41 | + |
| 42 | + |
| 43 | +def test_stem_pattern_matches_exactly(): |
| 44 | + """Patterns without `/` must match the file stem exactly — not as a |
| 45 | + substring.""" |
| 46 | + assert _pattern_matches(Path("scripts/a/foo.py"), "foo") is True |
| 47 | + assert _pattern_matches(Path("scripts/a/foobar.py"), "foo") is False |
| 48 | + assert _pattern_matches(Path("scripts/a/foo.py"), "foo.py") is False |
| 49 | + |
| 50 | + |
| 51 | +def test_directory_pattern_matches_anything_in_directory(): |
| 52 | + """A pattern ending in `/` matches every script under that directory.""" |
| 53 | + assert _pattern_matches(Path("scripts/aggregator/test_x.py"), "aggregator/") is True |
| 54 | + assert _pattern_matches(Path("scripts/foo/bar.py"), "aggregator/") is False |
| 55 | + |
| 56 | + |
| 57 | +def test_should_skip_uses_same_convention(): |
| 58 | + """`should_skip` shares the convention with `_pattern_matches`; the same |
| 59 | + `.py` anchoring works there.""" |
| 60 | + no_run = ["imaging/visualization.py"] |
| 61 | + assert should_skip(Path("scripts/imaging/visualization.py"), no_run) is True |
| 62 | + assert should_skip(Path("scripts/imaging/visualization_jax.py"), no_run) is False |
| 63 | + |
| 64 | + |
| 65 | +def test_should_skip_stem_pattern(): |
| 66 | + no_run = ["foo"] |
| 67 | + assert should_skip(Path("scripts/a/foo.py"), no_run) is True |
| 68 | + assert should_skip(Path("scripts/a/foobar.py"), no_run) is False |
| 69 | + |
| 70 | + |
| 71 | +def test_should_skip_directory_pattern(): |
| 72 | + no_run = ["aggregator/"] |
| 73 | + assert should_skip(Path("scripts/aggregator/test_x.py"), no_run) is True |
| 74 | + assert should_skip(Path("scripts/foo/bar.py"), no_run) is False |
| 75 | + |
| 76 | + |
| 77 | +def test_should_skip_empty_list(): |
| 78 | + assert should_skip(Path("scripts/a/foo.py"), []) is False |
0 commit comments