Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Simplify test compilation #76

Merged
merged 2 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
74 changes: 17 additions & 57 deletions tests/util.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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,
Expand Down
Loading