|
| 1 | +""" |
| 2 | +Copyright 2025 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +from unittest.mock import MagicMock, patch |
| 18 | +import pytest |
| 19 | +from pytest_mock import MockerFixture |
| 20 | + |
| 21 | +from xpk.utils.console import ask_for_user_consent |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(autouse=True) |
| 25 | +def mock_is_quiet(mocker: MockerFixture): |
| 26 | + return mocker.patch("xpk.utils.console.is_quiet", return_value=False) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize( |
| 30 | + "user_input,expected", |
| 31 | + [ |
| 32 | + ("yes", True), |
| 33 | + ("y", True), |
| 34 | + ("Y", True), |
| 35 | + ("Yes", True), |
| 36 | + ("YES", True), |
| 37 | + ("no", False), |
| 38 | + ("n", False), |
| 39 | + ("N", False), |
| 40 | + ("No", False), |
| 41 | + ("NO", False), |
| 42 | + ], |
| 43 | +) |
| 44 | +@patch("xpk.utils.console.input") |
| 45 | +def test_ask_for_user_consent(mock_input: MagicMock, user_input, expected): |
| 46 | + mock_input.return_value = user_input |
| 47 | + |
| 48 | + assert ask_for_user_consent("Test question?") is expected |
| 49 | + |
| 50 | + |
| 51 | +def fake_input_factory(user_inputs: list[str]): |
| 52 | + def fake_input(prompt: str) -> str: |
| 53 | + return user_inputs.pop(0) |
| 54 | + |
| 55 | + return fake_input |
| 56 | + |
| 57 | + |
| 58 | +@patch("xpk.utils.console.input", wraps=fake_input_factory(["invalid", "y"])) |
| 59 | +def test_ask_for_user_consent_invalid_input(mock_input: MagicMock): |
| 60 | + agreed = ask_for_user_consent("Test question?") |
| 61 | + |
| 62 | + assert agreed is True |
| 63 | + assert mock_input.call_count == 2 |
| 64 | + |
| 65 | + |
| 66 | +@patch("xpk.utils.console.input", return_value="") |
| 67 | +def test_ask_for_user_consent_default_No(mock_input: MagicMock): |
| 68 | + agreed = ask_for_user_consent("Test question?", default_option="N") |
| 69 | + |
| 70 | + assert agreed is False |
| 71 | + mock_input.assert_called_once_with("[XPK] Test question? (y/N): ") |
| 72 | + |
| 73 | + |
| 74 | +@patch("xpk.utils.console.input", return_value="") |
| 75 | +def test_ask_for_user_consent_default_Yes(mock_input: MagicMock): |
| 76 | + agreed = ask_for_user_consent("Test question?", default_option="Y") |
| 77 | + |
| 78 | + assert agreed is True |
| 79 | + mock_input.assert_called_once_with("[XPK] Test question? (Y/n): ") |
| 80 | + |
| 81 | + |
| 82 | +@patch("xpk.utils.console.input") |
| 83 | +def test_ask_for_user_consent_with_quiet_mode_always_agrees( |
| 84 | + mock_input: MagicMock, |
| 85 | + mock_is_quiet: MagicMock, |
| 86 | +): |
| 87 | + mock_is_quiet.return_value = True |
| 88 | + |
| 89 | + agreed = ask_for_user_consent("Test question?", default_option="N") |
| 90 | + |
| 91 | + assert agreed is True |
| 92 | + mock_input.assert_not_called() |
0 commit comments