| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "the-valley/change" |
| 13 | "the-valley/rebase" |
| 14 | ) |
| 15 | |
| 16 | // id is the nth of a run of change IDs, 32 letters from k to z. |
| 17 | func id(n int) string { |
| 18 | var b strings.Builder |
| 19 | for i := 0; i < 32; i++ { |
| 20 | b.WriteByte(byte('k' + (n>>(i%8*2))%16)) |
| 21 | } |
| 22 | return b.String() |
| 23 | } |
| 24 | |
| 25 | // oversized is an observation of a conflicted family that holds more than |
| 26 | // a report may in every way: 70 recorded changes, 150 conflict paths with |
| 27 | // unusual names among them, 70 blocking requests, one of whose refs is |
| 28 | // too long and one of whose names is no branch name, and a refusal longer |
| 29 | // than a report holds, with control characters in it. |
| 30 | func oversized() Observation { |
| 31 | o := Observation{Action: restack, Result: change.RestackConflict, Conflicts: []string{}, Requests: []RequestPlan{}} |
| 32 | for i := 0; i < 70; i++ { |
| 33 | o.Layers = append(o.Layers, ObservedLayer{ |
| 34 | Name: id(i)[:6], ID: id(i), Branch: fmt.Sprintf("stack/%02d", i), BranchTip: strings.Repeat("1", 40), |
| 35 | Record: strings.Repeat("2", 40), Version: 1, Head: strings.Repeat("1", 40), State: "ready", |
| 36 | }) |
| 37 | } |
| 38 | o.Failing = id(40) |
| 39 | o.Conflicts = append(o.Conflicts, "line\nbreak.txt", "bell\x07.txt", "latin1-\xe9.txt", strings.Repeat("long/", 220)+"x") |
| 40 | for i := len(o.Conflicts); i < 150; i++ { |
| 41 | o.Conflicts = append(o.Conflicts, fmt.Sprintf("src/file-%03d.go", i)) |
| 42 | } |
| 43 | o.Requests = append(o.Requests, |
| 44 | RequestPlan{Name: strings.Repeat("r", 300), Head: strings.Repeat("3", 40)}, |
| 45 | RequestPlan{Name: "no..branch", Head: strings.Repeat("3", 40)}) |
| 46 | for i := 0; i < 68; i++ { |
| 47 | o.Requests = append(o.Requests, RequestPlan{Name: fmt.Sprintf("request-%02d", i), Head: strings.Repeat("3", 40)}) |
| 48 | } |
| 49 | o.Refusal = strings.Repeat("the layer conflicts\nwith main\t", 150) |
| 50 | return o |
| 51 | } |
| 52 | |
| 53 | // A report of an observation that holds more than the format allows is a |
| 54 | // fallback that the format takes: the change's own layer and the failing |
| 55 | // one, the conflict paths and requests that fit, up to the format's |
| 56 | // count, and a refusal cut to fit, free of control characters, ending |
| 57 | // with what it leaves out and the digest of the whole observation. |
| 58 | func TestAReportFallsBackWithinEveryBound(t *testing.T) { |
| 59 | o := oversized() |
| 60 | self := id(3) |
| 61 | r, omitted, err := bound(fullReport("op-1", strings.Repeat("4", 40), self, o, nil), o.Digest()) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | data, err := change.EncodeRestackReport(r) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | if _, err := change.ParseRestackReport(data); err != nil || len(data) > change.MaxReportBlob { |
| 70 | t.Fatalf("the fallback is %d bytes and reads as %v", len(data), err) |
| 71 | } |
| 72 | if len(r.Layers) != 2 || r.Layers[0].ChangeID != self || r.Layers[1].ChangeID != o.Failing { |
| 73 | t.Errorf("the fallback keeps the layers %+v", r.Layers) |
| 74 | } |
| 75 | if len(r.Conflicts) != change.MaxConflictPaths || r.Conflicts[0] != "src/file-004.go" { |
| 76 | t.Errorf("the fallback keeps %d conflict paths, first %q", len(r.Conflicts), r.Conflicts[0]) |
| 77 | } |
| 78 | if len(r.Requests) != 64 || r.Requests[0].Ref != rebase.RequestPrefix+"request-00" { |
| 79 | t.Errorf("the fallback keeps %d requests, first %+v", len(r.Requests), r.Requests[0]) |
| 80 | } |
| 81 | if r.Refusal == nil || len(*r.Refusal) > change.MaxRefusal || !strings.Contains(*r.Refusal, "digest "+o.Digest()) || |
| 82 | !strings.HasPrefix(*r.Refusal, "the layer conflicts with main the layer") { |
| 83 | t.Errorf("the fallback's refusal is %q", *r.Refusal) |
| 84 | } |
| 85 | for _, want := range []string{"68 of the family's 70 recorded changes", "50 of its 150 conflict paths", "6 of its 70 blocking requests", "the end of its refusal"} { |
| 86 | if !strings.Contains(strings.Join(omitted, "; "), want) { |
| 87 | t.Errorf("the fallback does not say it leaves out %s: %v", want, omitted) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // A report whose every field fits on its own, and that holds more than 64 |
| 93 | // KiB in all, sheds its conflict paths, then its requests, then the |
| 94 | // layers it does not need, until it fits. |
| 95 | func TestAReportTooLargeInAllSheds(t *testing.T) { |
| 96 | o := Observation{Action: restack, Result: change.RestackConflict, Requests: []RequestPlan{}} |
| 97 | for i := 0; i < 64; i++ { |
| 98 | o.Layers = append(o.Layers, ObservedLayer{ |
| 99 | Name: id(i)[:6], ID: id(i), Branch: fmt.Sprintf("%02d/", i) + strings.Repeat("b", 900), BranchTip: strings.Repeat("1", 40), |
| 100 | Record: strings.Repeat("2", 40), Version: 1, Head: strings.Repeat("1", 40), State: "ready", |
| 101 | }) |
| 102 | } |
| 103 | o.Failing = id(0) |
| 104 | for i := 0; i < 100; i++ { |
| 105 | o.Conflicts = append(o.Conflicts, fmt.Sprintf("%03d/", i)+strings.Repeat("c", 1000)) |
| 106 | } |
| 107 | full := fullReport("op-1", strings.Repeat("4", 40), id(9), o, nil) |
| 108 | if err := full.Check(); err != nil { |
| 109 | t.Fatalf("each field should fit on its own: %v", err) |
| 110 | } |
| 111 | r, omitted, err := bound(full, o.Digest()) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | data, err := change.EncodeRestackReport(r) |
| 116 | if err != nil || len(data) > change.MaxReportBlob { |
| 117 | t.Fatalf("the fallback is %d bytes: %v", len(data), err) |
| 118 | } |
| 119 | if len(r.Conflicts) != 0 || len(r.Layers) != 2 || !strings.Contains(strings.Join(omitted, "; "), "the 100 conflict paths left") { |
| 120 | t.Errorf("the fallback keeps %d paths and %d layers, leaving out %v", len(r.Conflicts), len(r.Layers), omitted) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Two reports are the same when their observations and result are, |
| 125 | // whatever operation wrote each. Two fallbacks that show the same are told |
| 126 | // apart by the digest of what they leave out. |
| 127 | func TestReportsAreTheSameByWhatTheySay(t *testing.T) { |
| 128 | o := oversized() |
| 129 | tip := strings.Repeat("4", 40) |
| 130 | a, _, err := bound(fullReport("op-1", tip, id(3), o, nil), o.Digest()) |
| 131 | if err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | b, _, _ := bound(fullReport("op-2", tip, id(3), o, nil), o.Digest()) |
| 135 | if !sameReport(a, b) { |
| 136 | t.Error("the same observation written by two operations is not the same report") |
| 137 | } |
| 138 | o.Conflicts[149] = "src/another.go" |
| 139 | c, _, _ := bound(fullReport("op-2", tip, id(3), o, nil), o.Digest()) |
| 140 | if sameReport(a, c) { |
| 141 | t.Error("two fallbacks of different observations are the same report") |
| 142 | } |
| 143 | if strings.Join(a.Conflicts, " ") != strings.Join(c.Conflicts, " ") { |
| 144 | t.Fatal("the test's fallbacks should show the same conflict paths") |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // A conflict over more paths than a report holds keeps them all in the |
| 149 | // journal, and its report, which the format takes, names what fits and |
| 150 | // says what it leaves out. |
| 151 | func TestAConflictKeepsEveryPathInTheJournal(t *testing.T) { |
| 152 | f := newFixture(t, "") |
| 153 | f.git("checkout", "--quiet", "-b", "a", "main") |
| 154 | names := []string{"line\nbreak.txt", "bell\x07.txt"} |
| 155 | for i := 0; i < 120; i++ { |
| 156 | names = append(names, fmt.Sprintf("many/file-%03d.txt", i)) |
| 157 | } |
| 158 | write := func(content string) { |
| 159 | for _, n := range names { |
| 160 | path := filepath.Join(f.work, n) |
| 161 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | } |
| 168 | f.git("add", "-A") |
| 169 | } |
| 170 | write("a\n") |
| 171 | f.git("commit", "--quiet", "-m", "layer a") |
| 172 | f.git("push", "--quiet", "origin", "a") |
| 173 | head := f.git("rev-parse", "HEAD") |
| 174 | f.writeRecord(idA, &change.Record{ |
| 175 | Schema: change.Schema, ChangeID: idA, Target: rebase.Target, Owner: "alice", State: change.Ready, Seq: 1, Active: 1, |
| 176 | Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: "A", Branch: "a", Uploader: "alice"}}, |
| 177 | }, "", head) |
| 178 | f.git("checkout", "--quiet", "main") |
| 179 | write("main\n") |
| 180 | f.git("commit", "--quiet", "-m", "main has its own") |
| 181 | f.git("push", "--quiet", "origin", "main") |
| 182 | |
| 183 | r := newRestacker(t, f) |
| 184 | j := r.journal() |
| 185 | a := of(t, j, idA) |
| 186 | if a.Observed.Result != change.RestackConflict || len(a.Observed.Conflicts) != len(names) || a.Branches != nil { |
| 187 | t.Fatalf("the journal keeps %d conflict paths, of %d, as %s", len(a.Observed.Conflicts), len(names), a.Observed.Result) |
| 188 | } |
| 189 | rep := a.Reports[0] |
| 190 | if len(rep.Pending.Report.Conflicts) != change.MaxConflictPaths || *rep.Pending.Report.Failing != idA || |
| 191 | !strings.Contains(strings.Join(rep.Omitted, " "), "22 of its 122 conflict paths") { |
| 192 | t.Errorf("the report names %d paths and leaves out %v", len(rep.Pending.Report.Conflicts), rep.Omitted) |
| 193 | } |
| 194 | if !strings.Contains(*rep.Pending.Report.Refusal, a.Observed.Digest()) { |
| 195 | t.Errorf("the report's refusal names no digest: %q", *rep.Pending.Report.Refusal) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // A report that says what the change's current report on origin says is |
| 200 | // not built again, under a new operation: a held family whose report has |
| 201 | // been pushed has nothing left to write. |
| 202 | func TestAnUnchangedReportIsNotBuiltAgain(t *testing.T) { |
| 203 | f := newFixture(t, "") |
| 204 | f.readyAs(idE, "alice", "e", "main", "E's title") |
| 205 | f.plainBranch("e-plain", "e") |
| 206 | f.moveMain() |
| 207 | r := newRestacker(t, f) |
| 208 | j := r.journal() |
| 209 | if w := of(t, j, idE).Reports[0].Pending.Push(context.Background(), r.g); w.Push != rebase.Pushed { |
| 210 | t.Fatalf("e's report: %+v", w) |
| 211 | } |
| 212 | |
| 213 | p, s := r.plan() |
| 214 | out, err := build(context.Background(), r.g, s, p, r.c.binding(), newOperation(time.Now().Add(time.Hour))) |
| 215 | if err != nil { |
| 216 | t.Fatal(err) |
| 217 | } |
| 218 | if len(out.journal.Families) != 0 { |
| 219 | t.Errorf("an unchanged report was built again: %+v", out.journal.Families) |
| 220 | } |
| 221 | if w := p.Families[0].Writes; w == nil || len(w.Reports) != 1 || !w.Reports[0].Unchanged { |
| 222 | t.Errorf("the plan's writes are %+v", w) |
| 223 | } |
| 224 | } |