| 1 |
"""Test sphinx.util._importer.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from sphinx.errors import ExtensionError |
| 8 |
from sphinx.util._importer import import_object |
| 9 |
|
| 10 |
|
| 11 |
def test_import_object() -> None: |
| 12 |
module = import_object('sphinx') |
| 13 |
assert module.__name__ == 'sphinx' |
| 14 |
|
| 15 |
module = import_object('sphinx.application') |
| 16 |
assert module.__name__ == 'sphinx.application' |
| 17 |
|
| 18 |
obj = import_object('sphinx.application.Sphinx') |
| 19 |
assert obj.__name__ == 'Sphinx' |
| 20 |
|
| 21 |
with pytest.raises(ExtensionError) as exc: |
| 22 |
import_object('sphinx.unknown_module') |
| 23 |
assert exc.value.args[0] == 'Could not import sphinx.unknown_module' |
| 24 |
|
| 25 |
with pytest.raises(ExtensionError) as exc: |
| 26 |
import_object('sphinx.unknown_module', 'my extension') |
| 27 |
expected = 'Could not import sphinx.unknown_module (needed for my extension)' |
| 28 |
assert exc.value.args[0] == expected |