|
9 | 9 | from typing import List
|
10 | 10 |
|
11 | 11 | from _pytest.assertion.util import running_on_ci
|
| 12 | +from _pytest.compat import ensure_long_path |
12 | 13 | from _pytest.config import ExitCode
|
13 | 14 | from _pytest.fixtures import FixtureRequest
|
14 | 15 | from _pytest.main import _in_venv
|
@@ -1746,27 +1747,43 @@ def test_foo(): assert True
|
1746 | 1747 | assert result.parseoutcomes() == {"passed": 1}
|
1747 | 1748 |
|
1748 | 1749 |
|
1749 |
| -@pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only") |
1750 |
| -def test_collect_short_file_windows(pytester: Pytester) -> None: |
1751 |
| - """Reproducer for #11895: short paths not colleced on Windows.""" |
1752 |
| - short_path = tempfile.mkdtemp() |
1753 |
| - if "~" not in short_path: # pragma: no cover |
1754 |
| - if running_on_ci(): |
1755 |
| - # On CI, we are expecting that under the current GitHub actions configuration, |
1756 |
| - # tempfile.mkdtemp() is producing short paths, so we want to fail to prevent |
1757 |
| - # this from silently changing without us noticing. |
1758 |
| - pytest.fail( |
1759 |
| - f"tempfile.mkdtemp() failed to produce a short path on CI: {short_path}" |
1760 |
| - ) |
1761 |
| - else: |
1762 |
| - # We want to skip failing this test locally in this situation because |
1763 |
| - # depending on the local configuration tempfile.mkdtemp() might not produce a short path: |
1764 |
| - # For example, user might have configured %TEMP% exactly to avoid generating short paths. |
1765 |
| - pytest.skip( |
1766 |
| - f"tempfile.mkdtemp() failed to produce a short path: {short_path}, skipping" |
1767 |
| - ) |
1768 |
| - |
1769 |
| - test_file = Path(short_path).joinpath("test_collect_short_file_windows.py") |
1770 |
| - test_file.write_text("def test(): pass", encoding="UTF-8") |
1771 |
| - result = pytester.runpytest(short_path) |
1772 |
| - assert result.parseoutcomes() == {"passed": 1} |
| 1750 | +class TestCollectionShortPaths: |
| 1751 | + @pytest.fixture |
| 1752 | + def short_path(self) -> Path: |
| 1753 | + short_path = tempfile.mkdtemp() |
| 1754 | + if "~" not in short_path: # pragma: no cover |
| 1755 | + if running_on_ci(): |
| 1756 | + # On CI, we are expecting that under the current GitHub actions configuration, |
| 1757 | + # tempfile.mkdtemp() is producing short paths, so we want to fail to prevent |
| 1758 | + # this from silently changing without us noticing. |
| 1759 | + pytest.fail( |
| 1760 | + f"tempfile.mkdtemp() failed to produce a short path on CI: {short_path}" |
| 1761 | + ) |
| 1762 | + else: |
| 1763 | + # We want to skip failing this test locally in this situation because |
| 1764 | + # depending on the local configuration tempfile.mkdtemp() might not produce a short path: |
| 1765 | + # For example, user might have configured %TEMP% exactly to avoid generating short paths. |
| 1766 | + pytest.skip( |
| 1767 | + f"tempfile.mkdtemp() failed to produce a short path: {short_path}, skipping" |
| 1768 | + ) |
| 1769 | + return Path(short_path) |
| 1770 | + |
| 1771 | + @pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only") |
| 1772 | + def test_ensure_long_path_win(self, short_path: Path) -> None: |
| 1773 | + long_path = ensure_long_path(short_path) |
| 1774 | + assert len(os.fspath(long_path)) > len(os.fspath(short_path)) |
| 1775 | + |
| 1776 | + @pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only") |
| 1777 | + def test_collect_short_file_windows( |
| 1778 | + self, pytester: Pytester, short_path: Path |
| 1779 | + ) -> None: |
| 1780 | + """Reproducer for #11895: short paths not collected on Windows.""" |
| 1781 | + test_file = short_path.joinpath("test_collect_short_file_windows.py") |
| 1782 | + test_file.write_text("def test(): pass", encoding="UTF-8") |
| 1783 | + result = pytester.runpytest(short_path) |
| 1784 | + assert result.parseoutcomes() == {"passed": 1} |
| 1785 | + |
| 1786 | + def test_ensure_long_path_general(self, tmp_path: Path) -> None: |
| 1787 | + """Sanity check: a normal path to ensure_long_path works on all platforms.""" |
| 1788 | + assert ensure_long_path(tmp_path) == tmp_path |
| 1789 | + assert ensure_long_path(tmp_path / "non-existent") == tmp_path / "non-existent" |
0 commit comments