From ffa1669c5847cba6ab14383083af00a9d830c40d Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 29 Mar 2020 04:19:02 +0200 Subject: [PATCH] Backport PR #16949: TST: Don't modify actual pyplot file for boilerplate test. --- lib/matplotlib/tests/test_pyplot.py | 43 +++++++++++++++-------------- tools/boilerplate.py | 11 +++++--- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index e4860aa2117f..51470d2bf1b5 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -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(): diff --git a/tools/boilerplate.py b/tools/boilerplate.py index caec5247410e..faf6927da742 100644 --- a/tools/boilerplate.py +++ b/tools/boilerplate.py @@ -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. @@ -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] @@ -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)