52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
"""Tests for custom argparse actions."""
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
import pytest
|
|
|
|
from byteb4rb1e.utils.argparse.actions import KeyValueAction
|
|
|
|
|
|
def _parse(*args):
|
|
parser = ArgumentParser()
|
|
parser.add_argument("--config", action=KeyValueAction, default={}, metavar="KEY=VALUE")
|
|
return parser.parse_args(list(args))
|
|
|
|
|
|
class TestKeyValueAction:
|
|
|
|
def test_single_pair(self):
|
|
args = _parse("--config", "key=value")
|
|
assert args.config == {"key": "value"}
|
|
|
|
def test_multiple_pairs(self):
|
|
args = _parse("--config", "a=1", "--config", "b=2")
|
|
assert args.config == {"a": "1", "b": "2"}
|
|
|
|
def test_dotted_key(self):
|
|
args = _parse("--config", "provider.base_url=http://localhost")
|
|
assert args.config == {"provider.base_url": "http://localhost"}
|
|
|
|
def test_value_with_equals(self):
|
|
args = _parse("--config", "url=http://host?a=1&b=2")
|
|
assert args.config == {"url": "http://host?a=1&b=2"}
|
|
|
|
def test_empty_value(self):
|
|
args = _parse("--config", "key=")
|
|
assert args.config == {"key": ""}
|
|
|
|
def test_strips_whitespace(self):
|
|
args = _parse("--config", " key = value ")
|
|
assert args.config == {"key": "value"}
|
|
|
|
def test_overwrites_duplicate_key(self):
|
|
args = _parse("--config", "key=first", "--config", "key=second")
|
|
assert args.config == {"key": "second"}
|
|
|
|
def test_default_empty_dict(self):
|
|
args = _parse()
|
|
assert args.config == {}
|
|
|
|
def test_no_equals_raises(self):
|
|
with pytest.raises(SystemExit):
|
|
_parse("--config", "no_equals_here")
|