Skip to content

Commit b0dcf84

Browse files
committed
refactor: share regexlist code between the two config formats
1 parent 351d1d9 commit b0dcf84

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

coverage/config.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,7 @@ def getregexlist(self, section: str, option: str) -> list[str]:
132132
133133
"""
134134
line_list = self.get(section, option)
135-
value_list = []
136-
for value in line_list.splitlines():
137-
value = value.strip()
138-
try:
139-
re.compile(value)
140-
except re.error as e:
141-
raise ConfigError(
142-
f"Invalid [{section}].{option} value {value!r}: {e}",
143-
) from e
144-
if value:
145-
value_list.append(value)
146-
return value_list
135+
return process_regexlist(section, option, line_list.splitlines())
147136

148137

149138
TConfigParser = Union[HandyConfigParser, TomlConfigParser]
@@ -557,6 +546,20 @@ def debug_info(self) -> list[tuple[str, Any]]:
557546
)
558547

559548

549+
def process_regexlist(name: str, option: str, values: list[str]) -> list[str]:
550+
"""Check the values in a regex list and keep the non-blank ones."""
551+
value_list = []
552+
for value in values:
553+
value = value.strip()
554+
try:
555+
re.compile(value)
556+
except re.error as e:
557+
raise ConfigError(f"Invalid [{name}].{option} value {value!r}: {e}") from e
558+
if value:
559+
value_list.append(value)
560+
return value_list
561+
562+
560563
def config_files_to_try(config_file: bool | str) -> list[tuple[str, bool, bool]]:
561564
"""What config files should we try to read?
562565

coverage/tomlconfig.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import Any, Callable, TypeVar
1212
from collections.abc import Iterable
1313

14-
from coverage import env
14+
from coverage import config, env
1515
from coverage.exceptions import ConfigError
1616
from coverage.misc import import_third_party, isolate_module, substitute_variables
1717
from coverage.types import TConfigSectionOut, TConfigValueOut
@@ -191,13 +191,7 @@ def getlist(self, section: str, option: str) -> list[str]:
191191

192192
def getregexlist(self, section: str, option: str) -> list[str]:
193193
name, values = self._get_list(section, option)
194-
for value in values:
195-
value = value.strip()
196-
try:
197-
re.compile(value)
198-
except re.error as e:
199-
raise ConfigError(f"Invalid [{name}].{option} value {value!r}: {e}") from e
200-
return values
194+
return config.process_regexlist(name, option, values)
201195

202196
def getint(self, section: str, option: str) -> int:
203197
name, value = self._get_single(section, option)

0 commit comments

Comments
 (0)