diff --git a/.gitignore b/.gitignore index 05901afab0..98c7b77b4a 100644 --- a/.gitignore +++ b/.gitignore @@ -15,16 +15,12 @@ target/ # Vscode Settings .vscode/settings.json -*.iml - # Python **/__pycache__/ .venv # library generation -**/output/ **/googleapis -library_generation/test/resources/test_monorepo_postprocessing/ **/*egg-info/ **/build/ **/dist/ diff --git a/library_generation/generate_repo.py b/library_generation/generate_repo.py index c133c620dd..9a97e3555b 100755 --- a/library_generation/generate_repo.py +++ b/library_generation/generate_repo.py @@ -77,9 +77,11 @@ def generate( config_path=generation_config_yaml, config=config, repository_path=repository_path, - target_library_names=target_library_names.split(",") - if target_library_names is not None - else target_library_names, + target_library_names=( + target_library_names.split(",") + if target_library_names is not None + else target_library_names + ), ) diff --git a/library_generation/test/compare_poms.py b/library_generation/test/compare_poms.py index a1ef443799..11cc77c8b4 100644 --- a/library_generation/test/compare_poms.py +++ b/library_generation/test/compare_poms.py @@ -4,6 +4,7 @@ The only comparison points are: element path (e.g. project/dependencies) and element text There is a special case for `dependency`, where the maven coordinates are prepared as well """ + from library_generation.utils.utilities import eprint import xml.etree.ElementTree as et from collections import Counter diff --git a/library_generation/test/integration_tests.py b/library_generation/test/integration_tests.py index 08aef860f0..9c4663e373 100644 --- a/library_generation/test/integration_tests.py +++ b/library_generation/test/integration_tests.py @@ -51,9 +51,16 @@ class IntegrationTest(unittest.TestCase): def setUpClass(cls) -> None: IntegrationTest.__build_image(docker_file=build_file, cwd=repo_root_dir) - def test_entry_point_running_in_container(self): - shutil.rmtree(f"{golden_dir}", ignore_errors=True) + @classmethod + def tearDownClass(cls) -> None: + cls.__remove_docker_image() + + @classmethod + def setUp(cls) -> None: + cls.__remove_generated_files() os.makedirs(f"{golden_dir}", exist_ok=True) + + def test_entry_point_running_in_container(self): config_files = self.__get_config_files(config_dir) for repo, config_file in config_files: config = from_yaml(config_file) @@ -168,6 +175,7 @@ def test_entry_point_running_in_container(self): "The generated PR description does not match the expected golden file", ) print(" PR description comparison succeed.") + self.__remove_generated_files() @classmethod def __build_image(cls, docker_file: str, cwd: str): @@ -176,6 +184,13 @@ def __build_image(cls, docker_file: str, cwd: str): cwd=cwd, ) + @classmethod + def __remove_generated_files(cls): + # uncomment this line when the generated files don't owned by root. + # shutil.rmtree(f"{output_dir}", ignore_errors=True) + if os.path.isdir(f"{golden_dir}"): + shutil.rmtree(f"{golden_dir}") + @classmethod def __pull_repo_to(cls, dest: Path, repo: str, committish: str) -> str: shutil.rmtree(dest, ignore_errors=True) @@ -323,3 +338,7 @@ def __recursive_diff_files( cls.__recursive_diff_files( sub_dcmp, diff_files, left_only, right_only, dirname + sub_dirname + "/" ) + + @classmethod + def __remove_docker_image(cls): + subprocess.check_call(["docker", "image", "rmi", image_tag]) diff --git a/library_generation/test/utilities_unit_tests.py b/library_generation/test/utilities_unit_tests.py index bc8b965c2c..5e5869d0c1 100644 --- a/library_generation/test/utilities_unit_tests.py +++ b/library_generation/test/utilities_unit_tests.py @@ -15,7 +15,7 @@ """ Unit tests for python scripts """ - +import shutil import unittest import os import io @@ -313,6 +313,7 @@ def test_generate_prerequisite_files_non_monorepo_success(self): file_comparator.compare_files( f"{library_path}/owlbot.py", f"{library_path}/owlbot-golden.py" ) + self.__remove_prerequisite_files(path=library_path, is_monorepo=False) def test_generate_prerequisite_files_monorepo_success(self): library_path = self.__setup_prerequisite_files(num_libraries=2) @@ -328,6 +329,7 @@ def test_generate_prerequisite_files_monorepo_success(self): file_comparator.compare_files( f"{library_path}/owlbot.py", f"{library_path}/owlbot-golden.py" ) + self.__remove_prerequisite_files(path=library_path) def test_prepare_repo_monorepo_success(self): gen_config = self.__get_a_gen_config(2) @@ -373,6 +375,7 @@ def test_prepare_repo_split_repo_success(self): self.assertEqual("output", Path(repo_config.output_folder).name) library_path = sorted([Path(key).name for key in repo_config.libraries]) self.assertEqual(["misc"], library_path) + shutil.rmtree(repo_config.output_folder) def __setup_prerequisite_files( self, num_libraries: int, library_type: str = "GAPIC_AUTO" @@ -448,6 +451,16 @@ def __get_a_gen_config( libraries=libraries, ) + @staticmethod + def __remove_prerequisite_files(path: str, is_monorepo: bool = True) -> None: + os.remove(f"{path}/.repo-metadata.json") + os.remove(f"{path}/owlbot.py") + if is_monorepo: + os.remove(f"{path}/.OwlBot-hermetic.yaml") + return + if os.path.isdir(f"{path}/.github"): + shutil.rmtree(f"{path}/.github", ignore_errors=True) + if __name__ == "__main__": unittest.main() diff --git a/library_generation/test/utils/monorepo_postprocessor_unit_tests.py b/library_generation/test/utils/monorepo_postprocessor_unit_tests.py index c7df83acaf..5dfafe2086 100644 --- a/library_generation/test/utils/monorepo_postprocessor_unit_tests.py +++ b/library_generation/test/utils/monorepo_postprocessor_unit_tests.py @@ -43,3 +43,4 @@ def test_monorepo_postprocessing_valid_repository_success(self): expect=f"{repository_path}/gapic-libraries-bom/pom-golden.xml", actual=f"{repository_path}/gapic-libraries-bom/pom.xml", ) + cleanup(files)