-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Expand file tree
/
Copy pathtest_matplotlib.py
More file actions
142 lines (116 loc) · 5.13 KB
/
test_matplotlib.py
File metadata and controls
142 lines (116 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import subprocess
import sys
from unittest.mock import patch
import pytest
import matplotlib
from matplotlib.testing import subprocess_run_for_testing
@pytest.mark.parametrize('version_str, version_tuple', [
('3.5.0', (3, 5, 0, 'final', 0)),
('3.5.0rc2', (3, 5, 0, 'candidate', 2)),
('3.5.0.dev820+g6768ef8c4c', (3, 5, 0, 'alpha', 820)),
('3.5.0.post820+g6768ef8c4c', (3, 5, 1, 'alpha', 820)),
])
def test_parse_to_version_info(version_str, version_tuple):
assert matplotlib._parse_to_version_info(version_str) == version_tuple
@pytest.mark.skipif(sys.platform not in ["linux", "darwin"],
reason="chmod() doesn't work on this platform")
@pytest.mark.skipif(sys.platform in ["linux", "darwin"] and os.geteuid() == 0,
reason="chmod() doesn't work as root")
def test_tmpconfigdir_warning(tmp_path):
"""Test that a warning is emitted if a temporary configdir must be used."""
mode = os.stat(tmp_path).st_mode
try:
os.chmod(tmp_path, 0)
proc = subprocess_run_for_testing(
[sys.executable, "-c", "import matplotlib"],
env={**os.environ, "MPLCONFIGDIR": str(tmp_path)},
stderr=subprocess.PIPE, text=True, check=True)
assert "set the MPLCONFIGDIR" in proc.stderr
finally:
os.chmod(tmp_path, mode)
def test_importable_with_no_home(tmp_path):
subprocess_run_for_testing(
[sys.executable, "-c",
"import pathlib; pathlib.Path.home = lambda *args: 1/0; "
"import matplotlib.pyplot"],
env={**os.environ, "MPLCONFIGDIR": str(tmp_path)}, check=True)
def test_use_doc_standard_backends():
"""
Test that the standard backends mentioned in the docstring of
matplotlib.use() are the same as in matplotlib.rcsetup.
"""
def parse(key):
backends = []
for line in matplotlib.use.__doc__.split(key)[1].split('\n'):
if not line.strip():
break
backends += [e.strip().lower() for e in line.split(',') if e]
return backends
from matplotlib.backends import BackendFilter, backend_registry
assert (set(parse('- interactive backends:\n')) ==
set(backend_registry.list_builtin(BackendFilter.INTERACTIVE)))
assert (set(parse('- non-interactive backends:\n')) ==
set(backend_registry.list_builtin(BackendFilter.NON_INTERACTIVE)))
def test_importable_with__OO():
"""
When using -OO or export PYTHONOPTIMIZE=2, docstrings are discarded,
this simple test may prevent something like issue #17970.
"""
program = (
"import matplotlib as mpl; "
"import matplotlib.pyplot as plt; "
"import matplotlib.cbook as cbook; "
"import matplotlib.patches as mpatches"
)
subprocess_run_for_testing(
[sys.executable, "-OO", "-c", program],
env={**os.environ, "MPLBACKEND": ""}, check=True
)
@patch('matplotlib.subprocess.check_output')
def test_get_executable_info_timeout(mock_check_output):
"""
Test that _get_executable_info raises ExecutableNotFoundError if the
command times out.
"""
mock_check_output.side_effect = subprocess.TimeoutExpired(cmd=['mock'], timeout=30)
with pytest.raises(matplotlib.ExecutableNotFoundError, match='Timed out'):
matplotlib._get_executable_info.__wrapped__('inkscape')
@pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
def test_configdir_uses_localappdata_on_windows(tmp_path):
"""Test that on Windows, config/cache dir uses LOCALAPPDATA for fresh installs."""
localappdata = tmp_path / "AppData/Local"
localappdata.mkdir(parents=True)
# Set USERPROFILE to tmp_path so the old location check finds nothing
fake_home = tmp_path / "home"
fake_home.mkdir()
proc = subprocess_run_for_testing(
[sys.executable, "-c",
"import matplotlib; print(matplotlib.get_configdir())"],
env={**os.environ, "LOCALAPPDATA": str(localappdata),
"USERPROFILE": str(fake_home), "MPLCONFIGDIR": ""},
capture_output=True, text=True, check=True)
configdir = proc.stdout.strip()
# On Windows with no existing old config, should use LOCALAPPDATA\matplotlib
assert configdir == str(localappdata / "matplotlib")
@pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific test")
def test_configdir_uses_userprofile_on_windows_if_exists(tmp_path):
"""
Test that on Windows, config/cache dir uses %USERPROFILE% if .matplotlib
exists.
"""
localappdata = tmp_path / "AppData/Local"
localappdata.mkdir(parents=True)
fake_home = tmp_path / "home"
fake_home.mkdir()
old_configdir = fake_home / ".matplotlib"
old_configdir.mkdir()
proc = subprocess_run_for_testing(
[sys.executable, "-c",
"import matplotlib; print(matplotlib.get_configdir())"],
env={**os.environ, "LOCALAPPDATA": str(localappdata),
"USERPROFILE": str(fake_home), "MPLCONFIGDIR": ""},
capture_output=True, text=True, check=True)
configdir = proc.stdout.strip()
# On Windows with existing old config, should continue using it
assert configdir == str(old_configdir)