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

Code before:
    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):
can't we explicitly construct the expected configdir?
Something like `assert configdir = str(localappdata / "matplotlib")`

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