Skip to content

Commit 3c70f21

Browse files
#70 Messages should allow fields that are Python keywords
1 parent 4b7d5d3 commit 3c70f21

File tree

7 files changed

+118
-30
lines changed

7 files changed

+118
-30
lines changed

betterproto/tests/generate.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import glob
33
import os
44
import shutil
5+
import subprocess
56
import sys
67
from typing import Set
78

@@ -33,6 +34,8 @@ def generate(whitelist: Set[str]):
3334

3435
test_case_names = set(get_directories(inputs_path))
3536

37+
failed_test_cases = []
38+
3639
for test_case_name in sorted(test_case_names):
3740
test_case_input_path = os.path.realpath(
3841
os.path.join(inputs_path, test_case_name)
@@ -45,22 +48,39 @@ def generate(whitelist: Set[str]):
4548
):
4649
continue
4750

48-
test_case_output_path_reference = os.path.join(
49-
output_path_reference, test_case_name
50-
)
51-
test_case_output_path_betterproto = os.path.join(
52-
output_path_betterproto, test_case_name
51+
print(f"Generating output for {test_case_name}")
52+
try:
53+
generate_test_case_output(test_case_name, test_case_input_path)
54+
except subprocess.CalledProcessError as e:
55+
failed_test_cases.append(test_case_name)
56+
57+
if failed_test_cases:
58+
sys.stderr.write("\nFailed to generate the following test cases:\n")
59+
for failed_test_case in failed_test_cases:
60+
sys.stderr.write(f"- {failed_test_case}\n")
61+
62+
63+
def generate_test_case_output(test_case_name, test_case_input_path=None):
64+
if not test_case_input_path:
65+
test_case_input_path = os.path.realpath(
66+
os.path.join(inputs_path, test_case_name)
5367
)
5468

55-
print(f"Generating output for {test_case_name}")
56-
os.makedirs(test_case_output_path_reference, exist_ok=True)
57-
os.makedirs(test_case_output_path_betterproto, exist_ok=True)
69+
test_case_output_path_reference = os.path.join(
70+
output_path_reference, test_case_name
71+
)
72+
test_case_output_path_betterproto = os.path.join(
73+
output_path_betterproto, test_case_name
74+
)
75+
76+
os.makedirs(test_case_output_path_reference, exist_ok=True)
77+
os.makedirs(test_case_output_path_betterproto, exist_ok=True)
5878

59-
clear_directory(test_case_output_path_reference)
60-
clear_directory(test_case_output_path_betterproto)
79+
clear_directory(test_case_output_path_reference)
80+
clear_directory(test_case_output_path_betterproto)
6181

62-
protoc_reference(test_case_input_path, test_case_output_path_reference)
63-
protoc_plugin(test_case_input_path, test_case_output_path_betterproto)
82+
protoc_reference(test_case_input_path, test_case_output_path_reference)
83+
protoc_plugin(test_case_input_path, test_case_output_path_betterproto)
6484

6585

6686
HELP = "\n".join(

betterproto/tests/inputs/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"oneof_enum",
1111
"googletypes_service_returns_empty",
1212
"casing_message_field_uppercase",
13+
"namespace_keywords",
14+
"namespace_builtin_types"
1315
}
1416

1517
services = {

betterproto/tests/inputs/keywords/keywords.json

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

betterproto/tests/inputs/keywords/keywords.proto

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"False": 1,
3+
"None": 2,
4+
"True": 3,
5+
"and": 4,
6+
"as": 5,
7+
"assert": 6,
8+
"async": 7,
9+
"await": 8,
10+
"break": 9,
11+
"class": 10,
12+
"continue": 11,
13+
"def": 12,
14+
"del": 13,
15+
"elif": 14,
16+
"else": 15,
17+
"except": 16,
18+
"finally": 17,
19+
"for": 18,
20+
"from": 19,
21+
"global": 20,
22+
"if": 21,
23+
"import": 22,
24+
"in": 23,
25+
"is": 24,
26+
"lambda": 25,
27+
"nonlocal": 26,
28+
"not": 27,
29+
"or": 28,
30+
"pass": 29,
31+
"raise": 30,
32+
"return": 31,
33+
"try": 32,
34+
"while": 33,
35+
"with": 34,
36+
"yield": 35
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
syntax = "proto3";
2+
3+
// Tests that messages may contain fields that are Python keywords
4+
//
5+
// Generated with Python 3.7.6
6+
// print('\n'.join(f'string {k} = {i+1};' for i,k in enumerate(keyword.kwlist)))
7+
8+
message Test {
9+
string False = 1;
10+
string None = 2;
11+
string True = 3;
12+
string and = 4;
13+
string as = 5;
14+
string assert = 6;
15+
string async = 7;
16+
string await = 8;
17+
string break = 9;
18+
string class = 10;
19+
string continue = 11;
20+
string def = 12;
21+
string del = 13;
22+
string elif = 14;
23+
string else = 15;
24+
string except = 16;
25+
string finally = 17;
26+
string for = 18;
27+
string from = 19;
28+
string global = 20;
29+
string if = 21;
30+
string import = 22;
31+
string in = 23;
32+
string is = 24;
33+
string lambda = 25;
34+
string nonlocal = 26;
35+
string not = 27;
36+
string or = 28;
37+
string pass = 29;
38+
string raise = 30;
39+
string return = 31;
40+
string try = 32;
41+
string while = 33;
42+
string with = 34;
43+
string yield = 35;
44+
}

betterproto/tests/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ def read_relative(file: str, path: str):
3636
return fh.read()
3737

3838

39-
def protoc_plugin(path: str, output_dir: str):
40-
subprocess.run(
39+
def protoc_plugin(path: str, output_dir: str) -> subprocess.CompletedProcess:
40+
return subprocess.run(
4141
f"protoc --plugin=protoc-gen-custom={plugin_path} --custom_out={output_dir} --proto_path={path} {path}/*.proto",
4242
shell=True,
43+
check=True,
4344
)
4445

4546

0 commit comments

Comments
 (0)