|
22 | 22 | from _pytest.pathlib import on_rm_rf_error |
23 | 23 | from _pytest.pathlib import register_cleanup_lock_removal |
24 | 24 | from _pytest.pathlib import rm_rf |
| 25 | +from _pytest.pathlib import symlink_or_skip |
25 | 26 | from _pytest.pytester import Pytester |
26 | 27 | from _pytest.tmpdir import get_user |
27 | 28 | from _pytest.tmpdir import TempPathFactory |
@@ -70,6 +71,111 @@ def test_mktemp(self, tmp_path: Path) -> None: |
70 | 71 | assert str(tmp2.relative_to(t.getbasetemp())).startswith("this") |
71 | 72 | assert tmp2 != tmp |
72 | 73 |
|
| 74 | + def test_mktemp_does_not_list_basetemp( |
| 75 | + self, tmp_path: Path, monkeypatch: MonkeyPatch |
| 76 | + ) -> None: |
| 77 | + """Mktemp counts per prefix instead of listing basetemp (#15254).""" |
| 78 | + config = cast(Config, FakeConfig(tmp_path)) |
| 79 | + t = TempPathFactory.from_config(config, _ispytest=True) |
| 80 | + (t.getbasetemp() / "world1").mkdir() |
| 81 | + # Listing basetemp would fail. |
| 82 | + monkeypatch.setattr("_pytest.tmpdir.find_suffixes", None) |
| 83 | + names = [t.mktemp("world").name for _ in range(3)] |
| 84 | + # The existing "world1" is skipped. |
| 85 | + assert names == ["world0", "world2", "world3"] |
| 86 | + |
| 87 | + @pytest.mark.parametrize("kind", ["dir", "file", "dangling_symlink"]) |
| 88 | + def test_counted_dir_skips_unexpected_entries( |
| 89 | + self, tmp_path: Path, kind: str |
| 90 | + ) -> None: |
| 91 | + """Something else taking the next name is skipped (#15254).""" |
| 92 | + config = cast(Config, FakeConfig(tmp_path)) |
| 93 | + t = TempPathFactory.from_config(config, _ispytest=True) |
| 94 | + first = t._make_counted_dir("test_a") |
| 95 | + taken = t.getbasetemp() / "test_a1" |
| 96 | + if kind == "dir": |
| 97 | + taken.mkdir() |
| 98 | + elif kind == "file": |
| 99 | + taken.touch() |
| 100 | + else: |
| 101 | + symlink_or_skip(t.getbasetemp() / "missing", taken) |
| 102 | + names = [t._make_counted_dir("test_a").name for _ in range(2)] |
| 103 | + assert first.name == "test_a0" |
| 104 | + assert names == ["test_a2", "test_a3"] |
| 105 | + |
| 106 | + def test_counted_dir_jumps_past_long_run( |
| 107 | + self, tmp_path: Path, monkeypatch: MonkeyPatch |
| 108 | + ) -> None: |
| 109 | + """A long run of taken names is skipped with one listing (#15254).""" |
| 110 | + config = cast(Config, FakeConfig(tmp_path)) |
| 111 | + t = TempPathFactory.from_config(config, _ispytest=True) |
| 112 | + for i in range(50): |
| 113 | + (t.getbasetemp() / f"test_a{i}").mkdir() |
| 114 | + listed: list[str] = [] |
| 115 | + orig = pathlib.find_suffixes |
| 116 | + |
| 117 | + def find_suffixes(root: Path, prefix: str): |
| 118 | + listed.append(prefix) |
| 119 | + return orig(root, prefix) |
| 120 | + |
| 121 | + monkeypatch.setattr("_pytest.tmpdir.find_suffixes", find_suffixes) |
| 122 | + names = [t._make_counted_dir("test_a").name for _ in range(2)] |
| 123 | + assert names == ["test_a50", "test_a51"] |
| 124 | + assert listed == ["test_a"] |
| 125 | + |
| 126 | + def test_counted_dir_overlapping_prefixes(self, tmp_path: Path) -> None: |
| 127 | + """Prefixes "test_a" and "test_a1" share names like "test_a10" (#15254).""" |
| 128 | + config = cast(Config, FakeConfig(tmp_path)) |
| 129 | + t = TempPathFactory.from_config(config, _ispytest=True) |
| 130 | + names = [ |
| 131 | + t._make_counted_dir(prefix).name |
| 132 | + for _ in range(30) |
| 133 | + for prefix in ("test_a", "test_a1") |
| 134 | + ] |
| 135 | + assert len(set(names)) == len(names) |
| 136 | + |
| 137 | + def test_counted_dir_gives_up( |
| 138 | + self, tmp_path: Path, monkeypatch: MonkeyPatch |
| 139 | + ) -> None: |
| 140 | + """A filesystem that reports every name as taken raises instead of |
| 141 | + looping forever (#15254).""" |
| 142 | + config = cast(Config, FakeConfig(tmp_path)) |
| 143 | + t = TempPathFactory.from_config(config, _ispytest=True) |
| 144 | + t.getbasetemp() |
| 145 | + |
| 146 | + def mkdir(self: Path, mode: int = 0o777) -> None: |
| 147 | + raise FileExistsError(self) |
| 148 | + |
| 149 | + monkeypatch.setattr(Path, "mkdir", mkdir) |
| 150 | + with pytest.raises(OSError, match="after 20 tries"): |
| 151 | + t._make_counted_dir("test_a") |
| 152 | + |
| 153 | + def test_counted_dir_retries_other_errors( |
| 154 | + self, tmp_path: Path, monkeypatch: MonkeyPatch |
| 155 | + ) -> None: |
| 156 | + """Other OSErrors are retried too, e.g. PermissionError on Windows for a |
| 157 | + name that is still being deleted (#15254).""" |
| 158 | + config = cast(Config, FakeConfig(tmp_path)) |
| 159 | + t = TempPathFactory.from_config(config, _ispytest=True) |
| 160 | + t.getbasetemp() |
| 161 | + orig_mkdir = Path.mkdir |
| 162 | + |
| 163 | + def mkdir(self: Path, mode: int = 0o777) -> None: |
| 164 | + if self.name == "test_a0": |
| 165 | + raise PermissionError(self) |
| 166 | + orig_mkdir(self, mode) |
| 167 | + |
| 168 | + monkeypatch.setattr(Path, "mkdir", mkdir) |
| 169 | + assert t._make_counted_dir("test_a").name == "test_a1" |
| 170 | + |
| 171 | + def always_fail(self: Path, mode: int = 0o777) -> None: |
| 172 | + raise PermissionError(self) |
| 173 | + |
| 174 | + monkeypatch.setattr(Path, "mkdir", always_fail) |
| 175 | + with pytest.raises(OSError, match="after 20 tries") as excinfo: |
| 176 | + t._make_counted_dir("test_b") |
| 177 | + assert isinstance(excinfo.value.__cause__, PermissionError) |
| 178 | + |
73 | 179 | def test_tmppath_relative_basetemp_absolute( |
74 | 180 | self, tmp_path: Path, monkeypatch: MonkeyPatch |
75 | 181 | ) -> None: |
|
0 commit comments