/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/ui_testing.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2016 Canonical Ltd, 2017 Bazaar developers
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import io
 
18
 
 
19
from .. import (
 
20
    ui,
 
21
)
 
22
from ..ui import text as ui_text
 
23
 
 
24
 
 
25
class StringIOWithEncoding(io.StringIO):
 
26
 
 
27
    encoding = "ascii"
 
28
 
 
29
    def write(self, string):
 
30
        if isinstance(string, bytes):
 
31
            string = string.decode(self.encoding)
 
32
        io.StringIO.write(self, string)
 
33
 
 
34
 
 
35
class BytesIOWithEncoding(io.BytesIO):
 
36
 
 
37
    encoding = "ascii"
 
38
 
 
39
 
 
40
class StringIOAsTTY(StringIOWithEncoding):
 
41
    """A helper class which makes a StringIO look like a terminal"""
 
42
 
 
43
    def isatty(self):
 
44
        return True
 
45
 
 
46
 
 
47
class TextUIFactory(ui_text.TextUIFactory):
 
48
 
 
49
    def __init__(self, stdin=None, stdout=None, stderr=None):
 
50
        if isinstance(stdin, bytes):
 
51
            stdin = stdin.decode()
 
52
        if isinstance(stdin, str):
 
53
            stdin = StringIOWithEncoding(stdin)
 
54
        if stdout is None:
 
55
            stdout = StringIOWithEncoding()
 
56
        if stderr is None:
 
57
            stderr = StringIOWithEncoding()
 
58
        super(TextUIFactory, self).__init__(stdin, stdout, stderr)
 
59
 
 
60
    def _setup_streams(self):
 
61
        self.raw_stdin = self.stdin
 
62
        self.raw_stdout = self.stdout
 
63
        self.raw_stderr = self.stderr
 
64
 
 
65
 
 
66
class TestUIFactory(TextUIFactory):
 
67
    """A UI Factory for testing.
 
68
 
 
69
    Hide the progress bar but emit note()s.
 
70
    Redirect stdin.
 
71
    Allows get_password to be tested without real tty attached.
 
72
 
 
73
    See also CannedInputUIFactory which lets you provide programmatic input in
 
74
    a structured way.
 
75
    """
 
76
    # TODO: Capture progress events at the model level and allow them to be
 
77
    # observed by tests that care.
 
78
    #
 
79
    # XXX: Should probably unify more with CannedInputUIFactory or a
 
80
    # particular configuration of TextUIFactory, or otherwise have a clearer
 
81
    # idea of how they're supposed to be different.
 
82
    # See https://bugs.launchpad.net/bzr/+bug/408213
 
83
 
 
84
    def get_non_echoed_password(self):
 
85
        """Get password from stdin without trying to handle the echo mode"""
 
86
        password = self.stdin.readline()
 
87
        if not password:
 
88
            raise EOFError
 
89
        if password[-1] == '\n':
 
90
            password = password[:-1]
 
91
        return password
 
92
 
 
93
    def make_progress_view(self):
 
94
        return ui.NullProgressView()