Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regenerate generated files automatically if any are changed #3050

Merged
merged 6 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ci:
autoupdate_commit_msg: "[pre-commit.ci] pre-commit autoupdate"
autoupdate_schedule: weekly
submodules: false
skip: [regenerate-files]

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down Expand Up @@ -33,3 +34,11 @@ repos:
rev: v2.3.0
hooks:
- id: codespell
- repo: local
hooks:
- id: regenerate-files
name: regenerate generated files
language: system
entry: python src/trio/_tools/gen_exports.py
pass_filenames: false
files: ^src\/trio\/_core\/(_generated)?(_run|(_i(o_(common|epoll|kqueue|windows)|nstrumentation)))\.py$
CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 10 additions & 2 deletions src/trio/_tests/tools/test_gen_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ def test_create_pass_through_args() -> None:

@skip_lints
@pytest.mark.parametrize("imports", [IMPORT_1, IMPORT_2, IMPORT_3])
def test_process(tmp_path: Path, imports: str) -> None:
def test_process(
tmp_path: Path, imports: str, capsys: pytest.CaptureFixture[str]
) -> None:
try:
import black # noqa: F401
# there's no dedicated CI run that has astor+isort, but lacks black.
Expand All @@ -106,7 +108,13 @@ def test_process(tmp_path: Path, imports: str) -> None:
with pytest.raises(SystemExit) as excinfo:
process([file], do_test=True)
assert excinfo.value.code == 1
process([file], do_test=False)
captured = capsys.readouterr()
assert "Generated sources are outdated. Please regenerate." in captured.out
with pytest.raises(SystemExit) as excinfo:
process([file], do_test=False)
assert excinfo.value.code == 1
captured = capsys.readouterr()
assert "Regenerated sources successfully." in captured.out
assert genpath.exists()
process([file], do_test=True)
# But if we change the lookup path it notices
Expand Down
6 changes: 5 additions & 1 deletion src/trio/_tools/gen_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ def process(files: Iterable[File], *, do_test: bool) -> None:
dirname, basename = os.path.split(file.path)
new_path = os.path.join(dirname, PREFIX + basename)
new_files[new_path] = new_source
matches_disk = matches_disk_files(new_files)
if do_test:
if not matches_disk_files(new_files):
if not matches_disk:
print("Generated sources are outdated. Please regenerate.")
sys.exit(1)
else:
Expand All @@ -300,6 +301,9 @@ def process(files: Iterable[File], *, do_test: bool) -> None:
with open(new_path, "w", encoding="utf-8") as f:
f.write(new_source)
print("Regenerated sources successfully.")
if not matches_disk:
# With pre-commit integration, show that we edited files.
sys.exit(1)


# This is in fact run in CI, but only in the formatting check job, which
Expand Down
Loading