Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

229 lines · 7.1 KiB · Python

Last changed by

Fixed up pausing tests and added doc information.Armin Ronacherbefore adoption65ca572

History of this file

Take it

The walk stopped after 200 commits: lines older than 5f8c04f point at it.

Fixed a regression for echo_via_pager on 3.x. This fixes #185Armin Ronacherbefore adoptionb625654

line 1why, one step back
1import os

Unbroke colorama integrated echo for unicode.Armin Ronacherbefore adoption537d6d5

line 2why, one step back
2import sys

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

lines 3–4why, one step back
3import click
4

Fixed a regression for echo_via_pager on 3.x. This fixes #185Armin Ronacherbefore adoptionb625654

line 5why, one step back
5import click._termui_impl

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

line 6why, one step back
6

Fixed a regression for echo_via_pager on 3.x. This fixes #185Armin Ronacherbefore adoptionb625654

line 7why, one step back
7

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

lines 8–13why, one step back
8def test_echo(runner):
9 with runner.isolation() as out:10 click.echo(u'\N{SNOWMAN}')11 click.echo(b'\x44\x44')12 click.echo(42, nl=False)13 click.echo(b'a', nl=False)

Refactored pager support to not rely on pydocArmin Ronacherbefore adoptionaef1650

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

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

line 15why, one step back
15 bytes = out.getvalue()

Refactored pager support to not rely on pydocArmin Ronacherbefore adoptionaef1650

line 16why, one step back
16 assert bytes == b'\xe2\x98\x83\nDD\n42ax'

Unbroke colorama integrated echo for unicode.Armin Ronacherbefore adoption537d6d5

line 17why, one step back
17

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

lines 18–19why, one step back
18 # If we are in Python 2, we expect that writing bytes into a string io
19 # does not do anything crazy. In Python 3

Unbroke colorama integrated echo for unicode.Armin Ronacherbefore adoption537d6d5

lines 20–34why, one step back
20 if sys.version_info[0] == 2:
21 import StringIO22 sys.stdout = x = StringIO.StringIO()23 try:24 click.echo('\xf6')25 finally:26 sys.stdout = sys.__stdout__27 assert x.getvalue() == '\xf6\n'2829 # And in any case, if wrapped, we expect bytes to survive.30 @click.command()31 def cli():32 click.echo(b'\xf6')33 result = runner.invoke(cli, [])34 assert result.output_bytes == b'\xf6\n'

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

line 35why, one step back
35

Ensure we do not strip color codes for bytes.Armin Ronacherbefore adoption427492b

lines 36–38why, one step back
36 # Ensure we do not strip for bytes.
37 with runner.isolation() as out:38 click.echo(bytearray(b'\x1b[31mx\x1b[39m'), nl=False)

Fixed a test on python 3Armin Ronacherbefore adoptionb24e004

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

Ensure we do not strip color codes for bytes.Armin Ronacherbefore adoption427492b

line 40why, one step back
40

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

line 41why, one step back
41

Added tests for stylingArmin Ronacherbefore adoption3eb2c48

lines 42–69why, one step back
42def test_styling():
43 examples = [44 ('x', dict(fg='black'), '\x1b[30mx\x1b[0m'),45 ('x', dict(fg='red'), '\x1b[31mx\x1b[0m'),46 ('x', dict(fg='green'), '\x1b[32mx\x1b[0m'),47 ('x', dict(fg='yellow'), '\x1b[33mx\x1b[0m'),48 ('x', dict(fg='blue'), '\x1b[34mx\x1b[0m'),49 ('x', dict(fg='magenta'), '\x1b[35mx\x1b[0m'),50 ('x', dict(fg='cyan'), '\x1b[36mx\x1b[0m'),51 ('x', dict(fg='white'), '\x1b[37mx\x1b[0m'),52 ('x', dict(bg='black'), '\x1b[40mx\x1b[0m'),53 ('x', dict(bg='red'), '\x1b[41mx\x1b[0m'),54 ('x', dict(bg='green'), '\x1b[42mx\x1b[0m'),55 ('x', dict(bg='yellow'), '\x1b[43mx\x1b[0m'),56 ('x', dict(bg='blue'), '\x1b[44mx\x1b[0m'),57 ('x', dict(bg='magenta'), '\x1b[45mx\x1b[0m'),58 ('x', dict(bg='cyan'), '\x1b[46mx\x1b[0m'),59 ('x', dict(bg='white'), '\x1b[47mx\x1b[0m'),60 ('foo bar', dict(blink=True), '\x1b[5mfoo bar\x1b[0m'),61 ('foo bar', dict(underline=True), '\x1b[4mfoo bar\x1b[0m'),62 ('foo bar', dict(bold=True), '\x1b[1mfoo bar\x1b[0m'),63 ('foo bar', dict(dim=True), '\x1b[2mfoo bar\x1b[0m'),64 ]65 for text, styles, ref in examples:66 assert click.style(text, **styles) == ref67 assert click.unstyle(ref) == text6869

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

lines 70–74why, one step back
70def test_filename_formatting():
71 assert click.format_filename(b'foo.txt') == 'foo.txt'72 assert click.format_filename(b'/x/foo.txt') == '/x/foo.txt'73 assert click.format_filename(u'/x/foo.txt') == '/x/foo.txt'74 assert click.format_filename(u'/x/foo.txt', shorten=True) == 'foo.txt'

Merge branch '1.0-maintenance'Armin Ronacherbefore adoptionf36e404

line 75why, one step back
75 assert click.format_filename(b'/x/foo\xff.txt', shorten=True) \

Mentioned "--" in the docs.Armin Ronacherbefore adoption5f8c04f

line 76why, one step back
76 == u'foo\ufffd.txt'

yN->y/NArmin Ronacherbefore adoptionc7299c4

lines 77–116why, one step back
77
7879def test_prompts(runner):80 @click.command()81 def test():82 if click.confirm('Foo'):83 click.echo('yes!')84 else:85 click.echo('no :(')8687 result = runner.invoke(test, input='y\n')88 assert not result.exception89 assert result.output == 'Foo [y/N]: y\nyes!\n'9091 result = runner.invoke(test, input='\n')92 assert not result.exception93 assert result.output == 'Foo [y/N]: \nno :(\n'9495 result = runner.invoke(test, input='n\n')96 assert not result.exception97 assert result.output == 'Foo [y/N]: n\nno :(\n'9899 @click.command()100 def test_no():101 if click.confirm('Foo', default=True):102 click.echo('yes!')103 else:104 click.echo('no :(')105106 result = runner.invoke(test_no, input='y\n')107 assert not result.exception108 assert result.output == 'Foo [Y/n]: y\nyes!\n'109110 result = runner.invoke(test_no, input='\n')111 assert not result.exception112 assert result.output == 'Foo [Y/n]: \nyes!\n'113114 result = runner.invoke(test_no, input='n\n')115 assert not result.exception116 assert result.output == 'Foo [Y/n]: n\nno :(\n'

Fixed a regression for echo_via_pager on 3.x. This fixes #185Armin Ronacherbefore adoptionb625654

lines 117–124why, one step back
117
118119def test_echo_via_pager(monkeypatch, capfd):120 monkeypatch.setitem(os.environ, 'PAGER', 'cat')121 monkeypatch.setattr(click._termui_impl, 'isatty', lambda x: True)122 click.echo_via_pager('haha')123 out, err = capfd.readouterr()124 assert out == 'haha\n'

Added test for open_fileArmin Ronacherbefore adoption1318a20

lines 125–126why, one step back
125
126

Added support for explicit color control.Armin Ronacherbefore adoption5cf7f2d

lines 127–153why, one step back
127def test_echo_color_flag(monkeypatch, capfd):
128 isatty = True129 monkeypatch.setattr(click._compat, 'isatty', lambda x: isatty)130131 text = 'foo'132 styled_text = click.style(text, fg='red')133 assert styled_text == '\x1b[31mfoo\x1b[0m'134135 click.echo(styled_text, color=False)136 out, err = capfd.readouterr()137 assert out == text + '\n'138139 click.echo(styled_text, color=True)140 out, err = capfd.readouterr()141 assert out == styled_text + '\n'142143 isatty = True144 click.echo(styled_text)145 out, err = capfd.readouterr()146 assert out == styled_text + '\n'147148 isatty = False149 click.echo(styled_text)150 out, err = capfd.readouterr()151 assert out == text + '\n'152153

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

lines 154–196why, one step back
154def test_echo_writing_to_standard_error(capfd, monkeypatch):
155 def emulate_input(text):156 """Emulate keyboard input."""157 if sys.version_info[0] == 2:158 from StringIO import StringIO159 else:160 from io import StringIO161 monkeypatch.setattr(sys, 'stdin', StringIO(text))162163 click.echo('Echo to standard output')164 out, err = capfd.readouterr()165 assert out == 'Echo to standard output\n'166 assert err == ''167168 click.echo('Echo to standard error', err=True)169 out, err = capfd.readouterr()170 assert out == ''171 assert err == 'Echo to standard error\n'172173 emulate_input('asdlkj\n')174 click.prompt('Prompt to stdin')175 out, err = capfd.readouterr()176 assert out == 'Prompt to stdin: '177 assert err == ''178179 emulate_input('asdlkj\n')180 click.prompt('Prompt to stderr', err=True)181 out, err = capfd.readouterr()182 assert out == ''183 assert err == 'Prompt to stderr: '184185 emulate_input('y\n')186 click.confirm('Prompt to stdin')187 out, err = capfd.readouterr()188 assert out == 'Prompt to stdin [y/N]: '189 assert err == ''190191 emulate_input('y\n')192 click.confirm('Prompt to stderr', err=True)193 out, err = capfd.readouterr()194 assert out == ''195 assert err == 'Prompt to stderr [y/N]: '196

Fixed up pausing tests and added doc information.Armin Ronacherbefore adoption65ca572

lines 197–198why, one step back
197 monkeypatch.setattr(click.termui, 'isatty', lambda x: True)
198 monkeypatch.setattr(click.termui, 'getchar', lambda: ' ')

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

line 199why, one step back
199

Fixed up pausing tests and added doc information.Armin Ronacherbefore adoption65ca572

line 200why, one step back
200 click.pause('Pause to stdout')

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

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

Fixed up pausing tests and added doc information.Armin Ronacherbefore adoption65ca572

line 202why, one step back
202 assert out == 'Pause to stdout\n'

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

lines 203–207why, one step back
203 assert err == ''
204205 click.pause('Pause to stderr', err=True)206 out, err = capfd.readouterr()207 assert out == ''

Fixed up pausing tests and added doc information.Armin Ronacherbefore adoption65ca572

line 208why, one step back
208 assert err == 'Pause to stderr\n'

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

lines 209–210why, one step back
209
210

Added test for open_fileArmin Ronacherbefore adoption1318a20

lines 211–229why, one step back
211def test_open_file(runner):
212 with runner.isolated_filesystem():213 with open('hello.txt', 'w') as f:214 f.write('Cool stuff')215216 @click.command()217 @click.argument('filename')218 def cli(filename):219 with click.open_file(filename) as f:220 click.echo(f.read())221 click.echo('meep')222223 result = runner.invoke(cli, ['hello.txt'])224 assert result.exception is None225 assert result.output == 'Cool stuff\nmeep\n'226227 result = runner.invoke(cli, ['-'], input='foobar')228 assert result.exception is None229 assert result.output == 'foobar\nmeep\n'