diff --git a/betterproto/tests/generate.py b/betterproto/tests/generate.py index f3b92e6..fc85b7f 100644 --- a/betterproto/tests/generate.py +++ b/betterproto/tests/generate.py @@ -2,6 +2,7 @@ import glob import os import shutil +import subprocess import sys from typing import Set @@ -33,6 +34,8 @@ def generate(whitelist: Set[str]): test_case_names = set(get_directories(inputs_path)) + failed_test_cases = [] + for test_case_name in sorted(test_case_names): test_case_input_path = os.path.realpath( os.path.join(inputs_path, test_case_name) @@ -45,22 +48,39 @@ def generate(whitelist: Set[str]): ): continue - test_case_output_path_reference = os.path.join( - output_path_reference, test_case_name - ) - test_case_output_path_betterproto = os.path.join( - output_path_betterproto, test_case_name - ) - print(f"Generating output for {test_case_name}") - os.makedirs(test_case_output_path_reference, exist_ok=True) - os.makedirs(test_case_output_path_betterproto, exist_ok=True) + try: + generate_test_case_output(test_case_name, test_case_input_path) + except subprocess.CalledProcessError as e: + failed_test_cases.append(test_case_name) - clear_directory(test_case_output_path_reference) - clear_directory(test_case_output_path_betterproto) + if failed_test_cases: + sys.stderr.write("\nFailed to generate the following test cases:\n") + for failed_test_case in failed_test_cases: + sys.stderr.write(f"- {failed_test_case}\n") - protoc_reference(test_case_input_path, test_case_output_path_reference) - protoc_plugin(test_case_input_path, test_case_output_path_betterproto) + +def generate_test_case_output(test_case_name, test_case_input_path=None): + if not test_case_input_path: + test_case_input_path = os.path.realpath( + os.path.join(inputs_path, test_case_name) + ) + + test_case_output_path_reference = os.path.join( + output_path_reference, test_case_name + ) + test_case_output_path_betterproto = os.path.join( + output_path_betterproto, test_case_name + ) + + os.makedirs(test_case_output_path_reference, exist_ok=True) + os.makedirs(test_case_output_path_betterproto, exist_ok=True) + + clear_directory(test_case_output_path_reference) + clear_directory(test_case_output_path_betterproto) + + protoc_reference(test_case_input_path, test_case_output_path_reference) + protoc_plugin(test_case_input_path, test_case_output_path_betterproto) HELP = "\n".join( diff --git a/betterproto/tests/inputs/config.py b/betterproto/tests/inputs/config.py index b4b45c4..d4fffcf 100644 --- a/betterproto/tests/inputs/config.py +++ b/betterproto/tests/inputs/config.py @@ -10,6 +10,8 @@ tests = { "oneof_enum", "googletypes_service_returns_empty", "casing_message_field_uppercase", + "namespace_keywords", + "namespace_builtin_types" } services = { diff --git a/betterproto/tests/inputs/keywords/keywords.json b/betterproto/tests/inputs/keywords/keywords.json deleted file mode 100644 index c4f7b0c..0000000 --- a/betterproto/tests/inputs/keywords/keywords.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "for": 1, - "with": 2, - "as": 3 -} diff --git a/betterproto/tests/inputs/keywords/keywords.proto b/betterproto/tests/inputs/keywords/keywords.proto deleted file mode 100644 index 57c998b..0000000 --- a/betterproto/tests/inputs/keywords/keywords.proto +++ /dev/null @@ -1,11 +0,0 @@ -syntax = "proto3"; - -message Test { - int32 for = 1; - int32 with = 2; - int32 as = 3; -} - -service TestService { - rpc GetTest(Test) returns (Test) {} -} diff --git a/betterproto/tests/inputs/namespace_keywords/namespace_keywords.json b/betterproto/tests/inputs/namespace_keywords/namespace_keywords.json new file mode 100644 index 0000000..4f11b60 --- /dev/null +++ b/betterproto/tests/inputs/namespace_keywords/namespace_keywords.json @@ -0,0 +1,37 @@ +{ + "False": 1, + "None": 2, + "True": 3, + "and": 4, + "as": 5, + "assert": 6, + "async": 7, + "await": 8, + "break": 9, + "class": 10, + "continue": 11, + "def": 12, + "del": 13, + "elif": 14, + "else": 15, + "except": 16, + "finally": 17, + "for": 18, + "from": 19, + "global": 20, + "if": 21, + "import": 22, + "in": 23, + "is": 24, + "lambda": 25, + "nonlocal": 26, + "not": 27, + "or": 28, + "pass": 29, + "raise": 30, + "return": 31, + "try": 32, + "while": 33, + "with": 34, + "yield": 35 +} diff --git a/betterproto/tests/inputs/namespace_keywords/namespace_keywords.proto b/betterproto/tests/inputs/namespace_keywords/namespace_keywords.proto new file mode 100644 index 0000000..6d1a7c5 --- /dev/null +++ b/betterproto/tests/inputs/namespace_keywords/namespace_keywords.proto @@ -0,0 +1,44 @@ +syntax = "proto3"; + +// Tests that messages may contain fields that are Python keywords +// +// Generated with Python 3.7.6 +// print('\n'.join(f'string {k} = {i+1};' for i,k in enumerate(keyword.kwlist))) + +message Test { + string False = 1; + string None = 2; + string True = 3; + string and = 4; + string as = 5; + string assert = 6; + string async = 7; + string await = 8; + string break = 9; + string class = 10; + string continue = 11; + string def = 12; + string del = 13; + string elif = 14; + string else = 15; + string except = 16; + string finally = 17; + string for = 18; + string from = 19; + string global = 20; + string if = 21; + string import = 22; + string in = 23; + string is = 24; + string lambda = 25; + string nonlocal = 26; + string not = 27; + string or = 28; + string pass = 29; + string raise = 30; + string return = 31; + string try = 32; + string while = 33; + string with = 34; + string yield = 35; +} \ No newline at end of file diff --git a/betterproto/tests/util.py b/betterproto/tests/util.py index 11d5052..a7cff7a 100644 --- a/betterproto/tests/util.py +++ b/betterproto/tests/util.py @@ -36,10 +36,11 @@ def read_relative(file: str, path: str): return fh.read() -def protoc_plugin(path: str, output_dir: str): - subprocess.run( +def protoc_plugin(path: str, output_dir: str) -> subprocess.CompletedProcess: + return subprocess.run( f"protoc --plugin=protoc-gen-custom={plugin_path} --custom_out={output_dir} --proto_path={path} {path}/*.proto", shell=True, + check=True, )