Skip to content

Commit bb6cae6

Browse files
committed
update to account for api with proto/type dependency.
1 parent 6858033 commit bb6cae6

File tree

5 files changed

+82
-8
lines changed

5 files changed

+82
-8
lines changed

hermetic_build/common/tests/utils/proto_path_utils_unit_tests.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import unittest
1717
from pathlib import Path
1818
from common.utils.proto_path_utils import find_versioned_proto_path
19+
from common.utils.proto_path_utils import ends_with_version
1920

2021
script_dir = os.path.dirname(os.path.realpath(__file__))
2122
resources_dir = os.path.join(script_dir, "..", "resources")
@@ -37,3 +38,20 @@ def test_find_versioned_proto_without_version_return_itself(self):
3738
proto_path = "google/type/color.proto"
3839
expected = "google/type/color.proto"
3940
self.assertEqual(expected, find_versioned_proto_path(proto_path))
41+
42+
def test_ends_with_version_valid(self):
43+
self.assertTrue(ends_with_version("google/cloud/gsuiteaddons/v1"))
44+
self.assertTrue(ends_with_version("google/iam/v1beta"))
45+
self.assertTrue(ends_with_version("google/iam/v2betav1"))
46+
self.assertTrue(ends_with_version("google/cloud/alloydb/connectors/v1alpha"))
47+
self.assertTrue(ends_with_version("v1"))
48+
self.assertTrue(ends_with_version("anything/v123"))
49+
50+
def test_ends_with_version_invalid(self):
51+
self.assertFalse(ends_with_version("google/apps/script/type"))
52+
self.assertFalse(ends_with_version("google/apps/script/type/docs"))
53+
self.assertFalse(
54+
ends_with_version("google/cloud/alloydb/connectors/v123/something")
55+
)
56+
self.assertFalse(ends_with_version(""))
57+
self.assertFalse(ends_with_version("noVersion"))

hermetic_build/common/utils/proto_path_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,21 @@ def find_versioned_proto_path(proto_path: str) -> str:
3131
idx = proto_path.find(version)
3232
return proto_path[:idx] + version
3333
return proto_path
34+
35+
36+
def ends_with_version(proto_path: str) -> bool:
37+
"""
38+
Checks if a given proto_path string ends with a version identifier.
39+
40+
:param proto_path: The proto_path string to check.
41+
42+
:return:
43+
True if the proto_path ends with a version, False otherwise.
44+
"""
45+
version_regex = re.compile(r"^v[1-9].*")
46+
parts = proto_path.rsplit("/", 1)
47+
if len(parts) > 1:
48+
last_part = parts[1]
49+
else:
50+
last_part = parts[0]
51+
return bool(version_regex.match(last_part))

hermetic_build/library_generation/generate_repo.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
import shutil
1615
import copy
16+
import re
17+
import shutil
1718
from pathlib import Path
1819
from typing import Optional
1920
import library_generation.utils.utilities as util
2021
from common.model.generation_config import GenerationConfig
2122
from common.model.library_config import LibraryConfig
23+
from common.utils.proto_path_utils import ends_with_version
2224
from library_generation.generate_composed_library import generate_composed_library
2325
from library_generation.utils.monorepo_postprocessor import monorepo_postprocessing
2426

@@ -125,7 +127,8 @@ def _get_target_libraries_from_api_path(
125127
) -> list[LibraryConfig]:
126128
"""
127129
Retrieves a copy of the LibraryConfig objects that contain the specified
128-
target API path, removed other proto_path from LibraryConfig if any.
130+
target API path, removed other proto_path versions from LibraryConfig if any.
131+
proto_path that is dependency, not another version is kept.
129132
130133
:param config: The GenerationConfig object.
131134
:param target_api_path: The target proto path to search for.
@@ -136,11 +139,15 @@ def _get_target_libraries_from_api_path(
136139
"""
137140
target_libraries = []
138141
for library in config.libraries:
142+
target_library = copy.deepcopy(library)
143+
gapic_config_list = []
139144
for item in library.gapic_configs:
140-
if item.proto_path == target_api_path:
141-
target_library = copy.deepcopy(library)
142-
target_library.set_gapic_configs([GapicConfig(target_api_path)])
143-
target_libraries.append(target_library)
144-
return target_libraries
145-
145+
if item.proto_path == target_api_path or not ends_with_version(
146+
item.proto_path
147+
):
148+
gapic_config_list.append(GapicConfig(item.proto_path))
149+
if gapic_config_list:
150+
target_library.set_gapic_configs(gapic_config_list)
151+
target_libraries.append(target_library)
152+
return target_libraries
146153
return []

hermetic_build/library_generation/tests/cli/versions.txt

Whitespace-only changes.

hermetic_build/library_generation/tests/generate_repo_unit_tests.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,37 @@ def test_get_target_library_returns_selected_api_path(self):
8383

8484
self.assertEqual([another_library_v1], target_libraries)
8585

86+
def test_get_target_library_returns_selected_api_path_plus_dep(self):
87+
one_library = GenerateRepoTest.__get_an_empty_library_config()
88+
one_library.api_shortname = "one_library"
89+
one_library.gapic_configs = [GapicConfig("google/cloud/one/library/v1")]
90+
another_library = GenerateRepoTest.__get_an_empty_library_config()
91+
another_library.api_shortname = "another_library"
92+
another_library_gapic_config = list()
93+
another_library_gapic_config.append(
94+
GapicConfig("google/cloud/another/library/v1")
95+
)
96+
another_library_gapic_config.append(
97+
GapicConfig("google/cloud/another/library/v2")
98+
)
99+
another_library_gapic_config.append(
100+
GapicConfig("google/cloud/another/library/type")
101+
)
102+
another_library.gapic_configs = another_library_gapic_config
103+
config = GenerateRepoTest.__get_an_empty_generation_config()
104+
config.libraries.extend([one_library, another_library])
105+
target_libraries = get_target_libraries(
106+
config, target_api_path="google/cloud/another/library/v2"
107+
)
108+
another_library_v1 = GenerateRepoTest.__get_an_empty_library_config()
109+
another_library_v1.api_shortname = "another_library"
110+
another_library_v1.gapic_configs = [
111+
GapicConfig("google/cloud/another/library/v2"),
112+
GapicConfig("google/cloud/another/library/type"),
113+
]
114+
115+
self.assertEqual([another_library_v1], target_libraries)
116+
86117
@staticmethod
87118
def __get_an_empty_generation_config() -> GenerationConfig:
88119
return GenerationConfig(

0 commit comments

Comments
 (0)