530
531
return setattr(self._cstring, name, val)
533
class FakeStdin(StringIOWrapper):
534
"""Simulated stdin for tests only.
536
We pretend to be the real stdin by redirecting the fileno method so that
537
getpass.getpass can succeed changing the echo mode of the real
538
stdin. More precisely, getpass change the echo mode via tcsetattr which
539
requires a file descriptor, once the user have entered its password the
540
echo mode is restored (this is garanteed by a try-finally). So basically
541
the risk for the tester is to lose ist echo if he attemps to type some
542
characters *while* the echo is disabled.
544
That allows tests to can user inputs without having to implement a
534
class TestUIFactory(ui.CLIUIFactory):
535
"""A UI Factory for testing.
537
Hide the progress bar but emit note()s.
539
Allows get_password to be tested without real tty attached.
548
fileno = sys.stdin.fileno
550
def __init__(self, string, encoding='ascii'):
551
StringIOWrapper.__init__(self, string.encode(encoding))
552
self.encoding = encoding
546
super(TestUIFactory, self).__init__()
547
if stdin is not None:
548
# We use a StringIOWrapper to be able to test various
549
# encodings, but the user is still responsible to
550
# encode the string and to set the encoding attribute
551
# of StringIOWrapper.
552
self.stdin = StringIOWrapper(stdin)
554
self.stdout = sys.stdout
558
self.stderr = sys.stderr
563
"""See progress.ProgressBar.clear()."""
565
def clear_term(self):
566
"""See progress.ProgressBar.clear_term()."""
568
def clear_term(self):
569
"""See progress.ProgressBar.clear_term()."""
572
"""See progress.ProgressBar.finished()."""
574
def note(self, fmt_string, *args, **kwargs):
575
"""See progress.ProgressBar.note()."""
576
self.stdout.write((fmt_string + "\n") % args)
578
def progress_bar(self):
581
def nested_progress_bar(self):
584
def update(self, message, count=None, total=None):
585
"""See progress.ProgressBar.update()."""
587
def get_non_echoed_password(self, prompt):
588
"""Get password from stdin without trying to handle the echo mode"""
590
self.stdout.write(prompt)
591
password = self.stdin.readline()
594
if password[-1] == '\n':
595
password = password[:-1]
555
599
class TestCase(unittest.TestCase):
601
645
def _silenceUI(self):
602
646
"""Turn off UI for duration of test"""
603
647
# by default the UI is off; tests can turn it on if they want it.
604
saved = bzrlib.ui.ui_factory
648
saved = ui.ui_factory
606
bzrlib.ui.ui_factory = saved
607
bzrlib.ui.ui_factory = bzrlib.ui.SilentUIFactory()
650
ui.ui_factory = saved
651
ui.ui_factory = ui.SilentUIFactory()
608
652
self.addCleanup(_restore)
610
654
def _ndiff_strings(self, a, b):
970
1012
handler.setLevel(logging.INFO)
971
1013
logger = logging.getLogger('')
972
1014
logger.addHandler(handler)
973
old_ui_factory = bzrlib.ui.ui_factory
974
bzrlib.ui.ui_factory = bzrlib.tests.blackbox.TestUIFactory(
977
bzrlib.ui.ui_factory.stdin = stdin
1015
old_ui_factory = ui.ui_factory
1016
ui.ui_factory = TestUIFactory(stdin=stdin, stdout=stdout, stderr=stderr)
980
1019
if working_dir is not None:
985
1024
saved_debug_flags = frozenset(debug.debug_flags)
986
1025
debug.debug_flags.clear()
988
result = self.apply_redirected(stdin, stdout, stderr,
1027
result = self.apply_redirected(ui.ui_factory.stdin,
989
1029
bzrlib.commands.run_bzr_catch_errors,
992
1032
debug.debug_flags.update(saved_debug_flags)
994
1034
logger.removeHandler(handler)
995
bzrlib.ui.ui_factory = old_ui_factory
1035
ui.ui_factory = old_ui_factory
996
1036
if cwd is not None: