(monkeypatch, tmp_path: Path)
| 2222 | |
| 2223 | |
| 2224 | def test_try_makedirs(monkeypatch, tmp_path: Path) -> None: |
| 2225 | from _pytest.assertion.rewrite import try_makedirs |
| 2226 | |
| 2227 | p = tmp_path / "foo" |
| 2228 | |
| 2229 | # create |
| 2230 | assert try_makedirs(p) |
| 2231 | assert p.is_dir() |
| 2232 | |
| 2233 | # already exist |
| 2234 | assert try_makedirs(p) |
| 2235 | |
| 2236 | # monkeypatch to simulate all error situations |
| 2237 | def fake_mkdir(p, exist_ok=False, *, exc): |
| 2238 | assert isinstance(p, Path) |
| 2239 | raise exc |
| 2240 | |
| 2241 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=FileNotFoundError())) |
| 2242 | assert not try_makedirs(p) |
| 2243 | |
| 2244 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=NotADirectoryError())) |
| 2245 | assert not try_makedirs(p) |
| 2246 | |
| 2247 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=PermissionError())) |
| 2248 | assert not try_makedirs(p) |
| 2249 | |
| 2250 | err = OSError() |
| 2251 | err.errno = errno.EROFS |
| 2252 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=err)) |
| 2253 | assert not try_makedirs(p) |
| 2254 | |
| 2255 | err = OSError() |
| 2256 | err.errno = errno.ENOSYS |
| 2257 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=err)) |
| 2258 | assert not try_makedirs(p) |
| 2259 | |
| 2260 | # unhandled OSError should raise |
| 2261 | err = OSError() |
| 2262 | err.errno = errno.ECHILD |
| 2263 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=err)) |
| 2264 | with pytest.raises(OSError) as exc_info: |
| 2265 | try_makedirs(p) |
| 2266 | assert exc_info.value.errno == errno.ECHILD |
| 2267 | |
| 2268 | |
| 2269 | class TestPyCacheDir: |
nothing calls this directly
no test coverage detected