diff --git a/tools/check_format.py b/tools/check_format.py index 79951a18192e1..f0f849c2551b5 100755 --- a/tools/check_format.py +++ b/tools/check_format.py @@ -48,6 +48,7 @@ SUBDIR_SET = set(common.includeDirOrder()) INCLUDE_ANGLE = "#include <" INCLUDE_ANGLE_LEN = len(INCLUDE_ANGLE) +PROTO_PACKAGE_REGEX = re.compile("package (.*);") # yapf: disable PROTOBUF_TYPE_ERRORS = { @@ -143,6 +144,41 @@ def checkNamespace(file_path): return [] +def fixJavaProtoOptions(file_path): + java_multiple_files = False + java_package_correct = False + package_name = None + for line in fileinput.FileInput(file_path): + if line.startswith("package "): + result = PROTO_PACKAGE_REGEX.search(line) + if result is None or len(result.groups()) != 1: + continue + + package_name = result.group(1) + if "option java_multiple_files = true;" in line: + java_multiple_files = True + if "option java_package = \"io.envoyproxy.envoy" in line: + java_package_correct = True + if java_multiple_files and java_package_correct: + return [] + + if package_name is None: + return ["Unable to find package name for proto file: %s" % file_path] + + to_add = "" + if not java_package_correct: + to_add = to_add + "option java_package = \"io.envoyproxy.{}\";\n".format(package_name) + if not java_multiple_files: + to_add = to_add + "option java_multiple_files = true;\n" + + for line in fileinput.FileInput(file_path, inplace=True): + if line.startswith("package "): + line = line.replace(line, line + to_add) + sys.stdout.write(line) + + return [] + + def checkJavaProtoOptions(file_path): java_multiple_files = False java_package_correct = False @@ -383,6 +419,8 @@ def fixSourcePath(file_path): if not file_path.endswith(PROTO_SUFFIX): error_messages += fixHeaderOrder(file_path) error_messages += clangFormat(file_path) + if file_path.endswith(PROTO_SUFFIX) and isApiFile(file_path): + error_messages += fixJavaProtoOptions(file_path) return error_messages diff --git a/tools/check_format_test_helper.py b/tools/check_format_test_helper.py index 879f82097ca6a..6ffae4a8b73cd 100755 --- a/tools/check_format_test_helper.py +++ b/tools/check_format_test_helper.py @@ -49,6 +49,9 @@ def runCheckFormat(operation, filename): def getInputFile(filename): infile = os.path.join(src, filename) + directory = os.path.dirname(filename) + if not directory == '' and not os.path.isdir(directory): + os.makedirs(directory) shutil.copyfile(infile, filename) return filename @@ -58,6 +61,10 @@ def getInputFile(filename): # code. def fixFileHelper(filename): infile = os.path.join(src, filename) + directory = os.path.dirname(filename) + if not directory == '' and not os.path.isdir(directory): + os.makedirs(directory) + shutil.copyfile(infile, filename) command, status, stdout = runCheckFormat("fix", getInputFile(filename)) return (command, infile, filename, status, stdout) @@ -194,6 +201,10 @@ def checkFileExpectingOK(filename): errors += checkUnfixableError("testing_test.cc", "Don't use 'using testing::Test;, elaborate the type instead") + errors += fixFileExpectingFailure( + "api/missing_package.proto", + "Unable to find package name for proto file: ./api/missing_package.proto") + # The following files have errors that can be automatically fixed. errors += checkAndFixError("over_enthusiastic_spaces.cc", "./over_enthusiastic_spaces.cc:3: over-enthusiastic spaces") @@ -207,6 +218,7 @@ def checkFileExpectingOK(filename): errors += checkAndFixError("license.BUILD", "envoy_build_fixer check failed") errors += checkAndFixError("bad_envoy_build_sys_ref.BUILD", "Superfluous '@envoy//' prefix") errors += checkAndFixError("proto_format.proto", "clang-format check failed") + errors += checkAndFixError("api/java_options.proto", "Java proto option") errors += checkFileExpectingOK("real_time_source_override.cc") errors += checkFileExpectingOK("time_system_wait_for.cc") diff --git a/tools/testdata/check_format/api/java_options.proto b/tools/testdata/check_format/api/java_options.proto new file mode 100644 index 0000000000000..a979cc6d23747 --- /dev/null +++ b/tools/testdata/check_format/api/java_options.proto @@ -0,0 +1 @@ +package envoy.foo; diff --git a/tools/testdata/check_format/api/java_options.proto.gold b/tools/testdata/check_format/api/java_options.proto.gold new file mode 100644 index 0000000000000..aaf202ec0aae0 --- /dev/null +++ b/tools/testdata/check_format/api/java_options.proto.gold @@ -0,0 +1,3 @@ +package envoy.foo; +option java_package = "io.envoyproxy.envoy.foo"; +option java_multiple_files = true; diff --git a/tools/testdata/check_format/api/missing_package.proto b/tools/testdata/check_format/api/missing_package.proto new file mode 100644 index 0000000000000..e69de29bb2d1d