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

Commit 5664b71

Browse files
Simplify test compilation (#76)
* Simplify test compilation * Remove useless function
1 parent 541484b commit 5664b71

File tree

2 files changed

+18
-57
lines changed

2 files changed

+18
-57
lines changed

tests/generate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ async def generate_test_case_output(test_case_input_path: Path, test_case_name:
6464

6565
clear_directory(test_case_output_path_reference)
6666
clear_directory(test_case_output_path_betterproto)
67+
clear_directory(test_case_output_path_betterproto_pyd)
6768

6869
(
6970
(ref_out, ref_err, ref_code),

tests/util.py

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import asyncio
2-
import atexit
32
import os
4-
import platform
53
import sys
6-
import tempfile
7-
from collections.abc import Generator
84
from pathlib import Path
95

106
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
@@ -16,12 +12,6 @@
1612
output_path_betterproto_pydantic = root_path.joinpath("output_betterproto_pydantic")
1713

1814

19-
def get_files(path, suffix: str) -> Generator[str, None, None]:
20-
for r, dirs, files in os.walk(path):
21-
for filename in [f for f in files if f.endswith(suffix)]:
22-
yield os.path.join(r, filename)
23-
24-
2515
def get_directories(path):
2616
for root, directories, files in os.walk(path):
2717
yield from directories
@@ -30,53 +20,23 @@ def get_directories(path):
3020
async def protoc(path: str | Path, output_dir: str | Path, reference: bool = False, pydantic_dataclasses: bool = False):
3121
path: Path = Path(path).resolve()
3222
output_dir: Path = Path(output_dir).resolve()
33-
python_out_option: str = "python_betterproto2_out" if not reference else "python_out"
34-
35-
if pydantic_dataclasses:
36-
plugin_path = Path("src/betterproto2_compiler/plugin/main.py")
37-
38-
if "Win" in platform.system():
39-
with tempfile.NamedTemporaryFile("w", encoding="UTF-8", suffix=".bat", delete=False) as tf:
40-
# See https://stackoverflow.com/a/42622705
41-
tf.writelines(
42-
[
43-
"@echo off",
44-
f"\nchdir {os.getcwd()}",
45-
f"\n{sys.executable} -u {plugin_path.as_posix()}",
46-
],
47-
)
48-
49-
tf.flush()
50-
51-
plugin_path = Path(tf.name)
52-
atexit.register(os.remove, plugin_path)
53-
54-
command = [
55-
sys.executable,
56-
"-m",
57-
"grpc.tools.protoc",
58-
f"--plugin=protoc-gen-custom={plugin_path.as_posix()}",
59-
"--experimental_allow_proto3_optional",
60-
"--custom_opt=pydantic_dataclasses",
61-
"--custom_opt=client_generation=async_sync",
62-
"--custom_opt=server_generation=async",
63-
f"--proto_path={path.as_posix()}",
64-
f"--custom_out={output_dir.as_posix()}",
65-
*[p.as_posix() for p in path.glob("*.proto")],
66-
]
67-
else:
68-
command = [
69-
sys.executable,
70-
"-m",
71-
"grpc.tools.protoc",
72-
f"--proto_path={path.as_posix()}",
73-
f"--{python_out_option}={output_dir.as_posix()}",
74-
*[p.as_posix() for p in path.glob("*.proto")],
75-
]
76-
77-
if not reference:
78-
command.insert(3, "--python_betterproto2_opt=server_generation=async")
79-
command.insert(3, "--python_betterproto2_opt=client_generation=async_sync")
23+
python_out_option: str = "python_out" if reference else "python_betterproto2_out"
24+
25+
command = [
26+
sys.executable,
27+
"-m",
28+
"grpc.tools.protoc",
29+
f"--proto_path={path.as_posix()}",
30+
f"--{python_out_option}={output_dir.as_posix()}",
31+
*[p.as_posix() for p in path.glob("*.proto")],
32+
]
33+
34+
if not reference:
35+
command.insert(3, "--python_betterproto2_opt=server_generation=async")
36+
command.insert(3, "--python_betterproto2_opt=client_generation=async_sync")
37+
38+
if pydantic_dataclasses:
39+
command.insert(3, "--python_betterproto2_opt=pydantic_dataclasses")
8040

8141
proc = await asyncio.create_subprocess_exec(
8242
*command,

0 commit comments

Comments
 (0)