/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: Jelmer Vernooij
  • Date: 2020-04-05 19:11:34 UTC
  • mto: (7490.7.16 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200405191134-0aebh8ikiwygxma5
Populate the .gitignore file.

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
from __future__ import absolute_import
 
18
 
 
19
import io
 
20
 
 
21
from .. import (
 
22
    ui,
 
23
)
 
24
from ..ui import text as ui_text
 
25
from ..sixish import (
 
26
    text_type,
 
27
)
 
28
 
 
29
 
 
30
class StringIOWithEncoding(io.StringIO):
 
31
 
 
32
    encoding = "ascii"
 
33
 
 
34
    def write(self, string):
 
35
        if isinstance(string, bytes):
 
36
            string = string.decode(self.encoding)
 
37
        io.StringIO.write(self, string)
 
38
 
 
39
 
 
40
class BytesIOWithEncoding(io.BytesIO):
 
41
 
 
42
    encoding = "ascii"
 
43
 
 
44
 
 
45
class StringIOAsTTY(StringIOWithEncoding):
 
46
    """A helper class which makes a StringIO look like a terminal"""
 
47
 
 
48
    def isatty(self):
 
49
        return True
 
50
 
 
51
 
 
52
class TextUIFactory(ui_text.TextUIFactory):
 
53
 
 
54
    def __init__(self, stdin=None, stdout=None, stderr=None):
 
55
        if isinstance(stdin, bytes):
 
56
            stdin = stdin.decode()
 
57
        if isinstance(stdin, text_type):
 
58
            stdin = StringIOWithEncoding(stdin)
 
59
        if stdout is None:
 
60
            stdout = StringIOWithEncoding()
 
61
        if stderr is None:
 
62
            stderr = StringIOWithEncoding()
 
63
        super(TextUIFactory, self).__init__(stdin, stdout, stderr)
 
64
 
 
65
    def _setup_streams(self):
 
66
        self.raw_stdin = self.stdin
 
67
        self.raw_stdout = self.stdout
 
68
        self.raw_stderr = self.stderr
 
69
 
 
70
 
 
71
class TestUIFactory(TextUIFactory):
 
72
    """A UI Factory for testing.
 
73
 
 
74
    Hide the progress bar but emit note()s.
 
75
    Redirect stdin.
 
76
    Allows get_password to be tested without real tty attached.
 
77
 
 
78
    See also CannedInputUIFactory which lets you provide programmatic input in
 
79
    a structured way.
 
80
    """
 
81
    # TODO: Capture progress events at the model level and allow them to be
 
82
    # observed by tests that care.
 
83
    #
 
84
    # XXX: Should probably unify more with CannedInputUIFactory or a
 
85
    # particular configuration of TextUIFactory, or otherwise have a clearer
 
86
    # idea of how they're supposed to be different.
 
87
    # See https://bugs.launchpad.net/bzr/+bug/408213
 
88
 
 
89
    def get_non_echoed_password(self):
 
90
        """Get password from stdin without trying to handle the echo mode"""
 
91
        password = self.stdin.readline()
 
92
        if not password:
 
93
            raise EOFError
 
94
        if password[-1] == '\n':
 
95
            password = password[:-1]
 
96
        return password
 
97
 
 
98
    def make_progress_view(self):
 
99
        return ui.NullProgressView()