|
6 | 6 | unit tests for module modutils (module manipulation utilities)
|
7 | 7 | """
|
8 | 8 | import email
|
| 9 | +import logging |
9 | 10 | import os
|
10 | 11 | import shutil
|
11 | 12 | import sys
|
|
16 | 17 | from xml import etree
|
17 | 18 | from xml.etree import ElementTree
|
18 | 19 |
|
| 20 | +from pytest import CaptureFixture, LogCaptureFixture |
| 21 | + |
19 | 22 | import astroid
|
20 | 23 | from astroid import modutils
|
21 | 24 | from astroid.interpreter._import import spec
|
@@ -71,6 +74,35 @@ def test_raise_load_module_from_name_1(self) -> None:
|
71 | 74 | )
|
72 | 75 |
|
73 | 76 |
|
| 77 | +def test_import_dotted_library( |
| 78 | + capsys: CaptureFixture, |
| 79 | + caplog: LogCaptureFixture, |
| 80 | +) -> None: |
| 81 | + caplog.set_level(logging.INFO) |
| 82 | + del sys.modules["xml.etree.ElementTree"] |
| 83 | + expected_out = "INFO (TEST): Welcome to cElementTree!" |
| 84 | + expected_err = "WARNING (TEST): Monkey-patched version of cElementTree" |
| 85 | + |
| 86 | + def function_with_stdout_and_stderr(expected_out, expected_err): |
| 87 | + def mocked_function(*args, **kwargs): |
| 88 | + print(f"{expected_out} args={args} kwargs={kwargs}") |
| 89 | + print(expected_err, file=sys.stderr) |
| 90 | + |
| 91 | + return mocked_function |
| 92 | + |
| 93 | + with unittest.mock.patch( |
| 94 | + "importlib.import_module", |
| 95 | + side_effect=function_with_stdout_and_stderr(expected_out, expected_err), |
| 96 | + ): |
| 97 | + modutils.load_module_from_name("xml.etree.ElementTree") |
| 98 | + |
| 99 | + out, err = capsys.readouterr() |
| 100 | + assert expected_out in caplog.text |
| 101 | + assert expected_err in caplog.text |
| 102 | + assert not out |
| 103 | + assert not err |
| 104 | + |
| 105 | + |
74 | 106 | class GetModulePartTest(unittest.TestCase):
|
75 | 107 | """given a dotted name return the module part of the name"""
|
76 | 108 |
|
|
0 commit comments