Test encoding handling.
(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
)
| 546 | |
| 547 | |
| 548 | def test_encoding( |
| 549 | tmp_path: Path, |
| 550 | capsys: pytest.CaptureFixture[str], |
| 551 | ) -> None: |
| 552 | """Test encoding handling.""" |
| 553 | # Some simple Unicode things |
| 554 | fname = tmp_path / "tmp" |
| 555 | fname.touch() |
| 556 | # with CaptureStdout() as sio: |
| 557 | assert cs.main(fname) == 0 |
| 558 | fname.write_bytes("naïve\n".encode()) |
| 559 | assert cs.main(fname) == 0 |
| 560 | assert cs.main("-e", fname) == 0 |
| 561 | with fname.open("ab") as f: |
| 562 | f.write(b"naieve\n") |
| 563 | assert cs.main(fname) == 1 |
| 564 | # Encoding detection (only try ISO 8859-1 because UTF-8 is the default) |
| 565 | fname.write_bytes(b"Speling error, non-ASCII: h\xe9t\xe9rog\xe9n\xe9it\xe9\n") |
| 566 | # check warnings about wrong encoding are enabled with "-q 0" |
| 567 | result = cs.main("-q", "0", fname, std=True, count=True) |
| 568 | assert isinstance(result, tuple) |
| 569 | code, stdout, stderr = result |
| 570 | assert code == 1 |
| 571 | assert "Speling" in stdout |
| 572 | assert "iso-8859-1" in stderr |
| 573 | # check warnings about wrong encoding are disabled with "-q 1" |
| 574 | result = cs.main("-q", "1", fname, std=True, count=True) |
| 575 | assert isinstance(result, tuple) |
| 576 | code, stdout, stderr = result |
| 577 | assert code == 1 |
| 578 | assert "Speling" in stdout |
| 579 | assert "iso-8859-1" not in stderr |
| 580 | # Binary file warning |
| 581 | fname.write_bytes(b"\x00\x00naiive\x00\x00") |
| 582 | result = cs.main(fname, std=True, count=False) |
| 583 | assert isinstance(result, tuple) |
| 584 | code, stdout, stderr = result |
| 585 | assert code == 0 |
| 586 | assert not stdout |
| 587 | assert not stderr |
| 588 | result = cs.main("-q", "0", fname, std=True, count=False) |
| 589 | assert isinstance(result, tuple) |
| 590 | code, stdout, stderr = result |
| 591 | assert code == 0 |
| 592 | assert not stdout |
| 593 | assert "WARNING: Binary file" in stderr |
| 594 | |
| 595 | |
| 596 | def test_unknown_encoding_chardet( |