| 1 | package rebase |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "sort" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "the-valley/change" |
| 15 | ) |
| 16 | |
| 17 | // commitFiles writes each file on the current branch and commits them |
| 18 | // together. |
| 19 | func (w *world) commitFiles(subject string, files map[string]string) string { |
| 20 | w.t.Helper() |
| 21 | for name, content := range files { |
| 22 | path := filepath.Join(w.work, name) |
| 23 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 24 | w.t.Fatal(err) |
| 25 | } |
| 26 | if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 27 | w.t.Fatal(err) |
| 28 | } |
| 29 | } |
| 30 | w.git("add", "-A") |
| 31 | w.git("commit", "--quiet", "-m", subject) |
| 32 | return w.git("rev-parse", "HEAD") |
| 33 | } |
| 34 | |
| 35 | // A conflict keeps every path that conflicted, more than a report holds, |
| 36 | // whatever bytes their names hold: a newline, a control character, bytes |
| 37 | // that are not UTF-8, and a path longer than a report allows. |
| 38 | func TestEveryConflictingPathIsKept(t *testing.T) { |
| 39 | w := newWorld(t) |
| 40 | long := strings.Repeat("d", 200) + "/" + strings.Repeat("e", 200) + "/" + strings.Repeat("f", 200) + "/" + |
| 41 | strings.Repeat("g", 200) + "/" + strings.Repeat("h", 200) + "/" + strings.Repeat("i", 100) + ".txt" |
| 42 | names := []string{"line\nbreak.txt", "bell\x07.txt", "latin1-\xe9.txt", long} |
| 43 | for i := 0; i < 130; i++ { |
| 44 | names = append(names, filepath.Join("many", strings.Repeat("p", i%7+1)+"-"+string(rune('a'+i%26))+"-"+strings.Repeat("q", i/26+1)+".txt")) |
| 45 | } |
| 46 | files := func(content string) map[string]string { |
| 47 | m := map[string]string{} |
| 48 | for _, n := range names { |
| 49 | m[n] = content |
| 50 | } |
| 51 | return m |
| 52 | } |
| 53 | w.git("checkout", "--quiet", "-b", "a", "main") |
| 54 | w.commitFiles("layer a", files("a\n")) |
| 55 | w.git("push", "--quiet", "origin", "a") |
| 56 | w.ready(idA, "a") |
| 57 | w.git("checkout", "--quiet", "main") |
| 58 | w.commitFiles("main has its own", files("main\n")) |
| 59 | w.git("push", "--quiet", "origin", "main") |
| 60 | before := w.refs() |
| 61 | |
| 62 | _, err := Prepare(context.Background(), w.g, w.capture(Selection{ID: idA})) |
| 63 | var r *Refusal |
| 64 | if !errors.As(err, &r) || r.Kind != Conflict { |
| 65 | t.Fatalf("prepare: %v", err) |
| 66 | } |
| 67 | if len(names) <= change.MaxConflictPaths { |
| 68 | t.Fatalf("the test conflicts on %d paths, no more than a report holds", len(names)) |
| 69 | } |
| 70 | got := append([]string(nil), r.Paths...) |
| 71 | want := append([]string(nil), names...) |
| 72 | sort.Strings(got) |
| 73 | sort.Strings(want) |
| 74 | if !reflect.DeepEqual(got, want) { |
| 75 | t.Errorf("the conflict names %d paths, not the %d that conflicted", len(got), len(want)) |
| 76 | for i := 0; i < len(got) && i < len(want); i++ { |
| 77 | if got[i] != want[i] { |
| 78 | t.Errorf("first difference: %q, where %q conflicted", got[i], want[i]) |
| 79 | break |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | if after := w.refs(); after != before { |
| 84 | t.Errorf("a conflict moved something") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Each change's version is built before any branch moves, from the record |
| 89 | // the capture read: origin is untouched, each version keeps its active |
| 90 | // title and is leased on the record read, and every write restores from |
| 91 | // the form it is kept in. Restored, a version's push is refused while its |
| 92 | // branch has not moved. Once the restored branch push moves every branch |
| 93 | // at once, each restored version's push adds the version. |
| 94 | func TestEveryWriteIsBuiltBeforeAnyPush(t *testing.T) { |
| 95 | w := newWorld(t) |
| 96 | w.layer("a", "main") |
| 97 | w.layer("b", "a") |
| 98 | w.readyTitled(idA, "a", "A's own title") |
| 99 | w.readyTitled(idB, "b", "B's own title") |
| 100 | w.moveMain("main.txt", "main\n") |
| 101 | ctx := context.Background() |
| 102 | f := w.capture(Selection{ID: idB}) |
| 103 | before := w.refs() |
| 104 | |
| 105 | p, err := Prepare(ctx, w.g, f) |
| 106 | if err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | defer p.Release(ctx, w.g) |
| 110 | versions, err := p.BuildAppends(ctx, w.g, "restacker") |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | if after := w.refs(); after != before { |
| 115 | t.Fatalf("building the writes moved something:\n%s\nnow:\n%s", before, after) |
| 116 | } |
| 117 | if len(versions) != 2 || versions[0].ID != idA || versions[1].ID != idB { |
| 118 | t.Fatalf("the versions built are %+v", versions) |
| 119 | } |
| 120 | for i, v := range versions { |
| 121 | l := f.Layers[i] |
| 122 | active := v.Record.ActiveVersion() |
| 123 | if v.Old != l.Commit || v.Head != p.Heads[l] || v.Version != 2 || !v.Restack() || |
| 124 | active.Title != l.Record.ActiveVersion().Title || active.Uploader != "restacker" || active.Branch != l.Branch { |
| 125 | t.Errorf("the version of %s is %+v, recording %+v", l.Name, v, active) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | kept, err := json.Marshal(struct { |
| 130 | Branches *PendingBranches |
| 131 | Versions []*PendingVersion |
| 132 | }{p.Branches(), versions}) |
| 133 | if err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | var restored struct { |
| 137 | Branches *PendingBranches |
| 138 | Versions []*PendingVersion |
| 139 | } |
| 140 | if err := json.Unmarshal(kept, &restored); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | if !reflect.DeepEqual(restored.Branches, p.Branches()) || !reflect.DeepEqual(restored.Versions, versions) { |
| 144 | t.Fatalf("the writes restore as %+v; they were kept as %s", restored, kept) |
| 145 | } |
| 146 | for _, u := range restored.Branches.Updates { |
| 147 | if w.at("refs/heads/"+u.Branch) != u.Old { |
| 148 | t.Errorf("the kept push leases %s on %s, and origin's is at %s", u.Branch, u.Old, w.at("refs/heads/"+u.Branch)) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if ap := restored.Versions[0].Push(ctx, w.g); ap.Err == nil || w.refs() != before { |
| 153 | t.Fatalf("a version pushed before its branch moved: %+v", ap) |
| 154 | } |
| 155 | if push, refused := restored.Branches.Push(ctx, w.g); push != Pushed { |
| 156 | t.Fatalf("the restored branch push: %v %+v", push, refused) |
| 157 | } |
| 158 | for _, u := range restored.Branches.Updates { |
| 159 | if w.at("refs/heads/"+u.Branch) != u.New { |
| 160 | t.Errorf("origin's %s is at %s, not %s", u.Branch, w.at("refs/heads/"+u.Branch), u.New) |
| 161 | } |
| 162 | } |
| 163 | for _, v := range restored.Versions { |
| 164 | if ap := v.Push(ctx, w.g); ap.Err != nil || ap.Version != 2 || w.at(v.Ref) != v.Commit { |
| 165 | t.Fatalf("the restored version of %s: %+v", v.Handle, ap) |
| 166 | } |
| 167 | } |
| 168 | if a, b := w.record(idA).ActiveVersion(), w.record(idB).ActiveVersion(); a.Title != "A's own title" || b.Title != "B's own title" { |
| 169 | t.Errorf("the versions on origin are titled %q and %q", a.Title, b.Title) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // A kept branch push restores only from its own form, whole and agreeing |
| 174 | // with itself. |
| 175 | func TestBranchPushRestoresOnlyItsForm(t *testing.T) { |
| 176 | b := PendingBranches{Tip: strings.Repeat("1", 40), Updates: []BranchUpdate{ |
| 177 | {Branch: "a", Old: strings.Repeat("2", 40), New: strings.Repeat("3", 40)}, |
| 178 | {Branch: "b", Old: strings.Repeat("4", 40), New: strings.Repeat("5", 40)}, |
| 179 | }} |
| 180 | data, err := json.Marshal(b) |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | var again PendingBranches |
| 185 | if err := json.Unmarshal(data, &again); err != nil || !reflect.DeepEqual(again, b) { |
| 186 | t.Fatalf("%s restores as %+v, %v", data, again, err) |
| 187 | } |
| 188 | kept := string(data) |
| 189 | for what, form := range map[string]string{ |
| 190 | "no update": strings.Replace(kept, kept[strings.Index(kept, `"updates":`):], `"updates":[]}`, 1), |
| 191 | "a branch twice": strings.Replace(kept, `"branch":"b"`, `"branch":"a"`, 1), |
| 192 | "no move": strings.Replace(kept, strings.Repeat("3", 40), strings.Repeat("2", 40), 1), |
| 193 | "a short old tip": strings.Replace(kept, strings.Repeat("4", 40), "4444", 1), |
| 194 | "no branch name": strings.Replace(kept, `"branch":"a"`, `"branch":"a..b"`, 1), |
| 195 | "an unknown field": strings.Replace(kept, `{"schema"`, `{"lease":"none","schema"`, 1), |
| 196 | "a version's form": strings.Replace(kept, pendingBranchesSchema, pendingVersionSchema, 1), |
| 197 | "the tip twice": strings.Replace(kept, `"tip":`, `"tip":"`+strings.Repeat("6", 40)+`","tip":`, 1), |
| 198 | "a field left out": strings.Replace(kept, `,"new":"`+strings.Repeat("5", 40)+`"`, "", 1), |
| 199 | "another form next": kept + " " + kept, |
| 200 | } { |
| 201 | if form == kept { |
| 202 | t.Fatalf("%s: the edit changed nothing", what) |
| 203 | } |
| 204 | var q PendingBranches |
| 205 | if err := json.Unmarshal([]byte(form), &q); err == nil { |
| 206 | t.Errorf("%s restored: %+v", what, q) |
| 207 | } |
| 208 | } |
| 209 | } |