Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

250 lines · 7.7 KiB · Python

Last changed by

Merge pull request #342 from geowurster/masterMarkus Unterwaditzerbefore adoption36b8002

History of this file

Take it

The walk stopped after 200 commits: lines older than d27f3a6 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

Added notes on callback changes to the docsArmin Ronacherbefore adoptiond27f3a6

lines 2–3why, one step back
2import sys
3

Merge pull request #342 from geowurster/masterMarkus Unterwaditzerbefore adoption36b8002

lines 4–5why, one step back
4import click
5import click.utils

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

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

Added notes on callback changes to the docsArmin Ronacherbefore adoptiond27f3a6

line 7why, one step back
7

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

line 8why, one step back
8

Added notes on callback changes to the docsArmin Ronacherbefore adoptiond27f3a6

lines 9–18why, one step back
9def test_echo(runner):
10 with runner.isolation() as out:11 click.echo(u'\N{SNOWMAN}')12 click.echo(b'\x44\x44')13 click.echo(42, nl=False)14 click.echo(b'a', nl=False)15 click.echo('\x1b[31mx\x1b[39m', nl=False)16 bytes = out.getvalue()17 assert bytes == b'\xe2\x98\x83\nDD\n42ax'18

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

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

Added notes on callback changes to the docsArmin Ronacherbefore adoptiond27f3a6

lines 21–42why, one step back
21 if sys.version_info[0] == 2:
22 import StringIO23 sys.stdout = x = StringIO.StringIO()24 try:25 click.echo('\xf6')26 finally:27 sys.stdout = sys.__stdout__28 assert x.getvalue() == '\xf6\n'2930 # And in any case, if wrapped, we expect bytes to survive.31 @click.command()32 def cli():33 click.echo(b'\xf6')34 result = runner.invoke(cli, [])35 assert result.output_bytes == b'\xf6\n'3637 # Ensure we do not strip for bytes.38 with runner.isolation() as out:39 click.echo(bytearray(b'\x1b[31mx\x1b[39m'), nl=False)40 assert out.getvalue() == b'\x1b[31mx\x1b[39m'4142

Added tests for stylingArmin Ronacherbefore adoption3eb2c48

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

Added notes on callback changes to the docsArmin Ronacherbefore adoptiond27f3a6

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

yN->y/NArmin Ronacherbefore adoptionc7299c4

lines 78–117why, one step back
78
7980def test_prompts(runner):81 @click.command()82 def test():83 if click.confirm('Foo'):84 click.echo('yes!')85 else:86 click.echo('no :(')8788 result = runner.invoke(test, input='y\n')89 assert not result.exception90 assert result.output == 'Foo [y/N]: y\nyes!\n'9192 result = runner.invoke(test, input='\n')93 assert not result.exception94 assert result.output == 'Foo [y/N]: \nno :(\n'9596 result = runner.invoke(test, input='n\n')97 assert not result.exception98 assert result.output == 'Foo [y/N]: n\nno :(\n'99100 @click.command()101 def test_no():102 if click.confirm('Foo', default=True):103 click.echo('yes!')104 else:105 click.echo('no :(')106107 result = runner.invoke(test_no, input='y\n')108 assert not result.exception109 assert result.output == 'Foo [Y/n]: y\nyes!\n'110111 result = runner.invoke(test_no, input='\n')112 assert not result.exception113 assert result.output == 'Foo [Y/n]: \nyes!\n'114115 result = runner.invoke(test_no, input='n\n')116 assert not result.exception117 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 118–125why, one step back
118
119120def test_echo_via_pager(monkeypatch, capfd):121 monkeypatch.setitem(os.environ, 'PAGER', 'cat')122 monkeypatch.setattr(click._termui_impl, 'isatty', lambda x: True)123 click.echo_via_pager('haha')124 out, err = capfd.readouterr()125 assert out == 'haha\n'

Added test for open_fileArmin Ronacherbefore adoption1318a20

lines 126–127why, one step back
126
127

Added support for explicit color control.Armin Ronacherbefore adoption5cf7f2d

lines 128–154why, one step back
128def test_echo_color_flag(monkeypatch, capfd):
129 isatty = True130 monkeypatch.setattr(click._compat, 'isatty', lambda x: isatty)131132 text = 'foo'133 styled_text = click.style(text, fg='red')134 assert styled_text == '\x1b[31mfoo\x1b[0m'135136 click.echo(styled_text, color=False)137 out, err = capfd.readouterr()138 assert out == text + '\n'139140 click.echo(styled_text, color=True)141 out, err = capfd.readouterr()142 assert out == styled_text + '\n'143144 isatty = True145 click.echo(styled_text)146 out, err = capfd.readouterr()147 assert out == styled_text + '\n'148149 isatty = False150 click.echo(styled_text)151 out, err = capfd.readouterr()152 assert out == text + '\n'153154

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

lines 155–197why, one step back
155def test_echo_writing_to_standard_error(capfd, monkeypatch):
156 def emulate_input(text):157 """Emulate keyboard input."""158 if sys.version_info[0] == 2:159 from StringIO import StringIO160 else:161 from io import StringIO162 monkeypatch.setattr(sys, 'stdin', StringIO(text))163164 click.echo('Echo to standard output')165 out, err = capfd.readouterr()166 assert out == 'Echo to standard output\n'167 assert err == ''168169 click.echo('Echo to standard error', err=True)170 out, err = capfd.readouterr()171 assert out == ''172 assert err == 'Echo to standard error\n'173174 emulate_input('asdlkj\n')175 click.prompt('Prompt to stdin')176 out, err = capfd.readouterr()177 assert out == 'Prompt to stdin: '178 assert err == ''179180 emulate_input('asdlkj\n')181 click.prompt('Prompt to stderr', err=True)182 out, err = capfd.readouterr()183 assert out == ''184 assert err == 'Prompt to stderr: '185186 emulate_input('y\n')187 click.confirm('Prompt to stdin')188 out, err = capfd.readouterr()189 assert out == 'Prompt to stdin [y/N]: '190 assert err == ''191192 emulate_input('y\n')193 click.confirm('Prompt to stderr', err=True)194 out, err = capfd.readouterr()195 assert out == ''196 assert err == 'Prompt to stderr [y/N]: '197

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

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

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

line 200why, one step back
200

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

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

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

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

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

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

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

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

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

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

Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab

lines 210–211why, one step back
210
211

Added test for open_fileArmin Ronacherbefore adoption1318a20

lines 212–230why, one step back
212def test_open_file(runner):
213 with runner.isolated_filesystem():214 with open('hello.txt', 'w') as f:215 f.write('Cool stuff')216217 @click.command()218 @click.argument('filename')219 def cli(filename):220 with click.open_file(filename) as f:221 click.echo(f.read())222 click.echo('meep')223224 result = runner.invoke(cli, ['hello.txt'])225 assert result.exception is None226 assert result.output == 'Cool stuff\nmeep\n'227228 result = runner.invoke(cli, ['-'], input='foobar')229 assert result.exception is None230 assert result.output == 'foobar\nmeep\n'

Merge pull request #342 from geowurster/masterMarkus Unterwaditzerbefore adoption36b8002

lines 231–250why, one step back
231
232233def test_iter_keepopenfile(tmpdir):234235 expected = list(map(str, range(10)))236 p = tmpdir.mkdir('testdir').join('testfile')237 p.write(os.linesep.join(expected))238 f = p.open()239 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):240 assert e_line == a_line.strip()241242243def test_iter_lazyfile(tmpdir):244245 expected = list(map(str, range(10)))246 p = tmpdir.mkdir('testdir').join('testfile')247 p.write(os.linesep.join(expected))248 f = p.open()249 for e_line, a_line in zip(expected, click.utils.LazyFile(f.name)):250 assert e_line == a_line.strip()