(monkeypatch, tmp_path: Path)
| 1690 | |
| 1691 | |
| 1692 | def test_try_makedirs(monkeypatch, tmp_path: Path) -> None: |
| 1693 | from _pytest.assertion.rewrite import try_makedirs |
| 1694 | |
| 1695 | p = tmp_path / "foo" |
| 1696 | |
| 1697 | # create |
| 1698 | assert try_makedirs(p) |
| 1699 | assert p.is_dir() |
| 1700 | |
| 1701 | # already exist |
| 1702 | assert try_makedirs(p) |
| 1703 | |
| 1704 | # monkeypatch to simulate all error situations |
| 1705 | def fake_mkdir(p, exist_ok=False, *, exc): |
| 1706 | assert isinstance(p, Path) |
| 1707 | raise exc |
| 1708 | |
| 1709 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=FileNotFoundError())) |
| 1710 | assert not try_makedirs(p) |
| 1711 | |
| 1712 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=NotADirectoryError())) |
| 1713 | assert not try_makedirs(p) |
| 1714 | |
| 1715 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=PermissionError())) |
| 1716 | assert not try_makedirs(p) |
| 1717 | |
| 1718 | err = OSError() |
| 1719 | err.errno = errno.EROFS |
| 1720 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=err)) |
| 1721 | assert not try_makedirs(p) |
| 1722 | |
| 1723 | # unhandled OSError should raise |
| 1724 | err = OSError() |
| 1725 | err.errno = errno.ECHILD |
| 1726 | monkeypatch.setattr(os, "makedirs", partial(fake_mkdir, exc=err)) |
| 1727 | with pytest.raises(OSError) as exc_info: |
| 1728 | try_makedirs(p) |
| 1729 | assert exc_info.value.errno == errno.ECHILD |
| 1730 | |
| 1731 | |
| 1732 | class TestPyCacheDir: |
nothing calls this directly
no test coverage detected