|
5 | 5 |
|
6 | 6 | from pathlib import Path |
7 | 7 | from unittest import mock |
| 8 | +from unittest.mock import call |
8 | 9 |
|
9 | 10 | import pytest |
10 | 11 | from pytest_subprocess import FakeProcess |
@@ -255,6 +256,111 @@ def test_file_part_with_contents_with_destination_template(tmp_path: Path) -> No |
255 | 256 | runner.run() |
256 | 257 |
|
257 | 258 |
|
| 259 | +@pytest.mark.parametrize("prompt", ("test", "test ")) |
| 260 | +@pytest.mark.parametrize("answer", ("", "yes", "y", "no", "n", "foobar")) |
| 261 | +def test_enter_prompt(prompt: str, answer: str) -> None: |
| 262 | + """Test enter function.""" |
| 263 | + configuration = TutorialModel.model_validate({"path": "/dummy.yaml", "parts": [{"prompt": prompt}]}) |
| 264 | + runner = LocalTutorialRunner(configuration) |
| 265 | + with mock.patch("builtins.input", return_value=answer, autospec=True) as mock_input: |
| 266 | + runner.run() |
| 267 | + mock_input.assert_called_once_with(f"{prompt.strip()} ") |
| 268 | + |
| 269 | + |
| 270 | +@pytest.mark.parametrize("answer", ("", "y", "yes")) |
| 271 | +def test_confirm_prompt_confirms(answer: str) -> None: |
| 272 | + """Test confirm prompt where empty answer passes.""" |
| 273 | + configuration = TutorialModel.model_validate( |
| 274 | + {"path": "/dummy.yaml", "parts": [{"prompt": "example:", "type": "confirm"}]} |
| 275 | + ) |
| 276 | + runner = LocalTutorialRunner(configuration) |
| 277 | + with mock.patch("builtins.input", return_value=answer, autospec=True) as mock_input: |
| 278 | + runner.run() |
| 279 | + mock_input.assert_called_once_with("example: ") |
| 280 | + |
| 281 | + |
| 282 | +@pytest.mark.parametrize("answer", ("y", "yes")) |
| 283 | +def test_confirm_prompt_confirms_with_default_false(answer: str) -> None: |
| 284 | + """Test confirm prompt where answer passes with default=False.""" |
| 285 | + configuration = TutorialModel.model_validate( |
| 286 | + {"path": "/dummy.yaml", "parts": [{"prompt": "example:", "type": "confirm", "default": False}]} |
| 287 | + ) |
| 288 | + runner = LocalTutorialRunner(configuration) |
| 289 | + with mock.patch("builtins.input", return_value=answer, autospec=True) as mock_input: |
| 290 | + runner.run() |
| 291 | + mock_input.assert_called_once_with("example: ") |
| 292 | + |
| 293 | + |
| 294 | +@pytest.mark.parametrize("answer", ("", "n", "no")) |
| 295 | +def test_confirm_prompt_does_not_confirm_with_default_false(answer: str) -> None: |
| 296 | + """Test confirm prompt where answer does not confirm with default=False.""" |
| 297 | + configuration = TutorialModel.model_validate( |
| 298 | + { |
| 299 | + "path": "/dummy.yaml", |
| 300 | + "parts": [{"prompt": "example:", "type": "confirm", "default": False}], |
| 301 | + } |
| 302 | + ) |
| 303 | + runner = LocalTutorialRunner(configuration) |
| 304 | + with mock.patch("builtins.input", return_value=answer, autospec=True) as mock_input: |
| 305 | + with pytest.raises(RuntimeError, match=r"^State was not confirmed\.$"): |
| 306 | + runner.run() |
| 307 | + mock_input.assert_called_once_with("example: ") |
| 308 | + |
| 309 | + |
| 310 | +def test_confirm_prompt_with_invalid_response() -> None: |
| 311 | + """Test confirm prompt where we first give an invalid response.""" |
| 312 | + configuration = TutorialModel.model_validate( |
| 313 | + { |
| 314 | + "path": "/dummy.yaml", |
| 315 | + "parts": [{"prompt": "example:", "type": "confirm", "default": False}], |
| 316 | + } |
| 317 | + ) |
| 318 | + runner = LocalTutorialRunner(configuration) |
| 319 | + with mock.patch("builtins.input", side_effect=["foobar", "y"], autospec=True) as mock_input: |
| 320 | + runner.run() |
| 321 | + mock_input.assert_has_calls([call("example: "), call("example: ")]) |
| 322 | + |
| 323 | + |
| 324 | +def test_confirm_prompt_does_not_confirm_error_template() -> None: |
| 325 | + """Test confirm prompt where answer does not confirm with default=False.""" |
| 326 | + answer = "no" |
| 327 | + value = "example value" |
| 328 | + configuration = TutorialModel.model_validate( |
| 329 | + { |
| 330 | + "path": "/dummy.yaml", |
| 331 | + "configuration": {"run": {"context": {"example": value}}}, |
| 332 | + "parts": [ |
| 333 | + { |
| 334 | + "prompt": "example:", |
| 335 | + "type": "confirm", |
| 336 | + "default": False, |
| 337 | + "error": "{{ response }}: {{ example }}: This is wrong.", |
| 338 | + } |
| 339 | + ], |
| 340 | + } |
| 341 | + ) |
| 342 | + runner = LocalTutorialRunner(configuration) |
| 343 | + with mock.patch("builtins.input", return_value=answer, autospec=True) as mock_input: |
| 344 | + with pytest.raises(RuntimeError, match=rf"^{answer}: {value}: This is wrong\.$"): |
| 345 | + runner.run() |
| 346 | + mock_input.assert_called_once_with("example: ") |
| 347 | + |
| 348 | + |
| 349 | +def test_prompt_template() -> None: |
| 350 | + """Test that the prompt is rendered as a template.""" |
| 351 | + configuration = TutorialModel.model_validate( |
| 352 | + { |
| 353 | + "path": "/dummy.yaml", |
| 354 | + "configuration": {"run": {"context": {"example": "dest/"}}}, |
| 355 | + "parts": [{"prompt": "Go to {{ example }}"}], |
| 356 | + } |
| 357 | + ) |
| 358 | + runner = LocalTutorialRunner(configuration) |
| 359 | + with mock.patch("builtins.input", return_value="", autospec=True) as mock_input: |
| 360 | + runner.run() |
| 361 | + mock_input.assert_called_once_with("Go to dest/ ") |
| 362 | + |
| 363 | + |
258 | 364 | def test_temporary_directory(tmp_path: Path, fp: FakeProcess) -> None: |
259 | 365 | """Test running in temporary directory.""" |
260 | 366 | fp.register("pwd") |
|
0 commit comments