2024-08-11 17:24:26 +01:00
|
|
|
"""Test sphinx.util._importer."""
|
|
|
|
|
|
2024-11-22 21:54:26 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2024-08-11 17:24:26 +01:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from sphinx.errors import ExtensionError
|
|
|
|
|
from sphinx.util._importer import import_object
|
|
|
|
|
|
|
|
|
|
|
2025-02-15 23:23:56 +00:00
|
|
|
def test_import_object() -> None:
|
2024-08-11 17:24:26 +01:00
|
|
|
module = import_object('sphinx')
|
|
|
|
|
assert module.__name__ == 'sphinx'
|
|
|
|
|
|
|
|
|
|
module = import_object('sphinx.application')
|
|
|
|
|
assert module.__name__ == 'sphinx.application'
|
|
|
|
|
|
|
|
|
|
obj = import_object('sphinx.application.Sphinx')
|
|
|
|
|
assert obj.__name__ == 'Sphinx'
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ExtensionError) as exc:
|
|
|
|
|
import_object('sphinx.unknown_module')
|
|
|
|
|
assert exc.value.args[0] == 'Could not import sphinx.unknown_module'
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ExtensionError) as exc:
|
|
|
|
|
import_object('sphinx.unknown_module', 'my extension')
|
|
|
|
|
expected = 'Could not import sphinx.unknown_module (needed for my extension)'
|
|
|
|
|
assert exc.value.args[0] == expected
|