def test_echo_writing_to_standard_error(capfd, monkeypatch): def emulate_input(text): """Emulate keyboard input.""" if sys.version_info[0] == 2: from StringIO import StringIO else: from io import StringIO monkeypatch.setattr(sys, 'stdin', StringIO(text)) click.echo('Echo to standard output') out, err = capfd.readouterr() assert out == 'Echo to standard output\n' assert err == '' click.echo('Echo to standard error', err=True) out, err = capfd.readouterr() assert out == '' assert err == 'Echo to standard error\n' emulate_input('asdlkj\n') click.prompt('Prompt to stdin') out, err = capfd.readouterr() assert out == 'Prompt to stdin: ' assert err == '' emulate_input('asdlkj\n') click.prompt('Prompt to stderr', err=True) out, err = capfd.readouterr() assert out == '' assert err == 'Prompt to stderr: ' emulate_input('y\n') click.confirm('Prompt to stdin') out, err = capfd.readouterr() assert out == 'Prompt to stdin [y/N]: ' assert err == '' emulate_input('y\n') click.confirm('Prompt to stderr', err=True) out, err = capfd.readouterr() assert out == '' assert err == 'Prompt to stderr [y/N]: ' monkeypatch.setattr(click.termui, 'isatty', lambda x: True) monkeypatch.setattr(click.termui, 'getchar', lambda: ' ') click.pause('Pause to stdout') out, err = capfd.readouterr() assert out == 'Pause to stdout\n' assert err == '' click.pause('Pause to stderr', err=True) out, err = capfd.readouterr() assert out == '' assert err == 'Pause to stderr\n'
def test_echo_writing_to_standard_error(capfd, monkeypatch): def emulate_input(text): """Emulate keyboard input.""" if sys.version_info[0] == 2: from StringIO import StringIO else: from io import StringIO monkeypatch.setattr(sys, "stdin", StringIO(text)) click.echo("Echo to standard output") out, err = capfd.readouterr() assert out == "Echo to standard output\n" assert err == "" click.echo("Echo to standard error", err=True) out, err = capfd.readouterr() assert out == "" assert err == "Echo to standard error\n" emulate_input("asdlkj\n") click.prompt("Prompt to stdin") out, err = capfd.readouterr() assert out == "Prompt to stdin: " assert err == "" emulate_input("asdlkj\n") click.prompt("Prompt to stderr", err=True) out, err = capfd.readouterr() assert out == "" assert err == "Prompt to stderr: " emulate_input("y\n") click.confirm("Prompt to stdin") out, err = capfd.readouterr() assert out == "Prompt to stdin [y/N]: " assert err == "" emulate_input("y\n") click.confirm("Prompt to stderr", err=True) out, err = capfd.readouterr() assert out == "" assert err == "Prompt to stderr [y/N]: " monkeypatch.setattr(click.termui, "isatty", lambda x: True) monkeypatch.setattr(click.termui, "getchar", lambda: " ") click.pause("Pause to stdout") out, err = capfd.readouterr() assert out == "Pause to stdout\n" assert err == "" click.pause("Pause to stderr", err=True) out, err = capfd.readouterr() assert out == "" assert err == "Pause to stderr\n"
async def select_pool(ctx): click.clear() click.echo("==================") click.echo(" Download Pools ") click.echo("==================") click.echo("") click.echo("Please give me the URLs and/or pool ID.") click.echo("When you're done, you can give me an empty line.") pools = [] while True: # fmt: off response = click.prompt("", prompt_suffix="> ", default="", show_default=False) # fmt: on if not response or not response.strip(): if not pools: continue break pool_id = get_pool_id(response) if not pool_id: click.secho("Please send valid URL or pool ID!") continue obj = await get_pool(ctx, pool_id) if obj: print_pool(obj) pools.append(obj) # noqa else: click.secho(f'Pool "{pool_id}" was not found.') # noqa output = click.prompt("Where will the images be saved? ", type=click.Path(), default=".") jobs = click.prompt( "How many concurrent jobs will be done? " "(If you don't know what that means, just leave it as is.)", type=int, default=4, ) type_ = click.prompt( "Which quality do you want to download? ", type=click.Choice(["sample", "file", "preview"]), default="file", ) await ctx.invoke(pool, pool_id=-1, output=output, jobs=jobs, type=type_, pools=pools)
def test_prompts_abort(monkeypatch, capsys): def f(_): raise KeyboardInterrupt() monkeypatch.setattr('asyncclick.termui.hidden_prompt_func', f) try: click.prompt('Password', hide_input=True) except click.Abort: click.echo('Screw you.') out, err = capsys.readouterr() assert out == 'Password: \nScrew you.\n'
def test_prompts_abort(monkeypatch, capsys): def f(_): raise KeyboardInterrupt() monkeypatch.setattr("asyncclick.termui.hidden_prompt_func", f) try: click.prompt("Password", hide_input=True) except click.Abort: click.echo("interrupted") out, err = capsys.readouterr() assert out == "Password:\ninterrupted\n"
async def log_in_ids(ctx: Context) -> AsyncClient: username: str = set_value(key="username", text="账号") jar = cookies.load_cookies_from_file(COOKIES_PATH / username) client = create_client(cookies=jar) if jar: u = await ids.get_logged_in_user(client) if u == username: return client password: str = ctx.obj.get("password") or click.prompt("密码") await ids.log_in(client, username=username, password=password) return client
def test_prompt_cast_default(capfd, monkeypatch): monkeypatch.setattr(sys, "stdin", StringIO("\n")) value = click.prompt("value", default="100", type=int) capfd.readouterr() assert type(value) is int
def set_value(ctx: Context, *, key: Any, text=None, kw: dict = {}) -> Any: if (value := ctx.obj.get(key)) is None: value = click.prompt(text or key, **kw) ctx.obj[key] = value