MCPcopy
hub / github.com/pytest-dev/pytest / test_try_makedirs

Function test_try_makedirs

testing/test_assertrewrite.py:2224–2266  ·  view source on GitHub ↗
(monkeypatch, tmp_path: Path)

Source from the content-addressed store, hash-verified

2222
2223
2224def 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
2269class TestPyCacheDir:

Callers

nothing calls this directly

Calls 2

try_makedirsFunction · 0.90
setattrMethod · 0.80

Tested by

no test coverage detected