|
| 1 | +import io |
| 2 | +import logging |
1 | 3 | from pathlib import Path |
2 | 4 |
|
3 | 5 | import pytest |
4 | | -from pytest import CaptureFixture |
5 | 6 |
|
6 | 7 | from fastapi_cli.discover import get_import_data |
7 | 8 | from fastapi_cli.exceptions import FastAPICLIException |
| 9 | +from fastapi_cli.logging import PlainFormatter |
8 | 10 | from tests.utils import changing_dir |
9 | 11 |
|
10 | 12 | assets_path = Path(__file__).parent / "assets" |
@@ -158,15 +160,24 @@ def test_package_dir_explicit_app() -> None: |
158 | 160 | assert import_data.module_data.module_import_str == "package" |
159 | 161 |
|
160 | 162 |
|
161 | | -def test_broken_package_dir(capsys: CaptureFixture[str]) -> None: |
162 | | - with changing_dir(assets_path): |
163 | | - # TODO (when deprecating Python 3.8): remove ValueError |
164 | | - with pytest.raises((ImportError, ValueError)): |
165 | | - get_import_data(path=Path("broken_package/mod/app.py")) |
166 | | - |
167 | | - captured = capsys.readouterr() |
168 | | - assert "Import error:" in captured.out |
169 | | - assert "Ensure all the package directories have an __init__.py file" in captured.out |
| 163 | +def test_broken_package_dir() -> None: |
| 164 | + stream = io.StringIO() |
| 165 | + handler = logging.StreamHandler(stream) |
| 166 | + handler.setFormatter(PlainFormatter("%(levelname)s:%(name)s:%(message)s")) |
| 167 | + logger = logging.getLogger("fastapi_cli") |
| 168 | + logger.addHandler(handler) |
| 169 | + try: |
| 170 | + with changing_dir(assets_path): |
| 171 | + # TODO (when deprecating Python 3.8): remove ValueError |
| 172 | + with pytest.raises((ImportError, ValueError)): |
| 173 | + get_import_data(path=Path("broken_package/mod/app.py")) |
| 174 | + finally: |
| 175 | + logger.removeHandler(handler) |
| 176 | + handler.close() |
| 177 | + |
| 178 | + output = stream.getvalue() |
| 179 | + assert "Import error:" in output |
| 180 | + assert "Ensure all the package directories have an __init__.py file" in output |
170 | 181 |
|
171 | 182 |
|
172 | 183 | def test_package_dir_no_app() -> None: |
|
0 commit comments