diff --git a/tests/generate.py b/tests/generate.py index 1a54ecd8..8e422d6a 100644 --- a/tests/generate.py +++ b/tests/generate.py @@ -64,6 +64,7 @@ async def generate_test_case_output(test_case_input_path: Path, test_case_name: clear_directory(test_case_output_path_reference) clear_directory(test_case_output_path_betterproto) + clear_directory(test_case_output_path_betterproto_pyd) ( (ref_out, ref_err, ref_code), diff --git a/tests/util.py b/tests/util.py index 0508f9f2..0e8366ff 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,10 +1,6 @@ import asyncio -import atexit import os -import platform import sys -import tempfile -from collections.abc import Generator from pathlib import Path os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python" @@ -16,12 +12,6 @@ output_path_betterproto_pydantic = root_path.joinpath("output_betterproto_pydantic") -def get_files(path, suffix: str) -> Generator[str, None, None]: - for r, dirs, files in os.walk(path): - for filename in [f for f in files if f.endswith(suffix)]: - yield os.path.join(r, filename) - - def get_directories(path): for root, directories, files in os.walk(path): yield from directories @@ -30,53 +20,23 @@ def get_directories(path): async def protoc(path: str | Path, output_dir: str | Path, reference: bool = False, pydantic_dataclasses: bool = False): path: Path = Path(path).resolve() output_dir: Path = Path(output_dir).resolve() - python_out_option: str = "python_betterproto2_out" if not reference else "python_out" - - if pydantic_dataclasses: - plugin_path = Path("src/betterproto2_compiler/plugin/main.py") - - if "Win" in platform.system(): - with tempfile.NamedTemporaryFile("w", encoding="UTF-8", suffix=".bat", delete=False) as tf: - # See https://stackoverflow.com/a/42622705 - tf.writelines( - [ - "@echo off", - f"\nchdir {os.getcwd()}", - f"\n{sys.executable} -u {plugin_path.as_posix()}", - ], - ) - - tf.flush() - - plugin_path = Path(tf.name) - atexit.register(os.remove, plugin_path) - - command = [ - sys.executable, - "-m", - "grpc.tools.protoc", - f"--plugin=protoc-gen-custom={plugin_path.as_posix()}", - "--experimental_allow_proto3_optional", - "--custom_opt=pydantic_dataclasses", - "--custom_opt=client_generation=async_sync", - "--custom_opt=server_generation=async", - f"--proto_path={path.as_posix()}", - f"--custom_out={output_dir.as_posix()}", - *[p.as_posix() for p in path.glob("*.proto")], - ] - else: - command = [ - sys.executable, - "-m", - "grpc.tools.protoc", - f"--proto_path={path.as_posix()}", - f"--{python_out_option}={output_dir.as_posix()}", - *[p.as_posix() for p in path.glob("*.proto")], - ] - - if not reference: - command.insert(3, "--python_betterproto2_opt=server_generation=async") - command.insert(3, "--python_betterproto2_opt=client_generation=async_sync") + python_out_option: str = "python_out" if reference else "python_betterproto2_out" + + command = [ + sys.executable, + "-m", + "grpc.tools.protoc", + f"--proto_path={path.as_posix()}", + f"--{python_out_option}={output_dir.as_posix()}", + *[p.as_posix() for p in path.glob("*.proto")], + ] + + if not reference: + command.insert(3, "--python_betterproto2_opt=server_generation=async") + command.insert(3, "--python_betterproto2_opt=client_generation=async_sync") + + if pydantic_dataclasses: + command.insert(3, "--python_betterproto2_opt=pydantic_dataclasses") proc = await asyncio.create_subprocess_exec( *command,