Files
cozystack/cmd/backport-batch/shape_test.go
T
Myasnikov Daniil 9f8c88b4f1 feat(release): build the backport batch with backport-batch
The rules need a tool that applies them the same way on every run and
shows what it did. backport-batch reads main's first-parent history,
judges each pull request by the policy block it reads from the page on
main, and picks what qualifies onto one batch branch per line. Each
original lands as its own commits under a merge that names it, so
authors and sign-offs survive and the line's own history shows what it
carries.

check is the gate before a cut. fold holds a hand resolution of a
conflict to the batch's shape, verify holds a whole batch to it, and
notify reports what waits on the batch for a maintainer.

Assisted-By: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-11-06 12:33:36 +00:00

373 lines
15 KiB
Go

/*
Copyright 2026 The Cozystack Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// A conflict on a commit in the middle of a pull request takes the batch back
// to where it was before that original: none of its commits stays, the
// conflict is recorded with the commit that failed, and the next original is
// still picked.
func TestRunConflictInAMiddleCommit(t *testing.T) {
f := newFixture(t)
f.lines()
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
mc5 := f.prCommits(5, "fix(chart): five", "",
change{files: map[string]string{"app/five.txt": "5\n"}},
change{files: map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n"}},
change{files: map[string]string{"app/five.txt": "5, done\n"}})
own := strings.Fields(f.git("rev-list", "--reverse", mc5+"^1.."+mc5+"^2"))
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
t.Errorf("the batch carries %q, want only #6", got)
}
if log := f.git("log", "--format=%B", "origin/release-1.1..HEAD"); strings.Contains(log, own[0]) || strings.Contains(log, own[1]) {
t.Errorf("a commit of #5 stayed on the batch:\n%s", log)
}
if out := f.git("status", "--porcelain"); out != "" {
t.Errorf("the tree is left dirty:\n%s", out)
}
if _, err := os.Stat(filepath.Join(f.dir, ".git", "CHERRY_PICK_HEAD")); err == nil {
t.Error("a cherry-pick is left in progress")
}
l, _ := parseLine("release-1.1")
deferred, err := parseDeferred(stdout, l)
if err != nil || deferred[5].Commit != own[1] || !slices.Equal(deferred[5].Files, []string{"chart/values.yaml"}) {
t.Errorf("deferred = %+v, %v; want #5 at %s in chart/values.yaml", deferred, err, own[1])
}
if !strings.Contains(stdout, "`fix(chart): five` at `"+own[1][:12]+"`: `chart/values.yaml`") {
t.Errorf("the body does not name the commit that failed:\n%s", stdout)
}
}
// What counts as carrying a merged pull request of several commits: picks
// whose appended -x lines name every commit of its own, as a batch merge's do,
// or a pick whose appended -x line names its merge commit; not some of its
// commits. A Backported-from trailer counts only on a commit from before the
// batch-only day, and only in its trailer block.
func TestPresenceOfAMultiCommitOriginal(t *testing.T) {
cases := []struct {
name string
land func(f *fixture, mc string, own []string)
carried string
}{
{
name: "a batch merge of its commits",
land: func(f *fixture, mc string, own []string) {
f.git("fetch", "-q", "origin")
base := f.git("rev-parse", "origin/release-1.1")
f.git("switch", "-q", "--detach", base)
for _, c := range own {
f.git("cherry-pick", "-x", c)
}
f.git("switch", "-q", "--detach", f.joined(base, f.git("rev-parse", "HEAD"), "fix(app): five (#5)\n\n"+batchTrailer+": "+mc))
f.git("push", "-q", "origin", "HEAD:refs/heads/release-1.1")
f.git("switch", "-q", "main")
},
carried: "its 2 commits picked",
},
{
name: "a pick whose -x line names the merge commit",
land: func(f *fixture, mc string, _ []string) {
f.onLine("release-1.1", "port\n\n(cherry picked from commit "+mc+")", map[string]string{"app/a.txt": "a\n"})
},
carried: "picked in",
},
{
name: "picks whose -x lines name every commit of its own",
land: func(f *fixture, _ string, own []string) {
f.onLine("release-1.1", "a\n\n(cherry picked from commit "+own[0]+")", map[string]string{"app/a.txt": "a\n"})
f.onLine("release-1.1", "b\n\n(cherry picked from commit "+own[1]+")", map[string]string{"app/b.txt": "b, half\n"})
},
carried: "its 2 commits picked",
},
{
name: "a pick of one of its two commits",
land: func(f *fixture, _ string, own []string) {
f.onLine("release-1.1", "a\n\n(cherry picked from commit "+own[0]+")", map[string]string{"app/a.txt": "a\n"})
},
},
{
name: "the trailer alone, from before the batch-only day",
land: func(f *fixture, mc string, _ []string) {
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-01T00:00:00Z")
f.onLine("release-1.1", "fix(app): five (#5)\n\n"+batchTrailer+": "+mc, map[string]string{"app/a.txt": "a\n"})
},
carried: "named by a " + batchTrailer + " trailer",
},
{
name: "the trailer alone, after the batch-only day",
land: func(f *fixture, mc string, _ []string) {
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T00:00:00Z")
f.onLine("release-1.1", "fix(app): five (#5)\n\n"+batchTrailer+": "+mc, map[string]string{"app/a.txt": "a\n"})
},
},
{
name: "the trailer in the body, from before the batch-only day",
land: func(f *fixture, mc string, _ []string) {
f.t.Setenv("GIT_COMMITTER_DATE", "2026-10-01T00:00:00Z")
f.onLine("release-1.1", "port\n\n"+batchTrailer+": "+mc+"\n\nMore to say.", map[string]string{"app/a.txt": "a\n"})
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f := newFixture(t)
f.lines()
mc := f.prCommits(5, "fix(app): five", "",
change{files: map[string]string{"app/a.txt": "a\n"}},
change{files: map[string]string{"app/b.txt": "b\n"}})
own := strings.Fields(f.git("rev-list", "--reverse", mc+"^1.."+mc+"^2"))
tc.land(f, mc, own)
v := verdictOn(t, f, nil, "release-1.1", 5)
switch {
case tc.carried == "" && v.rule != 0:
t.Errorf("verdict = rule %d (%s), want it to qualify", v.rule, v.reason)
case tc.carried != "" && (v.rule != ruleOnLine || !strings.HasPrefix(v.reason, tc.carried)):
t.Errorf("verdict = rule %d (%s), want rule 1 with %q", v.rule, v.reason, tc.carried)
}
})
}
}
// What a pull request's author writes reaches the line with every commit the
// batch picks and with the merge message it carries. None of it makes another
// original count as carried: not an -x line, a Backported-from trailer or a
// Backport of naming it, not at the end of the message where the line git
// appends lands in the same trailer block, and not an -x line naming one of
// the other original's own commits. The line is read as after the batch-only
// day, as a batch made after it leaves it.
func TestPresenceCannotBeClaimedByText(t *testing.T) {
type world struct {
f *fixture
mc5, c5 string
}
claims := []struct {
name string
claim func(w *world) string
}{
{"an -x line naming the merge commit", func(w *world) string { return "(cherry picked from commit " + w.mc5 + ")" }},
{"an -x line naming its own commit", func(w *world) string { return "(cherry picked from commit " + w.c5 + ")" }},
{"a Backported-from trailer", func(w *world) string { return batchTrailer + ": " + w.mc5 }},
{"a Backport of", func(w *world) string { return "Backport of #5" }},
{"all of them after a sign-off", func(w *world) string {
return "Signed-off" + "-by: " + contributor + "\n(cherry picked from commit " + w.mc5 + ")\n(cherry picked from commit " + w.c5 + ")\n" + batchTrailer + ": " + w.mc5
}},
}
for _, where := range []string{"commit message", "pull request body"} {
for _, tc := range claims {
t.Run(where+": "+tc.name, func(t *testing.T) {
f := newFixture(t)
f.lines()
// #5 conflicts on the line, so the batch defers it.
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
w := &world{f: f}
w.mc5 = f.pr(5, "fix(chart): five", "", map[string]string{"chart/values.yaml": "replicas: 5\nimage: app:1\n"})
w.c5 = f.git("rev-parse", w.mc5+"^2")
claim := tc.claim(w)
msg, body := "fix(app): six\n\n"+claim, ""
if where == "pull request body" {
msg, body = "fix(app): six", "Six.\n\n"+claim
}
f.prCommits(6, "fix(app): six", body, change{files: map[string]string{"app/six.txt": "6\n"}, message: msg})
t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T06:00:00Z")
// The batch picks #6; on the open batch pull request the text
// claims nothing for #5.
gh := &fakeGH{}
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 {
t.Fatalf("run --push = %d\n%s", code, stderr)
}
f.git("switch", "-q", "main")
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, gh.bodies[0])}
if v := verdictOn(t, f, gh, "release-1.1", 6); v.onBatch == "" {
t.Fatal("#6 is not on the batch")
}
if v := verdictOn(t, f, gh, "release-1.1", 5); v.onBatch != "" {
t.Errorf("#5 is on the batch in %.12s, want nowhere", v.onBatch)
}
// Nor on the line, once the line takes the batch.
f.git("push", "-q", "origin", f.remoteHead(batchBranch1)+":refs/heads/release-1.1")
if v := verdictOn(t, f, nil, "release-1.1", 6); v.rule != ruleOnLine {
t.Fatalf("#6 = rule %d (%s), want it on the line", v.rule, v.reason)
}
if v := verdictOn(t, f, nil, "release-1.1", 5); v.rule != 0 {
t.Errorf("#5 = rule %d (%s), want it still to qualify", v.rule, v.reason)
}
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") {
t.Errorf("check = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
}
})
}
}
}
// verify passes a batch the run built and fails one with an original that is
// not in the batch's shape, reading nothing from GitHub when given the head.
func TestVerify(t *testing.T) {
type world struct {
f *fixture
gh *fakeGH
head string
mc5 string
sq6 string
}
setup := func(t *testing.T) *world {
f := newFixture(t)
f.lines()
w := &world{f: f}
w.mc5 = f.prCommits(5, "fix(app): five", "",
change{files: map[string]string{"app/five.txt": "5\n"}},
change{files: map[string]string{"app/five.txt": "5, done\n"}})
w.sq6 = f.squash(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
w.gh = &fakeGH{}
w.head = openBatch(t, f, w.gh)
return w
}
// on adds a commit with message and files on top of the batch head.
on := func(w *world, message string, files map[string]string) string {
w.f.git("switch", "-q", "--detach", w.head)
w.f.apply(files)
w.f.git("commit", "-q", "--allow-empty", "-m", message)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
}
cases := []struct {
name string
head func(w *world) string
refusal string
}{
{name: "the batch as the run built it", head: func(w *world) string { return w.head }},
{
name: "an original picked as one commit with -m 1",
head: func(w *world) string {
return on(w, "fix(app): five (#5)\n\n(cherry picked from commit "+w.mc5+")", map[string]string{"app/five.txt": "5, again\n"})
},
refusal: "picks the merge commit of #5 as one commit",
},
{
name: "a commit that is no original",
head: func(w *world) string {
return on(w, "fix(ci): adjust for the line", map[string]string{"ci.txt": "x\n"})
},
refusal: "ends in no (cherry picked from commit",
},
{
name: "an original carried twice",
head: func(w *world) string {
own := strings.Fields(w.f.git("rev-list", "--reverse", w.mc5+"^1.."+w.mc5+"^2"))
p := on(w, "again\n\n(cherry picked from commit "+own[1]+")", map[string]string{"app/five.txt": "5, done again\n"})
w.f.git("switch", "-q", "--detach", w.head)
w.f.git("merge", "-q", "--no-ff", "-m", "fix(app): five (#5)\n\n"+batchTrailer+": "+w.mc5, p)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
},
refusal: "carries #5 again",
},
{
name: "a squash-merged original carried as a merge",
head: func(w *world) string {
p := on(w, "six\n\n(cherry picked from commit "+w.sq6+")", map[string]string{"app/six.txt": "6, again\n"})
w.f.git("switch", "-q", "--detach", w.head)
w.f.git("merge", "-q", "--no-ff", "-m", "fix(app): six (#6)\n\n"+batchTrailer+": "+w.sq6, p)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
},
refusal: "which main took as one commit",
},
{
name: "a merge of the line into the batch",
head: func(w *world) string {
line := w.f.onLine("release-1.1", "fix(chart): on the line", map[string]string{"chart/values.yaml": "replicas: 2\nimage: app:1\n"})
w.f.git("switch", "-q", "--detach", w.head)
w.f.git("merge", "-q", "--no-ff", "-m", "Merge branch 'release-1.1' into the batch", line)
sha := w.f.git("rev-parse", "HEAD")
w.f.git("switch", "-q", "main")
return sha
},
refusal: "with 0 " + batchTrailer + " trailers",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
w := setup(t)
head := tc.head(w)
// GitHub is not asked: a failing fake would fail the run.
gh := &fakeGH{err: errString("gh: not to be asked")}
code, stdout, stderr := w.f.exec(gh, "verify", "--line", "release-1.1", "--head", head)
switch {
case tc.refusal == "" && code != 0:
t.Errorf("verify = %d\n%s\n%s", code, stdout, stderr)
case tc.refusal != "" && (code != 1 || !strings.Contains(stdout, tc.refusal)):
t.Errorf("verify = %d, want 1 with %q:\n%s\n%s", code, tc.refusal, stdout, stderr)
}
})
}
t.Run("the open batch pull request", func(t *testing.T) {
w := setup(t)
code, stdout, stderr := w.f.exec(w.gh, "verify", "--line", "release-1.1")
if code != 0 || !strings.Contains(stdout, "3 units at "+w.head[:12]) {
t.Errorf("verify = %d\n%s\n%s", code, stdout, stderr)
}
if code, stdout, _ := w.f.exec(&fakeGH{}, "verify", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, "nothing to verify") {
t.Errorf("verify with no batch open = %d, %q", code, stdout)
}
})
t.Run("a head that is not here", func(t *testing.T) {
w := setup(t)
if code, _, stderr := w.f.exec(w.gh, "verify", "--line", "release-1.1", "--head", strings.Repeat("ab", 20)); code != 2 || !strings.Contains(stderr, "fetch it first") {
t.Errorf("verify = %d (%s), want 2", code, stderr)
}
})
}
// A pick that lands among lines the line changed is the same change, and the
// body does not call it changed; one whose change differs is called so.
func TestSummaryCallsOnlyChangedPicksChanged(t *testing.T) {
f := newFixture(t)
f.release("release-1.0")
f.squash(2, "chore(app): a file to change", "", map[string]string{"app/ctx.txt": "a\nb\nc\nd\ne\n"})
f.pr(1, "feat(app): something for 1.1", "", map[string]string{"app/feature.txt": "1.1\n"})
f.release("release-1.1")
f.onLine("release-1.1", "fix(app): a line-only change", map[string]string{"app/ctx.txt": "A\nb\nc\nd\ne\n"})
f.pr(5, "fix(app): five", "", map[string]string{"app/ctx.txt": "a\nb\nC\nd\ne\n"})
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
if code != 0 {
t.Fatalf("run = %d\n%s", code, stderr)
}
if !strings.Contains(stdout, "`fix(app): five`, 1 commit\n") {
t.Errorf("a pick git only fit to its place is called changed:\n%s", stdout)
}
}