test_utils.py
This file
Path
tests/test_utils.py
275 lines · 8.2 KiB · Python
Last changed by
Ignore broken PAGER valuesMarkus Unterwaditzerbefore adoption349875d
History of this fileTake it
The walk stopped after 200 commits: lines older than 13ab804 point at it.
Fixed a regression for echo_via_pager on 3.x. This fixes #185Armin Ronacherbefore adoptionb625654
line 1why, one step back1import osAdd test to ensure click does not import too muchArmin Ronacherbefore adoption13ab804
lines 2–3why, one step back2import sys3Ignore broken PAGER valuesMarkus Unterwaditzerbefore adoption349875d
lines 4–5why, one step back4import pytest5Merge pull request #342 from geowurster/masterMarkus Unterwaditzerbefore adoption36b8002
lines 6–7why, one step back6import click7import click.utilsFixed a regression for echo_via_pager on 3.x. This fixes #185Armin Ronacherbefore adoptionb625654
line 8why, one step back8import click._termui_implAdd test to ensure click does not import too muchArmin Ronacherbefore adoption13ab804
line 9why, one step back9Fixed a regression for echo_via_pager on 3.x. This fixes #185Armin Ronacherbefore adoptionb625654
line 10why, one step back10Add test to ensure click does not import too muchArmin Ronacherbefore adoption13ab804
lines 11–20why, one step back11def test_echo(runner):12 with runner.isolation() as out:13 click.echo(u'\N{SNOWMAN}')14 click.echo(b'\x44\x44')15 click.echo(42, nl=False)16 click.echo(b'a', nl=False)17 click.echo('\x1b[31mx\x1b[39m', nl=False)18 bytes = out.getvalue()19 assert bytes == b'\xe2\x98\x83\nDD\n42ax'20Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a
lines 21–22why, one step back21 # If we are in Python 2, we expect that writing bytes into a string io22 # does not do anything crazy. In Python 3Add test to ensure click does not import too muchArmin Ronacherbefore adoption13ab804
lines 23–44why, one step back23 if sys.version_info[0] == 2:24 import StringIO25 sys.stdout = x = StringIO.StringIO()26 try:27 click.echo('\xf6')28 finally:29 sys.stdout = sys.__stdout__30 assert x.getvalue() == '\xf6\n'3132 # And in any case, if wrapped, we expect bytes to survive.33 @click.command()34 def cli():35 click.echo(b'\xf6')36 result = runner.invoke(cli, [])37 assert result.output_bytes == b'\xf6\n'3839 # Ensure we do not strip for bytes.40 with runner.isolation() as out:41 click.echo(bytearray(b'\x1b[31mx\x1b[39m'), nl=False)42 assert out.getvalue() == b'\x1b[31mx\x1b[39m'4344Merge branch '4.x-maintenance'Markus Unterwaditzerbefore adoptiona0d426d
lines 45–51why, one step back45def test_echo_custom_file():46 import io47 f = io.StringIO()48 click.echo(u'hello', file=f)49 assert f.getvalue() == u'hello\n'5051Add test to ensure click does not import too muchArmin Ronacherbefore adoption13ab804
lines 52–126why, one step back52def test_styling():53 examples = [54 ('x', dict(fg='black'), '\x1b[30mx\x1b[0m'),55 ('x', dict(fg='red'), '\x1b[31mx\x1b[0m'),56 ('x', dict(fg='green'), '\x1b[32mx\x1b[0m'),57 ('x', dict(fg='yellow'), '\x1b[33mx\x1b[0m'),58 ('x', dict(fg='blue'), '\x1b[34mx\x1b[0m'),59 ('x', dict(fg='magenta'), '\x1b[35mx\x1b[0m'),60 ('x', dict(fg='cyan'), '\x1b[36mx\x1b[0m'),61 ('x', dict(fg='white'), '\x1b[37mx\x1b[0m'),62 ('x', dict(bg='black'), '\x1b[40mx\x1b[0m'),63 ('x', dict(bg='red'), '\x1b[41mx\x1b[0m'),64 ('x', dict(bg='green'), '\x1b[42mx\x1b[0m'),65 ('x', dict(bg='yellow'), '\x1b[43mx\x1b[0m'),66 ('x', dict(bg='blue'), '\x1b[44mx\x1b[0m'),67 ('x', dict(bg='magenta'), '\x1b[45mx\x1b[0m'),68 ('x', dict(bg='cyan'), '\x1b[46mx\x1b[0m'),69 ('x', dict(bg='white'), '\x1b[47mx\x1b[0m'),70 ('foo bar', dict(blink=True), '\x1b[5mfoo bar\x1b[0m'),71 ('foo bar', dict(underline=True), '\x1b[4mfoo bar\x1b[0m'),72 ('foo bar', dict(bold=True), '\x1b[1mfoo bar\x1b[0m'),73 ('foo bar', dict(dim=True), '\x1b[2mfoo bar\x1b[0m'),74 ]75 for text, styles, ref in examples:76 assert click.style(text, **styles) == ref77 assert click.unstyle(ref) == text787980def test_filename_formatting():81 assert click.format_filename(b'foo.txt') == 'foo.txt'82 assert click.format_filename(b'/x/foo.txt') == '/x/foo.txt'83 assert click.format_filename(u'/x/foo.txt') == '/x/foo.txt'84 assert click.format_filename(u'/x/foo.txt', shorten=True) == 'foo.txt'85 assert click.format_filename(b'/x/foo\xff.txt', shorten=True) \86 == u'foo\ufffd.txt'878889def test_prompts(runner):90 @click.command()91 def test():92 if click.confirm('Foo'):93 click.echo('yes!')94 else:95 click.echo('no :(')9697 result = runner.invoke(test, input='y\n')98 assert not result.exception99 assert result.output == 'Foo [y/N]: y\nyes!\n'100101 result = runner.invoke(test, input='\n')102 assert not result.exception103 assert result.output == 'Foo [y/N]: \nno :(\n'104105 result = runner.invoke(test, input='n\n')106 assert not result.exception107 assert result.output == 'Foo [y/N]: n\nno :(\n'108109 @click.command()110 def test_no():111 if click.confirm('Foo', default=True):112 click.echo('yes!')113 else:114 click.echo('no :(')115116 result = runner.invoke(test_no, input='y\n')117 assert not result.exception118 assert result.output == 'Foo [Y/n]: y\nyes!\n'119120 result = runner.invoke(test_no, input='\n')121 assert not result.exception122 assert result.output == 'Foo [Y/n]: \nyes!\n'123124 result = runner.invoke(test_no, input='n\n')125 assert not result.exception126 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 127–128why, one step back127128Password prompt now prints a newline on `^C`.Markus Unterwaditzerbefore adoption36f83d9
lines 129–143why, one step back129def test_prompts_abort(monkeypatch, capsys):130 def f(_):131 raise KeyboardInterrupt()132133 monkeypatch.setattr('click.termui.hidden_prompt_func', f)134135 try:136 click.prompt('Password', hide_input=True)137 except click.Abort:138 click.echo('Screw you.')139140 out, err = capsys.readouterr()141 assert out == 'Password: \nScrew you.\n'142143Ignore broken PAGER valuesMarkus Unterwaditzerbefore adoption349875d
lines 144–146why, one step back144@pytest.mark.parametrize('cat', ['cat', 'cat ', 'cat '])145def test_echo_via_pager(monkeypatch, capfd, cat):146 monkeypatch.setitem(os.environ, 'PAGER', cat)Fixed a regression for echo_via_pager on 3.x. This fixes #185Armin Ronacherbefore adoptionb625654
lines 147–150why, one step back147 monkeypatch.setattr(click._termui_impl, 'isatty', lambda x: True)148 click.echo_via_pager('haha')149 out, err = capfd.readouterr()150 assert out == 'haha\n'Added test for open_fileArmin Ronacherbefore adoption1318a20
lines 151–152why, one step back151152Added support for explicit color control.Armin Ronacherbefore adoption5cf7f2d
lines 153–179why, one step back153def test_echo_color_flag(monkeypatch, capfd):154 isatty = True155 monkeypatch.setattr(click._compat, 'isatty', lambda x: isatty)156157 text = 'foo'158 styled_text = click.style(text, fg='red')159 assert styled_text == '\x1b[31mfoo\x1b[0m'160161 click.echo(styled_text, color=False)162 out, err = capfd.readouterr()163 assert out == text + '\n'164165 click.echo(styled_text, color=True)166 out, err = capfd.readouterr()167 assert out == styled_text + '\n'168169 isatty = True170 click.echo(styled_text)171 out, err = capfd.readouterr()172 assert out == styled_text + '\n'173174 isatty = False175 click.echo(styled_text)176 out, err = capfd.readouterr()177 assert out == text + '\n'178179Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab
lines 180–222why, one step back180def test_echo_writing_to_standard_error(capfd, monkeypatch):181 def emulate_input(text):182 """Emulate keyboard input."""183 if sys.version_info[0] == 2:184 from StringIO import StringIO185 else:186 from io import StringIO187 monkeypatch.setattr(sys, 'stdin', StringIO(text))188189 click.echo('Echo to standard output')190 out, err = capfd.readouterr()191 assert out == 'Echo to standard output\n'192 assert err == ''193194 click.echo('Echo to standard error', err=True)195 out, err = capfd.readouterr()196 assert out == ''197 assert err == 'Echo to standard error\n'198199 emulate_input('asdlkj\n')200 click.prompt('Prompt to stdin')201 out, err = capfd.readouterr()202 assert out == 'Prompt to stdin: '203 assert err == ''204205 emulate_input('asdlkj\n')206 click.prompt('Prompt to stderr', err=True)207 out, err = capfd.readouterr()208 assert out == ''209 assert err == 'Prompt to stderr: '210211 emulate_input('y\n')212 click.confirm('Prompt to stdin')213 out, err = capfd.readouterr()214 assert out == 'Prompt to stdin [y/N]: '215 assert err == ''216217 emulate_input('y\n')218 click.confirm('Prompt to stderr', err=True)219 out, err = capfd.readouterr()220 assert out == ''221 assert err == 'Prompt to stderr [y/N]: '222Fixed up pausing tests and added doc information.Armin Ronacherbefore adoption65ca572
lines 223–224why, one step back223 monkeypatch.setattr(click.termui, 'isatty', lambda x: True)224 monkeypatch.setattr(click.termui, 'getchar', lambda: ' ')Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab
line 225why, one step back225Fixed up pausing tests and added doc information.Armin Ronacherbefore adoption65ca572
line 226why, one step back226 click.pause('Pause to stdout')Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab
line 227why, one step back227 out, err = capfd.readouterr()Fixed up pausing tests and added doc information.Armin Ronacherbefore adoption65ca572
line 228why, one step back228 assert out == 'Pause to stdout\n'Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab
lines 229–233why, one step back229 assert err == ''230231 click.pause('Pause to stderr', err=True)232 out, err = capfd.readouterr()233 assert out == ''Fixed up pausing tests and added doc information.Armin Ronacherbefore adoption65ca572
line 234why, one step back234 assert err == 'Pause to stderr\n'Merge branch 'pr/212'Armin Ronacherbefore adoption04964ab
lines 235–236why, one step back235236Added test for open_fileArmin Ronacherbefore adoption1318a20
lines 237–255why, one step back237def test_open_file(runner):238 with runner.isolated_filesystem():239 with open('hello.txt', 'w') as f:240 f.write('Cool stuff')241242 @click.command()243 @click.argument('filename')244 def cli(filename):245 with click.open_file(filename) as f:246 click.echo(f.read())247 click.echo('meep')248249 result = runner.invoke(cli, ['hello.txt'])250 assert result.exception is None251 assert result.output == 'Cool stuff\nmeep\n'252253 result = runner.invoke(cli, ['-'], input='foobar')254 assert result.exception is None255 assert result.output == 'foobar\nmeep\n'Merge pull request #342 from geowurster/masterMarkus Unterwaditzerbefore adoption36b8002
lines 256–275why, one step back256257258def test_iter_keepopenfile(tmpdir):259260 expected = list(map(str, range(10)))261 p = tmpdir.mkdir('testdir').join('testfile')262 p.write(os.linesep.join(expected))263 f = p.open()264 for e_line, a_line in zip(expected, click.utils.KeepOpenFile(f)):265 assert e_line == a_line.strip()266267268def test_iter_lazyfile(tmpdir):269270 expected = list(map(str, range(10)))271 p = tmpdir.mkdir('testdir').join('testfile')272 p.write(os.linesep.join(expected))273 f = p.open()274 for e_line, a_line in zip(expected, click.utils.LazyFile(f.name)):275 assert e_line == a_line.strip()