|
70 | 70 | _verify_library_namespace, |
71 | 71 | _write_json_file, |
72 | 72 | _write_text_file, |
| 73 | + _copy_readme_to_docs, |
73 | 74 | handle_build, |
74 | 75 | handle_configure, |
75 | 76 | handle_generate, |
@@ -1514,3 +1515,100 @@ def test_stage_gapic_library(mocker): |
1514 | 1515 | mock_shutil_copytree.assert_called_once_with( |
1515 | 1516 | tmp_dir, staging_dir, dirs_exist_ok=True |
1516 | 1517 | ) |
| 1518 | + |
| 1519 | + |
| 1520 | +def test_copy_readme_to_docs(mocker): |
| 1521 | + """Tests that the README.rst is copied to the docs directory, handling symlinks.""" |
| 1522 | + mock_makedirs = mocker.patch("os.makedirs") |
| 1523 | + mock_shutil_copy = mocker.patch("shutil.copy") |
| 1524 | + mock_os_islink = mocker.patch("os.path.islink", return_value=False) |
| 1525 | + mock_os_remove = mocker.patch("os.remove") |
| 1526 | + mock_os_lexists = mocker.patch("os.path.lexists", return_value=True) |
| 1527 | + mock_open = mocker.patch("builtins.open", mocker.mock_open(read_data="dummy content")) |
| 1528 | + |
| 1529 | + output = "output" |
| 1530 | + library_id = "google-cloud-language" |
| 1531 | + _copy_readme_to_docs(output, library_id) |
| 1532 | + |
| 1533 | + expected_source = "output/packages/google-cloud-language/README.rst" |
| 1534 | + expected_docs_path = "output/packages/google-cloud-language/docs" |
| 1535 | + expected_destination = "output/packages/google-cloud-language/docs/README.rst" |
| 1536 | + |
| 1537 | + mock_os_lexists.assert_called_once_with(expected_source) |
| 1538 | + mock_open.assert_any_call(expected_source, "r") |
| 1539 | + mock_os_islink.assert_any_call(expected_destination) |
| 1540 | + mock_os_islink.assert_any_call(expected_docs_path) |
| 1541 | + mock_os_remove.assert_not_called() |
| 1542 | + mock_makedirs.assert_called_once_with(expected_docs_path, exist_ok=True) |
| 1543 | + mock_open.assert_any_call(expected_destination, "w") |
| 1544 | + mock_open().write.assert_called_once_with("dummy content") |
| 1545 | + |
| 1546 | + |
| 1547 | +def test_copy_readme_to_docs_handles_symlink(mocker): |
| 1548 | + """Tests that the README.rst is copied to the docs directory, handling symlinks.""" |
| 1549 | + mock_makedirs = mocker.patch("os.makedirs") |
| 1550 | + mock_shutil_copy = mocker.patch("shutil.copy") |
| 1551 | + mock_os_islink = mocker.patch("os.path.islink") |
| 1552 | + mock_os_remove = mocker.patch("os.remove") |
| 1553 | + mock_os_lexists = mocker.patch("os.path.lexists", return_value=True) |
| 1554 | + mock_open = mocker.patch("builtins.open", mocker.mock_open(read_data="dummy content")) |
| 1555 | + |
| 1556 | + # Simulate docs_path being a symlink |
| 1557 | + mock_os_islink.side_effect = [False, True] # First call for destination_path, second for docs_path |
| 1558 | + |
| 1559 | + output = "output" |
| 1560 | + library_id = "google-cloud-language" |
| 1561 | + _copy_readme_to_docs(output, library_id) |
| 1562 | + |
| 1563 | + expected_source = "output/packages/google-cloud-language/README.rst" |
| 1564 | + expected_docs_path = "output/packages/google-cloud-language/docs" |
| 1565 | + expected_destination = "output/packages/google-cloud-language/docs/README.rst" |
| 1566 | + |
| 1567 | + mock_os_lexists.assert_called_once_with(expected_source) |
| 1568 | + mock_open.assert_any_call(expected_source, "r") |
| 1569 | + mock_os_islink.assert_any_call(expected_destination) |
| 1570 | + mock_os_islink.assert_any_call(expected_docs_path) |
| 1571 | + mock_os_remove.assert_called_once_with(expected_docs_path) |
| 1572 | + mock_makedirs.assert_called_once_with(expected_docs_path, exist_ok=True) |
| 1573 | + mock_open.assert_any_call(expected_destination, "w") |
| 1574 | + mock_open().write.assert_called_once_with("dummy content") |
| 1575 | + |
| 1576 | + |
| 1577 | +def test_copy_readme_to_docs_destination_path_is_symlink(mocker): |
| 1578 | + """Tests that the README.rst is copied to the docs directory, handling destination_path being a symlink.""" |
| 1579 | + mock_makedirs = mocker.patch("os.makedirs") |
| 1580 | + mock_shutil_copy = mocker.patch("shutil.copy") |
| 1581 | + mock_os_islink = mocker.patch("os.path.islink", return_value=True) |
| 1582 | + mock_os_remove = mocker.patch("os.remove") |
| 1583 | + mock_os_lexists = mocker.patch("os.path.lexists", return_value=True) |
| 1584 | + mock_open = mocker.patch("builtins.open", mocker.mock_open(read_data="dummy content")) |
| 1585 | + |
| 1586 | + output = "output" |
| 1587 | + library_id = "google-cloud-language" |
| 1588 | + _copy_readme_to_docs(output, library_id) |
| 1589 | + |
| 1590 | + expected_destination = "output/packages/google-cloud-language/docs/README.rst" |
| 1591 | + mock_os_remove.assert_called_once_with(expected_destination) |
| 1592 | + |
| 1593 | + |
| 1594 | +def test_copy_readme_to_docs_source_not_exists(mocker): |
| 1595 | + """Tests that the function returns early if the source README.rst does not exist.""" |
| 1596 | + mock_makedirs = mocker.patch("os.makedirs") |
| 1597 | + mock_shutil_copy = mocker.patch("shutil.copy") |
| 1598 | + mock_os_islink = mocker.patch("os.path.islink") |
| 1599 | + mock_os_remove = mocker.patch("os.remove") |
| 1600 | + mock_os_lexists = mocker.patch("os.path.lexists", return_value=False) |
| 1601 | + mock_open = mocker.patch("builtins.open", mocker.mock_open(read_data="dummy content")) |
| 1602 | + |
| 1603 | + output = "output" |
| 1604 | + library_id = "google-cloud-language" |
| 1605 | + _copy_readme_to_docs(output, library_id) |
| 1606 | + |
| 1607 | + expected_source = "output/packages/google-cloud-language/README.rst" |
| 1608 | + |
| 1609 | + mock_os_lexists.assert_called_once_with(expected_source) |
| 1610 | + mock_open.assert_not_called() |
| 1611 | + mock_os_islink.assert_not_called() |
| 1612 | + mock_os_remove.assert_not_called() |
| 1613 | + mock_makedirs.assert_not_called() |
| 1614 | + mock_shutil_copy.assert_not_called() |
0 commit comments