|
| 1 | +import matplotlib |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import pytest |
| 4 | +from helpers import pytester_path, skip_if_format_unsupported |
| 5 | +from packaging.version import Version |
| 6 | +from PIL import Image |
| 7 | + |
| 8 | +MPL_VERSION = Version(matplotlib.__version__) |
| 9 | + |
| 10 | +METADATA = { |
| 11 | + "png": {"Software": None}, |
| 12 | + "pdf": {"Creator": None, "Producer": None, "CreationDate": None}, |
| 13 | + "eps": {"Creator": "test"}, |
| 14 | + "svg": {"Date": None}, |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +def test_multiple_cli_flags(pytester): |
| 19 | + result = pytester.runpytest("--mpl", "--mpl-deterministic", "--mpl-no-deterministic") |
| 20 | + result.stderr.fnmatch_lines( |
| 21 | + ["*ValueError: Only one of `--mpl-deterministic` and `--mpl-no-deterministic` can be set.*"] |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +def test_warning(pytester): |
| 26 | + path = pytester_path(pytester) |
| 27 | + hash_library = path / "hash_library.json" |
| 28 | + kwarg = f"hash_library=r'{hash_library}'" |
| 29 | + pytester.makepyfile( |
| 30 | + f""" |
| 31 | + import matplotlib.pyplot as plt |
| 32 | + import pytest |
| 33 | + @pytest.mark.mpl_image_compare({kwarg}) |
| 34 | + def test_mpl(): |
| 35 | + fig, ax = plt.subplots() |
| 36 | + ax.plot([1, 3, 2]) |
| 37 | + return fig |
| 38 | + """ |
| 39 | + ) |
| 40 | + result = pytester.runpytest(f"--mpl-generate-hash-library={hash_library}") |
| 41 | + result.stdout.fnmatch_lines(["*FutureWarning: deterministic option not set*"]) |
| 42 | + result.assert_outcomes(failed=1) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize("file_format", ["eps", "pdf", "png", "svg"]) |
| 46 | +@pytest.mark.parametrize( |
| 47 | + "ini, cli, kwarg, success_expected", |
| 48 | + [ |
| 49 | + ("true", "", None, True), |
| 50 | + ("false", "--mpl-deterministic", None, True), |
| 51 | + ("true", "--mpl-no-deterministic", None, False), |
| 52 | + ("", "--mpl-no-deterministic", True, True), |
| 53 | + ("true", "", False, False), |
| 54 | + ], |
| 55 | +) |
| 56 | +@pytest.mark.skipif(MPL_VERSION < Version("3.3.0"), reason="Test unsupported: Default metadata is different in MPL<3.3") |
| 57 | +def test_config(pytester, file_format, ini, cli, kwarg, success_expected): |
| 58 | + skip_if_format_unsupported(file_format, using_hashes=True) |
| 59 | + |
| 60 | + path = pytester_path(pytester) |
| 61 | + baseline_dir = path / "baseline" |
| 62 | + hash_library = path / "hash_library.json" |
| 63 | + |
| 64 | + ini = f"mpl-deterministic = {ini}" if ini else "" |
| 65 | + pytester.makeini( |
| 66 | + f""" |
| 67 | + [pytest] |
| 68 | + mpl-hash-library = {hash_library} |
| 69 | + {ini} |
| 70 | + """ |
| 71 | + ) |
| 72 | + |
| 73 | + kwarg = f", deterministic={kwarg}" if isinstance(kwarg, bool) else "" |
| 74 | + pytester.makepyfile( |
| 75 | + f""" |
| 76 | + import matplotlib.pyplot as plt |
| 77 | + import pytest |
| 78 | + @pytest.mark.mpl_image_compare(savefig_kwargs={{'format': '{file_format}'}}{kwarg}) |
| 79 | + def test_mpl(): |
| 80 | + fig, ax = plt.subplots() |
| 81 | + ax.plot([1, 2, 3]) |
| 82 | + return fig |
| 83 | + """ |
| 84 | + ) |
| 85 | + |
| 86 | + # Generate baseline hashes |
| 87 | + assert not hash_library.exists() |
| 88 | + pytester.runpytest( |
| 89 | + f"--mpl-generate-path={baseline_dir}", |
| 90 | + f"--mpl-generate-hash-library={hash_library}", |
| 91 | + cli, |
| 92 | + ) |
| 93 | + assert hash_library.exists() |
| 94 | + baseline_image = baseline_dir / f"test_mpl.{file_format}" |
| 95 | + assert baseline_image.exists() |
| 96 | + deterministic_metadata = METADATA[file_format] |
| 97 | + |
| 98 | + if file_format == "svg": # The only format that is reliably non-deterministic between runs |
| 99 | + result = pytester.runpytest("--mpl", f"--mpl-baseline-path={baseline_dir}", cli) |
| 100 | + if success_expected: |
| 101 | + result.assert_outcomes(passed=1) |
| 102 | + else: |
| 103 | + result.assert_outcomes(failed=1) |
| 104 | + |
| 105 | + elif file_format == "pdf": |
| 106 | + with open(baseline_image, "rb") as fp: |
| 107 | + file = str(fp.read()) |
| 108 | + for metadata_key in deterministic_metadata.keys(): |
| 109 | + key_in_file = fr"/{metadata_key}" in file |
| 110 | + if success_expected: # metadata keys should not be in the file |
| 111 | + assert not key_in_file |
| 112 | + else: |
| 113 | + assert key_in_file |
| 114 | + |
| 115 | + else: # "eps" or "png" |
| 116 | + actual_metadata = Image.open(str(baseline_image)).info |
| 117 | + for k, expected in deterministic_metadata.items(): |
| 118 | + actual = actual_metadata.get(k, None) |
| 119 | + if success_expected: # metadata keys should not be in the file |
| 120 | + if expected is None: |
| 121 | + assert actual is None |
| 122 | + else: |
| 123 | + assert actual == expected |
| 124 | + else: # metadata keys should still be in the file |
| 125 | + if expected is None: |
| 126 | + assert actual is not None |
| 127 | + else: |
| 128 | + assert actual != expected |
0 commit comments