|
41 | 41 | _get_library_id, |
42 | 42 | _get_libraries_to_prepare_for_release, |
43 | 43 | _get_previous_version, |
| 44 | + _get_setup_dist_name, |
| 45 | + _get_toml_dist_name, |
44 | 46 | _locate_and_extract_artifact, |
45 | 47 | _process_changelog, |
46 | 48 | _process_version_file, |
|
52 | 54 | _update_changelog_for_library, |
53 | 55 | _update_global_changelog, |
54 | 56 | _update_version_for_library, |
| 57 | + _verify_library_dist_name, |
55 | 58 | _verify_library_namespace, |
56 | 59 | _write_json_file, |
57 | 60 | _write_text_file, |
@@ -525,7 +528,8 @@ def test_handle_build_success(caplog, mocker, mock_build_request_file): |
525 | 528 | caplog.set_level(logging.INFO) |
526 | 529 |
|
527 | 530 | mocker.patch("cli._run_nox_sessions") |
528 | | - mocker.patch("cli._verify_library_namespace", return_value=True) |
| 531 | + mocker.patch("cli._verify_library_namespace") |
| 532 | + mocker.patch("cli._verify_library_dist_name") |
529 | 533 | handle_build() |
530 | 534 |
|
531 | 535 | assert "'build' command executed." in caplog.text |
@@ -922,6 +926,70 @@ def test_determine_library_namespace_fails_not_subpath(): |
922 | 926 | _determine_library_namespace(gapic_parent_path, pkg_root_path) |
923 | 927 |
|
924 | 928 |
|
| 929 | +def test_get_setup_dist_name_exists(mocker): |
| 930 | + """Tests that a valid library distribution name exists in `pyproject.toml`.""" |
| 931 | + mock_dist = MagicMock() |
| 932 | + mock_dist.get_name.return_value = "my-lib" |
| 933 | + mocker.patch("cli.run_setup", return_value=mock_dist) |
| 934 | + assert _get_setup_dist_name("my-lib", "repo") == "my-lib" |
| 935 | + |
| 936 | + |
| 937 | +def test_get_setup_dist_name_file_not_found(caplog): |
| 938 | + """Tests that distribution name is None if `setup.py` does not exist.""" |
| 939 | + caplog.set_level(logging.DEBUG) |
| 940 | + assert _get_setup_dist_name("my-lib", "repo") is None |
| 941 | + assert len(caplog.records) == 1 |
| 942 | + |
| 943 | + |
| 944 | +def test_get_toml_dist_name_exists(mocker): |
| 945 | + """Tests that a valid library distribution name exists in `pyproject.toml`.""" |
| 946 | + mock_data = {"project": {"name": "my-lib"}} |
| 947 | + mocker.patch("tomli.load", return_value=mock_data) |
| 948 | + mocker.patch("builtins.open", mocker.mock_open(read_data=b"fake toml data")) |
| 949 | + assert _get_toml_dist_name("my-lib", "repo") == "my-lib" |
| 950 | + |
| 951 | + |
| 952 | +def test_get_toml_dist_name_file_not_found(caplog): |
| 953 | + """Tests that distribution name is None if `pyproject.toml` does not exist.""" |
| 954 | + caplog.set_level(logging.DEBUG) |
| 955 | + assert _get_toml_dist_name("my-lib", "repo") is None |
| 956 | + assert len(caplog.records) == 1 |
| 957 | + |
| 958 | + |
| 959 | +def test_verify_library_dist_name_setup_success(mocker): |
| 960 | + """Tests success when a library distribution name in setup.py is valid.""" |
| 961 | + mock_setup_file = mocker.patch("cli._get_setup_dist_name", return_value="my-lib") |
| 962 | + _verify_library_dist_name("my-lib", "repo") |
| 963 | + mock_setup_file.assert_called_once_with("my-lib", "repo") |
| 964 | + |
| 965 | + |
| 966 | +def test_verify_library_dist_name_setup_success(mocker): |
| 967 | + """Tests success when a library distribution name in toml is valid.""" |
| 968 | + mock_toml_file = mocker.patch("cli._get_toml_dist_name", return_value="my-lib") |
| 969 | + _verify_library_dist_name("my-lib", "repo") |
| 970 | + mock_toml_file.assert_called_once_with("my-lib", "repo") |
| 971 | + |
| 972 | + |
| 973 | +def test_verify_library_dist_name_fail(): |
| 974 | + """Tests failure when a library does not have a `setup.py` or `pyproject.toml`.""" |
| 975 | + with pytest.raises(ValueError): |
| 976 | + _verify_library_dist_name("my-lib", "repo") |
| 977 | + |
| 978 | + |
| 979 | +def test_verify_library_dist_name_setup_fail(mocker): |
| 980 | + """Tests failure when a library has an invalid distribution name in `setup.py`.""" |
| 981 | + mocker.patch("cli._get_setup_dist_name", return_value="invalid-lib-name") |
| 982 | + with pytest.raises(ValueError): |
| 983 | + _verify_library_dist_name("my-lib", "repo") |
| 984 | + |
| 985 | + |
| 986 | +def test_verify_library_dist_name_toml_fail(mocker): |
| 987 | + """Tests failure when a library has an invalid distribution name in `pyproject.toml`.""" |
| 988 | + mocker.patch("cli._get_toml_dist_name", return_value="invalid-lib-name") |
| 989 | + with pytest.raises(ValueError): |
| 990 | + _verify_library_dist_name("my-lib", "repo") |
| 991 | + |
| 992 | + |
925 | 993 | def test_verify_library_namespace_success_valid(mocker, mock_path_class): |
926 | 994 | """Tests success when a single valid namespace is found.""" |
927 | 995 | # 1. Get the mock instance from the mock class's return_value |
|
0 commit comments