|
| 1 | +# Copyright 2023-2026 Buf Technologies, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import os |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | +from pathlib import Path |
| 21 | +from textwrap import dedent |
| 22 | + |
| 23 | +import pytest |
| 24 | + |
| 25 | +from test.versions import PROTOVALIDATE_VERSION |
| 26 | + |
| 27 | + |
| 28 | +def maybe_patch_args_with_debug(args: list[str]) -> list[str]: |
| 29 | + # Do a best effort to invoke the child with debugging. |
| 30 | + # This invokes internal methods from bundles provided by the IDE |
| 31 | + # and may not always work. |
| 32 | + try: |
| 33 | + from pydevd import ( # ty: ignore[unresolved-import] - provided by IDE # pyright: ignore[reportMissingImports] # noqa: PLC0415 |
| 34 | + _pydev_bundle, |
| 35 | + ) |
| 36 | + |
| 37 | + return _pydev_bundle.pydev_monkey.patch_args(args) |
| 38 | + except Exception: |
| 39 | + return args |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("legacy", [False, True], ids=["py", "legacy"]) |
| 43 | +def test_conformance(*, legacy: bool) -> None: |
| 44 | + # Workaround pydevd monkeypatching of -m invocation not being compatible |
| 45 | + # with Python 3.14 yet by executing a script that uses runpy itself. |
| 46 | + # pydevd does monkeypatch -c form correctly. |
| 47 | + script = dedent( |
| 48 | + """ |
| 49 | + import runpy |
| 50 | + runpy.run_module( |
| 51 | + 'test.conformance.runner', |
| 52 | + run_name='__main__', |
| 53 | + alter_sys=True |
| 54 | + ) |
| 55 | + """ |
| 56 | + ) |
| 57 | + command = [sys.executable, "--", "-c", script] |
| 58 | + command = maybe_patch_args_with_debug(command) |
| 59 | + |
| 60 | + env = os.environ.copy() |
| 61 | + if legacy: |
| 62 | + env["PROTOVALIDATE_CONFORMANCE_LEGACY"] = "1" |
| 63 | + |
| 64 | + subprocess.run( # noqa: S603 |
| 65 | + [ # noqa: S607 |
| 66 | + "go", |
| 67 | + "run", |
| 68 | + f"github.com/bufbuild/protovalidate/tools/protovalidate-conformance@{PROTOVALIDATE_VERSION}", |
| 69 | + "--strict_message", |
| 70 | + f"--expected_failures={Path(__file__).parent / 'nonconforming.yaml'}", |
| 71 | + "--timeout", |
| 72 | + "10s", |
| 73 | + *command, |
| 74 | + ], |
| 75 | + env=env, |
| 76 | + check=True, |
| 77 | + ) |
0 commit comments