summaryrefslogtreecommitdiff
path: root/tests/test_arguments.py
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2020-03-06 13:50:04 -0800
committerDavid Lord <davidism@gmail.com>2020-03-06 13:50:04 -0800
commit93ba3ba112d2f8ba7bdd8b231e510f74dd0b037e (patch)
tree409b93ee7ec2209b9e52256ed77b0292f4e49720 /tests/test_arguments.py
parent488739dfe0d51f415c7b20466648cc519962ecbb (diff)
downloadclick-93ba3ba112d2f8ba7bdd8b231e510f74dd0b037e.tar.gz
apply black
Diffstat (limited to 'tests/test_arguments.py')
-rw-r--r--tests/test_arguments.py221
1 files changed, 115 insertions, 106 deletions
diff --git a/tests/test_arguments.py b/tests/test_arguments.py
index f92a560..7b214ad 100644
--- a/tests/test_arguments.py
+++ b/tests/test_arguments.py
@@ -10,45 +10,47 @@ from click._compat import text_type
def test_nargs_star(runner):
@click.command()
- @click.argument('src', nargs=-1)
- @click.argument('dst')
+ @click.argument("src", nargs=-1)
+ @click.argument("dst")
def copy(src, dst):
- click.echo('src=%s' % '|'.join(src))
- click.echo('dst=%s' % dst)
+ click.echo("src=%s" % "|".join(src))
+ click.echo("dst=%s" % dst)
- result = runner.invoke(copy, ['foo.txt', 'bar.txt', 'dir'])
+ result = runner.invoke(copy, ["foo.txt", "bar.txt", "dir"])
assert not result.exception
assert result.output.splitlines() == [
- 'src=foo.txt|bar.txt',
- 'dst=dir',
+ "src=foo.txt|bar.txt",
+ "dst=dir",
]
def test_nargs_default(runner):
try:
+
@click.command()
- @click.argument('src', nargs=-1, default=42)
+ @click.argument("src", nargs=-1, default=42)
def copy(src):
pass
+
except TypeError as e:
- assert 'nargs=-1' in str(e)
+ assert "nargs=-1" in str(e)
else:
assert False
def test_nargs_tup(runner):
@click.command()
- @click.argument('name', nargs=1)
- @click.argument('point', nargs=2, type=click.INT)
+ @click.argument("name", nargs=1)
+ @click.argument("point", nargs=2, type=click.INT)
def copy(name, point):
- click.echo('name=%s' % name)
- click.echo('point=%d/%d' % point)
+ click.echo("name=%s" % name)
+ click.echo("point=%d/%d" % point)
- result = runner.invoke(copy, ['peter', '1', '2'])
+ result = runner.invoke(copy, ["peter", "1", "2"])
assert not result.exception
assert result.output.splitlines() == [
- 'name=peter',
- 'point=1/2',
+ "name=peter",
+ "point=1/2",
]
@@ -61,57 +63,63 @@ def test_nargs_tup_composite(runner):
]
for opts in variations:
+
@click.command()
- @click.argument('item', **opts)
+ @click.argument("item", **opts)
def copy(item):
- click.echo('name=%s id=%d' % item)
+ click.echo("name=%s id=%d" % item)
- result = runner.invoke(copy, ['peter', '1'])
+ result = runner.invoke(copy, ["peter", "1"])
assert not result.exception
assert result.output.splitlines() == [
- 'name=peter id=1',
+ "name=peter id=1",
]
def test_nargs_err(runner):
@click.command()
- @click.argument('x')
+ @click.argument("x")
def copy(x):
click.echo(x)
- result = runner.invoke(copy, ['foo'])
+ result = runner.invoke(copy, ["foo"])
assert not result.exception
- assert result.output == 'foo\n'
+ assert result.output == "foo\n"
- result = runner.invoke(copy, ['foo', 'bar'])
+ result = runner.invoke(copy, ["foo", "bar"])
assert result.exit_code == 2
- assert 'Got unexpected extra argument (bar)' in result.output
+ assert "Got unexpected extra argument (bar)" in result.output
def test_bytes_args(runner, monkeypatch):
@click.command()
- @click.argument('arg')
+ @click.argument("arg")
def from_bytes(arg):
- assert isinstance(arg, text_type), "UTF-8 encoded argument should be implicitly converted to Unicode"
+ assert isinstance(
+ arg, text_type
+ ), "UTF-8 encoded argument should be implicitly converted to Unicode"
# Simulate empty locale environment variables
if PY2:
- monkeypatch.setattr(sys.stdin, 'encoding', 'ANSI_X3.4-1968')
- monkeypatch.setattr(sys, 'getfilesystemencoding', lambda: 'ANSI_X3.4-1968')
- monkeypatch.setattr(sys, 'getdefaultencoding', lambda: 'ascii')
+ monkeypatch.setattr(sys.stdin, "encoding", "ANSI_X3.4-1968")
+ monkeypatch.setattr(sys, "getfilesystemencoding", lambda: "ANSI_X3.4-1968")
+ monkeypatch.setattr(sys, "getdefaultencoding", lambda: "ascii")
else:
- monkeypatch.setattr(sys.stdin, 'encoding', 'utf-8')
- monkeypatch.setattr(sys, 'getfilesystemencoding', lambda: 'utf-8')
- monkeypatch.setattr(sys, 'getdefaultencoding', lambda: 'utf-8')
+ monkeypatch.setattr(sys.stdin, "encoding", "utf-8")
+ monkeypatch.setattr(sys, "getfilesystemencoding", lambda: "utf-8")
+ monkeypatch.setattr(sys, "getdefaultencoding", lambda: "utf-8")
- runner.invoke(from_bytes, [u'Something outside of ASCII range: 林'.encode('UTF-8')],
- catch_exceptions=False)
+ runner.invoke(
+ from_bytes,
+ [u"Something outside of ASCII range: 林".encode("UTF-8")],
+ catch_exceptions=False,
+ )
def test_file_args(runner):
@click.command()
- @click.argument('input', type=click.File('rb'))
- @click.argument('output', type=click.File('wb'))
+ @click.argument("input", type=click.File("rb"))
+ @click.argument("output", type=click.File("wb"))
def inout(input, output):
while True:
chunk = input.read(1024)
@@ -120,96 +128,96 @@ def test_file_args(runner):
output.write(chunk)
with runner.isolated_filesystem():
- result = runner.invoke(inout, ['-', 'hello.txt'], input='Hey!')
- assert result.output == ''
+ result = runner.invoke(inout, ["-", "hello.txt"], input="Hey!")
+ assert result.output == ""
assert result.exit_code == 0
- with open('hello.txt', 'rb') as f:
- assert f.read() == b'Hey!'
+ with open("hello.txt", "rb") as f:
+ assert f.read() == b"Hey!"
- result = runner.invoke(inout, ['hello.txt', '-'])
- assert result.output == 'Hey!'
+ result = runner.invoke(inout, ["hello.txt", "-"])
+ assert result.output == "Hey!"
assert result.exit_code == 0
def test_path_args(runner):
@click.command()
- @click.argument('input', type=click.Path(dir_okay=False, allow_dash=True))
+ @click.argument("input", type=click.Path(dir_okay=False, allow_dash=True))
def foo(input):
click.echo(input)
- result = runner.invoke(foo, ['-'])
- assert result.output == '-\n'
+ result = runner.invoke(foo, ["-"])
+ assert result.output == "-\n"
assert result.exit_code == 0
def test_file_atomics(runner):
@click.command()
- @click.argument('output', type=click.File('wb', atomic=True))
+ @click.argument("output", type=click.File("wb", atomic=True))
def inout(output):
- output.write(b'Foo bar baz\n')
+ output.write(b"Foo bar baz\n")
output.flush()
- with open(output.name, 'rb') as f:
+ with open(output.name, "rb") as f:
old_content = f.read()
- assert old_content == b'OLD\n'
+ assert old_content == b"OLD\n"
with runner.isolated_filesystem():
- with open('foo.txt', 'wb') as f:
- f.write(b'OLD\n')
- result = runner.invoke(inout, ['foo.txt'], input='Hey!',
- catch_exceptions=False)
- assert result.output == ''
+ with open("foo.txt", "wb") as f:
+ f.write(b"OLD\n")
+ result = runner.invoke(inout, ["foo.txt"], input="Hey!", catch_exceptions=False)
+ assert result.output == ""
assert result.exit_code == 0
- with open('foo.txt', 'rb') as f:
- assert f.read() == b'Foo bar baz\n'
+ with open("foo.txt", "rb") as f:
+ assert f.read() == b"Foo bar baz\n"
def test_stdout_default(runner):
@click.command()
- @click.argument('output', type=click.File('w'), default='-')
+ @click.argument("output", type=click.File("w"), default="-")
def inout(output):
- output.write('Foo bar baz\n')
+ output.write("Foo bar baz\n")
output.flush()
result = runner.invoke(inout, [])
assert not result.exception
- assert result.output == 'Foo bar baz\n'
+ assert result.output == "Foo bar baz\n"
def test_nargs_envvar(runner):
@click.command()
- @click.option('--arg', nargs=2)
+ @click.option("--arg", nargs=2)
def cmd(arg):
- click.echo('|'.join(arg))
+ click.echo("|".join(arg))
- result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',
- env={'TEST_ARG': 'foo bar'})
+ result = runner.invoke(
+ cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}
+ )
assert not result.exception
- assert result.output == 'foo|bar\n'
+ assert result.output == "foo|bar\n"
@click.command()
- @click.option('--arg', envvar='X', nargs=2)
+ @click.option("--arg", envvar="X", nargs=2)
def cmd(arg):
- click.echo('|'.join(arg))
+ click.echo("|".join(arg))
- result = runner.invoke(cmd, [], env={'X': 'foo bar'})
+ result = runner.invoke(cmd, [], env={"X": "foo bar"})
assert not result.exception
- assert result.output == 'foo|bar\n'
+ assert result.output == "foo|bar\n"
def test_empty_nargs(runner):
@click.command()
- @click.argument('arg', nargs=-1)
+ @click.argument("arg", nargs=-1)
def cmd(arg):
- click.echo('arg:' + '|'.join(arg))
+ click.echo("arg:" + "|".join(arg))
result = runner.invoke(cmd, [])
assert result.exit_code == 0
- assert result.output == 'arg:\n'
+ assert result.output == "arg:\n"
@click.command()
- @click.argument('arg', nargs=-1, required=True)
+ @click.argument("arg", nargs=-1, required=True)
def cmd2(arg):
- click.echo('arg:' + '|'.join(arg))
+ click.echo("arg:" + "|".join(arg))
result = runner.invoke(cmd2, [])
assert result.exit_code == 2
@@ -218,9 +226,9 @@ def test_empty_nargs(runner):
def test_missing_arg(runner):
@click.command()
- @click.argument('arg')
+ @click.argument("arg")
def cmd(arg):
- click.echo('arg:' + arg)
+ click.echo("arg:" + arg)
result = runner.invoke(cmd, [])
assert result.exit_code == 2
@@ -238,94 +246,95 @@ def test_missing_argument_string_cast():
def test_implicit_non_required(runner):
@click.command()
- @click.argument('f', default='test')
+ @click.argument("f", default="test")
def cli(f):
click.echo(f)
result = runner.invoke(cli, [])
assert result.exit_code == 0
- assert result.output == 'test\n'
+ assert result.output == "test\n"
def test_eat_options(runner):
@click.command()
- @click.option('-f')
- @click.argument('files', nargs=-1)
+ @click.option("-f")
+ @click.argument("files", nargs=-1)
def cmd(f, files):
for filename in files:
click.echo(filename)
click.echo(f)
- result = runner.invoke(cmd, ['--', '-foo', 'bar'])
+ result = runner.invoke(cmd, ["--", "-foo", "bar"])
assert result.output.splitlines() == [
- '-foo',
- 'bar',
- '',
+ "-foo",
+ "bar",
+ "",
]
- result = runner.invoke(cmd, ['-f', '-x', '--', '-foo', 'bar'])
+ result = runner.invoke(cmd, ["-f", "-x", "--", "-foo", "bar"])
assert result.output.splitlines() == [
- '-foo',
- 'bar',
- '-x',
+ "-foo",
+ "bar",
+ "-x",
]
def test_nargs_star_ordering(runner):
@click.command()
- @click.argument('a', nargs=-1)
- @click.argument('b')
- @click.argument('c')
+ @click.argument("a", nargs=-1)
+ @click.argument("b")
+ @click.argument("c")
def cmd(a, b, c):
for arg in (a, b, c):
click.echo(arg)
- result = runner.invoke(cmd, ['a', 'b', 'c'])
+ result = runner.invoke(cmd, ["a", "b", "c"])
assert result.output.splitlines() == [
PY2 and "(u'a',)" or "('a',)",
- 'b',
- 'c',
+ "b",
+ "c",
]
def test_nargs_specified_plus_star_ordering(runner):
@click.command()
- @click.argument('a', nargs=-1)
- @click.argument('b')
- @click.argument('c', nargs=2)
+ @click.argument("a", nargs=-1)
+ @click.argument("b")
+ @click.argument("c", nargs=2)
def cmd(a, b, c):
for arg in (a, b, c):
click.echo(arg)
- result = runner.invoke(cmd, ['a', 'b', 'c', 'd', 'e', 'f'])
+ result = runner.invoke(cmd, ["a", "b", "c", "d", "e", "f"])
assert result.output.splitlines() == [
PY2 and "(u'a', u'b', u'c')" or "('a', 'b', 'c')",
- 'd',
+ "d",
PY2 and "(u'e', u'f')" or "('e', 'f')",
]
def test_defaults_for_nargs(runner):
@click.command()
- @click.argument('a', nargs=2, type=int, default=(1, 2))
+ @click.argument("a", nargs=2, type=int, default=(1, 2))
def cmd(a):
x, y = a
click.echo(x + y)
result = runner.invoke(cmd, [])
- assert result.output.strip() == '3'
+ assert result.output.strip() == "3"
- result = runner.invoke(cmd, ['3', '4'])
- assert result.output.strip() == '7'
+ result = runner.invoke(cmd, ["3", "4"])
+ assert result.output.strip() == "7"
- result = runner.invoke(cmd, ['3'])
+ result = runner.invoke(cmd, ["3"])
assert result.exception is not None
- assert 'argument a takes 2 values' in result.output
+ assert "argument a takes 2 values" in result.output
def test_multiple_param_decls_not_allowed(runner):
with pytest.raises(TypeError):
+
@click.command()
- @click.argument('x', click.Choice(['a', 'b']))
+ @click.argument("x", click.Choice(["a", "b"]))
def copy(x):
click.echo(x)