mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: Do config file existence check in Config.read()
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 {})
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user