File: test_cmd.py

package info (click to toggle)
python-click-shell 2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 248 kB
  • sloc: python: 623; makefile: 158; sh: 24
file content (229 lines) | stat: -rw-r--r-- 5,783 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import sys
from cmd import Cmd

import click

from click_shell._cmd import ClickCmd
from click_shell._compat import PY2

if PY2:
    from StringIO import StringIO
else:
    from io import StringIO


class BadStringIO(StringIO, object):
    def __init__(self, *args, **kwargs):
        super(BadStringIO, self).__init__(*args, **kwargs)
        self.first = True

    def read(self, *args, **kwargs):
        if self.first:
            self.first = False
            raise KeyboardInterrupt()
        return super(BadStringIO, self).read(*args, **kwargs)

    def readline(self, *args, **kwargs):
        if self.first:
            self.first = False
            raise KeyboardInterrupt()
        return super(BadStringIO, self).readline(*args, **kwargs)


def test_create():
    cmd = ClickCmd()

    # Make sure it's a Cmd
    assert isinstance(cmd, Cmd)

    # Make sure it's a new-style class
    assert isinstance(cmd, object)

    # Make sure we have our exit functions
    assert hasattr(cmd, 'do_quit')
    assert hasattr(cmd, 'do_exit')


def test_intro_default(cli_runner):
    with cli_runner.isolation() as outstreams:
        cmd = ClickCmd(hist_file='.history')

        cmd.cmdloop()

        output = outstreams[0].getvalue() \
            .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

    assert output == '(Cmd) \n'
    os.remove('.history')


def test_intro_custom(cli_runner):
    for test_in in ('foo', 'bar', 'blah\n version 2'):
        with cli_runner.isolation() as outstreams:
            cmd = ClickCmd(hist_file='.history')
            cmd.cmdloop(test_in)

            output = outstreams[0].getvalue() \
                .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

        assert output == '{0}\n(Cmd) \n'.format(test_in)

    os.remove('.history')


def test_prompt(cli_runner):
    with cli_runner.isolation() as outstreams:
        cmd = ClickCmd(hist_file='.history')

        cmd.prompt = 'foobar > '

        cmd.cmdloop()

        output = outstreams[0].getvalue() \
            .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

    assert output == 'foobar > \n'

    os.remove('.history')


def test_bad_input(cli_runner):
    with cli_runner.isolation(input='foobar\n') as outstreams:
        cmd = ClickCmd(hist_file='.history')

        cmd.cmdloop()

        output = outstreams[0].getvalue() \
            .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

    assert output == '{0}{1}\n{0}\n'.format(ClickCmd.prompt,
                                            ClickCmd.nocommand % 'foobar')

    os.remove('.history')


def test_empty_input(cli_runner):
    with cli_runner.isolation(input='\n') as outstreams:
        cmd = ClickCmd(hist_file='.history')

        cmd.cmdloop()

        output = outstreams[0].getvalue() \
            .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

    assert output == '{0}{0}\n'.format(ClickCmd.prompt)

    os.remove('.history')


def test_quit(cli_runner):
    with cli_runner.isolation(input='quit\n') as outstreams:
        cmd = ClickCmd(hist_file='.history')

        cmd.cmdloop()

        output = outstreams[0].getvalue() \
            .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

    assert output == ClickCmd.prompt

    os.remove('.history')


def test_exit(cli_runner):
    with cli_runner.isolation(input='exit\n') as outstreams:
        cmd = ClickCmd(hist_file='.history')

        cmd.cmdloop()

        output = outstreams[0].getvalue() \
            .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

    assert output == ClickCmd.prompt

    os.remove('.history')


def test_on_finished(cli_runner):
    with cli_runner.isolation() as outstreams:
        def finisher(c):
            click.echo(c + '#finished')

        cmd = ClickCmd(ctx='dummy-ctx', hist_file='.history', on_finished=finisher)

        cmd.cmdloop()

        output = outstreams[0].getvalue() \
            .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

    assert output == ClickCmd.prompt + '\ndummy-ctx#finished\n'

    os.remove('.history')


def test_help(cli_runner):
    with cli_runner.isolation(input='help\n') as outstreams:
        cmd = ClickCmd(hist_file='.history')

        cmd.cmdloop()

        output = outstreams[0].getvalue() \
            .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

    assert output == '{0}\n' \
                     'Undocumented commands:\n' \
                     '======================\n' \
                     'exit  help  quit\n' \
                     '\n' \
                     '{0}\n'.format(ClickCmd.prompt)

    os.remove('.history')


def test_keyboard_interrupt():
    stdin = BadStringIO()
    stdout = StringIO()

    old_in = sys.stdin
    old_out = sys.stdout
    try:
        sys.stdin = stdin
        sys.stdout = stdout

        cmd = ClickCmd(hist_file='.history')

        cmd.cmdloop()
    finally:
        sys.stdin = old_in
        sys.stdout = old_out

    assert stdout.getvalue() == '{0}\nKeyboardInterrupt\n{0}\n'.format(ClickCmd.prompt)

    os.remove('.history')


def test_changable_prompt(cli_runner):
    with cli_runner.isolation(input='\n\n\n') as outstreams:

        cmd = ClickCmd(hist_file='.history')

        class Prompt(object):

            def __init__(self):
                self.num = 0

            def __call__(self):
                self.num += 1
                return "prompt #{} > ".format(self.num)

        cmd.prompt = Prompt()

        cmd.cmdloop()

        output = outstreams[0].getvalue() \
            .decode(cli_runner.charset, 'replace').replace('\r\n', '\n')

    assert output == 'prompt #1 > prompt #2 > prompt #3 > prompt #4 > \n'

    os.remove('.history')