|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
| 3 | +import subprocess |
3 | 4 | import textwrap
|
4 | 5 |
|
5 | 6 | import pytest
|
@@ -119,3 +120,38 @@ def task_example():
|
119 | 120 | assert result.exit_code == ExitCode.OK
|
120 | 121 | assert ("Warnings" in result.output) is not add_config
|
121 | 122 | assert ("warning!!!" in result.output) is not add_config
|
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize("warning", ["DeprecationWarning", "PendingDeprecationWarning"]) |
| 126 | +def test_deprecation_warnings_are_not_captured(tmp_path, warning): |
| 127 | + path_to_warn_module = tmp_path.joinpath("warning.py") |
| 128 | + source = """ |
| 129 | + import importlib.util |
| 130 | + import sys |
| 131 | + from pathlib import Path |
| 132 | +
|
| 133 | + def task_example(): |
| 134 | + spec = importlib.util.spec_from_file_location( |
| 135 | + "warning", Path(__file__).parent / "warning.py" |
| 136 | + ) |
| 137 | + warning_module = importlib.util.module_from_spec(spec) |
| 138 | + sys.modules["warning"] = warning_module |
| 139 | + spec.loader.exec_module(warning_module) |
| 140 | + warning_module.warn_now() |
| 141 | + """ |
| 142 | + tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source)) |
| 143 | + |
| 144 | + warn_module = f""" |
| 145 | + import warnings |
| 146 | +
|
| 147 | + def warn_now(): |
| 148 | + warnings.warn("warning!!!", {warning}) |
| 149 | + """ |
| 150 | + path_to_warn_module.write_text(textwrap.dedent(warn_module)) |
| 151 | + |
| 152 | + # Cannot use runner since then warnings are not ignored by default. |
| 153 | + result = subprocess.run(("pytask"), cwd=tmp_path, capture_output=True) |
| 154 | + |
| 155 | + assert result.returncode == ExitCode.OK |
| 156 | + assert "Warnings" not in result.stdout.decode() |
| 157 | + assert "warning!!!" not in result.stdout.decode() |
0 commit comments