Skip to content

Backport PR #16949 on branch v3.2.x (TST: Don't modify actual pyplot file for boilerplate test.) #16950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions lib/matplotlib/tests/test_pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@
from matplotlib import pyplot as plt


def test_pyplot_up_to_date():
def test_pyplot_up_to_date(tmpdir):
gen_script = Path(mpl.__file__).parents[2] / "tools/boilerplate.py"
if not gen_script.exists():
pytest.skip("boilerplate.py not found")
orig_contents = Path(plt.__file__).read_text()
try:
subprocess.run([sys.executable, str(gen_script)], check=True)
new_contents = Path(plt.__file__).read_text()

if orig_contents != new_contents:
diff_msg = '\n'.join(
difflib.unified_diff(
orig_contents.split('\n'), new_contents.split('\n'),
fromfile='found pyplot.py',
tofile='expected pyplot.py',
n=0, lineterm=''))
pytest.fail(
"pyplot.py is not up-to-date. Please run "
"'python tools/boilerplate.py' to update pyplot.py. "
"This needs to be done from an environment where your "
"current working copy is installed (e.g. 'pip install -e'd). "
"Here is a diff of unexpected differences:\n%s" % diff_msg
)
finally:
Path(plt.__file__).write_text(orig_contents)
plt_file = tmpdir.join('pyplot.py')
plt_file.write_text(orig_contents, 'utf-8')

subprocess.run([sys.executable, str(gen_script), str(plt_file)],
check=True)
new_contents = plt_file.read_text('utf-8')

if orig_contents != new_contents:
diff_msg = '\n'.join(
difflib.unified_diff(
orig_contents.split('\n'), new_contents.split('\n'),
fromfile='found pyplot.py',
tofile='expected pyplot.py',
n=0, lineterm=''))
pytest.fail(
"pyplot.py is not up-to-date. Please run "
"'python tools/boilerplate.py' to update pyplot.py. "
"This needs to be done from an environment where your "
"current working copy is installed (e.g. 'pip install -e'd). "
"Here is a diff of unexpected differences:\n%s" % diff_msg
)


def test_pyplot_box():
Expand Down
11 changes: 7 additions & 4 deletions tools/boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import inspect
from inspect import Parameter
from pathlib import Path
import sys
import textwrap

# This line imports the installed copy of matplotlib, and not the local copy.
Expand Down Expand Up @@ -327,9 +328,7 @@ def boilerplate_gen():
yield '_setup_pyplot_info_docstrings()'


def build_pyplot():
pyplot_path = Path(__file__).parent / "../lib/matplotlib/pyplot.py"

def build_pyplot(pyplot_path):
pyplot_orig = pyplot_path.read_text().splitlines(keepends=True)
try:
pyplot_orig = pyplot_orig[:pyplot_orig.index(PYPLOT_MAGIC_HEADER) + 1]
Expand All @@ -345,4 +344,8 @@ def build_pyplot():

if __name__ == '__main__':
# Write the matplotlib.pyplot file.
build_pyplot()
if len(sys.argv) > 1:
pyplot_path = Path(sys.argv[1])
else:
pyplot_path = Path(__file__).parent / "../lib/matplotlib/pyplot.py"
build_pyplot(pyplot_path)