Add test to cover when unable to replace magics (#2471)

Another follow-up from #2357, adding a test for uncovered code.
This commit is contained in:
Marco Edward Gorelli 2021-09-25 20:46:36 +01:00 committed by GitHub
parent 1af5331089
commit 39b55f787c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View File

@ -53,6 +53,7 @@
"%%writefile",
)
)
TOKEN_HEX = secrets.token_hex
@dataclasses.dataclass(frozen=True)
@ -188,10 +189,10 @@ def get_token(src: str, magic: str) -> str:
"""
assert magic
nbytes = max(len(magic) // 2 - 1, 1)
token = secrets.token_hex(nbytes)
token = TOKEN_HEX(nbytes)
counter = 0
while token in src: # pragma: nocover
token = secrets.token_hex(nbytes)
while token in src:
token = TOKEN_HEX(nbytes)
counter += 1
if counter > 100:
raise AssertionError(

View File

@ -453,3 +453,12 @@ def test_ipynb_and_pyi_flags() -> None:
assert isinstance(result.exception, SystemExit)
expected = "Cannot pass both `pyi` and `ipynb` flags!\n"
assert result.output == expected
def test_unable_to_replace_magics(monkeypatch: MonkeyPatch) -> None:
src = "%%time\na = 'foo'"
monkeypatch.setattr("black.handle_ipynb_magics.TOKEN_HEX", lambda _: "foo")
with pytest.raises(
AssertionError, match="Black was not able to replace IPython magic"
):
format_cell(src, fast=True, mode=JUPYTER_MODE)