Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,30 @@ def _generate_repo_metadata_file(
_write_json_file(output_repo_metadata, metadata_content)


def _copy_readme_to_docs(output: str, library_id: str):
"""Copies the generated README.rst to the docs directory.

Args:
output(str): Path to the directory in the container where code
should be generated.
library_id(str): The library id.
"""
path_to_library = f"packages/{library_id}"
source_readme_path = Path(output) / path_to_library / "README.rst"
if source_readme_path.exists():
destination_docs_dir = Path(output) / path_to_library / "docs"
destination_readme_path = destination_docs_dir / "README.rst"

os.makedirs(destination_docs_dir, exist_ok=True)

# If docs/README.rst already exists (as a file or symlink), remove it.
if destination_readme_path.exists() or destination_readme_path.is_symlink():
destination_readme_path.unlink()

# Create a relative symlink from docs/README.rst to ../README.rst
os.symlink("../README.rst", destination_readme_path)


def handle_generate(
librarian: str = LIBRARIAN_DIR,
source: str = SOURCE_DIR,
Expand Down Expand Up @@ -542,6 +566,7 @@ def handle_generate(
_copy_files_needed_for_post_processing(output, input, library_id)
_generate_repo_metadata_file(output, library_id, source, apis_to_generate)
_run_post_processor(output, library_id)
_copy_readme_to_docs(output, library_id)
_clean_up_files_after_post_processing(output, library_id)
except Exception as e:
raise ValueError("Generation failed.") from e
Expand Down
45 changes: 44 additions & 1 deletion .generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
_update_version_for_library,
_verify_library_dist_name,
_verify_library_namespace,
_copy_readme_to_docs,
_write_json_file,
_write_text_file,
handle_build,
Expand Down Expand Up @@ -639,19 +640,35 @@ def test_handle_generate_success(
"cli._clean_up_files_after_post_processing"
)
mocker.patch("cli._generate_repo_metadata_file")
mock_makedirs = mocker.patch("os.makedirs")
mock_symlink = mocker.patch("os.symlink")
mock_path_exists = mocker.patch("pathlib.Path.exists", return_value=True)
mock_path_is_symlink = mocker.patch("pathlib.Path.is_symlink", return_value=False)
mock_path_unlink = mocker.patch("pathlib.Path.unlink")

handle_generate()

mock_run_post_processor.assert_called_once_with("output", "google-cloud-language")
mock_copy_files_needed_for_post_processing.assert_called_once_with(
"output", "input", "google-cloud-language"
)
mock_makedirs.assert_called_with(
Path("output") / "packages" / "google-cloud-language" / "docs", exist_ok=True
)
mock_path_unlink.assert_called_once()
mock_symlink.assert_called_with(
"../README.rst",
Path("output")
/ "packages"
/ "google-cloud-language"
/ "docs"
/ "README.rst",
)
mock_clean_up_files_after_post_processing.assert_called_once_with(
"output", "google-cloud-language"
)
mock_generate_api.assert_called_once()


def test_handle_generate_fail(caplog):
"""
Tests the failed to read `librarian/generate-request.json` file in handle_generates.
Expand Down Expand Up @@ -826,6 +843,32 @@ def test_copy_files_needed_for_post_processing_skips_metadata_if_not_exists(mock
mock_makedirs.assert_called()


def test_copy_readme_to_docs(mocker):
"""Tests that a symlink is created from docs/README.rst to ../README.rst."""
mock_makedirs = mocker.patch("os.makedirs")
mock_symlink = mocker.patch("os.symlink")
mock_path_exists = mocker.patch("pathlib.Path.exists", return_value=True)
mock_path_is_symlink = mocker.patch("pathlib.Path.is_symlink", return_value=False)
mock_path_unlink = mocker.patch("pathlib.Path.unlink")

_copy_readme_to_docs("output", "google-cloud-language")

mock_makedirs.assert_called_with(
Path("output") / "packages" / "google-cloud-language" / "docs", exist_ok=True
)
mock_path_unlink.assert_called_once()
mock_symlink.assert_called_with(
"../README.rst",
Path("output")
/ "packages"
/ "google-cloud-language"
/ "docs"
/ "README.rst",
)




def test_clean_up_files_after_post_processing_success(mocker):
mock_shutil_rmtree = mocker.patch("shutil.rmtree")
mock_os_remove = mocker.patch("os.remove")
Expand Down
Loading
Loading