refactor: Do config file existence check in Config.read()

This commit is contained in:
Takeshi KOMIYA
2021-05-16 23:48:42 +09:00
parent 8d87dde43b
commit e5f6c77263
3 changed files with 13 additions and 12 deletions

View File

@@ -152,12 +152,6 @@ class Sphinx:
self.srcdir = abspath(srcdir)
self.outdir = abspath(outdir)
self.doctreedir = abspath(doctreedir)
self.confdir = confdir
if self.confdir: # confdir is optional
self.confdir = abspath(self.confdir)
if not path.isfile(path.join(self.confdir, 'conf.py')):
raise ApplicationError(__("config directory doesn't contain a "
"conf.py file (%s)") % confdir)
if not path.isdir(self.srcdir):
raise ApplicationError(__('Cannot find source directory (%s)') %
@@ -212,9 +206,13 @@ class Sphinx:
# read config
self.tags = Tags(tags)
if self.confdir is None:
if confdir is None:
# set confdir to srcdir if -C given (!= no confdir); a few pieces
# of code expect a confdir to be set
self.confdir = self.srcdir
self.config = Config({}, confoverrides or {})
else:
self.confdir = abspath(confdir)
self.config = Config.read(self.confdir, confoverrides or {}, self.tags)
# initialize some limited config variables before initialize i18n and loading
@@ -230,11 +228,6 @@ class Sphinx:
__('This project needs at least Sphinx v%s and therefore cannot '
'be built with this version.') % self.config.needs_sphinx)
# set confdir to srcdir if -C given (!= no confdir); a few pieces
# of code expect a confdir to be set
if self.confdir is None:
self.confdir = self.srcdir
# load all built-in extension modules
for extension in builtin_extensions:
self.setup_extension(extension)

View File

@@ -166,6 +166,9 @@ class Config:
def read(cls, confdir: str, overrides: Dict = None, tags: Tags = None) -> "Config":
"""Create a Config object from configuration file."""
filename = path.join(confdir, CONFIG_FILENAME)
if not path.isfile(filename):
raise ConfigError(__("config directory doesn't contain a conf.py file (%s)") %
confdir)
namespace = eval_config_file(filename, tags)
return cls(namespace, overrides or {})

View File

@@ -74,6 +74,11 @@ def test_core_config(app, status, warning):
assert cfg['project'] == cfg.project == 'Sphinx Tests'
def test_config_not_found(tempdir):
with pytest.raises(ConfigError):
Config.read(tempdir)
def test_extension_values():
config = Config()