Skip to content

Commit

Permalink
type annotate tests, swap older pytest tmpdir fixture for tmp_path
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Mar 16, 2022
1 parent 24fd7e7 commit 926e264
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,3 @@ disallow_any_generics = true
disallow_incomplete_defs = true
warn_redundant_casts = true
warn_unused_ignores = true

[mypy-tests.*]
disallow_untyped_defs = false
12 changes: 6 additions & 6 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@


@pytest.mark.parametrize("raw_delim", ["# %%", "#%%"])
def test_format_cell_delimiters(raw_delim):
def test_format_cell_delimiters(raw_delim: str) -> None:

assert format_cell_delimiters(raw_delim) == "# %%"


@pytest.mark.parametrize("input", ["# %%some comment", "# %% some comment"])
def test_format_comments_after_cell_delimiters(input):
def test_format_comments_after_cell_delimiters(input: str) -> None:

assert format_comments_after_cell_delimiters(input) == "# %% some comment"


def test_remove_empty_cells():
def test_remove_empty_cells() -> None:
# single empty cell
out = remove_empty_cells("# %%\n\n# %%")
assert out == "# %%"
Expand Down Expand Up @@ -51,12 +51,12 @@ def test_remove_empty_cells():
"# %%\n\t\t\n \n \nfoo = 'bar'",
],
)
def test_remove_empty_lines_starting_cell(input):
def test_remove_empty_lines_starting_cell(input: str) -> None:

assert remove_empty_lines_starting_cell(input) == "# %%\nfoo = 'bar'"


def test_ensure_two_blank_lines_preceding_cell():
def test_ensure_two_blank_lines_preceding_cell() -> None:
# single preceding blank line
out = ensure_two_blank_lines_preceding_cell("\n# %%\n")
assert out == "\n\n\n# %%\n"
Expand All @@ -81,6 +81,6 @@ def test_ensure_two_blank_lines_preceding_cell():
"\n# %%\na = 5\n\n\n# %%\n\t \n\n \n",
],
)
def test_delete_last_cell_if_empty(input):
def test_delete_last_cell_if_empty(input: str) -> None:

assert delete_last_cell_if_empty(input) == "\n# %%\na = 5\n"
27 changes: 15 additions & 12 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import filecmp
import shutil
from importlib.metadata import version
from shutil import copy2
from pathlib import Path

import pytest
from pytest import CaptureFixture, MonkeyPatch

from format_ipy_cells.main import main


def test_main_format_cells(capsys, tmpdir):
def test_main_format_cells(
tmp_path: Path, capsys: CaptureFixture[str], monkeypatch: MonkeyPatch
) -> None:
shutil.copy2("tests/fixtures/raw_nb.py", raw_tmp := str(tmp_path / "raw_nb.py"))
# test we leave clean file as is and don't print logs about it
clean_nb = "tests/fixtures/clean_nb.py"
shutil.copy2(clean_nb, clean_tmp := str(tmp_path / "clean_nb.py"))

raw_file = copy2("tests/fixtures/raw_nb.py", tmpdir)
# test we leave clean file as is and don't write logs about it
clean_file = copy2(clean_nb, tmpdir)

ret = main((raw_file, clean_file))
ret = main((raw_tmp, clean_tmp))

assert ret == 1
assert filecmp.cmp(raw_file, clean_nb), "Formatted file has unexpected content"
assert filecmp.cmp(clean_file, clean_nb), "clean file should not have changed"
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"

out, err = capsys.readouterr()
assert out == f"Rewriting {raw_file}\n"
assert out == f"Rewriting {raw_tmp}\n"
assert err == ""

ret = main([clean_file])
ret = main([clean_tmp])
assert ret == 0, "expected exit code 0 when no files were changed"
out, err = capsys.readouterr()
assert out == err == ""


def test_main_print_version(capsys):
def test_main_print_version(capsys: CaptureFixture[str]) -> None:

with pytest.raises(SystemExit):
ret_val = main(["-v"])
Expand Down

0 comments on commit 926e264

Please sign in to comment.