This commit is contained in:
Sai Asish Y 2026-08-16 02:46:20 -07:00 committed by GitHub
commit ac2cf1fce7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 0 deletions

3
RELEASE.md Normal file
View file

@ -0,0 +1,3 @@
Release type: patch
Raise a clear error when the specified settings file does not exist

View file

@ -17,6 +17,8 @@ from pelican.paginator import PaginationRule
def load_source(name: str, path: str) -> ModuleType:
spec = importlib.util.spec_from_file_location(name, path)
if spec is None or spec.loader is None:
raise ImportError(f"Cannot import settings from {path}")
mod = importlib.util.module_from_spec(spec)
sys.modules[name] = mod
spec.loader.exec_module(mod)
@ -242,6 +244,8 @@ def get_settings_from_module(module: ModuleType | None = None) -> Settings:
def get_settings_from_file(path: str) -> Settings:
"""Loads settings from a file path, returning a dict."""
if not os.path.isfile(path):
raise FileNotFoundError(f"Settings file not found: {path}")
name, ext = os.path.splitext(os.path.basename(path))
module = load_source(name, path)
return get_settings_from_module(module)

View file

@ -184,6 +184,13 @@ class TestSettingsConfiguration(unittest.TestCase):
locale.setlocale(locale.LC_TIME, "")
self.assertEqual(lc_time, locale.getlocale(locale.LC_TIME))
def test_read_settings_missing_file(self):
missing = join(self.PATH, "no_such_conf.py")
with self.assertRaises(FileNotFoundError):
read_settings(missing)
with self.assertRaises(FileNotFoundError):
read_settings(join(self.PATH, "no_such_conf"))
def test_invalid_settings_throw_exception(self):
# Test that the path name is valid