File size: 1,224 Bytes
c4f3819
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""PR-style patch export for generated tests."""

from __future__ import annotations

import difflib
from pathlib import Path

from generator import GeneratedSuite


def make_pr_patch(suite: GeneratedSuite) -> str:
    """Return a unified diff that adds the generated tests and a CI hint."""
    sections: list[str] = []
    for relative, content in sorted(suite.files.items()):
        new_lines = content.splitlines(keepends=True)
        diff = difflib.unified_diff(
            [],
            new_lines,
            fromfile="/dev/null",
            tofile=f"b/{relative}",
            lineterm="",
        )
        sections.append("\n".join(diff))

    ci_hint = "pytest tests -q\n"
    sections.append(
        "\n".join(
            difflib.unified_diff(
                [],
                [ci_hint],
                fromfile="/dev/null",
                tofile="b/TESTFORGE_CI_HINT.txt",
                lineterm="",
            )
        )
    )
    return "\n\n".join(sections) + "\n"


def write_patch(suite: GeneratedSuite, path: str | Path) -> Path:
    output = Path(path)
    output.parent.mkdir(parents=True, exist_ok=True)
    output.write_text(make_pr_patch(suite), encoding="utf-8")
    return output