Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
cav71 committed Nov 9, 2024
1 parent d720953 commit e93d0d3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 104 deletions.
4 changes: 3 additions & 1 deletion src/luxos/cli/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ def validate(self, txt) -> None | tuple[str, None | int]:
ip, _, port = txt.partition(":")
return ip, int(port) if port else self.port
except (RuntimeError, ValueError) as exc:
raise argparse.ArgumentTypeError(f"failed to parse {txt=}: {exc.args[0]}")
raise argparse.ArgumentTypeError(
f"failed to convert to a strict ip address: {exc.args[0]}"
)


def type_range(txt: str) -> Sequence[tuple[str, int | None]]:
Expand Down
133 changes: 30 additions & 103 deletions tests/test_cli_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,107 +4,6 @@
import pytest

from luxos.cli import flags
from luxos.cli.v1 import AbortWrongArgumentError


def test_type_ipaddress_type():
assert flags.type_ipaddress(strict=False)("hello").value == ("hello", None)
assert flags.type_ipaddress(strict=False)("hello:123").value == ("hello", 123)

pytest.raises(argparse.ArgumentTypeError, flags.type_ipaddress(), "12:dwedwe")


def test_type_ipaddress():
from luxos.cli import v1 as cli

# test with the class as type
parser = cli.LuxosParser([])
parser.add_argument("--address", type=flags.type_ipaddress)

assert parser.parse_args([]).address is None
assert parser.parse_args(["--address", "127.0.0.1"]).address == ("127.0.0.1", None)
assert parser.parse_args(["--address", "127.0.0.1:123"]).address == (
"127.0.0.1",
123,
)
pytest.raises(
AbortWrongArgumentError, parser.parse_args, ["--address", "hello:123"]
)
pytest.raises(AbortWrongArgumentError, parser.parse_args, ["--address", "hello"])

# test with the class as type and a default of None
parser = cli.LuxosParser([])
parser.add_argument("--address", type=flags.type_ipaddress, default=None)

assert parser.parse_args([]).address is None
assert parser.parse_args(["--address", "127.0.0.1"]).address == ("127.0.0.1", None)
assert parser.parse_args(["--address", "127.0.0.1:123"]).address == (
"127.0.0.1",
123,
)
pytest.raises(
AbortWrongArgumentError, parser.parse_args, ["--address", "hello:123"]
)
pytest.raises(AbortWrongArgumentError, parser.parse_args, ["--address", "hello"])

# test with the instance as type and a default of None
parser = cli.LuxosParser([])
parser.add_argument(
"--address", type=flags.type_ipaddress(strict=True), default=None
)

assert parser.parse_args([]).address is None
assert parser.parse_args(["--address", "127.0.0.1"]).address == ("127.0.0.1", None)
assert parser.parse_args(["--address", "127.0.0.1:123"]).address == (
"127.0.0.1",
123,
)
pytest.raises(
AbortWrongArgumentError, parser.parse_args, ["--address", "hello:123"]
)
pytest.raises(AbortWrongArgumentError, parser.parse_args, ["--address", "hello"])

# test with the instance as type with strict set to False and a default of None
parser = cli.LuxosParser([])
parser.add_argument(
"--address", type=flags.type_ipaddress(strict=False), default=None
)

assert parser.parse_args([]).address is None
assert parser.parse_args(["--address", "127.0.0.1"]).address == ("127.0.0.1", None)
assert parser.parse_args(["--address", "127.0.0.1:123"]).address == (
"127.0.0.1",
123,
)
assert parser.parse_args(["--address", "hello"]).address == ("hello", None)
assert parser.parse_args(["--address", "hello:123"]).address == ("hello", 123)

# tests with a invalid default
with pytest.raises(RuntimeError) as x:
cli.LuxosParser([]).add_argument(
"--address", type=flags.type_ipaddress, default="xwc"
)
assert x.value.args[0].startswith("cannot use value='xwc' as default")
with pytest.raises(RuntimeError) as x:
cli.LuxosParser([]).add_argument(
"--address", type=flags.type_ipaddress(strict=True), default="xwc"
)
assert x.value.args[0].startswith("cannot use value='xwc' as default")

cli.LuxosParser([]).add_argument(
"--address", type=flags.type_ipaddress(strict=False), default="xwc"
)
cli.LuxosParser([]).add_argument(
"--address", type=flags.type_ipaddress(strict=False), default="xwc:123"
)
with pytest.raises(RuntimeError) as x:
cli.LuxosParser([]).add_argument(
"--address", type=flags.type_ipaddress(strict=False), default="xwc:12:2"
)
with pytest.raises(RuntimeError) as x:
cli.LuxosParser([]).add_argument(
"--address", type=flags.type_ipaddress(strict=False), default="xwc:sqwq"
)


def test_type_range(resolver):
Expand Down Expand Up @@ -143,5 +42,33 @@ def test_type_range(resolver):


def test_type_hhmm():
assert flags.type_hhmm()("12:34").value == datetime.time(12, 34)
pytest.raises(argparse.ArgumentTypeError, flags.type_hhmm(), "12")
assert flags.type_hhmm().validate("12:34") == datetime.time(12, 34)

with pytest.raises(argparse.ArgumentTypeError) as e:
flags.type_hhmm().validate("12")
assert e.value.args[-1] == "failed conversion into HH:MM for '12'"

with pytest.raises(argparse.ArgumentTypeError) as e:
flags.type_hhmm().validate("hello")
assert e.value.args[-1] == "failed conversion into HH:MM for 'hello'"


def test_type_ipaddress():
assert flags.type_ipaddress(strict=False).validate("hello") == ("hello", None)
assert flags.type_ipaddress(strict=False).validate("hello:123") == ("hello", 123)

with pytest.raises(argparse.ArgumentTypeError) as e:
flags.type_ipaddress(strict=True).validate("hello")
assert e.value.args[-1] == (
"failed to convert to a strict ip address: "
"cannot convert 'hello' into an ipv4 address N.N.N.N"
)

with pytest.raises(argparse.ArgumentTypeError) as e:
flags.type_ipaddress(strict=True).validate("1.2.3.4:hello")
assert e.value.args[-1] == (
"failed to convert to a strict ip address: "
"cannot convert 'hello' to an integer"
)

assert flags.type_ipaddress(strict=True).validate("1.2.3.4:567") == ("1.2.3.4", 567)

0 comments on commit e93d0d3

Please sign in to comment.