本文整理汇总了Python中fudge.Fake.returns方法的典型用法代码示例。如果您正苦于以下问题:Python Fake.returns方法的具体用法?Python Fake.returns怎么用?Python Fake.returns使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fudge.Fake
的用法示例。
在下文中一共展示了Fake.returns方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: password_response
# 需要导入模块: from fudge import Fake [as 别名]
# 或者: from fudge.Fake import returns [as 别名]
def password_response(password, times_called=None, silent=True):
"""
Context manager which patches ``getpass.getpass`` to return ``password``.
``password`` may be a single string or an iterable of strings:
* If single string, given password is returned every time ``getpass`` is
called.
* If iterable, iterated over for each call to ``getpass``, after which
``getpass`` will error.
If ``times_called`` is given, it is used to add a ``Fake.times_called``
clause to the mock object, e.g. ``.times_called(1)``. Specifying
``times_called`` alongside an iterable ``password`` list is unsupported
(see Fudge docs on ``Fake.next_call``).
If ``silent`` is True, no prompt will be printed to ``sys.stderr``.
"""
fake = Fake('getpass', callable=True)
# Assume stringtype or iterable, turn into mutable iterable
if isinstance(password, StringTypes):
passwords = [password]
else:
passwords = list(password)
# Optional echoing of prompt to mimic real behavior of getpass
# NOTE: also echo a newline if the prompt isn't a "passthrough" from the
# server (as it means the server won't be sending its own newline for us).
echo = lambda x, y: y.write(x + ("\n" if x != " " else ""))
# Always return first (only?) password right away
fake = fake.returns(passwords.pop(0))
if not silent:
fake = fake.calls(echo)
# If we had >1, return those afterwards
for pw in passwords:
fake = fake.next_call().returns(pw)
if not silent:
fake = fake.calls(echo)
# Passthrough times_called
if times_called:
fake = fake.times_called(times_called)
return patched_context(getpass, 'getpass', fake)