Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore comment in BUILD #2492

Merged
merged 5 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 20 additions & 10 deletions library_generation/model/gapic_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@
(.*?)
\)
"""
resource_pattern = r"//google/cloud:common_resources_proto"
location_pattern = r"//google/cloud/location:location_proto"
iam_pattern = r"//google/iam/v1:iam_policy_proto"
# match a line which the first character is "#".
comment_pattern = r"^\s*\#+"
pattern_to_proto = {
r"//google/cloud:common_resources_proto": "google/cloud/common_resources.proto",
r"//google/cloud/location:location_proto": "google/cloud/location/locations.proto",
r"//google/iam/v1:iam_policy_proto": "google/iam/v1/iam_policy.proto",
}
transport_pattern = r"transport = \"(.*?)\""
rest_pattern = r"rest_numeric_enums = True"
gapic_yaml_pattern = r"gapic_yaml = \"(.*?)\""
Expand Down Expand Up @@ -97,7 +101,9 @@ def parse(
if len(assembly_target) > 0:
include_samples = __parse_include_samples(assembly_target[0])
if len(gapic_target) == 0:
return GapicInputs(include_samples=include_samples)
return GapicInputs(
additional_protos=additional_protos, include_samples=include_samples
)

transport = __parse_transport(gapic_target[0])
rest_numeric_enum = __parse_rest_numeric_enums(gapic_target[0])
Expand All @@ -119,12 +125,16 @@ def parse(

def __parse_additional_protos(proto_library_target: str) -> str:
res = [" "]
if len(re.findall(resource_pattern, proto_library_target)) != 0:
res.append("google/cloud/common_resources.proto")
if len(re.findall(location_pattern, proto_library_target)) != 0:
res.append("google/cloud/location/locations.proto")
if len(re.findall(iam_pattern, proto_library_target)) != 0:
res.append("google/iam/v1/iam_policy.proto")
lines = proto_library_target.split("\n")
for line in lines:
if len(re.findall(comment_pattern, line)) != 0:
# skip a line which the first charactor is "#" since it's
# a comment.
continue
for pattern in pattern_to_proto:
if len(re.findall(pattern, line)) == 0:
continue
res.append(pattern_to_proto[pattern])
return " ".join(res)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
proto_library_with_info(
deps = [
#"//google/cloud:common_resources_proto",
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
proto_library_with_info(
deps = [
# "//google/iam/v1:iam_policy_proto",
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
proto_library_with_info(
deps = [
# "//google/cloud/location:location_proto",
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
proto_library_with_info(
deps = [
"//google/cloud:common_resources_proto",
]
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# this file is only used in testing `get_gapic_additional_protos_from_BUILD` in utilities.sh

proto_library_with_info(
deps = [
"//google/iam/v1:iam_policy_proto",
"//google/cloud/location:location_proto",
"//google/iam/v1:iam_policy_proto",
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
proto_library_with_info(
deps = [
"//google/iam/v1:iam_policy_proto",
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
proto_library_with_info(
deps = [
"//google/cloud/location:location_proto",
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
proto_library_with_info(
deps = [
]
)

This file was deleted.

This file was deleted.

This file was deleted.

22 changes: 22 additions & 0 deletions library_generation/test/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ def test_from_yaml_succeeds(self):
self.assertEqual("google/cloud/asset/v1p5beta1", gapics[3].proto_path)
self.assertEqual("google/cloud/asset/v1p7beta1", gapics[4].proto_path)

@parameterized.expand(
[
("BUILD_no_additional_protos.bazel", " "),
("BUILD_common_resources.bazel", " google/cloud/common_resources.proto"),
("BUILD_comment_common_resources.bazel", " "),
("BUILD_locations.bazel", " google/cloud/location/locations.proto"),
("BUILD_comment_locations.bazel", " "),
("BUILD_iam_policy.bazel", " google/iam/v1/iam_policy.proto"),
("BUILD_comment_iam_policy.bazel", " "),
(
"BUILD_iam_locations.bazel",
" google/cloud/location/locations.proto google/iam/v1/iam_policy.proto",
),
]
)
def test_gapic_inputs_parse_additional_protos(self, build_name, expected):
parsed = parse_build_file(build_file, "", build_name)
self.assertEqual(
expected,
parsed.additional_protos,
)

def test_gapic_inputs_parse_grpc_only_succeeds(self):
parsed = parse_build_file(build_file, "", "BUILD_grpc.bazel")
self.assertEqual("grpc", parsed.transport)
Expand Down