Skip to content

Commit e9c939b

Browse files
committed
create changelog if not present
1 parent 1514c2d commit e9c939b

File tree

2 files changed

+39
-53
lines changed

2 files changed

+39
-53
lines changed

.generator/cli.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ def _clean_up_files_after_post_processing(output: str, library_id: str):
379379
shutil.rmtree(f"{output}/owl-bot-staging", ignore_errors=True)
380380

381381
# Safely remove specific files if they exist using pathlib.
382-
Path(f"{output}/{path_to_library}/CHANGELOG.md").unlink(missing_ok=True)
383382
Path(f"{output}/{path_to_library}/docs/CHANGELOG.md").unlink(missing_ok=True)
384383

385384
# The glob loops are already safe, as they do nothing if no files match.
@@ -524,9 +523,7 @@ def _copy_file_to_docs(output: str, library_id: str, filename: str):
524523
if not os.path.lexists(source_path):
525524
return
526525

527-
# Read the content from the source, which will resolve any symlinks.
528-
with open(source_path, "r") as f:
529-
content = f.read()
526+
content = _read_text_file(source_path)
530527

531528
# Remove any symlinks at the destination to prevent errors.
532529
if os.path.islink(destination_path):
@@ -536,10 +533,7 @@ def _copy_file_to_docs(output: str, library_id: str, filename: str):
536533

537534
# Ensure the destination directory exists as a real directory.
538535
os.makedirs(docs_path, exist_ok=True)
539-
540-
# Write the content to the destination, creating a new physical file.
541-
with open(destination_path, "w") as f:
542-
f.write(content)
536+
_write_text_file(destination_path, content)
543537

544538

545539
def _copy_readme_to_docs(output: str, library_id: str):
@@ -565,6 +559,12 @@ def _copy_changelog_to_docs(output: str, library_id: str):
565559
should be generated.
566560
library_id(str): The library id.
567561
"""
562+
path_to_library = f"packages/{library_id}"
563+
source_path = f"{output}/{path_to_library}/CHANGELOG.md"
564+
if not os.path.lexists(source_path):
565+
# Create a valid CHANGELOG.md if it doesn't exist.
566+
content = "# Changelog\n"
567+
_write_text_file(source_path, content)
568568
_copy_file_to_docs(output, library_id, "CHANGELOG.md")
569569

570570

.generator/test_cli.py

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ def test_copy_readme_to_docs(mocker):
15401540
mock_os_islink.assert_any_call(expected_destination)
15411541
mock_os_islink.assert_any_call(expected_docs_path)
15421542
mock_os_remove.assert_not_called()
1543-
mock_makedirs.assert_called_once_with(expected_docs_path, exist_ok=True)
1543+
mock_makedirs.assert_called_with(Path(expected_docs_path), exist_ok=True)
15441544
mock_open.assert_any_call(expected_destination, "w")
15451545
mock_open().write.assert_called_once_with("dummy content")
15461546

@@ -1575,7 +1575,7 @@ def islink_side_effect(path):
15751575
mock_os_islink.assert_any_call(expected_destination)
15761576
mock_os_islink.assert_any_call(expected_docs_path)
15771577
mock_os_remove.assert_called_once_with(expected_docs_path)
1578-
mock_makedirs.assert_called_once_with(expected_docs_path, exist_ok=True)
1578+
mock_makedirs.assert_called_with(Path(expected_docs_path), exist_ok=True)
15791579
mock_open.assert_any_call(expected_destination, "w")
15801580
mock_open().write.assert_called_once_with("dummy content")
15811581

@@ -1621,81 +1621,67 @@ def test_copy_readme_to_docs_source_not_exists(mocker):
16211621

16221622

16231623
def test_copy_changelog_to_docs_handles_symlink(mocker):
1624-
"""Tests that the CHANGELOG.md is copied to the docs directory, handling symlinks."""
1625-
mock_makedirs = mocker.patch("os.makedirs")
1626-
mock_shutil_copy = mocker.patch("shutil.copy")
1627-
mock_os_remove = mocker.patch("os.remove")
1628-
mock_os_lexists = mocker.patch("os.path.lexists", return_value=True)
1629-
mock_open = mocker.patch("builtins.open", mocker.mock_open(read_data="dummy content"))
1624+
"""Tests that the CHANGELOG.md is created if it does not exist."""
1625+
mock_os_lexists = mocker.patch("os.path.lexists", return_value=False)
1626+
mock_open = mocker.patch("builtins.open", mocker.mock_open())
1627+
mocker.patch("cli._copy_file_to_docs") # Mock the call to _copy_file_to_docs
16301628

16311629
output = "output"
16321630
library_id = "google-cloud-language"
16331631
expected_source = f"{output}/packages/{library_id}/CHANGELOG.md"
1634-
expected_docs_path = f"{output}/packages/{library_id}/docs"
1635-
expected_destination = f"{expected_docs_path}/CHANGELOG.md"
1636-
1637-
def islink_side_effect(path):
1638-
if path == expected_destination:
1639-
return False
1640-
if path == expected_docs_path:
1641-
return True
1642-
return False
1643-
1644-
mock_os_islink = mocker.patch("os.path.islink", side_effect=islink_side_effect)
16451632

16461633
_copy_changelog_to_docs(output, library_id)
16471634

16481635
mock_os_lexists.assert_called_once_with(expected_source)
1649-
mock_open.assert_any_call(expected_source, "r")
1650-
mock_os_islink.assert_any_call(expected_destination)
1651-
mock_os_islink.assert_any_call(expected_docs_path)
1652-
mock_os_remove.assert_called_once_with(expected_docs_path)
1653-
mock_makedirs.assert_called_once_with(expected_docs_path, exist_ok=True)
1654-
mock_open.assert_any_call(expected_destination, "w")
1655-
mock_open().write.assert_called_once_with("dummy content")
1636+
mock_open.assert_called_once_with(expected_source, "w")
1637+
mock_open().write.assert_called_once_with("# Changelog\n")
16561638

16571639

16581640
def test_copy_changelog_to_docs_source_not_exists(mocker):
1659-
"""Tests that the function returns early if the source CHANGELOG.md does not exist."""
1660-
mock_makedirs = mocker.patch("os.makedirs")
1661-
mock_shutil_copy = mocker.patch("shutil.copy")
1662-
mock_os_islink = mocker.patch("os.path.islink")
1663-
mock_os_remove = mocker.patch("os.remove")
1641+
"""Tests that the function creates CHANGELOG.md with correct content if it does not exist."""
16641642
mock_os_lexists = mocker.patch("os.path.lexists", return_value=False)
1665-
mock_open = mocker.patch("builtins.open", mocker.mock_open(read_data="dummy content"))
1643+
mock_open = mocker.patch("builtins.open", mocker.mock_open())
1644+
mocker.patch("cli._copy_file_to_docs") # Mock the call to _copy_file_to_docs
16661645

16671646
output = "output"
16681647
library_id = "google-cloud-language"
1669-
_copy_changelog_to_docs(output, library_id)
1648+
expected_source = f"{output}/packages/{library_id}/CHANGELOG.md"
16701649

1671-
expected_source = "output/packages/google-cloud-language/CHANGELOG.md"
1650+
_copy_changelog_to_docs(output, library_id)
16721651

16731652
mock_os_lexists.assert_called_once_with(expected_source)
1674-
mock_open.assert_not_called()
1675-
mock_os_islink.assert_not_called()
1676-
mock_os_remove.assert_not_called()
1677-
mock_makedirs.assert_not_called()
1678-
mock_shutil_copy.assert_not_called()
1653+
mock_open.assert_called_once_with(expected_source, "w")
1654+
mock_open().write.assert_called_once_with("# Changelog\n")
16791655

16801656

16811657
def test_copy_changelog_to_docs_destination_path_is_symlink(mocker):
16821658
"""Tests that the CHANGELOG.md is copied to the docs directory, handling destination_path being a symlink."""
16831659
mock_makedirs = mocker.patch("os.makedirs")
16841660
mock_shutil_copy = mocker.patch("shutil.copy")
1685-
mock_os_islink = mocker.patch("os.path.islink", return_value=True)
16861661
mock_os_remove = mocker.patch("os.remove")
16871662
mock_os_lexists = mocker.patch("os.path.lexists", return_value=True)
1688-
mock_open = mocker.patch("builtins.open", mocker.mock_open(read_data="dummy content"))
1663+
mock_open = mocker.patch("builtins.open", mocker.mock_open(read_data="# Changelog\n"))
16891664

16901665
output = "output"
16911666
library_id = "google-cloud-language"
1692-
_copy_changelog_to_docs(output, library_id)
1693-
16941667
expected_destination = "output/packages/google-cloud-language/docs/CHANGELOG.md"
1695-
mock_os_remove.assert_called_once_with(expected_destination)
1668+
expected_docs_path = "output/packages/google-cloud-language/docs"
16961669

1670+
def islink_side_effect(path):
1671+
if path == expected_destination:
1672+
return True
1673+
if path == expected_docs_path:
1674+
return False
1675+
return False
16971676

1698-
def test_copy_readme_to_docs_source_not_exists(mocker):
1677+
mock_os_islink = mocker.patch("os.path.islink", side_effect=islink_side_effect)
1678+
1679+
_copy_changelog_to_docs(output, library_id)
1680+
1681+
mock_os_remove.assert_called_once_with(expected_destination)
1682+
mock_makedirs.assert_called_with(Path(expected_docs_path), exist_ok=True)
1683+
mock_open.assert_any_call(expected_destination, "w")
1684+
mock_open().write.assert_called_with("# Changelog\n")
16991685
"""Tests that the function returns early if the source README.rst does not exist."""
17001686
mock_makedirs = mocker.patch("os.makedirs")
17011687
mock_shutil_copy = mocker.patch("shutil.copy")

0 commit comments

Comments
 (0)