Skip to content

Commit

Permalink
Add app-level tests and fix fix command
Browse files Browse the repository at this point in the history
fix command was broken - somehow missed adding @file_options to
it...
  • Loading branch information
phyrwork committed Aug 29, 2024
1 parent f1e22d3 commit f766b1f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/copyright2/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def process(file: fs.File) -> None:

@main.command
@path_options
@file_options
def fix(
find_path_type: PathFinderType,
find_path_args: Tuple[str, ...],
Expand Down
86 changes: 86 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from collections.abc import Iterator
from contextlib import contextmanager
from pathlib import Path
from tempfile import TemporaryDirectory
from shutil import copytree

from click.testing import CliRunner
from pytest import MonkeyPatch, fixture

from copyright2.app import list, check, fix


__DIR__ = Path(__file__).parent


EXAMPLES = __DIR__.parent / "examples"


@contextmanager
def clone_example(monkeypatch: MonkeyPatch, name: str) -> Iterator[None]:
with TemporaryDirectory() as _dir:
dir = Path(_dir)
copytree(EXAMPLES / name, dir / name)
monkeypatch.chdir(dir / name)
yield


@fixture
def examples_readme(monkeypatch: MonkeyPatch) -> Iterator[None]:
with clone_example(monkeypatch, "readme"):
yield


@fixture
def examples_subdirs(monkeypatch: MonkeyPatch) -> Iterator[None]:
with clone_example(monkeypatch, "subdirs"):
yield


def test_list_readme(examples_readme: None) -> None:
result = CliRunner().invoke(list)

assert result.exit_code == 0

assert result.stdout.splitlines() == [
"README.md",
"src/readme.py",
"src/ext/readme.h",
"src/ext/readme.c",
"4",
]


def test_check_readme(examples_readme: None) -> None:
result = CliRunner().invoke(check)

assert result.exit_code != 0

assert result.stdout.splitlines() == [
"README.md: notice not found",
"src/ext/readme.h: 2: simplified timestamp expression",
"2",
]


def test_fix_readme(examples_readme: None) -> None:
result = CliRunner().invoke(fix)

assert result.exit_code != 0

assert result.stdout.splitlines() == [
"README.md: notice not found",
"fixing src/ext/readme.h... ok",
"1",
]


def test_list_subdirs(examples_subdirs: None) -> None:
result = CliRunner().invoke(list)

assert result.exit_code == 0

assert result.stdout.splitlines() == [
"a/b/included.py",
"1",
]

0 comments on commit f766b1f

Please sign in to comment.