mirror of
https://github.com/rpm-software-management/tito.git
synced 2025-02-23 12:12:47 +00:00
Merge pull request #374 from FrostyX/less-aggressive-yes-or-no
Make yes or no input less aggressive
This commit is contained in:
commit
27cb63e043
3 changed files with 57 additions and 11 deletions
|
@ -102,12 +102,16 @@ class Releaser(ConfigObject):
|
|||
def _ask_yes_no(self, prompt="Y/N? ", default_auto_answer=True):
|
||||
if self.auto_accept:
|
||||
return default_auto_answer
|
||||
else:
|
||||
if PY2:
|
||||
answer = raw_input(prompt)
|
||||
else:
|
||||
answer = input(prompt)
|
||||
return answer.lower() in ['y', 'yes', 'ok', 'sure']
|
||||
|
||||
yes = ['y', 'yes', 'ok', 'sure']
|
||||
no = ['n', 'no', 'nah', 'nope']
|
||||
answers = yes + no
|
||||
|
||||
while True:
|
||||
input_function = raw_input if PY2 else input
|
||||
answer = input_function(prompt).lower()
|
||||
if answer in answers:
|
||||
return answer in yes
|
||||
|
||||
def _check_releaser_config(self):
|
||||
"""
|
||||
|
|
|
@ -25,6 +25,16 @@ is_rawhide = sys.version_info[:2] >= (3, 8)
|
|||
is_epel6 = sys.version_info[:2] == (2, 6)
|
||||
|
||||
|
||||
if PY2:
|
||||
builtins = "__builtin__"
|
||||
builtins_open = "__builtin__.open"
|
||||
builtins_input = "__builtin__.raw_input"
|
||||
else:
|
||||
builtins = "builtins"
|
||||
builtins_open = "builtins.open"
|
||||
builtins_input = "builtins.input"
|
||||
|
||||
|
||||
file_spec = None
|
||||
|
||||
|
||||
|
@ -92,11 +102,7 @@ def open_mock(content, **kwargs):
|
|||
|
||||
content_out = StringIO()
|
||||
|
||||
if PY2:
|
||||
patch_module = "__builtin__.open"
|
||||
else:
|
||||
patch_module = "builtins.open"
|
||||
with patch(patch_module, m, create=True, **kwargs) as mo:
|
||||
with patch(builtins_open, m, create=True, **kwargs) as mo:
|
||||
stream = StringIO(content)
|
||||
rv = mo.return_value
|
||||
rv.write = lambda x: content_out.write(bytes(x, "utf-8"))
|
||||
|
|
36
test/unit/releaser_tests.py
Normal file
36
test/unit/releaser_tests.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
import unittest
|
||||
import mock
|
||||
from tito.compat import PY2, RawConfigParser
|
||||
from tito.release import Releaser
|
||||
from unit import builtins_input
|
||||
|
||||
|
||||
class ReleaserTests(unittest.TestCase):
|
||||
|
||||
@mock.patch("tito.release.main.create_builder")
|
||||
@mock.patch("tito.release.main.mkdtemp")
|
||||
def setUp(self, mkdtemp, create_builder):
|
||||
self.config = RawConfigParser()
|
||||
|
||||
self.releaser_config = RawConfigParser()
|
||||
self.releaser_config.add_section("test")
|
||||
self.releaser_config.set('test', "releaser",
|
||||
"tito.release.Releaser")
|
||||
|
||||
self.releaser = Releaser("titotestpkg", None, "/tmp/tito/",
|
||||
self.config, {}, "test", self.releaser_config, False,
|
||||
False, False, **{"offline": True})
|
||||
|
||||
@mock.patch(builtins_input)
|
||||
def test_ask_yes_or_no(self, input_mock):
|
||||
input_mock.side_effect = "y"
|
||||
assert self.releaser._ask_yes_no()
|
||||
|
||||
input_mock.side_effect = "n"
|
||||
assert not self.releaser._ask_yes_no()
|
||||
|
||||
input_mock.side_effect = ["yy", "y"]
|
||||
assert self.releaser._ask_yes_no()
|
||||
|
||||
input_mock.side_effect = ["yy", "no"]
|
||||
assert not self.releaser._ask_yes_no()
|
Loading…
Add table
Reference in a new issue