Skip to content
Myr

test_formatting.py

This file

Path

tests/test_formatting.py

214 lines · 5.1 KiB · Python

Last changed by

Merge pull request #557 from Nudies/masterMarkus Unterwaditzerbefore adoption42e2821

History of this file

Take it

1# -*- coding: utf-8 -*-2import click345def test_basic_functionality(runner):6    @click.command()7    def cli():8        """First paragraph.910        This is a very long second11        paragraph and not correctly12        wrapped but it will be rewrapped.1314        \b15        This is16        a paragraph17        without rewrapping.1819        \b20        121         222          32324        And this is a paragraph25        that will be rewrapped again.26        """2728    result = runner.invoke(cli, ['--help'], terminal_width=60)29    assert not result.exception30    assert result.output.splitlines() == [31        'Usage: cli [OPTIONS]',32        '',33        '  First paragraph.',34        '',35        '  This is a very long second paragraph and not correctly',36        '  wrapped but it will be rewrapped.',37        '',38        '  This is',39        '  a paragraph',40        '  without rewrapping.',41        '',42        '  1',43        '   2',44        '    3',45        '',46        '  And this is a paragraph that will be rewrapped again.',47        '',48        'Options:',49        '  --help  Show this message and exit.',50    ]515253def test_wrapping_long_options_strings(runner):54    @click.group()55    def cli():56        """Top level command57        """5859    @cli.group()60    def a_very_long():61        """Second level62        """6364    @a_very_long.command()65    @click.argument('first')66    @click.argument('second')67    @click.argument('third')68    @click.argument('fourth')69    @click.argument('fifth')70    @click.argument('sixth')71    def command():72        """A command.73        """7475    # 54 is chosen as a length where the second line is one character76    # longer than the maximum length.77    result = runner.invoke(cli, ['a_very_long', 'command', '--help'],78                           terminal_width=54)79    assert not result.exception80    assert result.output.splitlines() == [81        'Usage: cli a_very_long command [OPTIONS] FIRST SECOND',82        '                               THIRD FOURTH FIFTH',83        '                               SIXTH',84        '',85        '  A command.',86        '',87        'Options:',88        '  --help  Show this message and exit.',89    ]909192def test_wrapping_long_command_name(runner):93    @click.group()94    def cli():95        """Top level command96        """9798    @cli.group()99    def a_very_very_very_long():100        """Second level101        """102103    @a_very_very_very_long.command()104    @click.argument('first')105    @click.argument('second')106    @click.argument('third')107    @click.argument('fourth')108    @click.argument('fifth')109    @click.argument('sixth')110    def command():111        """A command.112        """113114    result = runner.invoke(cli, ['a_very_very_very_long', 'command', '--help'],115                           terminal_width=54)116    assert not result.exception117    assert result.output.splitlines() == [118        'Usage: cli a_very_very_very_long command ',119        '           [OPTIONS] FIRST SECOND THIRD FOURTH FIFTH',120        '           SIXTH',121        '',122        '  A command.',123        '',124        'Options:',125        '  --help  Show this message and exit.',126    ]127128129def test_formatting_empty_help_lines(runner):130    @click.command()131    def cli():132        """Top level command133134        """135136    result = runner.invoke(cli, ['--help'])137    assert not result.exception138    assert result.output.splitlines() == [139        'Usage: cli [OPTIONS]',140        '',141        '  Top level command',142        '',143        '',144        '',145        'Options:',146        '  --help  Show this message and exit.',147    ]148149150def test_formatting_usage_error(runner):151    @click.command()152    @click.argument('arg')153    def cmd(arg):154        click.echo('arg:' + arg)155156    result = runner.invoke(cmd, [])157    assert result.exit_code == 2158    assert result.output.splitlines() == [159        'Usage: cmd [OPTIONS] ARG',160        'Try "cmd --help" for help.',161        '',162        'Error: Missing argument "arg".'163    ]164165166def test_formatting_usage_error_nested(runner):167    @click.group()168    def cmd():169        pass170171    @cmd.command()172    @click.argument('bar')173    def foo(bar):174        click.echo('foo:' + bar)175176    result = runner.invoke(cmd, ['foo'])177    assert result.exit_code == 2178    assert result.output.splitlines() == [179        'Usage: cmd foo [OPTIONS] BAR',180        'Try "cmd foo --help" for help.',181        '',182        'Error: Missing argument "bar".'183    ]184185186def test_formatting_usage_error_no_help(runner):187    @click.command(add_help_option=False)188    @click.argument('arg')189    def cmd(arg):190        click.echo('arg:' + arg)191192    result = runner.invoke(cmd, [])193    assert result.exit_code == 2194    assert result.output.splitlines() == [195        'Usage: cmd [OPTIONS] ARG',196        '',197        'Error: Missing argument "arg".'198    ]199200201def test_formatting_usage_custom_help(runner):202    @click.command(context_settings=dict(help_option_names=['--man']))203    @click.argument('arg')204    def cmd(arg):205        click.echo('arg:' + arg)206207    result = runner.invoke(cmd, [])208    assert result.exit_code == 2209    assert result.output.splitlines() == [210        'Usage: cmd [OPTIONS] ARG',211        'Try "cmd --man" for help.',212        '',213        'Error: Missing argument "arg".'214    ]