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:
parent
1af5331089
commit
39b55f787c
@ -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(
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user