Skip to content

Commit c27510f

Browse files
committed
fix: fix_poms.py to add one line in versions.txt for new library
1 parent 2c94a9e commit c27510f

File tree

2 files changed

+117
-13
lines changed

2 files changed

+117
-13
lines changed

hermetic_build/library_generation/owlbot/src/fix_poms.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,10 @@ def main(versions_file, monorepo):
362362
base_name = re.sub(
363363
r"^(google-cloud-|grpc-google-cloud-|proto-google-cloud-)", "", artifact_id
364364
)
365-
release_please_key = f"java-{base_name}"
365+
release_please_key = base_name
366366
name = repo_metadata["name_pretty"]
367367
existing_modules = load_versions(versions_file, group_id)
368+
original_modules = set(existing_modules.keys())
368369
print(f"monorepo? {monorepo}")
369370

370371
# extra modules that need to be manages in versions.txt
@@ -597,24 +598,38 @@ def main(versions_file, monorepo):
597598
)
598599

599600
print(f"updating modules in {versions_file}")
601+
# The parent pom is not a standalone artifact, so we don't add it to
602+
# versions.txt.
600603
existing_modules.pop(parent_artifact_id)
601604

602-
# Consolidate all modules into a single `java-` entry.
603-
# It's safe to use the first module's version and group ID,
604-
# as they will be consistent for a new library.
605+
# Determine if new modules were added.
606+
added_modules = set(existing_modules.keys()) - original_modules
607+
if added_modules:
608+
print(f"New modules detected: {added_modules}")
609+
# Remove all the individual components that were just added.
610+
for key in added_modules:
611+
existing_modules.pop(key)
612+
# Add the single consolidated entry instead.
613+
existing_modules[release_please_key] = module.Module(
614+
group_id=group_id,
615+
artifact_id=release_please_key,
616+
version=main_module.version,
617+
release_version=main_module.release_version,
618+
)
605619

606-
templates.render(
607-
template_name="versions.txt.j2",
608-
output_name=versions_file,
609-
# Only write the main artifact module to versions.txt
610-
modules=[
611-
module.Module(
612-
group_id=main_module.group_id,
613-
artifact_id=release_please_key,
620+
# add extra modules to versions.txt
621+
for dependency_module in extra_managed_modules:
622+
if dependency_module not in existing_modules:
623+
existing_modules[dependency_module] = module.Module(
624+
group_id=__proto_group_id(group_id),
625+
artifact_id=dependency_module,
614626
version=main_module.version,
615627
release_version=main_module.release_version,
616628
)
617-
],
629+
templates.render(
630+
template_name="versions.txt.j2",
631+
output_name=versions_file,
632+
modules=existing_modules.values(),
618633
)
619634

620635

hermetic_build/library_generation/tests/owlbot/fix_poms_unit_tests.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import os
1515
import shutil
1616
import unittest
17+
import tempfile
18+
from pathlib import Path
1719
from library_generation.owlbot.src.fix_poms import main
1820
from library_generation.tests.compare_poms import compare_pom_in_subdir
1921

@@ -22,6 +24,15 @@
2224

2325

2426
class FixPomsTest(unittest.TestCase):
27+
def setUp(self):
28+
self.temp_dir = tempfile.mkdtemp()
29+
self.start_dir = os.getcwd()
30+
os.chdir(self.temp_dir)
31+
32+
def tearDown(self):
33+
os.chdir(self.start_dir)
34+
35+
2536
def test_update_poms_group_id_does_not_start_with_google_correctly(self):
2637
ad_manager_resource = os.path.join(resources_dir, "java-admanager")
2738
versions_file = os.path.join(ad_manager_resource, "versions.txt")
@@ -35,6 +46,84 @@ def test_update_poms_group_id_does_not_start_with_google_correctly(self):
3546
for sub_dir in sub_dirs:
3647
self.__remove_file_in_subdir(ad_manager_resource, sub_dir)
3748

49+
50+
def test_add_new_library(self):
51+
# Setup test environment in a structure similar to a real repo
52+
repo_dir = os.path.join(self.temp_dir, "google-cloud-java")
53+
os.makedirs(repo_dir)
54+
source_resource_dir = os.path.join(resources_dir, "google-cloud-java")
55+
new_library_dir_name = "java-newapi"
56+
new_library_path = os.path.join(repo_dir, new_library_dir_name)
57+
58+
# Copy test resources to the temporary directory
59+
shutil.copytree(os.path.join(source_resource_dir, "java-newapi"), new_library_path)
60+
61+
# Modify the .repo-metadata.json in the temporary new library folder
62+
repo_metadata_path = os.path.join(new_library_path, ".repo-metadata.json")
63+
with open(repo_metadata_path, "r") as f:
64+
repo_metadata_content = f.read()
65+
repo_metadata_content = repo_metadata_content.replace("google-cloud-newfolder", "google-cloud-newapi")
66+
with open(repo_metadata_path, "w") as f:
67+
f.write(repo_metadata_content)
68+
69+
source_versions_file = os.path.join(source_resource_dir, "versions.txt")
70+
dest_versions_file = os.path.join(repo_dir, "versions.txt")
71+
shutil.copyfile(source_versions_file, dest_versions_file)
72+
73+
# Create dummy directories for proto and grpc modules.
74+
# In the full generation process, `generate_library.sh` would create these.
75+
os.makedirs(os.path.join(new_library_path, "proto-google-cloud-newapi-v1"))
76+
os.makedirs(os.path.join(new_library_path, "grpc-google-cloud-newapi-v1"))
77+
os.chdir(new_library_path)
78+
79+
# Get initial state of versions.txt (excluding comments and empty lines)
80+
with open(dest_versions_file, "r") as f:
81+
initial_active_lines = [line.strip() for line in f if line.strip() and not line.strip().startswith("#")]
82+
83+
# Execute the main function from fix_poms.py
84+
main(dest_versions_file, "true")
85+
86+
# --- Verify versions.txt ---
87+
with open(dest_versions_file, "r") as f:
88+
updated_versions_content = f.readlines()
89+
90+
updated_active_lines = [line.strip() for line in updated_versions_content if line.strip() and not line.strip().startswith("#")]
91+
92+
self.assertEqual(len(initial_active_lines) + 1, len(updated_active_lines),
93+
"A single line should have been added to versions.txt")
94+
95+
expected_new_line = "newapi:0.0.0:0.0.1-SNAPSHOT"
96+
self.assertTrue(any(expected_new_line in line for line in updated_active_lines),
97+
f"The new line '{expected_new_line}' was not found in versions.txt")
98+
99+
filtered_updated_lines = [line for line in updated_active_lines if expected_new_line not in line]
100+
self.assertCountEqual(initial_active_lines, filtered_updated_lines,
101+
"Existing lines in versions.txt should be unaffected.")
102+
self.assertFalse(any("google-cloud-newapi" in line for line in updated_versions_content),
103+
"The artifactId 'google-cloud-newapi' should not be in versions.txt")
104+
105+
# --- Verify generated pom.xml files ---
106+
expected_poms = [
107+
"pom.xml",
108+
"google-cloud-newapi/pom.xml",
109+
"google-cloud-newapi-bom/pom.xml",
110+
"proto-google-cloud-newapi-v1/pom.xml",
111+
"grpc-google-cloud-newapi-v1/pom.xml",
112+
]
113+
for pom_path in expected_poms:
114+
full_pom_path = os.path.join(new_library_path, pom_path)
115+
self.assertTrue(os.path.exists(full_pom_path), f"Expected POM file not found: {full_pom_path}")
116+
with open(full_pom_path, "r") as f:
117+
pom_content = f.read()
118+
self.assertIn("<!-- {x-version-update:newapi:current} -->", pom_content,
119+
f"x-version-update tag in {pom_path} does not reference 'newapi'")
120+
# Ensure the artifactId is not changed to the release_please_key
121+
if pom_path == "google-cloud-newapi/pom.xml":
122+
self.assertIn("<artifactId>google-cloud-newapi</artifactId>", pom_content)
123+
if pom_path == "proto-google-cloud-newapi-v1/pom.xml":
124+
self.assertIn("<artifactId>proto-google-cloud-newapi-v1</artifactId>", pom_content)
125+
126+
38127
@classmethod
39128
def __copy__golden(cls, base_dir: str, subdir: str):
40129
golden = os.path.join(base_dir, subdir, "pom-golden.xml")

0 commit comments

Comments
 (0)