File: test_basic.py

package info (click to toggle)
click-help-colors 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 156 kB
  • sloc: python: 716; makefile: 4
file content (98 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import click
import pytest

from click_help_colors import HelpColorsGroup, HelpColorsException


def test_basic_group(runner):
    @click.command(
        cls=HelpColorsGroup,
        help_headers_color='yellow',
        help_options_color='green'
    )
    @click.option('--name', help='The person to greet.')
    def cli(count):
        pass

    result = runner.invoke(cli, ['--help'], color=True)
    assert not result.exception
    assert result.output.splitlines() == [
        '\x1b[33mUsage\x1b[0m: cli [OPTIONS] COMMAND [ARGS]...',
        '',
        '\x1b[33mOptions\x1b[0m:',
        '  \x1b[32m--name TEXT\x1b[0m  The person to greet.',
        '  \x1b[32m--help\x1b[0m       Show this message and exit.'
    ]


def test_basic_command(runner):
    @click.group(
        cls=HelpColorsGroup,
        help_headers_color='yellow',
        help_options_color='green'
    )
    def cli():
        pass

    @cli.command()
    @click.option('--name', help='The person to greet.')
    def command(name):
        pass

    result = runner.invoke(cli, ['--help'], color=True)
    assert not result.exception
    assert result.output.splitlines() == [
        '\x1b[33mUsage\x1b[0m: cli [OPTIONS] COMMAND [ARGS]...',
        '',
        '\x1b[33mOptions\x1b[0m:',
        '  \x1b[32m--help\x1b[0m  Show this message and exit.',
        '',
        '\x1b[33mCommands\x1b[0m:',
        '  \x1b[32mcommand\x1b[0m'
    ]

    result = runner.invoke(cli, ['command', '--help'], color=True)
    assert not result.exception
    assert result.output.splitlines() == [
        '\x1b[33mUsage\x1b[0m: cli command [OPTIONS]',
        '',
        '\x1b[33mOptions\x1b[0m:',
        '  \x1b[32m--name TEXT\x1b[0m  The person to greet.',
        '  \x1b[32m--help\x1b[0m       Show this message and exit.'
    ]


def test_unknown_color(runner):
    @click.command(
        cls=HelpColorsGroup,
        help_headers_color='unknwnclr'
    )
    @click.option('--name', help='The person to greet.')
    def cli(count):
        pass

    result = runner.invoke(cli, ['--help'], color=True)
    assert result.exception
    assert isinstance(result.exception, HelpColorsException)
    assert str(result.exception) == "Unknown color 'unknwnclr'"


def test_env_no_color(runner):
    @click.command(
        cls=HelpColorsGroup,
        help_headers_color='yellow',
        help_options_color='green'
    )
    @click.option('--name', help='The person to greet.')
    def cli(count):
        pass

    result = runner.invoke(cli, ['--help'], color=True, env={'NO_COLOR': '1'})
    assert not result.exception
    assert result.output.splitlines() == [
        'Usage: cli [OPTIONS] COMMAND [ARGS]...',
        '',
        'Options:',
        '  --name TEXT  The person to greet.',
        '  --help       Show this message and exit.'
    ]