Skip to content

Commit

Permalink
test: Increase config coverage. items() cannot raise NoOptionError.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Jul 20, 2024
1 parent 632fe3c commit f144395
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 26 deletions.
53 changes: 27 additions & 26 deletions scrapyd/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import glob
import os.path
from configparser import ConfigParser, NoOptionError, NoSectionError
from contextlib import suppress
from os.path import expanduser
from pkgutil import get_data

from scrapy.utils.conf import closest_scrapy_cfg
Expand All @@ -15,51 +15,52 @@ class Config:

def __init__(self, values=None, extra_sources=()):
if values is None:
sources = self._getsources()
default_config = get_data(__package__, "default_scrapyd.conf").decode("utf8")
self.cp = ConfigParser()
self.cp.read_string(default_config)
sources.extend(extra_sources)
for fname in sources:
with suppress(OSError), open(fname) as fp:
self.cp.read_file(fp)
self.cp.read_string(get_data(__package__, "default_scrapyd.conf").decode("utf8"))
for source in self._get_sources(extra_sources):
with suppress(OSError), open(source) as f:
self.cp.read_file(f)
else:
self.cp = ConfigParser(values)
self.cp.add_section(self.SECTION)

def _getsources(self):
sources = ["/etc/scrapyd/scrapyd.conf", r"c:\scrapyd\scrapyd.conf"]
sources += sorted(glob.glob("/etc/scrapyd/conf.d/*"))
sources += ["scrapyd.conf"]
sources += [expanduser("~/.scrapyd.conf")]
def _get_sources(self, extra_sources):
sources = [
"/etc/scrapyd/scrapyd.conf",
"c:\\scrapyd\\scrapyd.conf",
*sorted(glob.glob("/etc/scrapyd/conf.d/*")),
"scrapyd.conf",
os.path.expanduser("~/.scrapyd.conf"),
]
if scrapy_cfg := closest_scrapy_cfg():
sources.append(scrapy_cfg)
sources.extend(extra_sources)
return sources

def _getany(self, method, option, default):
try:
return method(self.SECTION, option)
except (NoSectionError, NoOptionError):
if default is not None:
return default
raise

def get(self, option, default=None):
return self._getany(self.cp.get, option, default)
return self._get(self.cp.get, option, default)

def getint(self, option, default=None):
return self._getany(self.cp.getint, option, default)
return self._get(self.cp.getint, option, default)

def getfloat(self, option, default=None):
return self._getany(self.cp.getfloat, option, default)
return self._get(self.cp.getfloat, option, default)

def getboolean(self, option, default=None):
return self._getany(self.cp.getboolean, option, default)
return self._get(self.cp.getboolean, option, default)

def _get(self, method, option, default):
try:
return method(self.SECTION, option)
except (NoSectionError, NoOptionError):
if default is not None:
return default
raise

def items(self, section, default=None):
try:
return self.cp.items(section)
except (NoSectionError, NoOptionError):
except NoSectionError:
if default is not None:
return default
raise
30 changes: 30 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from configparser import NoOptionError, NoSectionError

import pytest

from scrapyd.config import Config


def test_items_no_section():
with pytest.raises(NoSectionError):
Config().items("nonexistent")


def test_get_no_section():
with pytest.raises(NoOptionError):
Config().get("nonexistent")


def test_get_no_option():
config = Config()
config.cp.set("scrapyd", "http_port", "8000")

with pytest.raises(NoOptionError):
config.get("nonexistent")


def test_closest_scrapy_cfg(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
(tmp_path / "scrapy.cfg").write_text("[scrapyd]\nhttp_port = 1234")

assert Config().getint("http_port") == 1234

0 comments on commit f144395

Please sign in to comment.