Skip to content

Commit 3bc99bf

Browse files
committed
feat: wip
1 parent 5702360 commit 3bc99bf

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

.generator/cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,13 @@ def _copy_changelog_to_docs(output: str, library_id: str):
561561
"""
562562
path_to_library = f"packages/{library_id}"
563563
source_path = f"{output}/{path_to_library}/CHANGELOG.md"
564+
565+
# If the source CHANGELOG.md doesn't exist, create it at the source location.
564566
if not os.path.lexists(source_path):
565-
# Create a valid CHANGELOG.md if it doesn't exist.
566567
content = "# Changelog\n"
567568
_write_text_file(source_path, content)
569+
570+
# Now, copy the (guaranteed to exist) source CHANGELOG.md to the docs directory.
568571
_copy_file_to_docs(output, library_id, "CHANGELOG.md")
569572

570573

.generator/test_cli.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,37 +1666,64 @@ def test_copy_readme_to_docs_source_not_exists(mocker):
16661666

16671667

16681668
def test_copy_changelog_to_docs_handles_symlink(mocker):
1669-
"""Tests that the CHANGELOG.md is created if it does not exist."""
1670-
mock_os_lexists = mocker.patch("os.path.lexists", return_value=False)
1671-
mock_open = mocker.patch("builtins.open", mocker.mock_open())
1672-
mocker.patch("cli._copy_file_to_docs") # Mock the call to _copy_file_to_docs
1669+
"""Tests that the CHANGELOG.md is created at the source and then copied to docs, handling symlinks."""
1670+
mock_makedirs = mocker.patch("os.makedirs")
1671+
mock_os_remove = mocker.patch("os.remove")
1672+
mock_os_islink = mocker.patch("os.path.islink")
1673+
mock_os_lexists = mocker.patch("os.path.lexists")
1674+
mock_write_text_file = mocker.patch("cli._write_text_file")
1675+
mock_read_text_file = mocker.patch("cli._read_text_file", return_value="# Changelog\n")
16731676

16741677
output = "output"
16751678
library_id = "google-cloud-language"
1676-
expected_source = f"{output}/packages/{library_id}/CHANGELOG.md"
1679+
source_changelog_path = f"{output}/packages/{library_id}/CHANGELOG.md"
1680+
docs_path = f"{output}/packages/{library_id}/docs"
1681+
destination_changelog_path = f"{docs_path}/CHANGELOG.md"
1682+
1683+
# Scenario: Source CHANGELOG.md does not exist initially, docs_path is a symlink
1684+
mock_os_lexists.side_effect = [False, True] # First for source_changelog_path, second for source_path in _copy_file_to_docs
1685+
1686+
def islink_side_effect(path):
1687+
if path == destination_changelog_path:
1688+
return False
1689+
if path == docs_path:
1690+
return True
1691+
return False
1692+
1693+
mock_os_islink = mocker.patch("os.path.islink", side_effect=islink_side_effect)
16771694

16781695
_copy_changelog_to_docs(output, library_id)
16791696

1680-
mock_os_lexists.assert_called_once_with(expected_source)
1681-
mock_open.assert_called_once_with(expected_source, "w")
1682-
mock_open().write.assert_called_once_with("# Changelog\n")
1697+
# Assert that source CHANGELOG.md was checked for existence
1698+
mock_os_lexists.assert_any_call(source_changelog_path)
1699+
1700+
# Assert that source CHANGELOG.md was created with default content
1701+
mock_write_text_file.assert_any_call(source_changelog_path, "# Changelog\n")
1702+
1703+
# Assert that _copy_file_to_docs was called to copy from source to destination
1704+
mock_read_text_file.assert_any_call(source_changelog_path)
1705+
mock_os_islink.assert_any_call(destination_changelog_path)
1706+
mock_os_islink.assert_any_call(docs_path)
1707+
mock_os_remove.assert_called_once_with(docs_path)
1708+
mock_makedirs.assert_called_once_with(Path(docs_path), exist_ok=True)
1709+
mock_write_text_file.assert_any_call(destination_changelog_path, "# Changelog\n")
16831710

16841711

16851712
def test_copy_changelog_to_docs_source_not_exists(mocker):
1686-
"""Tests that the function creates CHANGELOG.md with correct content if it does not exist."""
1687-
mock_os_lexists = mocker.patch("os.path.lexists", return_value=False)
1688-
mock_open = mocker.patch("builtins.open", mocker.mock_open())
1689-
mocker.patch("cli._copy_file_to_docs") # Mock the call to _copy_file_to_docs
1713+
"""Tests that CHANGELOG.md is created at source and then copied to docs."""
1714+
mock_lexists = mocker.patch("os.path.lexists", return_value=False)
1715+
mock_write_text = mocker.patch("cli._write_text_file")
1716+
mock_copy_file = mocker.patch("cli._copy_file_to_docs")
16901717

16911718
output = "output"
16921719
library_id = "google-cloud-language"
1693-
expected_source = f"{output}/packages/{library_id}/CHANGELOG.md"
1720+
source_path = f"{output}/packages/{library_id}/CHANGELOG.md"
16941721

16951722
_copy_changelog_to_docs(output, library_id)
16961723

1697-
mock_os_lexists.assert_called_once_with(expected_source)
1698-
mock_open.assert_called_once_with(expected_source, "w")
1699-
mock_open().write.assert_called_once_with("# Changelog\n")
1724+
mock_lexists.assert_called_once_with(source_path)
1725+
mock_write_text.assert_called_once_with(source_path, "# Changelog\n")
1726+
mock_copy_file.assert_called_once_with(output, library_id, "CHANGELOG.md")
17001727

17011728

17021729
def test_copy_changelog_to_docs_destination_path_is_symlink(mocker):

0 commit comments

Comments
 (0)