/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: 2017-05-22 00:56:52 UTC
  • mfrom: (6621.2.26 py3_pokes)
  • Revision ID: jelmer@jelmer.uk-20170522005652-yjahcr9hwmjkno7n
Merge Python3 porting work ('py3 pokes')

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