Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /microsoft_agent-governance-toolkit /agent-governance-python /agt-policies /tests /test_re2.py
| # Copyright (c) Microsoft Corporation. | |
| # Licensed under the MIT License. | |
| """RE2 compatibility unit tests for :mod:`agt.cli._migrate_re2`. | |
| These guard the fail-open class the OPA-less runtime bridge path is exposed to: | |
| ``fnmatch.translate`` emits atomic groups ``(?>...)`` on CPython >= 3.12 that | |
| Go RE2 rejects, and validation must never silently succeed when OPA is absent. | |
| """ | |
| from __future__ import annotations | |
| import builtins | |
| import pytest | |
| from agt.cli import _migrate_re2 as _re2 | |
| def _no_opa(monkeypatch: pytest.MonkeyPatch) -> None: | |
| monkeypatch.delenv("ACS_OPA_PATH", raising=False) | |
| monkeypatch.setattr(_re2.shutil, "which", lambda _name: None) | |
| def _no_google_re2(monkeypatch: pytest.MonkeyPatch) -> None: | |
| real_import = builtins.__import__ | |
| def guarded(name, *args, **kwargs): | |
| if name == "re2": | |
| raise ImportError("blocked for test") | |
| return real_import(name, *args, **kwargs) | |
| monkeypatch.setattr(builtins, "__import__", guarded) | |
| def test_glob_to_re2_never_emits_atomic_groups( | |
| pattern: str, monkeypatch: pytest.MonkeyPatch | |
| ) -> None: | |
| """Multi-wildcard globs must translate to RE2-safe output (no ``(?>``).""" | |
| _no_opa(monkeypatch) | |
| translated = _re2.glob_to_re2(pattern) | |
| assert "(?>" not in translated | |
| assert translated.endswith(r"\z") | |
| def test_glob_to_re2_uses_re2_end_anchor(monkeypatch: pytest.MonkeyPatch) -> None: | |
| _no_opa(monkeypatch) | |
| translated = _re2.glob_to_re2("*.exe") | |
| assert r"\Z" not in translated | |
| assert translated.endswith(r"\z") | |
| def test_glob_to_re2_handles_literal_bracket_in_class( | |
| pattern: str, monkeypatch: pytest.MonkeyPatch | |
| ) -> None: | |
| """A ']' as the first class member must not confuse the compile-tier scan. | |
| In a regex class '[]...]' the leading ']' is a literal, so text after it is | |
| still inside the class; mis-tracking it made the scanner read a following | |
| '(?x)' as an inline flag and falsely reject a valid glob. | |
| """ | |
| _no_opa(monkeypatch) | |
| _no_google_re2(monkeypatch) | |
| translated = _re2.glob_to_re2(pattern) | |
| assert "(?>" not in translated | |
| def test_validate_re2_rejects_pcre_only_without_opa_or_re2( | |
| pattern: str, monkeypatch: pytest.MonkeyPatch | |
| ) -> None: | |
| """The compile tier must reject PCRE-only/malformed input, not fail open.""" | |
| _no_opa(monkeypatch) | |
| _no_google_re2(monkeypatch) | |
| with pytest.raises(ValueError, match="Go RE2"): | |
| _re2.validate_re2(pattern, require_opa=False) | |
| def test_validate_re2_accepts_valid_re2_without_opa_or_re2( | |
| pattern: str, monkeypatch: pytest.MonkeyPatch | |
| ) -> None: | |
| """Valid RE2 must not be false-rejected by the compile tier.""" | |
| _no_opa(monkeypatch) | |
| _no_google_re2(monkeypatch) | |
| _re2.validate_re2(pattern, require_opa=False) | |
| def test_validate_re2_requires_opa_when_asked(monkeypatch: pytest.MonkeyPatch) -> None: | |
| """require_opa fails closed when no authoritative validator is present.""" | |
| _no_opa(monkeypatch) | |
| _no_google_re2(monkeypatch) | |
| with pytest.raises(ValueError, match="authoritative RE2 validator"): | |
| _re2.validate_re2(r"(?s:secret)\z", require_opa=True) | |
Xet Storage Details
- Size:
- 4.89 kB
- Xet hash:
- c3b05bd8d5250ee1744b7fa372f7f9eaaec4c1a60cbd235b3acdb38da62bff2f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.