Skip to content

Commit d491877

Browse files
committed
test: switch to use the protogen.test method
1 parent 035691f commit d491877

File tree

4 files changed

+35
-105
lines changed

4 files changed

+35
-105
lines changed

protogen/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,8 +1704,8 @@ def __init__(
17041704
self,
17051705
*,
17061706
py_import_func: Callable[[str, str], PyImportPath] = default_py_import_func,
1707-
input: BinaryIO | None = None,
1708-
output: BinaryIO | None = None,
1707+
input: Optional[BinaryIO] = None,
1708+
output: Optional[BinaryIO] = None,
17091709
supported_features: List[
17101710
"google.protobuf.compiler.plugin_pb2.CodeGeneratorResponse.Feature"
17111711
] = [],

test/optional/optional.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
syntax="proto3";
2+
3+
package hello;
4+
5+
message X {
6+
string a = 1;
7+
optional string b = 2;
8+
}

test/test_protogen.py

Lines changed: 25 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,45 @@
1-
import unittest
2-
import subprocess
3-
import os.path
4-
import os
5-
from typing import Tuple, List
1+
from protogen.test import run_plugin
62

73

84
def test_protogen():
9-
# test won't work if using relative path. protoc seems to have troubles
10-
# finding files then:
11-
#
12-
# - protoc-gen-empty=./test/plugin/main.py: warning: directory does not exist.
13-
# - vendor/google/api/annotations.proto: File does not reside within any path
14-
# specified using --proto_path (or -I). You must specify a --proto_path which
15-
# encompasses this file. Note that the proto_path must be an exact prefix of
16-
# the .proto file names -- protoc is too dumb to figure out when two paths
17-
# (e.g. absolute and relative) are equivalent (it's harder than you think).
18-
#
19-
pwd = os.getcwd()
20-
vendor = os.path.join(pwd, "test", "vendor")
21-
proto = os.path.join(pwd, "test", "vendor", "google", "api", "annotations.proto")
22-
plugin = os.path.join(pwd, "test", "plugin", "main.py")
5+
resp = run_plugin(
6+
proto_paths=["test/vendor"],
7+
files_to_generate=["google/api/annotations.proto"],
8+
plugin="test.plugin.main",
9+
)
2310

24-
# Create the output directory if it does not exists. protoc requires it
25-
# to exist before generating output to it.
26-
out_dir = os.path.join(pwd, "testout", "main")
27-
if not os.path.exists(out_dir):
28-
os.makedirs(out_dir)
11+
assert resp.proto.error == ""
2912

30-
# Run protoc.
31-
code, output = _run_protoc(
32-
[
33-
"protoc",
34-
"-I",
35-
vendor,
36-
f"--plugin=protoc-gen-empty={plugin}",
37-
f"--empty_out={out_dir}",
38-
proto,
39-
]
40-
)
41-
assert output == ""
42-
assert code == 0
13+
generated, ok = resp.file_content("google/api/annotations.out")
14+
assert ok
4315

44-
# Check content of directory.
4516
with open("test/golden/google/api/annotations.out") as f:
4617
golden = f.read()
47-
with open("testout/main/google/api/annotations.out") as f:
48-
generated = f.read()
4918

5019
assert golden == generated
5120

5221

5322
def test_parameter():
54-
pwd = os.getcwd()
55-
vendor = os.path.join(pwd, "test", "vendor")
56-
proto = os.path.join(pwd, "test", "vendor", "google", "api", "annotations.proto")
57-
plugin = os.path.join(pwd, "test", "plugin", "parameter.py")
23+
resp = run_plugin(
24+
proto_paths=["test/vendor"],
25+
files_to_generate=["google/api/annotations.proto"],
26+
plugin="test.plugin.parameter",
27+
parameter={
28+
"k1": "v1",
29+
"k2": "v2;k3=v3",
30+
"k4": "",
31+
"k5": "v5",
32+
"abc": "x",
33+
"5": "2",
34+
},
35+
)
5836

59-
# Create the output directory if it does not exists. protoc requires it
60-
# to exist before generating output to it.
61-
out_dir = os.path.join(pwd, "testout", "params")
62-
if not os.path.exists(out_dir):
63-
os.makedirs(out_dir)
37+
assert resp.proto.error == ""
6438

65-
code, output = _run_protoc(
66-
[
67-
"protoc",
68-
"-I",
69-
vendor,
70-
f"--plugin=protoc-gen-params={plugin}",
71-
f"--params_opt=k1=v1,k2=v2;k3=v3,",
72-
f"--params_opt=k4",
73-
f"--params_opt=k5=v5",
74-
f"--params_out=abc=x,5=2:{out_dir}",
75-
proto,
76-
]
77-
)
78-
assert output == ""
79-
assert code == 0
39+
generated, ok = resp.file_content("out.txt")
40+
assert ok
8041

81-
# Check content of directory.
8242
with open("test/golden/params.txt") as f:
8343
golden = f.read()
84-
with open("testout/params/out.txt") as f:
85-
generated = f.read()
8644

8745
assert golden == generated
88-
89-
90-
def _run_protoc(args: List[str]) -> Tuple[int, str]:
91-
proc = subprocess.Popen(
92-
args, text=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE
93-
)
94-
# Executed from current directory (repo root)
95-
code = proc.wait()
96-
if code == 0:
97-
output = proc.stdout.read()
98-
else:
99-
output = proc.stderr.read()
100-
proc.terminate() # TODO necessary?
101-
return code, output

test/test_run_plugin.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)