Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions .cursor/rules/python-style.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
description: Python style for scripts in this repo
globs: "**/*.py"
alwaysApply: false
---

# Python style

## Comments

A comment is `#` plus a space. It sits on the line above a step, or at the end of the line it explains. It is a short label or one sentence. Do not write a paragraph where a label will do. Capitalization is not strict.

```python
# Don't need to increment self.pos because it is handled by read_u8()
return ret
```

```python
if tile < transparency_tile: # transparency tile is the first tile in the source image
transparency_tile = tile
```

Every function gets a docstring. Use triple double quotes, with the opening and closing quotes on their own lines and the text indented with the body. One short line saying what the function does. No paragraph, in a docstring or in a `#` comment. Do not leave a blank line between the closing quotes and the code.

```python
def split_stereo_wav(in_wav, left_out_wav, right_out_wav):
"""
Use ffmpeg to split a stereo wav to two mono wavs
"""
subprocess.run([...])
```

A file-level note, when there is one, is a triple-quoted block above the imports.

## Blank lines

One blank line between functions. The docstring sits directly under `def`, and the code follows the closing quotes with no blank line. Do not stack blank lines.

Group the lines that do one step, and put one blank line between steps. A run of the same kind of statement stays together. A blank line is where that run stops and something else starts.

A long run of assignments is not one step just because the lines look alike. Split where a new stage begins, when later lines are built from the results above.

Leave one blank line after a substantial `if`, `for`, `while`, `with`, or `try` before the next step. `elif`, `else`, `except`, and `finally` stay attached. Nested blocks that end together share that one blank line. None when they end the function.

After a guard (`if` whose body is a single `continue`, `break`, or `return`), leave a blank line before the next step. Skip it when the next line is another guard of the same kind, or when the whole body is a short loop: the guard plus one or two lines. An `if`/`else` pair stays together. A chain of `if`s that each return or transform stays together, and the fallback `return` after that chain gets a blank line.

A `print` or other short call stays with the `return` under it. In a very short block the `return` can also follow a small loop. No blank line when the `return` is the only line in its block.

```python
for path in paths:
if not path.endswith('.java'):
continue
found.append(path)

if status is None:
return

rows.append(status)
```

```python
if token == 'KILLED':
return 'killed'
if token == 'SURVIVED':
return 'survived'

return 'other'
```

## Layout

Four spaces, no tabs. A space after `if`, `for`, `while`, and `except` when it takes an expression: `if rom_in is not None:`. `else` starts its own line.

Long calls put each argument on its own line, indented one level:

```python
create_button = ttk.Button(
top_frame,
text='Repair ROM',
command=create,
width=30,
)
```

Long messages break with `+` at the end of the line, or inside parentheses. There is no hard column.

Slices and calls stay tight: `self.data[self.pos:self.pos + read_length]`, `read_u8()`. Not `data[start - 1 : end]`.

## Names, quotes, imports

Functions and locals are `snake_case`. Classes are `CapWords`. Real constants are `UPPER_SNAKE`.

Prefer single quotes. An f-string is fine when the line would otherwise be a chain of `+`.

Imports are one name per statement. A local module comes after a blank line. Do not use `from module import *`.

## Types and `main`

Skip annotations unless the file already has them.

A CLI ends in `if __name__ == '__main__':` and calls `main()`. Use single quotes.
13 changes: 13 additions & 0 deletions .cursor/rules/shell-style.mdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
description: Shell style for the quality scripts and the pre-commit hook
globs: "**/*.sh,.githooks/pre-commit"
alwaysApply: false
---

# Shell style

`#!/usr/bin/env bash`, then `set -euo pipefail`. A file-level note is one `#` line under the shebang. Two-space indent.

Group the lines that do one step, and put one blank line between steps. A long run of assignments is not one step just because the lines look alike; split where later lines are built from the results above. Leave one blank line after `if`/`fi`, `for`/`done`, and `case`/`esac` before the next step. None when that block ends the function or the file. `else` and `elif` stay attached. A run of similar short blocks, such as back-to-back flag checks, stays together.

A comment is `#` plus a space, one short line, above the step it explains.
52 changes: 52 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
# Spotless, crapper, and dryer on staged Java. Mutation stays in CI.
set -euo pipefail

root="$(git rev-parse --show-toplevel)"
cd "$root"

staged=()
while IFS= read -r file; do
[[ -z "$file" ]] && continue
staged+=("$file")
done < <(git diff --cached --name-only --diff-filter=ACMR)

if [[ ${#staged[@]} -eq 0 ]]; then
exit 0
fi

stashed=0
cleanup() {
status=$?
if [[ "$stashed" -eq 1 ]]; then
git stash pop --quiet || echo "pre-commit: git stash pop failed; look in git stash" >&2
fi
exit "$status"
}

trap cleanup EXIT

if ! git diff --quiet || [[ -n "$(git ls-files --others --exclude-standard)" ]]; then
git stash push --keep-index --include-untracked --quiet
stashed=1
fi

java=()
quality=()
for file in "${staged[@]}"; do
case "$file" in
src/main/generated-sources/*.java) ;;
*.java)
java+=("$file")
quality+=("$file")
;;
esac
done

if [[ ${#java[@]} -gt 0 ]]; then
mvn -B -ntp spotless:check
fi

if [[ ${#quality[@]} -gt 0 ]]; then
scripts/quality/check-quality.sh --staged
fi
39 changes: 39 additions & 0 deletions .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Mutation testing

# Changed files on pull requests. Full run weekly and on workflow_dispatch.
on:
pull_request:
schedule:
- cron: "0 7 * * 1"
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
mutation:
name: PIT
runs-on: ubuntu-latest
timeout-minutes: 120
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-java@v4
with:
distribution: temurin
# This module compiles at source/target 20. Same JDK as the build workflow.
java-version: '21'
cache: maven

- uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Mutation score
env:
QUALITY_BASE: origin/${{ github.base_ref || 'main' }}
QUALITY_MODE: ${{ github.event_name == 'pull_request' && 'changed' || 'full' }}
run: scripts/quality/mutate.sh
38 changes: 38 additions & 0 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Quality gates

# CRAP and dryer ratchet. Coverage is regenerated so a stale report cannot raise a score.
on:
push:
branches: ['**']
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
crap-and-dry:
name: CRAP and duplication
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-java@v4
with:
distribution: temurin
# This module compiles at source/target 20. Same JDK as the build workflow.
java-version: '21'
cache: maven

- uses: actions/setup-python@v5
with:
python-version: "3.12"

- name: Coverage, crapper, and dryer
env:
QUALITY_BASE: origin/${{ github.base_ref || 'main' }}
run: |
scripts/quality/coverage.sh
scripts/quality/check-quality.sh --base "$QUALITY_BASE"
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ build/
*.nds
.idea
src/main/generated-sources/

# Quality-gate tools and reports. Baselines under .quality/ are committed.
.quality/py/
__pycache__/
*.pyc
7 changes: 7 additions & 0 deletions .quality/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Quality gates

CRAP scores cannot rise, new or changed functions stay under 10, and new dryer pairs fail. Mutation scores cannot drop. Generated ANTLR sources are not scored.

Enable the hook with `git config core.hooksPath .githooks`, or run `scripts/install-git-hooks.sh`. The hook reads `target/site/jacoco/jacoco.xml` from the last test run.

Refresh a baseline with `scripts/quality/refresh-baseline.sh`. Pass `--with-mutation` to include PIT.
Loading
Loading