Skip to content

Commit 8f50dd4

Browse files
authored
Move and use ask_question in release.py and test (#319)
1 parent 2aa5002 commit 8f50dd4

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

release.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,20 @@ def run_cmd(
291291
error(f"{cmd} failed")
292292

293293

294+
def ask_question(question: str) -> bool:
295+
answer = ""
296+
print(question)
297+
while answer not in ("yes", "no"):
298+
answer = input("Enter yes or no: ")
299+
if answer == "yes":
300+
return True
301+
elif answer == "no":
302+
return False
303+
else:
304+
print("Please enter yes or no.")
305+
return True
306+
307+
294308
readme_re = re.compile(r"This is Python version 3\.\d").match
295309

296310

@@ -763,7 +777,7 @@ def make_tag(tag: Tag, *, sign_gpg: bool = True) -> bool:
763777
print("There are still reST files in NEWS.d/next/...")
764778
if not good_files:
765779
print(f"There is no Misc/NEWS.d/{tag}.rst file.")
766-
if input("Are you sure you want to tag? (y/n) > ") not in ("y", "yes"):
780+
if not ask_question("Are you sure you want to tag?"):
767781
print("Aborting.")
768782
return False
769783

@@ -774,10 +788,7 @@ def make_tag(tag: Tag, *, sign_gpg: bool = True) -> bool:
774788
!= f"branch-{tag}"
775789
):
776790
print("It doesn't look like you're on the correct branch.")
777-
if input("Are you sure you want to tag? (y/n) > ").lower() not in (
778-
"y",
779-
"yes",
780-
):
791+
if not ask_question("Are you sure you want to tag?"):
781792
print("Aborting.")
782793
return False
783794

run_release.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import sbom
3737
import update_version_next
3838
from buildbotapi import BuildBotAPI, Builder
39-
from release import ReleaseShelf, Tag, Task
39+
from release import ReleaseShelf, Tag, Task, ask_question
4040

4141
API_KEY_REGEXP = re.compile(r"(?P<user>\w+):(?P<key>\w+)")
4242
RELEASE_REGEXP = re.compile(
@@ -287,20 +287,6 @@ def run(self) -> None:
287287
print(f"Congratulations, Python {self.db['release']} is released 🎉🎉🎉")
288288

289289

290-
def ask_question(question: str) -> bool:
291-
answer = ""
292-
print(question)
293-
while answer not in ("yes", "no"):
294-
answer = input("Enter yes or no: ")
295-
if answer == "yes":
296-
return True
297-
elif answer == "no":
298-
return False
299-
else:
300-
print("Please enter yes or no.")
301-
return True
302-
303-
304290
@contextlib.contextmanager
305291
def cd(path: Path) -> Iterator[None]:
306292
current_path = os.getcwd()

tests/test_release.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,38 @@ def test_task(mocker: MockerFixture) -> None:
4646
my_task.assert_called_once_with(cast(release.ReleaseShelf, db))
4747

4848

49+
@pytest.mark.parametrize(
50+
["test_inputs", "expected"],
51+
[
52+
(["yes"], True),
53+
(["no"], False),
54+
(["maybe", "yes"], True),
55+
(["maybe", "no"], False),
56+
(["", "nope", "y", "yes"], True),
57+
(["", "nope", "n", "no"], False),
58+
],
59+
)
60+
def test_ask_question(
61+
mocker: MockerFixture,
62+
capsys: pytest.CaptureFixture[str],
63+
test_inputs: list[str],
64+
expected: bool,
65+
) -> None:
66+
# Arrange
67+
mocker.patch("release.input", side_effect=test_inputs)
68+
69+
# Act
70+
result = release.ask_question("Do you want to proceed?")
71+
72+
# Assert
73+
assert result is expected
74+
captured = capsys.readouterr()
75+
assert "Do you want to proceed?" in captured.out
76+
# All inputs except the last are invalid
77+
invalid_count = len(test_inputs) - 1
78+
assert captured.out.count("Please enter yes or no.") == invalid_count
79+
80+
4981
def test_tweak_patchlevel(tmp_path: Path) -> None:
5082
# Arrange
5183
tag = release.Tag("3.14.0b2")

0 commit comments

Comments
 (0)