Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

306 lines · 9.4 KiB · Python

Last changed by

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

History of this file

Take it

The walk stopped after 200 commits: lines older than 5620b05 point at it.

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 1–8why, one step back
1import os
2import sys34import pytest56import click7import click.utils8import click._termui_impl

Added two xfailsArmin Ronacherbefore adoptiondde42ed

line 9why, one step back
9from click._compat import WIN, PY2

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 10–12why, one step back
10
1112def test_echo(runner):

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 13why, one step back
13 with runner.isolation() as outstreams:

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 14–18why, one step back
14 click.echo(u'\N{SNOWMAN}')
15 click.echo(b'\x44\x44')16 click.echo(42, nl=False)17 click.echo(b'a', nl=False)18 click.echo('\x1b[31mx\x1b[39m', nl=False)

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 19why, one step back
19 bytes = outstreams[0].getvalue().replace(b'\r\n', b'\n')

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 20–37why, one step back
20 assert bytes == b'\xe2\x98\x83\nDD\n42ax'
2122 # If we are in Python 2, we expect that writing bytes into a string io23 # does not do anything crazy. In Python 324 if sys.version_info[0] == 2:25 import StringIO26 sys.stdout = x = StringIO.StringIO()27 try:28 click.echo('\xf6')29 finally:30 sys.stdout = sys.__stdout__31 assert x.getvalue() == '\xf6\n'3233 # And in any case, if wrapped, we expect bytes to survive.34 @click.command()35 def cli():36 click.echo(b'\xf6')37 result = runner.invoke(cli, [])

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 38why, one step back
38 assert result.stdout_bytes == b'\xf6\n'

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 39–40why, one step back
39
40 # Ensure we do not strip for bytes.

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 41why, one step back
41 with runner.isolation() as outstreams:

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

line 42why, one step back
42 click.echo(bytearray(b'\x1b[31mx\x1b[39m'), nl=False)

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 43why, one step back
43 assert outstreams[0].getvalue() == b'\x1b[31mx\x1b[39m'

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 44–86why, one step back
44
4546def test_echo_custom_file():47 import io48 f = io.StringIO()49 click.echo(u'hello', file=f)50 assert f.getvalue() == u'hello\n'515253def test_styling():54 examples = [55 ('x', dict(fg='black'), '\x1b[30mx\x1b[0m'),56 ('x', dict(fg='red'), '\x1b[31mx\x1b[0m'),57 ('x', dict(fg='green'), '\x1b[32mx\x1b[0m'),58 ('x', dict(fg='yellow'), '\x1b[33mx\x1b[0m'),59 ('x', dict(fg='blue'), '\x1b[34mx\x1b[0m'),60 ('x', dict(fg='magenta'), '\x1b[35mx\x1b[0m'),61 ('x', dict(fg='cyan'), '\x1b[36mx\x1b[0m'),62 ('x', dict(fg='white'), '\x1b[37mx\x1b[0m'),63 ('x', dict(bg='black'), '\x1b[40mx\x1b[0m'),64 ('x', dict(bg='red'), '\x1b[41mx\x1b[0m'),65 ('x', dict(bg='green'), '\x1b[42mx\x1b[0m'),66 ('x', dict(bg='yellow'), '\x1b[43mx\x1b[0m'),67 ('x', dict(bg='blue'), '\x1b[44mx\x1b[0m'),68 ('x', dict(bg='magenta'), '\x1b[45mx\x1b[0m'),69 ('x', dict(bg='cyan'), '\x1b[46mx\x1b[0m'),70 ('x', dict(bg='white'), '\x1b[47mx\x1b[0m'),71 ('foo bar', dict(blink=True), '\x1b[5mfoo bar\x1b[0m'),72 ('foo bar', dict(underline=True), '\x1b[4mfoo bar\x1b[0m'),73 ('foo bar', dict(bold=True), '\x1b[1mfoo bar\x1b[0m'),74 ('foo bar', dict(dim=True), '\x1b[2mfoo bar\x1b[0m'),75 ]76 for text, styles, ref in examples:77 assert click.style(text, **styles) == ref78 assert click.unstyle(ref) == text798081def test_filename_formatting():82 assert click.format_filename(b'foo.txt') == 'foo.txt'83 assert click.format_filename(b'/x/foo.txt') == '/x/foo.txt'84 assert click.format_filename(u'/x/foo.txt') == '/x/foo.txt'85 assert click.format_filename(u'/x/foo.txt', shorten=True) == 'foo.txt'86

Fixed broken tests and behavior on windows.Armin Ronacherbefore adoption262c275

lines 87–90why, one step back
87 # filesystem encoding on windows permits this.
88 if not WIN:89 assert click.format_filename(b'/x/foo\xff.txt', shorten=True) \90 == u'foo\ufffd.txt'

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

line 91why, one step back
91

Fixed broken tests and behavior on windows.Armin Ronacherbefore adoption262c275

line 92why, one step back
92

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 93–132why, one step back
93def test_prompts(runner):
94 @click.command()95 def test():96 if click.confirm('Foo'):97 click.echo('yes!')98 else:99 click.echo('no :(')100101 result = runner.invoke(test, input='y\n')102 assert not result.exception103 assert result.output == 'Foo [y/N]: y\nyes!\n'104105 result = runner.invoke(test, input='\n')106 assert not result.exception107 assert result.output == 'Foo [y/N]: \nno :(\n'108109 result = runner.invoke(test, input='n\n')110 assert not result.exception111 assert result.output == 'Foo [y/N]: n\nno :(\n'112113 @click.command()114 def test_no():115 if click.confirm('Foo', default=True):116 click.echo('yes!')117 else:118 click.echo('no :(')119120 result = runner.invoke(test_no, input='y\n')121 assert not result.exception122 assert result.output == 'Foo [Y/n]: y\nyes!\n'123124 result = runner.invoke(test_no, input='\n')125 assert not result.exception126 assert result.output == 'Foo [Y/n]: \nyes!\n'127128 result = runner.invoke(test_no, input='n\n')129 assert not result.exception130 assert result.output == 'Foo [Y/n]: n\nno :(\n'131132

Fixed broken tests and behavior on windows.Armin Ronacherbefore adoption262c275

line 133why, one step back
133@pytest.mark.skipif(WIN, reason='Different behavior on windows.')

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 134–148why, one step back
134def test_prompts_abort(monkeypatch, capsys):
135 def f(_):136 raise KeyboardInterrupt()137138 monkeypatch.setattr('click.termui.hidden_prompt_func', f)139140 try:141 click.prompt('Password', hide_input=True)142 except click.Abort:143 click.echo('Screw you.')144145 out, err = capsys.readouterr()146 assert out == 'Password: \nScrew you.\n'147148

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

lines 149–155why, one step back
149def _test_gen_func():
150 yield 'a'151 yield 'b'152 yield 'c'153 yield 'abc'154155

Fixed broken tests and behavior on windows.Armin Ronacherbefore adoption262c275

line 156why, one step back
156@pytest.mark.skipif(WIN, reason='Different behavior on windows.')

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

line 157why, one step back
157@pytest.mark.parametrize('cat', ['cat', 'cat ', 'cat '])

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

lines 158–168why, one step back
158@pytest.mark.parametrize('test', [
159 # We need lambda here, because pytest will160 # reuse the parameters, and then the generators161 # are already used and will not yield anymore162 ('just text\n', lambda: 'just text'),163 ('iterable\n', lambda: ["itera", "ble"]),164 ('abcabc\n', lambda: _test_gen_func),165 ('abcabc\n', lambda: _test_gen_func()),166 ('012345\n', lambda: (c for c in range(6))),167])168def test_echo_via_pager(monkeypatch, capfd, cat, test):

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 169–170why, one step back
169 monkeypatch.setitem(os.environ, 'PAGER', cat)
170 monkeypatch.setattr(click._termui_impl, 'isatty', lambda x: True)

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

lines 171–176why, one step back
171
172 expected_output = test[0]173 test_input = test[1]()174175 click.echo_via_pager(test_input)176

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

line 177why, one step back
177 out, err = capfd.readouterr()

Merge pull request #889 from stefreak/masterDan Sullybefore adoptionb1b4449

line 178why, one step back
178 assert out == expected_output

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 179–180why, one step back
179
180

Fixed broken tests and behavior on windows.Armin Ronacherbefore adoption262c275

line 181why, one step back
181@pytest.mark.skipif(WIN, reason='Test does not make sense on Windows.')

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 182–208why, one step back
182def test_echo_color_flag(monkeypatch, capfd):
183 isatty = True184 monkeypatch.setattr(click._compat, 'isatty', lambda x: isatty)185186 text = 'foo'187 styled_text = click.style(text, fg='red')188 assert styled_text == '\x1b[31mfoo\x1b[0m'189190 click.echo(styled_text, color=False)191 out, err = capfd.readouterr()192 assert out == text + '\n'193194 click.echo(styled_text, color=True)195 out, err = capfd.readouterr()196 assert out == styled_text + '\n'197198 isatty = True199 click.echo(styled_text)200 out, err = capfd.readouterr()201 assert out == styled_text + '\n'202203 isatty = False204 click.echo(styled_text)205 out, err = capfd.readouterr()206 assert out == text + '\n'207208

Fixed broken tests and behavior on windows.Armin Ronacherbefore adoption262c275

line 209why, one step back
209@pytest.mark.skipif(WIN, reason='Test too complex to make work windows.')

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 210–287why, one step back
210def test_echo_writing_to_standard_error(capfd, monkeypatch):
211 def emulate_input(text):212 """Emulate keyboard input."""213 if sys.version_info[0] == 2:214 from StringIO import StringIO215 else:216 from io import StringIO217 monkeypatch.setattr(sys, 'stdin', StringIO(text))218219 click.echo('Echo to standard output')220 out, err = capfd.readouterr()221 assert out == 'Echo to standard output\n'222 assert err == ''223224 click.echo('Echo to standard error', err=True)225 out, err = capfd.readouterr()226 assert out == ''227 assert err == 'Echo to standard error\n'228229 emulate_input('asdlkj\n')230 click.prompt('Prompt to stdin')231 out, err = capfd.readouterr()232 assert out == 'Prompt to stdin: '233 assert err == ''234235 emulate_input('asdlkj\n')236 click.prompt('Prompt to stderr', err=True)237 out, err = capfd.readouterr()238 assert out == ''239 assert err == 'Prompt to stderr: '240241 emulate_input('y\n')242 click.confirm('Prompt to stdin')243 out, err = capfd.readouterr()244 assert out == 'Prompt to stdin [y/N]: '245 assert err == ''246247 emulate_input('y\n')248 click.confirm('Prompt to stderr', err=True)249 out, err = capfd.readouterr()250 assert out == ''251 assert err == 'Prompt to stderr [y/N]: '252253 monkeypatch.setattr(click.termui, 'isatty', lambda x: True)254 monkeypatch.setattr(click.termui, 'getchar', lambda: ' ')255256 click.pause('Pause to stdout')257 out, err = capfd.readouterr()258 assert out == 'Pause to stdout\n'259 assert err == ''260261 click.pause('Pause to stderr', err=True)262 out, err = capfd.readouterr()263 assert out == ''264 assert err == 'Pause to stderr\n'265266267def test_open_file(runner):268 with runner.isolated_filesystem():269 with open('hello.txt', 'w') as f:270 f.write('Cool stuff')271272 @click.command()273 @click.argument('filename')274 def cli(filename):275 with click.open_file(filename) as f:276 click.echo(f.read())277 click.echo('meep')278279 result = runner.invoke(cli, ['hello.txt'])280 assert result.exception is None281 assert result.output == 'Cool stuff\nmeep\n'282283 result = runner.invoke(cli, ['-'], input='foobar')284 assert result.exception is None285 assert result.output == 'foobar\nmeep\n'286287

Added two xfailsArmin Ronacherbefore adoptiondde42ed

line 288why, one step back
288@pytest.mark.xfail(WIN and not PY2, reason='God knows ...')

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 289–292why, one step back
289def test_iter_keepopenfile(tmpdir):
290 expected = list(map(str, range(10)))291 p = tmpdir.mkdir('testdir').join('testfile')292 p.write(os.linesep.join(expected))

Merge pull request #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498

lines 293–295why, one step back
293 with p.open() as f:
294 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):295 assert e_line == a_line.strip()

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 296–297why, one step back
296
297

Added two xfailsArmin Ronacherbefore adoptiondde42ed

line 298why, one step back
298@pytest.mark.xfail(WIN and not PY2, reason='God knows ...')

We're not rejecting multi commands below chained multi commands. This fixes #415Armin Ronacherbefore adoption5620b05

lines 299–302why, one step back
299def test_iter_lazyfile(tmpdir):
300 expected = list(map(str, range(10)))301 p = tmpdir.mkdir('testdir').join('testfile')302 p.write(os.linesep.join(expected))

Merge pull request #878 from jdufresne/resource-warningZachery Birbefore adoption3f66498

lines 303–306why, one step back
303 with p.open() as f:
304 with click.utils.LazyFile(f.name) as lf:305 for e_line, a_line in zip(expected, lf):306 assert e_line == a_line.strip()