1414import os
1515import shutil
1616import unittest
17+ import tempfile
18+ from pathlib import Path
1719from library_generation .owlbot .src .fix_poms import main
1820from library_generation .tests .compare_poms import compare_pom_in_subdir
1921
2224
2325
2426class 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