Skip to content

Commit

Permalink
Add code check script (#29)
Browse files Browse the repository at this point in the history
* Add code check script

* Remove nerfstudio license header

* Nits

---------

Co-authored-by: Brent Yi <[email protected]>
  • Loading branch information
tancik and brentyi authored May 26, 2023
1 parent 4a5f50b commit d135bc0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
11 changes: 5 additions & 6 deletions docs/source/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ pre-commit install
```

It would be hard to write unit tests for `viser`. We rely on static typing for
robustness. From the `~/viser` root directory, you can run:
robustness. To check your code, you can run the following:

- `pyright . && mypy .`

For formatting and linting:

- `black . && ruff --fix .`
```bash
# runs linting, formatting, and type-checking
viser-dev-checks
```

## Message updates

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ trimesh = { version = ">=3.21.7", optional = true}
dev = ["pyright", "mypy", "ruff", "black"]
examples = ["smplx", "torch", "yourdfpy", "trimesh"]

[tool.poetry.scripts]
viser-dev-checks = "viser.scripts.dev_checks:entrypoint"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Expand Down
Empty file added viser/scripts/__init__.py
Empty file.
85 changes: 85 additions & 0 deletions viser/scripts/dev_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env python
"""Runs formatting, linting, and type checking tests."""
import subprocess
import sys

import tyro
from rich.style import Style

from rich import console

CONSOLE = console.Console()

TYPE_TESTS = ["pyright .", "mypy ."]
FORMAT_TESTS = ["black .", "ruff --fix ."]


def run_command(command: str, continue_on_fail: bool = False) -> bool:
"""Run a command kill actions if it fails
Args:
command: Command to run.
continue_on_fail: Whether to continue running commands if the current one fails..
"""
ret_code = subprocess.call(command, shell=True)
if ret_code != 0:
CONSOLE.print(f"[bold red]Error: `{command}` failed.")
if not continue_on_fail:
sys.exit(1)
return ret_code == 0


def run_code_checks(
continue_on_fail: bool = False,
skip_format_checks: bool = False,
skip_type_checks: bool = False,
):
"""Runs formatting, linting, and type checking tests.
Args:
continue_on_fail: Whether or not to continue running actions commands if the current one fails.
skip_format_checks: Whether or not to skip format tests.
skip_type_checks: Whether or not to skip type tests.
"""

success = True

assert (
not skip_format_checks or not skip_type_checks
), "Cannot skip format and type tests at the same time."
tests = []
if not skip_format_checks:
tests += FORMAT_TESTS
if not skip_type_checks:
tests += TYPE_TESTS

for test in tests:
CONSOLE.line()
CONSOLE.rule(f"[bold green]Running: {test}")
success = success and run_command(test, continue_on_fail=continue_on_fail)

if success:
CONSOLE.line()
CONSOLE.rule(characters="=")
CONSOLE.print(
"[bold green]:TADA: :TADA: :TADA: ALL CHECKS PASSED :TADA: :TADA: :TADA:",
justify="center",
)
CONSOLE.rule(characters="=")
else:
CONSOLE.line()
CONSOLE.rule(characters="=", style=Style(color="red"))
CONSOLE.print(
"[bold red]:skull: :skull: :skull: ERRORS FOUND :skull: :skull: :skull:",
justify="center",
)
CONSOLE.rule(characters="=", style=Style(color="red"))


def entrypoint():
"""Entrypoint for use with pyproject scripts."""
tyro.cli(run_code_checks)


if __name__ == "__main__":
entrypoint()

0 comments on commit d135bc0

Please sign in to comment.