Пример #1
0
async def test_stderr():
    @click.command()
    def cli_stderr():
        click.echo("stdout")
        click.echo("stderr", err=True)

    runner = CliRunner(mix_stderr=False)

    result = await runner.invoke(cli_stderr)

    assert result.output == "stdout\n"
    assert result.stdout == "stdout\n"
    assert result.stderr == "stderr\n"

    runner_mix = CliRunner(mix_stderr=True)
    result_mix = await runner_mix.invoke(cli_stderr)

    assert result_mix.output == "stdout\nstderr\n"
    assert result_mix.stdout == "stdout\nstderr\n"

    with pytest.raises(ValueError):
        result_mix.stderr

    @click.command()
    def cli_empty_stderr():
        click.echo("stdout")

    runner = CliRunner(mix_stderr=False)

    result = await runner.invoke(cli_empty_stderr)

    assert result.output == "stdout\n"
    assert result.stdout == "stdout\n"
    assert result.stderr == ""
Пример #2
0
async def test_getchar():
    @click.command()
    def continue_it():
        click.echo(click.getchar())

    runner = CliRunner()
    result = await runner.invoke(continue_it, input="y")
    if result.exception:
        raise result.exception
    assert result.output == "y\n"

    runner = CliRunner(echo_stdin=True)
    result = await runner.invoke(continue_it, input="y")
    if result.exception:
        raise result.exception
    assert result.output == "y\n"

    @click.command()
    def getchar_echo():
        click.echo(click.getchar(echo=True))

    runner = CliRunner()
    result = await runner.invoke(getchar_echo, input="y")
    if result.exception:
        raise result.exception
    assert result.output == "yy\n"

    runner = CliRunner(echo_stdin=True)
    result = await runner.invoke(getchar_echo, input="y")
    if result.exception:
        raise result.exception
    assert result.output == "yy\n"
Пример #3
0
def test_isolation_stderr_errors():
    """Writing to stderr should escape invalid characters instead of
    raising a UnicodeEncodeError.
    """
    runner = CliRunner(mix_stderr=False)

    with runner.isolation() as (_, err):
        click.echo("\udce2", err=True, nl=False)

    assert err.getvalue() == b"\\udce2"
Пример #4
0
async def test_deprecated_type(dev, type_class):
    """Make sure that using deprecated types yields a warning."""
    type, cls = type_class
    if type == "dimmer":
        return
    runner = CliRunner()
    res = await runner.invoke(cli, ["--host", "127.0.0.2", f"--{type}"])
    assert "Using --bulb, --plug, --strip, and --lightstrip is deprecated" in res.output
Пример #5
0
async def test_getchar():
    @click.command()
    def continue_it():
        click.echo(click.getchar())

    runner = CliRunner()
    result = await runner.invoke(continue_it, input='y')
    assert not result.exception
    assert result.output == 'y\n'
Пример #6
0
async def test_setting_prog_name_in_extra():
    @click.command()
    def cli():
        click.echo("ok")

    runner = CliRunner()
    result = await runner.invoke(cli, prog_name="foobar")
    assert not result.exception
    assert result.output == 'ok\n'
Пример #7
0
async def test_with_color_but_pause_not_blocking():
    @click.command()
    def cli():
        click.pause()

    runner = CliRunner()
    result = await runner.invoke(cli, color=True)
    assert not result.exception
    assert result.output == ''
Пример #8
0
async def test_state(dev, turn_on):
    await handle_turn_on(dev, turn_on)
    runner = CliRunner()
    res = await runner.invoke(state, obj=dev)
    await dev.update()

    if dev.is_on:
        assert "Device state: ON" in res.output
    else:
        assert "Device state: OFF" in res.output
Пример #9
0
async def test_args(args, expected_output):
    @click.command()
    @click.option("--foo", default="bar")
    def cli_args(foo):
        click.echo(foo)

    runner = CliRunner()
    result = await runner.invoke(cli_args, args=args)
    assert result.exit_code == 0
    assert result.output == expected_output
Пример #10
0
async def test_raw_command(dev):
    runner = CliRunner()
    res = await runner.invoke(raw_command, ["system", "get_sysinfo"], obj=dev)

    assert res.exit_code == 0
    assert dev.alias in res.output

    res = await runner.invoke(raw_command, obj=dev)
    assert res.exit_code != 0
    assert "Usage" in res.output
Пример #11
0
async def test_command_standalone_mode_returns_value():
    @click.command()
    def cli():
        click.echo("ok")
        return "Hello, World!"

    runner = CliRunner()
    result = await runner.invoke(cli, standalone_mode=False)
    assert result.output == "ok\n"
    assert result.return_value == "Hello, World!"
    assert result.exit_code == 0
Пример #12
0
async def test_prompts():
    @click.command()
    @click.option('--foo', prompt=True)
    def test(foo):
        click.echo('foo=%s' % foo)

    runner = CliRunner()
    result = await runner.invoke(test, input='wau wau\n')
    assert not result.exception
    assert result.output == 'Foo: wau wau\nfoo=wau wau\n'

    @click.command()
    @click.option('--foo', prompt=True, hide_input=True)
    def test(foo):
        click.echo('foo=%s' % foo)

    runner = CliRunner()
    result = await runner.invoke(test, input='wau wau\n')
    assert not result.exception
    assert result.output == 'Foo: \nfoo=wau wau\n'
Пример #13
0
async def test_runner():
    @click.command()
    def test():
        i = click.get_binary_stream('stdin')
        o = click.get_binary_stream('stdout')
        while 1:
            chunk = i.read(4096)
            if not chunk:
                break
            o.write(chunk)
            o.flush()

    runner = CliRunner()
    result = await runner.invoke(test, input='Hello World!\n')
    assert not result.exception
    assert result.output == 'Hello World!\n'

    runner = CliRunner(echo_stdin=True)
    result = await runner.invoke(test, input='Hello World!\n')
    assert not result.exception
    assert result.output == 'Hello World!\nHello World!\n'
Пример #14
0
async def test_alias(dev):
    runner = CliRunner()

    res = await runner.invoke(alias, obj=dev)
    assert f"Alias: {dev.alias}" in res.output

    new_alias = "new alias"
    res = await runner.invoke(alias, [new_alias], obj=dev)
    assert f"Setting alias to {new_alias}" in res.output

    res = await runner.invoke(alias, obj=dev)
    assert f"Alias: {new_alias}" in res.output
Пример #15
0
async def test_prompts():
    @click.command()
    @click.option("--foo", prompt=True)
    def test(foo):
        click.echo(f"foo={foo}")

    runner = CliRunner()
    result = await runner.invoke(test, input="wau wau\n")
    if result.exception:
        raise result.exception
    assert result.output == "Foo: wau wau\nfoo=wau wau\n"

    @click.command()
    @click.option("--foo", prompt=True, hide_input=True)
    def test(foo):
        click.echo(f"foo={foo}")

    runner = CliRunner()
    result = await runner.invoke(test, input="wau wau\n")
    if result.exception:
        raise result.exception
    assert result.output == "Foo: \nfoo=wau wau\n"
Пример #16
0
async def test_state(dev, turn_on):
    await handle_turn_on(dev, turn_on)
    runner = CliRunner()
    res = await runner.invoke(state, obj=dev)
    print(res.output)

    if dev.is_on:
        assert "Device state: ON" in res.output
    else:
        assert "Device state: OFF" in res.output

    if not dev.has_emeter:
        assert "Device has no emeter" in res.output
Пример #17
0
async def test_with_color():
    @click.command()
    def cli():
        click.secho('hello world', fg='blue')

    runner = CliRunner()

    result = await runner.invoke(cli)
    assert result.output == 'hello world\n'
    assert not result.exception

    result = await runner.invoke(cli, color=True)
    assert result.output == click.style('hello world', fg='blue') + '\n'
    assert not result.exception
Пример #18
0
async def test_runner_with_stream():
    @click.command()
    def test():
        i = click.get_binary_stream("stdin")
        o = click.get_binary_stream("stdout")
        while True:
            chunk = i.read(4096)
            if not chunk:
                break
            o.write(chunk)
            o.flush()

    runner = CliRunner()
    result = await runner.invoke(test, input=BytesIO(b"Hello World!\n"))
    if result.exception:
        raise result.exception
    assert result.output == "Hello World!\n"

    runner = CliRunner(echo_stdin=True)
    result = await runner.invoke(test, input=BytesIO(b"Hello World!\n"))
    if result.exception:
        raise result.exception
    assert result.output == "Hello World!\nHello World!\n"
Пример #19
0
async def test_brightness(dev):
    runner = CliRunner()
    res = await runner.invoke(brightness, obj=dev)
    if not dev.is_dimmable:
        assert "This device does not support brightness." in res.output
        return

    res = await runner.invoke(brightness, obj=dev)
    assert f"Brightness: {dev.brightness}" in res.output

    res = await runner.invoke(brightness, ["12"], obj=dev)
    assert "Setting brightness" in res.output

    res = await runner.invoke(brightness, obj=dev)
    assert "Brightness: 12" in res.output
Пример #20
0
async def test_env():
    @click.command()
    def cli_env():
        click.echo('ENV=%s' % os.environ['TEST_CLICK_ENV'])

    runner = CliRunner()

    env_orig = dict(os.environ)
    env = dict(env_orig)
    assert 'TEST_CLICK_ENV' not in env
    env['TEST_CLICK_ENV'] = 'some_value'
    result = await runner.invoke(cli_env, env=env)
    assert result.exit_code == 0
    assert result.output == 'ENV=some_value\n'

    assert os.environ == env_orig
Пример #21
0
async def test_with_color():
    @click.command()
    def cli():
        click.secho("hello world", fg="blue")

    runner = CliRunner()

    result = await runner.invoke(cli)
    assert result.output == "hello world\n"
    if result.exception:
        raise result.exception

    result = await runner.invoke(cli, color=True)
    assert result.output == f"{click.style('hello world', fg='blue')}\n"
    if result.exception:
        raise result.exception
Пример #22
0
async def test_env():
    @click.command()
    def cli_env():
        click.echo(f"ENV={os.environ['TEST_CLICK_ENV']}")

    runner = CliRunner()

    env_orig = dict(os.environ)
    env = dict(env_orig)
    assert "TEST_CLICK_ENV" not in env
    env["TEST_CLICK_ENV"] = "some_value"
    result = await runner.invoke(cli_env, env=env)
    assert result.exit_code == 0
    assert result.output == "ENV=some_value\n"

    assert os.environ == env_orig
Пример #23
0
async def test_emeter(dev: SmartDevice, mocker):
    runner = CliRunner()

    res = await runner.invoke(emeter, obj=dev)
    if not dev.has_emeter:
        assert "Device has no emeter" in res.output
        return

    assert "== Emeter ==" in res.output

    monthly = mocker.patch.object(dev, "get_emeter_monthly")
    res = await runner.invoke(emeter, ["--year", "1900"], obj=dev)
    assert "For year" in res.output
    monthly.assert_called()

    daily = mocker.patch.object(dev, "get_emeter_daily")
    res = await runner.invoke(emeter, ["--month", "1900-12"], obj=dev)
    assert "For month" in res.output
    daily.assert_called()
Пример #24
0
async def test_echo_stdin_prompts():
    @click.command()
    def test_python_input():
        foo = input("Foo: ")
        click.echo(f"foo={foo}")

    runner = CliRunner(echo_stdin=True)
    result = await runner.invoke(test_python_input, input="bar bar\n")
    if result.exception:
        raise result.exception
    assert result.output == "Foo: bar bar\nfoo=bar bar\n"

    @click.command()
    @click.option("--foo", prompt=True)
    def test_prompt(foo):
        click.echo(f"foo={foo}")

    result = await runner.invoke(test_prompt, input="bar bar\n")
    if result.exception:
        raise result.exception
    assert result.output == "Foo: bar bar\nfoo=bar bar\n"

    @click.command()
    @click.option("--foo", prompt=True, hide_input=True)
    def test_hidden_prompt(foo):
        click.echo(f"foo={foo}")

    result = await runner.invoke(test_hidden_prompt, input="bar bar\n")
    if result.exception:
        raise result.exception
    assert result.output == "Foo: \nfoo=bar bar\n"

    @click.command()
    @click.option("--foo", prompt=True)
    @click.option("--bar", prompt=True)
    def test_multiple_prompts(foo, bar):
        click.echo(f"foo={foo}, bar={bar}")

    result = await runner.invoke(test_multiple_prompts, input="one\ntwo\n")
    if result.exception:
        raise result.exception
    assert result.output == "Foo: one\nBar: two\nfoo=one, bar=two\n"
Пример #25
0
async def test_catch_exceptions():
    class CustomError(Exception):
        pass

    @click.command()
    def cli():
        raise CustomError(1)

    runner = CliRunner()

    result = await runner.invoke(cli)
    assert isinstance(result.exception, CustomError)
    assert type(result.exc_info) is tuple
    assert len(result.exc_info) == 3

    with pytest.raises(CustomError):
        await runner.invoke(cli, catch_exceptions=False)

    CustomError = SystemExit

    result = await runner.invoke(cli)
    assert result.exit_code == 1
Пример #26
0
async def test_sysinfo(dev):
    runner = CliRunner()
    res = await runner.invoke(sysinfo, obj=dev)
    assert "System info" in res.output
    assert dev.alias in res.output
Пример #27
0
async def test_exit_code_and_output_from_sys_exit():
    # See issue #362
    @click.command()
    def cli_string():
        click.echo("hello world")
        sys.exit("error")

    @click.command()
    @click.pass_context
    def cli_string_ctx_exit(ctx):
        click.echo("hello world")
        ctx.exit("error")

    @click.command()
    def cli_int():
        click.echo("hello world")
        sys.exit(1)

    @click.command()
    @click.pass_context
    def cli_int_ctx_exit(ctx):
        click.echo("hello world")
        ctx.exit(1)

    @click.command()
    def cli_float():
        click.echo("hello world")
        sys.exit(1.0)

    @click.command()
    @click.pass_context
    def cli_float_ctx_exit(ctx):
        click.echo("hello world")
        ctx.exit(1.0)

    @click.command()
    def cli_no_error():
        click.echo("hello world")

    runner = CliRunner()

    result = await runner.invoke(cli_string)
    assert result.exit_code == 1
    assert result.output == "hello world\nerror\n"

    result = await runner.invoke(cli_string_ctx_exit)
    assert result.exit_code == 1
    assert result.output == "hello world\nerror\n"

    result = await runner.invoke(cli_int)
    assert result.exit_code == 1
    assert result.output == "hello world\n"

    result = await runner.invoke(cli_int_ctx_exit)
    assert result.exit_code == 1
    assert result.output == "hello world\n"

    result = await runner.invoke(cli_float)
    assert result.exit_code == 1
    assert result.output == "hello world\n1.0\n"

    result = await runner.invoke(cli_float_ctx_exit)
    assert result.exit_code == 1
    assert result.output == "hello world\n1.0\n"

    result = await runner.invoke(cli_no_error)
    assert result.exit_code == 0
    assert result.output == "hello world\n"