1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
import os
import sys
from argparse import ArgumentParser
from dataclasses import dataclass, fields
from typing import Tuple
import pytest
from pytoolconfig import PyToolConfig, UniversalKey, field
from pytoolconfig.sources import IniConfig
from pytoolconfig.universal_config import UniversalConfig
@dataclass
class SimpleModel:
formatter: str = "NOT THIS"
@dataclass
class EmptyModel:
pass
@dataclass
class SubTool:
foo: str = field(description="foobar", default="lo")
@dataclass
class NestedModel:
subtool: SubTool = field(default_factory=lambda: SubTool())
foo_other: str = field(description="w", default="no", command_line=("--foo", "-f"))
target: Tuple[int, int] = field(
description="Minimum python version",
default=(3, 1),
universal_config=UniversalKey.min_py_version,
)
def test_simple(cwd):
config = PyToolConfig("pytoolconfig", cwd, SimpleModel)
result = config.parse()
assert result.formatter == "black"
def test_invalid_key(cwd):
config = PyToolConfig("pytoolconfig", cwd, EmptyModel)
result = config.parse()
with pytest.raises(AttributeError):
assert result.formatter
def test_nested(cwd):
config = PyToolConfig(
"bogus",
cwd,
NestedModel,
custom_sources=[IniConfig(cwd, "test_config.ini", "bogus")],
)
result = config.parse()
assert result.subtool.foo == "barr"
config = PyToolConfig(
"bogus",
cwd,
NestedModel,
)
result = config.parse()
# Default argument
assert result.subtool.foo == "lo"
assert result.target == (3, 7)
def test_cli(cwd):
config = PyToolConfig("bogus", cwd, NestedModel, arg_parser=ArgumentParser())
result = config.parse()
assert result.subtool.foo == "lo"
result = config.parse(["--foo", "bar"])
assert result.foo_other == "bar"
def test_global(cwd):
if sys.platform != "linux":
pytest.skip()
os.environ["XDG_CONFIG_HOME"] = cwd.as_posix()
config = PyToolConfig("bogus", cwd, NestedModel, global_config=True)
result = config.parse()
assert result.subtool.foo == "ajf"
def test_fall_through(cwd):
config = PyToolConfig(
"fall_through",
cwd,
NestedModel,
custom_sources=[IniConfig(cwd, "test_config.ini", "bogus")],
fall_through=True,
)
result = config.parse()
assert result.subtool.foo == "barr"
assert result.foo_other == "ba"
def test_universal_key():
assert [field.name for field in fields(UniversalConfig)] == list(
UniversalKey.__members__,
)
|