Repository: matplotlib/matplotlib
File: lib/matplotlib/tests/test_matplotlib.py (Python)

Code before:
        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 instead of home."""
    # Clear cached values
    matplotlib.get_configdir.cache_clear()
    matplotlib.get_cachedir.cache_clear()

    localappdata = str(tmp_path / "AppData" / "Local")
    os.makedirs(localappdata, exist_ok=True)

    proc = subprocess_run_for_testing(
        [sys.executable, "-c",
         "import matplotlib; print(matplotlib.get_configdir())"],
        env={**os.environ, "LOCALAPPDATA": localappdata, "MPLCONFIGDIR": ""},
        capture_output=True, text=True, check=True)

    configdir = proc.stdout.strip()
    # On Windows, should use LOCALAPPDATA\matplotlib
    assert "matplotlib" in configdir
    assert localappdata in configdir or "AppData" in configdir


Review comment (suggestion):
```suggestion
    localappdata = tmp_path / "AppData/Local"
    localappdata.makedirs()
```
Slight preference for pathlib. Also, since this is a tmp_path, the directory should not exist, so no need for `extist_ok=True`.

Code after:
        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()