Skip to content

Commit 5792181

Browse files
authoredJul 1, 2023
Fail CI on encountering deprecation warnings (#404)
Because we run flake8 via a subprocess in our test suite, pytest can't detect any DeprecationWarnings emitted while the plugin is being run. Instead, use Python's warnings filter to have all warnings encountered in the subprocess be printed to stderr. This will cause tests to fail with a nice diff if any deprecation warnings are encountered in the subprocess. Closes #392
1 parent a5bbfbb commit 5792181

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed
 

‎pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ ignore_missing_imports = true
102102

103103
[tool.pytest.ini_options]
104104
addopts = "--doctest-modules"
105+
filterwarnings = ["error"]

‎tests/test_pyi_files.py

+26-7
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,42 @@ def test_pyi_file(path: str) -> None:
4242
option = flag.split("=")[0]
4343
assert option != "--ignore", bad_flag_msg
4444

45+
# Silence DeprecationWarnings from our dependencies (pyflakes, flake8-bugbear, etc.)
46+
#
47+
# For DeprecationWarnings coming from flake8-pyi itself,
48+
# print the first occurence of each warning to stderr.
49+
# This will fail CI the same as `-Werror:::pyi`,
50+
# but the test failure report that pytest gives is much easier to read
51+
# if we use `-Wdefault:::pyi`
52+
flake8_invocation = [
53+
sys.executable,
54+
"-Wignore",
55+
"-Wdefault:::pyi",
56+
"-m",
57+
"flake8",
58+
"-j0",
59+
]
60+
4561
run_results = [
4662
# Passing a file on command line
4763
subprocess.run(
48-
["flake8", "-j0", *flags, path],
64+
[*flake8_invocation, *flags, path],
4965
env={**os.environ, "PYTHONPATH": "."},
50-
stdout=subprocess.PIPE,
66+
capture_output=True,
67+
text=True,
5168
),
5269
# Passing "-" as the file, and reading from stdin instead
5370
subprocess.run(
54-
["flake8", "-j0", "--stdin-display-name", path, *flags, "-"],
71+
[*flake8_invocation, "--stdin-display-name", path, *flags, "-"],
5572
env={**os.environ, "PYTHONPATH": "."},
56-
input=file_contents.encode("utf-8"),
57-
stdout=subprocess.PIPE,
73+
input=file_contents,
74+
capture_output=True,
75+
text=True,
5876
),
5977
]
6078

6179
for run_result in run_results:
62-
output = run_result.stdout.decode("utf-8")
63-
output = re.sub(":[0-9]+: ", ": ", output) # ignore column numbers
80+
output = re.sub(":[0-9]+: ", ": ", run_result.stdout) # ignore column numbers
81+
if run_result.stderr:
82+
output += "\n" + run_result.stderr
6483
assert output == expected_output

0 commit comments

Comments
 (0)
Please sign in to comment.