test_matplotlib.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import os
  2. import subprocess
  3. import sys
  4. from unittest.mock import patch
  5. import pytest
  6. import matplotlib
  7. from matplotlib.testing import subprocess_run_for_testing
  8. @pytest.mark.parametrize('version_str, version_tuple', [
  9. ('3.5.0', (3, 5, 0, 'final', 0)),
  10. ('3.5.0rc2', (3, 5, 0, 'candidate', 2)),
  11. ('3.5.0.dev820+g6768ef8c4c', (3, 5, 0, 'alpha', 820)),
  12. ('3.5.0.post820+g6768ef8c4c', (3, 5, 1, 'alpha', 820)),
  13. ])
  14. def test_parse_to_version_info(version_str, version_tuple):
  15. assert matplotlib._parse_to_version_info(version_str) == version_tuple
  16. @pytest.mark.skipif(sys.platform == "win32",
  17. reason="chmod() doesn't work as is on Windows")
  18. @pytest.mark.skipif(sys.platform != "win32" and os.geteuid() == 0,
  19. reason="chmod() doesn't work as root")
  20. def test_tmpconfigdir_warning(tmp_path):
  21. """Test that a warning is emitted if a temporary configdir must be used."""
  22. mode = os.stat(tmp_path).st_mode
  23. try:
  24. os.chmod(tmp_path, 0)
  25. proc = subprocess_run_for_testing(
  26. [sys.executable, "-c", "import matplotlib"],
  27. env={**os.environ, "MPLCONFIGDIR": str(tmp_path)},
  28. stderr=subprocess.PIPE, text=True, check=True)
  29. assert "set the MPLCONFIGDIR" in proc.stderr
  30. finally:
  31. os.chmod(tmp_path, mode)
  32. def test_importable_with_no_home(tmp_path):
  33. subprocess_run_for_testing(
  34. [sys.executable, "-c",
  35. "import pathlib; pathlib.Path.home = lambda *args: 1/0; "
  36. "import matplotlib.pyplot"],
  37. env={**os.environ, "MPLCONFIGDIR": str(tmp_path)}, check=True)
  38. def test_use_doc_standard_backends():
  39. """
  40. Test that the standard backends mentioned in the docstring of
  41. matplotlib.use() are the same as in matplotlib.rcsetup.
  42. """
  43. def parse(key):
  44. backends = []
  45. for line in matplotlib.use.__doc__.split(key)[1].split('\n'):
  46. if not line.strip():
  47. break
  48. backends += [e.strip().lower() for e in line.split(',') if e]
  49. return backends
  50. from matplotlib.backends import BackendFilter, backend_registry
  51. assert (set(parse('- interactive backends:\n')) ==
  52. set(backend_registry.list_builtin(BackendFilter.INTERACTIVE)))
  53. assert (set(parse('- non-interactive backends:\n')) ==
  54. set(backend_registry.list_builtin(BackendFilter.NON_INTERACTIVE)))
  55. def test_importable_with__OO():
  56. """
  57. When using -OO or export PYTHONOPTIMIZE=2, docstrings are discarded,
  58. this simple test may prevent something like issue #17970.
  59. """
  60. program = (
  61. "import matplotlib as mpl; "
  62. "import matplotlib.pyplot as plt; "
  63. "import matplotlib.cbook as cbook; "
  64. "import matplotlib.patches as mpatches"
  65. )
  66. subprocess_run_for_testing(
  67. [sys.executable, "-OO", "-c", program],
  68. env={**os.environ, "MPLBACKEND": ""}, check=True
  69. )
  70. @patch('matplotlib.subprocess.check_output')
  71. def test_get_executable_info_timeout(mock_check_output):
  72. """
  73. Test that _get_executable_info raises ExecutableNotFoundError if the
  74. command times out.
  75. """
  76. mock_check_output.side_effect = subprocess.TimeoutExpired(cmd=['mock'], timeout=30)
  77. with pytest.raises(matplotlib.ExecutableNotFoundError, match='Timed out'):
  78. matplotlib._get_executable_info.__wrapped__('inkscape')