summaryrefslogtreecommitdiff
path: root/tests/test_compat.py
blob: c26834a9a3b9d7a1cfcc978bd24e921dc696a2f0 (plain)
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
import pytest

import click
from click._compat import should_strip_ansi
from click._compat import WIN


def test_legacy_callbacks(runner):
    def legacy_callback(ctx, value):
        return value.upper()

    @click.command()
    @click.option("--foo", callback=legacy_callback)
    def cli(foo):
        click.echo(foo)

    with pytest.warns(Warning, match="Invoked legacy parameter callback"):
        result = runner.invoke(cli, ["--foo", "wat"])
        assert result.exit_code == 0
        assert "WAT" in result.output


def test_bash_func_name():
    from click._bashcomplete import get_completion_script

    script = get_completion_script("foo-bar baz_blah", "_COMPLETE_VAR", "bash").strip()
    assert script.startswith("_foo_barbaz_blah_completion()")
    assert "_COMPLETE_VAR=complete $1" in script


def test_zsh_func_name():
    from click._bashcomplete import get_completion_script

    script = get_completion_script("foo-bar", "_COMPLETE_VAR", "zsh").strip()
    assert script.startswith("#compdef foo-bar")
    assert "compdef _foo_bar_completion foo-bar;" in script
    assert "(( ! $+commands[foo-bar] )) && return 1" in script


@pytest.mark.xfail(WIN, reason="Jupyter not tested/supported on Windows")
def test_is_jupyter_kernel_output():
    class JupyterKernelFakeStream(object):
        pass

    # implementation detail, aka cheapskate test
    JupyterKernelFakeStream.__module__ = "ipykernel.faked"
    assert not should_strip_ansi(stream=JupyterKernelFakeStream())