Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tests/test_check_em_dash.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""The em-dash check scans every tracked prose-like file type, not only Markdown and code (#1020)."""

import importlib
import subprocess

import pytest

check_em_dash = importlib.import_module("scripts.check_em_dash")

Expand All @@ -15,3 +18,54 @@ def test_tracked_notebooks_and_requirements_are_scanned_and_clean():
assert "requirements.txt" in scanned
assert any(path.endswith(".ipynb") for path in scanned)
assert check_em_dash.main() == 0


@pytest.fixture
def temp_git_repo(tmp_path, monkeypatch):
subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True)
monkeypatch.chdir(tmp_path)
return tmp_path


def test_tracked_em_dash_reports_path_line_and_ignores_untracked_file(
temp_git_repo, capsys
):
tracked = temp_git_repo / "notes.md"
tracked.write_text("clean line\nfound an \u2014 em dash\n", encoding="utf-8")
(temp_git_repo / "untracked.md").write_text(
"untracked \u2014 text\n", encoding="utf-8"
)
subprocess.run(["git", "add", "notes.md"], cwd=temp_git_repo, check=True)

assert check_em_dash.main() == 1

output = capsys.readouterr().err
assert "notes.md:2:10: found an \u2014 em dash" in output
assert "1 em dash(es) across 1 file(s)." in output
assert "untracked.md" not in output


def test_allowlisted_tracked_path_passes(temp_git_repo, capsys):
(temp_git_repo / "CONTRIBUTING.md").write_text(
"rule example: \u2014\n", encoding="utf-8"
)
subprocess.run(["git", "add", "CONTRIBUTING.md"], cwd=temp_git_repo, check=True)

assert list(check_em_dash._tracked_files()) == []
assert check_em_dash.main() == 0
assert (
capsys.readouterr().out == "OK: no em dashes in tracked source/prose files.\n"
)


def test_tracked_binary_file_is_skipped(temp_git_repo, capsys):
(temp_git_repo / "binary.md").write_bytes(
b"binary with em dash: \xe2\x80\x94 and invalid byte: \xff\n"
)
subprocess.run(["git", "add", "binary.md"], cwd=temp_git_repo, check=True)

assert list(check_em_dash._tracked_files()) == ["binary.md"]
assert check_em_dash.main() == 0
assert (
capsys.readouterr().out == "OK: no em dashes in tracked source/prose files.\n"
)
Loading