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

View File

@ -5,6 +5,7 @@ import re
import sys import sys
import textwrap import textwrap
from pathlib import Path from pathlib import Path
from typing import Any
from typing import Dict from typing import Dict
from typing import List from typing import List
from typing import Sequence from typing import Sequence
@ -21,6 +22,7 @@ from _pytest.config import Config
from _pytest.config import ConftestImportFailure from _pytest.config import ConftestImportFailure
from _pytest.config import ExitCode from _pytest.config import ExitCode
from _pytest.config import parse_warning_filter 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.exceptions import UsageError
from _pytest.config.findpaths import determine_setup from _pytest.config.findpaths import determine_setup
from _pytest.config.findpaths import get_common_ancestor from _pytest.config.findpaths import get_common_ancestor
@ -904,6 +906,21 @@ class TestConfigAPI:
value = config.getini("no_type") value = config.getini("no_type")
assert value == "" 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: def test_confcutdir_check_isdir(self, pytester: Pytester) -> None:
"""Give an error if --confcutdir is not a valid directory (#2078)""" """Give an error if --confcutdir is not a valid directory (#2078)"""
exp_match = r"^--confcutdir must be a directory, given: " exp_match = r"^--confcutdir must be a directory, given: "