Unit-test get_init_default_for_type

This commit is contained in:
Bruno Oliveira 2023-11-07 18:37:29 -03:00
parent 172bb15625
commit 4a7856e829
2 changed files with 34 additions and 14 deletions

View File

@ -212,24 +212,27 @@ class Parser:
"""
assert type in (None, "string", "paths", "pathlist", "args", "linelist", "bool")
if default is NOT_SET:
default = self._get_ini_default_for_type(type)
default = get_ini_default_for_type(type)
self._inidict[name] = (help, type, default)
self._ininames.append(name)
def _get_ini_default_for_type(self, type) -> Any:
default = Any
def get_ini_default_for_type(
type: Optional[Literal["string", "paths", "pathlist", "args", "linelist", "bool"]]
) -> Any:
"""
Used by addini to get the default value for a given ini-option type, when
default is not supplied.
"""
if type is None:
default = ""
else:
if type in ["paths", "pathlist", "args", "linelist"]:
default = []
return ""
elif type in ("paths", "pathlist", "args", "linelist"):
return []
elif type == "bool":
default = False
return False
else:
# for string type
default = ""
return default
return ""
class ArgumentError(Exception):

View File

@ -5,6 +5,7 @@ import re
import sys
import textwrap
from pathlib import Path
from typing import Any
from typing import Dict
from typing import List
from typing import Sequence
@ -21,6 +22,7 @@ from _pytest.config import Config
from _pytest.config import ConftestImportFailure
from _pytest.config import ExitCode
from _pytest.config import parse_warning_filter
from _pytest.config.argparsing import get_ini_default_for_type
from _pytest.config.exceptions import UsageError
from _pytest.config.findpaths import determine_setup
from _pytest.config.findpaths import get_common_ancestor
@ -904,6 +906,21 @@ class TestConfigAPI:
value = config.getini("no_type")
assert value == ""
@pytest.mark.parametrize(
"type, expected",
[
pytest.param(None, "", id="None"),
pytest.param("string", "", id="string"),
pytest.param("paths", [], id="paths"),
pytest.param("pathlist", [], id="pathlist"),
pytest.param("args", [], id="args"),
pytest.param("linelist", [], id="linelist"),
pytest.param("bool", False, id="bool"),
],
)
def test_get_ini_default_for_type(self, type: Any, expected: Any) -> None:
assert get_ini_default_for_type(type) == expected
def test_confcutdir_check_isdir(self, pytester: Pytester) -> None:
"""Give an error if --confcutdir is not a valid directory (#2078)"""
exp_match = r"^--confcutdir must be a directory, given: "