Skip to content

Commit

Permalink
Merge pull request #51 from frenzymadness/path
Browse files Browse the repository at this point in the history
Drop dependency on `py` and simplify tests
  • Loading branch information
murilo-cunha authored Nov 5, 2022
2 parents d39d3c2 + db162c9 commit a7ead55
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 360 deletions.
510 changes: 197 additions & 313 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typing-extensions = "^4.0.1"

[tool.poetry.dev-dependencies]
pre-commit = "^2.17.0"
pytest = "^6.2.4"
pytest = "^7.2.0"
pytest-cov = "^3.0.0"
mike = "^1.1.2"
mkdocstrings = "^0.17.0"
Expand Down
47 changes: 24 additions & 23 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from textwrap import dedent

from _pytest.logging import LogCaptureFixture
from py._path.local import LocalPath
from typer import Context
from typer.core import TyperCommand
from typer.testing import CliRunner
Expand Down Expand Up @@ -38,9 +37,9 @@ def test_config_callback() -> None:
assert parsed_config == conf


def test_meta(tmpdir: LocalPath) -> None:
def test_meta(tmp_path: Path) -> None:
"""Remove notebook metadata."""
read_path = tmpdir.mkdir("notebooks") / "test_meta_nb.ipynb" # type: ignore
read_path = tmp_path / "test_meta_nb.ipynb" # type: ignore
TestJupyterNotebook().jupyter_notebook.write(read_path)

nb_read = JupyterNotebook.parse_file(path=read_path)
Expand All @@ -67,11 +66,11 @@ def test_meta(tmpdir: LocalPath) -> None:
)


def test_meta__check(tmpdir: LocalPath, caplog: LogCaptureFixture) -> None:
def test_meta__check(tmp_path: Path, caplog: LogCaptureFixture) -> None:
"""Report on existing notebook metadata (both when it is and isn't present)."""
caplog.set_level(logging.INFO)

read_path = tmpdir.mkdir("notebooks") / "test_meta_nb.ipynb" # type: ignore
read_path = tmp_path / "test_meta_nb.ipynb" # type: ignore
TestJupyterNotebook().jupyter_notebook.write(read_path)

nb_read = JupyterNotebook.parse_file(path=read_path)
Expand All @@ -94,9 +93,9 @@ def test_meta__check(tmpdir: LocalPath, caplog: LogCaptureFixture) -> None:
assert logs[-1].message == "No unwanted metadata!"


def test_meta__config(tmpdir: LocalPath) -> None:
def test_meta__config(tmp_path: Path) -> None:
"""Check notebook metadata with configuration overriding defaults."""
read_path = tmpdir.mkdir("notebooks") / "test_meta_nb.ipynb" # type: ignore
read_path = tmp_path / "test_meta_nb.ipynb" # type: ignore
TestJupyterNotebook().jupyter_notebook.write(read_path)

nb_read = JupyterNotebook.parse_file(path=read_path)
Expand Down Expand Up @@ -130,9 +129,9 @@ def test_meta__config(tmpdir: LocalPath) -> None:
assert all(c.execution_count is None for c in nb_write.cells)


def test_meta__script(tmpdir: LocalPath) -> None:
def test_meta__script(tmp_path: Path) -> None:
"""Raise `typer.BadParameter` when passing a script instead of a notebook."""
py_path = tmpdir.mkdir("files") / "a_script.py" # type: ignore
py_path = tmp_path / "a_script.py" # type: ignore
py_path.write_text("# some python code", encoding="utf-8")

result = runner.invoke(app, ["meta", str(py_path)])
Expand All @@ -143,9 +142,9 @@ def test_meta__script(tmpdir: LocalPath) -> None:
)


def test_meta__no_confirm(tmpdir: LocalPath) -> None:
def test_meta__no_confirm(tmp_path: Path) -> None:
"""Don't make any changes without confirmation to overwrite files (prompt)."""
nb_path = tmpdir.mkdir("notebooks") / "test_meta_nb.ipynb" # type: ignore
nb_path = tmp_path / "test_meta_nb.ipynb" # type: ignore
TestJupyterNotebook().jupyter_notebook.write(nb_path)

result = runner.invoke(app, ["meta", str(nb_path)])
Expand All @@ -158,9 +157,9 @@ def test_meta__no_confirm(tmpdir: LocalPath) -> None:
)


def test_meta__confirm(tmpdir: LocalPath) -> None:
def test_meta__confirm(tmp_path: Path) -> None:
"""Make changes when confirming overwrite via the prompt."""
nb_path = tmpdir.mkdir("notebooks") / "test_meta_nb.ipynb" # type: ignore
nb_path = tmp_path / "test_meta_nb.ipynb" # type: ignore
TestJupyterNotebook().jupyter_notebook.write(nb_path)

result = runner.invoke(app, ["meta", str(nb_path)], input="y")
Expand All @@ -174,10 +173,10 @@ def test_meta__confirm(tmpdir: LocalPath) -> None:
)


def test_meta__no_notebooks_found(tmpdir: LocalPath, caplog: LogCaptureFixture) -> None:
def test_meta__no_notebooks_found(tmp_path: Path, caplog: LogCaptureFixture) -> None:
"""Log that no notebook was found in the paths passed."""
caplog.set_level(logging.INFO)
nb_path = tmpdir.mkdir("notebooks") / "inexistent_nb.ipynb" # type: ignore
nb_path = tmp_path / "inexistent_nb.ipynb" # type: ignore

result = runner.invoke(app, ["meta", str(nb_path), "--check"])
logs = list(caplog.records)
Expand Down Expand Up @@ -226,7 +225,7 @@ def test_assert__config(caplog: LogCaptureFixture) -> None:
)


def test_fix(tmpdir: LocalPath) -> None:
def test_fix(tmp_path: Path) -> None:
"""Fix notebook conflicts."""
# Setup
nb_path = Path("test_conflicts_nb.ipynb")
Expand All @@ -251,7 +250,7 @@ def test_fix(tmpdir: LocalPath) -> None:
notebook_2.nbformat_minor += 1

git_repo = init_repo_conflicts(
tmpdir=tmpdir,
tmp_path=tmp_path,
filename=nb_path,
contents_main=notebook_1.json(),
contents_other=notebook_2.json(),
Expand All @@ -264,8 +263,8 @@ def test_fix(tmpdir: LocalPath) -> None:
id_other = conflict_files[0].last_log

# Run CLI and check conflict resolution
result = runner.invoke(app, ["fix", str(tmpdir)])
fixed_notebook = JupyterNotebook.parse_file(path=tmpdir / nb_path)
result = runner.invoke(app, ["fix", str(tmp_path)])
fixed_notebook = JupyterNotebook.parse_file(path=tmp_path / nb_path)

assert len(conflict_files) == 1
assert result.exit_code == 0
Expand Down Expand Up @@ -300,7 +299,7 @@ def test_fix(tmpdir: LocalPath) -> None:
]


def test_fix__config(tmpdir: LocalPath) -> None:
def test_fix__config(tmp_path: Path) -> None:
"""Fix notebook conflicts with configuration overriding defaults."""
# Setup
nb_path = Path("test_conflicts_nb.ipynb")
Expand All @@ -325,7 +324,7 @@ def test_fix__config(tmpdir: LocalPath) -> None:
notebook_2.nbformat_minor += 1

git_repo = init_repo_conflicts(
tmpdir=tmpdir,
tmp_path=tmp_path,
filename=nb_path,
contents_main=notebook_1.json(),
contents_other=notebook_2.json(),
Expand All @@ -339,8 +338,10 @@ def test_fix__config(tmpdir: LocalPath) -> None:

with resources.path("tests.files", "pyproject.toml") as config_path:
# Run CLI and check conflict resolution
result = runner.invoke(app, ["fix", str(tmpdir), "--config", str(config_path)])
fixed_notebook = JupyterNotebook.parse_file(path=tmpdir / nb_path)
result = runner.invoke(
app, ["fix", str(tmp_path), "--config", str(config_path)]
)
fixed_notebook = JupyterNotebook.parse_file(path=tmp_path / nb_path)

assert len(conflict_files) == 1
assert result.exit_code == 0
Expand Down
10 changes: 4 additions & 6 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from pathlib import Path

from py._path.local import LocalPath

from databooks.common import find_obj


def test_find_obj(tmpdir: LocalPath) -> None:
def test_find_obj(tmp_path: Path) -> None:
"""Find file based on name, and search path."""
filename = "SAMPLE_FILE.ext"

start_dir = Path(tmpdir)
start_dir = tmp_path
end_dir = start_dir / "to" / "some" / "dir"
end_dir.mkdir(parents=True)
(start_dir / "to" / filename).touch()
Expand All @@ -19,11 +17,11 @@ def test_find_obj(tmpdir: LocalPath) -> None:
assert filepath.is_file()


def test_find_obj__missing(tmpdir: LocalPath) -> None:
def test_find_obj__missing(tmp_path: Path) -> None:
"""Return `None` when looking for file along path."""
filename = "SAMPLE_FILE.ext"

start_dir = Path(tmpdir)
start_dir = tmp_path
end_dir = start_dir / "to" / "some" / "dir"
end_dir.mkdir(parents=True)

Expand Down
6 changes: 2 additions & 4 deletions tests/test_conflicts.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from pathlib import Path

from py._path.local import LocalPath

from databooks.conflicts import path2conflicts
from databooks.data_models.cell import BaseCell, CellMetadata
from databooks.data_models.notebook import NotebookMetadata
from tests.test_data_models.test_notebook import TestJupyterNotebook
from tests.test_git_utils import ConflictFile, init_repo_conflicts


def test_path2diff(tmpdir: LocalPath) -> None:
def test_path2diff(tmp_path: Path) -> None:
"""Return a DiffFile based on a path and git conflicts."""
notebook_main = TestJupyterNotebook().jupyter_notebook
notebook_other = TestJupyterNotebook().jupyter_notebook
Expand All @@ -31,7 +29,7 @@ def test_path2diff(tmpdir: LocalPath) -> None:
nb_filepath = Path("test_notebook.ipynb")

git_repo = init_repo_conflicts(
tmpdir=tmpdir,
tmp_path=tmp_path,
filename=nb_filepath,
contents_main=notebook_main.json(),
contents_other=notebook_other.json(),
Expand Down
6 changes: 3 additions & 3 deletions tests/test_data_models/test_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import logging
from copy import deepcopy
from importlib import resources
from pathlib import Path
from typing import List, Tuple

import pytest
from _pytest.logging import LogCaptureFixture
from py._path.local import LocalPath

from databooks.data_models.cell import (
CellMetadata,
Expand Down Expand Up @@ -369,9 +369,9 @@ def test_parse_file() -> None:
)


def test_write_file(tmpdir: LocalPath) -> None:
def test_write_file(tmp_path: Path) -> None:
"""Check that serialization and deserialization are valid."""
write_path = tmpdir / "serialized_demo.ipynb"
write_path = tmp_path / "serialized_demo.ipynb"
with resources.path("tests.files", "demo.ipynb") as nb_path:
notebook = JupyterNotebook.parse_file(nb_path)
in_json_str = nb_path.read_text(encoding="utf-8")
Expand Down
9 changes: 4 additions & 5 deletions tests/test_git_utils.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
from pathlib import Path

from git import GitCommandError, Repo
from py._path.local import LocalPath
from pytest import raises

from databooks.git_utils import ConflictFile, get_conflict_blobs, get_repo


def init_repo_conflicts(
tmpdir: LocalPath,
tmp_path: Path,
filename: Path,
contents_main: str,
contents_other: str,
commit_message_main: str,
commit_message_other: str,
) -> Repo:
"""Create git repo and create a conflict."""
git_repo = Repo.init(path=tmpdir)
git_repo = Repo.init(path=tmp_path)

if not isinstance(git_repo.working_dir, (Path, str)):
raise RuntimeError(
Expand Down Expand Up @@ -57,11 +56,11 @@ def test_get_repo() -> None:
assert Path(repo.working_dir).stem == "databooks"


def test_get_conflict_blobs(tmpdir: LocalPath) -> None:
def test_get_conflict_blobs(tmp_path: Path) -> None:
"""Return `databooks.git_utils.ConflctFile` from git merge conflict."""
filepath = Path("hello.txt")
git_repo = init_repo_conflicts(
tmpdir=tmpdir,
tmp_path=tmp_path,
filename=filepath,
contents_main="HELLO EVERYONE!",
contents_other="hello world",
Expand Down
9 changes: 4 additions & 5 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pathlib import Path

from _pytest.logging import LogCaptureFixture
from py._path.local import LocalPath

from databooks.data_models.cell import CellMetadata, CellOutputs
from databooks.data_models.notebook import JupyterNotebook
Expand All @@ -11,11 +10,11 @@


def test_metadata_clear__check_verbose(
tmpdir: LocalPath, caplog: LogCaptureFixture
tmp_path: Path, caplog: LogCaptureFixture
) -> None:
"""Clear metadata from a notebook and write clean notebook."""
caplog.set_level(logging.DEBUG)
read_path = Path(tmpdir.mkdir("notebooks") / "test_nb.ipynb") # type: ignore
read_path = tmp_path / "test_nb.ipynb" # type: ignore
TestJupyterNotebook().jupyter_notebook.write(read_path)
write_path = read_path.parent / ("clean_" + read_path.name)

Expand All @@ -40,9 +39,9 @@ def test_metadata_clear__check_verbose(
)


def test_metadata_clear(tmpdir: LocalPath) -> None:
def test_metadata_clear(tmp_path: Path) -> None:
"""Clear metadata from a notebook and write clean notebook."""
read_path = Path(tmpdir.mkdir("notebooks") / "test_nb.ipynb") # type: ignore
read_path = tmp_path / "test_nb.ipynb" # type: ignore
TestJupyterNotebook().jupyter_notebook.write(read_path)
write_path = read_path.parent / ("clean_" + read_path.name)

Expand Down

0 comments on commit a7ead55

Please sign in to comment.