Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/kaggle/api/kaggle_api_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -10130,12 +10130,14 @@ def confirmation(self, action: str = "", default_to_yes: bool = False):
options[""] = True
while True:
sys.stdout.write("{} {} ".format(question, prompt))
choice = input().lower()
try:
choice = input().strip().lower()
except EOFError:
# Closed stdin can never produce a valid answer; treat it as "no" rather than looping.
return False
if choice in options:
return options[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no'.\n")
return False
sys.stdout.write("Please respond with 'y' or 'n'.\n")

def _check_response_version(self, response: Response):
if self.already_printed_version_warning:
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_confirmation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from unittest.mock import patch

import pytest


@pytest.mark.parametrize(
"answer, default_to_yes, expected",
[
("", True, True),
(" Y \r", True, True),
("NO", True, False),
],
)
def test_confirmation_single_answer(api, answer, default_to_yes, expected):
with patch("builtins.input", return_value=answer):
assert api.confirmation("do it", default_to_yes=default_to_yes) is expected


def test_confirmation_reprompts_after_invalid_answer(api):
# Without default_to_yes, Enter is not an answer either.
with patch("builtins.input", side_effect=["", "x", "y"]) as mock_input:
assert api.confirmation("do it") is True
assert mock_input.call_count == 3


def test_confirmation_closed_stdin_cancels(api):
with patch("builtins.input", side_effect=["x", EOFError]):
assert api.confirmation("do it") is False
Loading