From 14360fcb49a93a6b63e94df0525002107855e298 Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Sat, 2 Mar 2024 21:19:04 +0100 Subject: [PATCH] test single_blank_line_after_doc_string() with module docstring and longer comment in test notebooks --- pyproject.toml | 1 + tests/fixtures/clean_nb.py | 3 +++ tests/fixtures/raw_nb.py | 6 ++++++ tests/test_main.py | 15 ++++++++++++--- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9472756..9c0d219 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,6 +54,7 @@ no_implicit_optional = false target-version = "py310" select = ["ALL"] ignore = [ + "COM812", # trailing comma missing "D100", # Missing docstring in public module "D104", # Missing docstring in public package "D205", # 1 blank line required between summary line and description diff --git a/tests/fixtures/clean_nb.py b/tests/fixtures/clean_nb.py index ea45f89..72e6cc6 100644 --- a/tests/fixtures/clean_nb.py +++ b/tests/fixtures/clean_nb.py @@ -1,3 +1,5 @@ +"""module doc string.""" + # %% foo = "hello" @@ -9,6 +11,7 @@ # %% a comment following a cell delimiter with too much space baz = 42 +"""longer comment""" # %% empty cell with comment (should not be removed despite empty) diff --git a/tests/fixtures/raw_nb.py b/tests/fixtures/raw_nb.py index 3d618f1..23ec433 100644 --- a/tests/fixtures/raw_nb.py +++ b/tests/fixtures/raw_nb.py @@ -1,3 +1,6 @@ +"""module doc string.""" + + # %% foo = "hello" # %%a comment following a cell delimiter without space @@ -5,7 +8,10 @@ # %% a comment following a cell delimiter with too much space baz = 42 +"""longer comment""" + + # %% empty cell with comment (should not be removed despite empty) diff --git a/tests/test_main.py b/tests/test_main.py index 1e27a73..37e40ad 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,3 +1,4 @@ +import difflib import filecmp import shutil from importlib.metadata import version @@ -15,10 +16,18 @@ def test_main_format_cells(tmp_path: Path, capsys: pytest.CaptureFixture[str]) - shutil.copy2(clean_nb, clean_tmp := str(tmp_path / "clean_nb.py")) ret = main((raw_tmp, clean_tmp)) - assert ret == 1 - assert filecmp.cmp(raw_tmp, clean_nb), "Formatted file has unexpected content" - assert filecmp.cmp(clean_tmp, clean_nb), "clean file should not have changed" + + with open(clean_nb) as file, open(clean_tmp) as tmp_file: + clean_lines, tmp_lines = file.readlines(), tmp_file.readlines() + file_diff = "\n".join(difflib.unified_diff(clean_lines, tmp_lines)) + + assert filecmp.cmp( + raw_tmp, clean_nb, shallow=False + ), f"Formatted file has unexpected content:\n{file_diff}" + assert filecmp.cmp( + clean_tmp, clean_nb, shallow=False + ), "clean file should not have changed" out, err = capsys.readouterr() assert out == f"Rewriting {raw_tmp}\n"