Skip to content

Commit 6ca20e5

Browse files
authored
fix: ignore comment in BUILD (#2492)
In this PR: - Ignore comment (leading with `#`) when parsing BUILD.bazel - Add unit tests Context: There's a [BUILD](https://github.com/googleapis/googleapis/blob/master/google/cloud/resourcemanager/v3/BUILD.bazel#L52C10-L52C50) in googleapis has common_resources.proto comment out. We need to adjust the regex pattern to adjust this case.
1 parent 6e8d098 commit 6ca20e5

13 files changed

+77
-33
lines changed

library_generation/model/gapic_inputs.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@
3131
(.*?)
3232
\)
3333
"""
34-
resource_pattern = r"//google/cloud:common_resources_proto"
35-
location_pattern = r"//google/cloud/location:location_proto"
36-
iam_pattern = r"//google/iam/v1:iam_policy_proto"
34+
# match a line which the first character is "#".
35+
comment_pattern = r"^\s*\#+"
36+
pattern_to_proto = {
37+
r"//google/cloud:common_resources_proto": "google/cloud/common_resources.proto",
38+
r"//google/cloud/location:location_proto": "google/cloud/location/locations.proto",
39+
r"//google/iam/v1:iam_policy_proto": "google/iam/v1/iam_policy.proto",
40+
}
3741
transport_pattern = r"transport = \"(.*?)\""
3842
rest_pattern = r"rest_numeric_enums = True"
3943
gapic_yaml_pattern = r"gapic_yaml = \"(.*?)\""
@@ -97,7 +101,9 @@ def parse(
97101
if len(assembly_target) > 0:
98102
include_samples = __parse_include_samples(assembly_target[0])
99103
if len(gapic_target) == 0:
100-
return GapicInputs(include_samples=include_samples)
104+
return GapicInputs(
105+
additional_protos=additional_protos, include_samples=include_samples
106+
)
101107

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

120126
def __parse_additional_protos(proto_library_target: str) -> str:
121127
res = [" "]
122-
if len(re.findall(resource_pattern, proto_library_target)) != 0:
123-
res.append("google/cloud/common_resources.proto")
124-
if len(re.findall(location_pattern, proto_library_target)) != 0:
125-
res.append("google/cloud/location/locations.proto")
126-
if len(re.findall(iam_pattern, proto_library_target)) != 0:
127-
res.append("google/iam/v1/iam_policy.proto")
128+
lines = proto_library_target.split("\n")
129+
for line in lines:
130+
if len(re.findall(comment_pattern, line)) != 0:
131+
# skip a line which the first charactor is "#" since it's
132+
# a comment.
133+
continue
134+
for pattern in pattern_to_proto:
135+
if len(re.findall(pattern, line)) == 0:
136+
continue
137+
res.append(pattern_to_proto[pattern])
128138
return " ".join(res)
129139

130140

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
proto_library_with_info(
2+
deps = [
3+
#"//google/cloud:common_resources_proto",
4+
]
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
proto_library_with_info(
2+
deps = [
3+
# "//google/iam/v1:iam_policy_proto",
4+
]
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
proto_library_with_info(
2+
deps = [
3+
# "//google/cloud/location:location_proto",
4+
]
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
proto_library_with_info(
2+
deps = [
3+
"//google/cloud:common_resources_proto",
4+
]
5+
)
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
# this file is only used in testing `get_gapic_additional_protos_from_BUILD` in utilities.sh
2-
31
proto_library_with_info(
42
deps = [
5-
"//google/iam/v1:iam_policy_proto",
63
"//google/cloud/location:location_proto",
4+
"//google/iam/v1:iam_policy_proto",
75
]
86
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
proto_library_with_info(
2+
deps = [
3+
"//google/iam/v1:iam_policy_proto",
4+
]
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
proto_library_with_info(
2+
deps = [
3+
"//google/cloud/location:location_proto",
4+
]
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
proto_library_with_info(
2+
deps = [
3+
]
4+
)

library_generation/test/resources/search_additional_protos/BUILD_common_resources.bazel

-6
This file was deleted.

library_generation/test/resources/search_additional_protos/BUILD_iam_policy.bazel

-7
This file was deleted.

library_generation/test/resources/search_additional_protos/BUILD_locations.bazel

-7
This file was deleted.

library_generation/test/unit_tests.py

+22
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,28 @@ def test_from_yaml_succeeds(self):
214214
self.assertEqual("google/cloud/asset/v1p5beta1", gapics[3].proto_path)
215215
self.assertEqual("google/cloud/asset/v1p7beta1", gapics[4].proto_path)
216216

217+
@parameterized.expand(
218+
[
219+
("BUILD_no_additional_protos.bazel", " "),
220+
("BUILD_common_resources.bazel", " google/cloud/common_resources.proto"),
221+
("BUILD_comment_common_resources.bazel", " "),
222+
("BUILD_locations.bazel", " google/cloud/location/locations.proto"),
223+
("BUILD_comment_locations.bazel", " "),
224+
("BUILD_iam_policy.bazel", " google/iam/v1/iam_policy.proto"),
225+
("BUILD_comment_iam_policy.bazel", " "),
226+
(
227+
"BUILD_iam_locations.bazel",
228+
" google/cloud/location/locations.proto google/iam/v1/iam_policy.proto",
229+
),
230+
]
231+
)
232+
def test_gapic_inputs_parse_additional_protos(self, build_name, expected):
233+
parsed = parse_build_file(build_file, "", build_name)
234+
self.assertEqual(
235+
expected,
236+
parsed.additional_protos,
237+
)
238+
217239
def test_gapic_inputs_parse_grpc_only_succeeds(self):
218240
parsed = parse_build_file(build_file, "", "BUILD_grpc.bazel")
219241
self.assertEqual("grpc", parsed.transport)

0 commit comments

Comments
 (0)