@@ -1666,37 +1666,64 @@ def test_copy_readme_to_docs_source_not_exists(mocker):
16661666
16671667
16681668def 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
16851712def 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
17021729def test_copy_changelog_to_docs_destination_path_is_symlink (mocker ):
0 commit comments