Skip to content

Commit

Permalink
More tests (#48)
Browse files Browse the repository at this point in the history
* more tests

* 90%+ coverage

* specify cov-config
  • Loading branch information
jaykv authored Jan 24, 2025
1 parent b78dc14 commit 97ede5b
Show file tree
Hide file tree
Showing 18 changed files with 1,592 additions and 25 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ sync:
uv sync --all-extras --group dev --group docs

test:
uv run coverage run -m pytest -vv --capture=tee-sys -n auto
@uv run coverage report
uv run pytest --cov --cov-config=pyproject.toml -vv --capture=tee-sys -n auto

clean:
rm -rf build/ dist/ *.egg-info .*_cache test-builds test-manifest-builds
Expand Down
2 changes: 1 addition & 1 deletion cliffy/commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def build_groups(self) -> None:
group_name = command.name.split(".")[:-1][-1]

if "|" in command.name:
command_aliases = command.name.rsplit(".", 1)[1].split("|")
command_aliases = [s.strip() for s in command.name.rsplit(".", 1)[1].split("|")]
command_name_sub_alias = command.name.split("|", 1)[0]
for alias in command_aliases[1:]:
command.aliases.append(alias)
Expand Down
1 change: 1 addition & 0 deletions cliffy/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def delete_temp_files() -> None:
with contextlib.suppress(Exception):
file.close()
os.unlink(file.name)
TEMP_FILES.clear()


def indent_block(block: str, spaces: int = 4) -> str:
Expand Down
2 changes: 1 addition & 1 deletion cliffy/homer.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_metadata_bypath(path: Path) -> CLIMetadata:
except Exception as e:
return CLIMetadata(
cli_name=path.name,
runner_path=path.absolute().name,
runner_path=str(path.absolute()),
version="error",
loaded=datetime.now(),
manifest=f"could not load: {e}",
Expand Down
2 changes: 1 addition & 1 deletion cliffy/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ class IncludeManifest(BaseModel):
can be merged with other manifests.
"""

requires: list[str]
requires: list[str] = []
commands: dict[str, CommandBlock] = {}
vars: dict[str, VarBlock] = {}
imports: Union[str, list[str]] = []
Expand Down
18 changes: 11 additions & 7 deletions cliffy/reloader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .transformer import Transformer
from .loader import Loader
from .homer import save_metadata
from .helper import out
from .helper import out, out_err
from .builder import run_cli as cli_runner

import time
Expand All @@ -13,7 +13,7 @@


class Reloader(FileSystemEventHandler):
def __init__(self, manifest_path: str, run_cli: bool, run_cli_args: tuple[str]) -> None:
def __init__(self, manifest_path: str, run_cli: bool, run_cli_args: tuple) -> None:
self.manifest_path = manifest_path
self.run_cli = run_cli
self.run_cli_args = run_cli_args
Expand All @@ -34,11 +34,15 @@ def on_modified(self, event: FileSystemEvent) -> None:
self.last_modified = datetime.now()

t = threading.Thread(target=self.reload, args=(self.manifest_path, self.run_cli, self.run_cli_args))
t.daemon = True
t.start()
t.setDaemon(True)
try:
t.start()
except RuntimeError as e:
# Log the error or handle it as needed
out_err(f"Failed to start thread: {e}")

@classmethod
def watch(cls, manifest_path: str, run_cli: bool, run_cli_args: tuple[str]) -> None:
def watch(cls, manifest_path: str, run_cli: bool, run_cli_args: tuple) -> None:
event_handler = cls(manifest_path, run_cli, run_cli_args)
observer = Observer()
observer.schedule(event_handler, path=str(Path(manifest_path).parent), recursive=False)
Expand All @@ -52,12 +56,12 @@ def watch(cls, manifest_path: str, run_cli: bool, run_cli_args: tuple[str]) -> N
observer.join()

@staticmethod
def reload(manifest_path: str, run_cli: bool, run_cli_args: tuple[str]) -> None:
def reload(manifest_path: str, run_cli: bool, run_cli_args: tuple) -> None:
manifest_io = open(manifest_path, "r")

T = Transformer(manifest_io)
Loader.load_from_cli(T.cli)
save_metadata(manifest_io.name, T.cli)
save_metadata(manifest_path, T.cli)
out(f"✨ Reloaded {T.cli.name} CLI v{T.cli.version} ✨", fg="green")

if run_cli:
Expand Down
8 changes: 4 additions & 4 deletions cliffy/rich.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ def print(self, text: Any, **kwargs: Any) -> None:
print(text)

class Table: # type: ignore[no-redef]
__slots__ = ("cols", "rows", "styles")
__slots__ = ("columns", "rows", "styles")

def __init__(self) -> None:
self.cols: list[str] = []
self.columns: list[str] = []
self.rows: list[list[str]] = []
self.styles: list[str] = []

def add_column(self, col: str, style: str = "") -> None:
self.cols.append(col)
self.columns.append(col)
self.styles.append(style)

def add_row(self, *row: Any) -> None:
self.rows.append([*row])

def __str__(self) -> str:
text = "".join([click.style(f"{col:10}", fg=self.styles.pop(0)) for col in self.cols]) + "\n"
text = "".join([click.style(f"{col:10}", fg=self.styles.pop(0)) for col in self.columns]) + "\n"
for row in self.rows:
for col in row:
text += f"{col:10}"
Expand Down
8 changes: 8 additions & 0 deletions examples/nested.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# yaml-language-server: $schema=cliffy_schema.json

name: nestedcli
version: 0.1.0
commands:
group1.subgroup.command1: $ echo "hello"
group1.subgroup.command2: $ echo "world"
group2.command3: $ echo "foo"
12 changes: 7 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ dev = [
"pytest-xdist>=3.6.1",
"ruff>=0.9.1",
"tabulate>=0.9.0",
"coverage[toml]>=7.6.2"
"pytest-cov>=6.0.0",
"pytest-mock>=3.14.0",
]


Expand All @@ -58,7 +59,8 @@ dev = [
"pytest-xdist>=3.6.1",
"ruff>=0.9.1",
"tabulate>=0.9.0",
"coverage[toml]>=7.6.2"
"pytest-cov>=6.0.0",
"pytest-mock>=3.14.0",
]
docs = [
"mkdocs-git-committers-plugin-2>=2.4.1",
Expand Down Expand Up @@ -106,8 +108,8 @@ ignore_missing_imports = true

# https://coverage.readthedocs.io/en/latest/config.html#run
[tool.coverage.run]
include = ["cliffy/**/*.py"]
omit = []
include = ["cliffy/**"]
omit = ["cliffy/clis/**"]
branch = true

# https://coverage.readthedocs.io/en/latest/config.html#report
Expand All @@ -131,4 +133,4 @@ exclude_lines = [
]

[tool.uv]
default-groups = ["dev", "docs", "extras"]
default-groups = ["dev", "docs", "extras"]
Loading

0 comments on commit 97ede5b

Please sign in to comment.