Skip to content

Commit 62d5562

Browse files
benflexcomputeflow360-auto-hotfix-bot
authored andcommitted
feat: add autohooks plugin to sort JSON keys in test ref files (#1936)
1 parent 5355322 commit 62d5562

4 files changed

Lines changed: 78 additions & 9 deletions

File tree

README.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,31 @@ Environment ``FLOW360_APIKEY`` variable takes precedence before Flow360 configur
5959
- You can also run examples without activating shell:
6060
``poetry run python examples/case_results.py``
6161

62-
## check in
63-
1. ``black .`` - performs auto-formatting
64-
2. ``isort .`` - sorts imports
65-
3. ``poetry run pylint $(git ls-files "flow360/*.py") --rcfile .pylintrc`` - checks code style
66-
4. ``poetry run pytest -rA tests/simulation`` - run V2 related tests
67-
5. ``poetry run pytest -rA --ignore tests/simulation`` - run V1 related tests (cannot be run altogether)
68-
6. ``pytest -rA tests/simulation --cov-report=html --cov=flow360/component/simulation`` - see test coverage report for V2 client
69-
7. ``pytest -rA --ignore tests/simulation --cov-report=html --cov=flow360 && open htmlcov/index.html`` - see test coverage report for V1+V2 client
62+
## pre-commit hooks
63+
64+
This project uses [autohooks](https://github.com/greenbone/autohooks) to run formatting and linting checks automatically before each commit. To activate:
65+
66+
```bash
67+
poetry run autohooks activate
68+
```
69+
70+
This installs a git pre-commit hook that runs:
71+
- **black** — auto-formatting
72+
- **isort** — import sorting
73+
- **pylint** — code style checking
74+
- **sort_json** — sorts keys in `tests/**/*.json` reference files for clean diffs
75+
76+
The hooks only process staged files, so they are fast. If you skip activation, CI will still catch issues.
77+
78+
## check in (manual)
79+
1. ``black .`` — auto-formatting
80+
2. ``isort .`` — sorts imports
81+
3. ``python tools/sort_ref_json.py`` — sorts keys in test reference JSON files
82+
4. ``poetry run pylint $(git ls-files "flow360/*.py") --rcfile .pylintrc`` — checks code style
83+
5. ``poetry run pytest -rA tests/simulation`` — run V2 related tests
84+
6. ``poetry run pytest -rA --ignore tests/simulation`` — run V1 related tests (cannot be run altogether)
85+
7. ``pytest -rA tests/simulation --cov-report=html --cov=flow360/component/simulation`` — see test coverage report for V2 client
86+
8. ``pytest -rA --ignore tests/simulation --cov-report=html --cov=flow360 && open htmlcov/index.html`` — see test coverage report for V1+V2 client
7087

7188
## Documentation Requirements
7289

flow360/_autohooks/__init__.py

Whitespace-only changes.

flow360/_autohooks/sort_json.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Autohooks plugin: sort keys in staged JSON files under tests/."""
2+
3+
import importlib.util
4+
from pathlib import Path
5+
6+
from autohooks.api import ok
7+
from autohooks.api.git import (
8+
get_staged_status,
9+
stage_files_from_status_list,
10+
stash_unstaged_changes,
11+
)
12+
from autohooks.api.path import match
13+
14+
_TOOLS_DIR = Path(__file__).resolve().parent.parent.parent / "tools"
15+
_spec = importlib.util.spec_from_file_location("sort_ref_json", _TOOLS_DIR / "sort_ref_json.py")
16+
_module = importlib.util.module_from_spec(_spec)
17+
_spec.loader.exec_module(_module)
18+
process_file = _module.process_file
19+
20+
DEFAULT_INCLUDE = ("*.json",)
21+
TESTS_DIR = Path(__file__).resolve().parent.parent.parent / "tests"
22+
23+
24+
def precommit(config=None, report_progress=None, **kwargs): # pylint: disable=unused-argument
25+
"""Sort JSON keys in staged test reference files."""
26+
files = [
27+
f
28+
for f in get_staged_status()
29+
if match(f.path, DEFAULT_INCLUDE) and TESTS_DIR in f.absolute_path().parents
30+
]
31+
32+
if not files:
33+
ok("No staged JSON files under tests/.")
34+
return 0
35+
36+
if report_progress:
37+
report_progress.init(len(files))
38+
39+
with stash_unstaged_changes(files):
40+
for f in files:
41+
already_sorted = process_file(f.absolute_path(), check=False)
42+
if already_sorted:
43+
ok(f"Already sorted: {f.path}")
44+
else:
45+
ok(f"Sorted keys in: {f.path}")
46+
47+
if report_progress:
48+
report_progress.update()
49+
50+
stage_files_from_status_list(files)
51+
52+
return 0

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ exclude = '''
119119

120120
[tool.autohooks]
121121
mode = "poetry"
122-
pre-commit = ["autohooks.plugins.black", "autohooks.plugins.isort", "autohooks.plugins.pylint"]
122+
pre-commit = ["autohooks.plugins.black", "autohooks.plugins.isort", "autohooks.plugins.pylint", "flow360._autohooks.sort_json"]
123123

124124
[tool.autohooks.plugins.pylint]
125125
arguments = ["--rcfile=.pylintrc"]

0 commit comments

Comments
 (0)