Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

113 lines · 3.8 KiB · Python

Last changed by

yN->y/NArmin Ronacherbefore adoptionc7299c4

History of this file

Take it

Unbroke colorama integrated echo for unicode.Armin Ronacherbefore adoption537d6d5

line 1why, one step back
1import sys

Added echo testsArmin Ronacherbefore adoption159ffc5

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

Refactored pager support to not rely on pydocArmin Ronacherbefore adoptionaef1650

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

Added echo testsArmin Ronacherbefore adoption159ffc5

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

Refactored pager support to not rely on pydocArmin Ronacherbefore adoptionaef1650

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

Unbroke colorama integrated echo for unicode.Armin Ronacherbefore adoption537d6d5

lines 14–31why, one step back
14
15 # If we are on python 2 we expect that writing bytes into a string io16 # does not do anything crazy. On Python 317 if sys.version_info[0] == 2:18 import StringIO19 sys.stdout = x = StringIO.StringIO()20 try:21 click.echo('\xf6')22 finally:23 sys.stdout = sys.__stdout__24 assert x.getvalue() == '\xf6\n'2526 # And in any case, if wrapped, we expect bytes to survive.27 @click.command()28 def cli():29 click.echo(b'\xf6')30 result = runner.invoke(cli, [])31 assert result.output_bytes == b'\xf6\n'

Added filename format functionArmin Ronacherbefore adoption31043c3

line 32why, one step back
32

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

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

Fixed a test on python 3Armin Ronacherbefore adoptionb24e004

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

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

line 37why, one step back
37

Added filename format functionArmin Ronacherbefore adoption31043c3

line 38why, one step back
38

Added tests for stylingArmin Ronacherbefore adoption3eb2c48

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

Added filename format functionArmin Ronacherbefore adoption31043c3

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

Merge branch '1.0-maintenance'Armin Ronacherbefore adoptionf36e404

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

Added filename format functionArmin Ronacherbefore adoption31043c3

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

yN->y/NArmin Ronacherbefore adoptionc7299c4

lines 74–113why, one step back
74
7576def test_prompts(runner):77 @click.command()78 def test():79 if click.confirm('Foo'):80 click.echo('yes!')81 else:82 click.echo('no :(')8384 result = runner.invoke(test, input='y\n')85 assert not result.exception86 assert result.output == 'Foo [y/N]: y\nyes!\n'8788 result = runner.invoke(test, input='\n')89 assert not result.exception90 assert result.output == 'Foo [y/N]: \nno :(\n'9192 result = runner.invoke(test, input='n\n')93 assert not result.exception94 assert result.output == 'Foo [y/N]: n\nno :(\n'9596 @click.command()97 def test_no():98 if click.confirm('Foo', default=True):99 click.echo('yes!')100 else:101 click.echo('no :(')102103 result = runner.invoke(test_no, input='y\n')104 assert not result.exception105 assert result.output == 'Foo [Y/n]: y\nyes!\n'106107 result = runner.invoke(test_no, input='\n')108 assert not result.exception109 assert result.output == 'Foo [Y/n]: \nyes!\n'110111 result = runner.invoke(test_no, input='n\n')112 assert not result.exception113 assert result.output == 'Foo [Y/n]: n\nno :(\n'