diff --git a/tests/restyle.py b/tests/restyle.py index 25b437db98..d0477c2099 100755 --- a/tests/restyle.py +++ b/tests/restyle.py @@ -7,7 +7,9 @@ import subprocess import contextlib -from dataclasses import dataclass +from dataclasses import dataclass, fields +from dataclasses import field as dataclass_field + GIT_ROOT = pathlib.Path( subprocess.check_output( @@ -89,11 +91,11 @@ class Changed: @dataclass class Context: - append_hunk: bool - deleted: bool - file: str - hunk: list[str] - markers: list[str] + append_hunk: bool = False + deleted: bool = False + file: str = "" + hunk: list[str] = dataclass_field(default_factory=list) + markers: list[str] = dataclass_field(default_factory=list) # naive git-diff parser for clang-format aftercare @@ -108,28 +110,24 @@ def changed_files(): universal_newlines=True, ) - def reset_context(line): + def reset_context(ctx): + other = Context() + for field in fields(other): + setattr(ctx, field.name, getattr(other, field.name)) + + def reset_with_line(ctx, line): + reset_context(ctx) if line: - hunk = [line] - else: - hunk = [] - - return Context( - append_hunk=False, - deleted=False, - file="", - hunk=hunk, - markers=[], - ) + ctx.hunk = [line] def pop(out, context, line): if ctx.file and ctx.hunk and ctx.markers: out.append(Changed(ctx.file, "\n".join(ctx.hunk), ", ".join(ctx.markers))) - context = reset_context(line) + reset_with_line(context, line) out = [] - ctx = reset_context(None) + ctx = Context() for line in proc.stdout.split("\n"): # '--- a/path/to/changed/file' most likely @@ -180,6 +178,7 @@ def warning_changed(changed: Changed): SUMMARY_PATH = pathlib.Path(os.environ.get("GITHUB_STEP_SUMMARY", os.devnull)) SUMMARY_OUTPUT = SUMMARY_PATH.open("a") + def summary_diff(changed: Changed): with contextlib.redirect_stdout(SUMMARY_OUTPUT): print(f"# {changed.file} (suggested change)")