|
44 | 44 | _get_library_dist_name, |
45 | 45 | _get_library_id, |
46 | 46 | _get_libraries_to_prepare_for_release, |
| 47 | + _get_new_library_config, |
47 | 48 | _get_previous_version, |
48 | 49 | _process_changelog, |
49 | 50 | _process_version_file, |
@@ -162,23 +163,28 @@ def mock_build_request_file(tmp_path, monkeypatch): |
162 | 163 |
|
163 | 164 |
|
164 | 165 | @pytest.fixture |
165 | | -def mock_configure_request_file(tmp_path, monkeypatch): |
166 | | - """Creates the mock request file at the correct path inside a temp dir.""" |
167 | | - # Create the path as expected by the script: .librarian/configure-request.json |
168 | | - request_path = f"{LIBRARIAN_DIR}/{CONFIGURE_REQUEST_FILE}" |
169 | | - request_dir = tmp_path / os.path.dirname(request_path) |
170 | | - request_dir.mkdir(parents=True, exist_ok=True) |
171 | | - request_file = request_dir / os.path.basename(request_path) |
172 | | - |
173 | | - request_content = { |
| 166 | +def mock_configure_request_data(): |
| 167 | + """Returns mock data for configure-request.json.""" |
| 168 | + return { |
174 | 169 | "libraries": [ |
175 | 170 | { |
176 | 171 | "id": "google-cloud-language", |
177 | 172 | "apis": [{"path": "google/cloud/language/v1", "status": "new"}], |
178 | 173 | } |
179 | 174 | ] |
180 | 175 | } |
181 | | - request_file.write_text(json.dumps(request_content)) |
| 176 | + |
| 177 | + |
| 178 | +@pytest.fixture |
| 179 | +def mock_configure_request_file(tmp_path, monkeypatch, mock_configure_request_data): |
| 180 | + """Creates the mock request file at the correct path inside a temp dir.""" |
| 181 | + # Create the path as expected by the script: .librarian/configure-request.json |
| 182 | + request_path = f"{LIBRARIAN_DIR}/{CONFIGURE_REQUEST_FILE}" |
| 183 | + request_dir = tmp_path / os.path.dirname(request_path) |
| 184 | + request_dir.mkdir(parents=True, exist_ok=True) |
| 185 | + request_file = request_dir / os.path.basename(request_path) |
| 186 | + |
| 187 | + request_file.write_text(json.dumps(mock_configure_request_data)) |
182 | 188 |
|
183 | 189 | # Change the current working directory to the temp path for the test. |
184 | 190 | monkeypatch.chdir(tmp_path) |
@@ -261,6 +267,52 @@ def mock_state_file(tmp_path, monkeypatch): |
261 | 267 | return request_file |
262 | 268 |
|
263 | 269 |
|
| 270 | +def test_handle_configure(mock_configure_request_file, mocker): |
| 271 | + """Tests the successful execution path of handle_configure.""" |
| 272 | + mock_write_json = mocker.patch("cli._write_json_file") |
| 273 | + |
| 274 | + handle_configure() |
| 275 | + |
| 276 | + # Verify that the correct configuration is written to the response file. |
| 277 | + expected_config = { |
| 278 | + "id": "google-cloud-language", |
| 279 | + "apis": [{"path": "google/cloud/language/v1"}], |
| 280 | + } |
| 281 | + mock_write_json.assert_called_once_with( |
| 282 | + f"{LIBRARIAN_DIR}/configure-response.json", expected_config |
| 283 | + ) |
| 284 | + |
| 285 | + |
| 286 | +def test_handle_configure_failure(mocker): |
| 287 | + """Tests that handle_configure raises ValueError on failure.""" |
| 288 | + with pytest.raises(ValueError, match="Configuring a new library failed."): |
| 289 | + handle_configure() |
| 290 | + |
| 291 | + |
| 292 | +def test_get_new_library_config_new_library_found(mock_configure_request_data): |
| 293 | + """Tests that the new library configuration is returned when found.""" |
| 294 | + config = _get_new_library_config(mock_configure_request_data) |
| 295 | + assert config["id"] == "google-cloud-language" |
| 296 | + assert "status" not in config["apis"][0] |
| 297 | + |
| 298 | + |
| 299 | +def test_get_new_library_config_no_new_library(): |
| 300 | + """Tests that an empty dictionary is returned when no new library is found.""" |
| 301 | + request_data = { |
| 302 | + "libraries": [ |
| 303 | + {"id": "existing-library", "apis": [{"path": "path/v1", "status": "existing"}]}, |
| 304 | + ] |
| 305 | + } |
| 306 | + config = _get_new_library_config(request_data) |
| 307 | + assert config == {} |
| 308 | + |
| 309 | + |
| 310 | +def test_get_new_library_config_empty_input(): |
| 311 | + """Tests that an empty dictionary is returned for empty input.""" |
| 312 | + config = _get_new_library_config({}) |
| 313 | + assert config == {} |
| 314 | + |
| 315 | + |
264 | 316 | def test_get_library_id_success(): |
265 | 317 | """Tests that _get_library_id returns the correct ID when present.""" |
266 | 318 | request_data = {"id": "test-library", "name": "Test Library"} |
|
0 commit comments