History diff: keep a stepped-back object in place; number hunks right
Rewind put an object's old form at the end of its list, so a history entry's diff showed the changed object moving (turning hairpin off for one forward also moved its forward and DNAT lines). It now sorts after stepping back. go-udiff v0.4.1 numbers the new side of hunks after one with a moved line too early, so the chain after @@ could be the one before; hunkStarts numbers them again. History diffs name their sides "before N" and "after N". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
540f4b85ac
commit
0d16358617
4 files changed
+143
-2
No files matched your search
@@ -1,6 +1,8 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -70,3 +72,68 @@ func TestHistoryDiff(t *testing.T) {
|
||||
t.Errorf("unknown entry: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHunkStarts: after a hunk with unchanged lines between its edits (a
|
||||
// moved line), the later hunks' new sides start where they do.
|
||||
func TestHunkStarts(t *testing.T) {
|
||||
var a, b strings.Builder
|
||||
for i := 1; i <= 20; i++ {
|
||||
fmt.Fprintf(&a, "%d\n", i)
|
||||
switch i {
|
||||
case 4:
|
||||
case 6:
|
||||
b.WriteString("6\n4\n")
|
||||
case 14:
|
||||
default:
|
||||
fmt.Fprintf(&b, "%d\n", i)
|
||||
}
|
||||
}
|
||||
diff := hunkStarts(udiff.Unified("a", "b", a.String(), b.String()))
|
||||
var heads []string
|
||||
for _, l := range strings.Split(diff, "\n") {
|
||||
if strings.HasPrefix(l, "@@") {
|
||||
heads = append(heads, l)
|
||||
}
|
||||
}
|
||||
// diff -u: the move nets nothing, the deleted 14 one line.
|
||||
if want := []string{"@@ -1,9 +1,9 @@", "@@ -11,7 +11,6 @@"}; strings.Join(heads, "|") != strings.Join(want, "|") {
|
||||
t.Fatalf("hunk headers %q, want %q\n%s", heads, want, diff)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHistoryDiffUpdate: an update's diff is only what it changed; the
|
||||
// object stepped back to its old form stays where it was.
|
||||
func TestHistoryDiffUpdate(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
e.call(e.token, "POST", "/zones", model.Zone{Name: "lan", Interfaces: []string{"eth1"}}, nil)
|
||||
e.call(e.token, "POST", "/zones", model.Zone{Name: "wan", Interfaces: []string{"wan"}, External: true}, nil)
|
||||
var ids []int64
|
||||
for i, port := range []string{"8096", "443", "50300", "7777", "25"} {
|
||||
var mr api.MutationResponse
|
||||
e.call(e.token, "POST", "/forwards", map[string]any{"name": "f" + port, "ports": port, "to": fmt.Sprintf("10.0.10.%d", 30+i),
|
||||
"public_addresses": []string{"203.0.113.1"}, "hairpin": true}, &mr)
|
||||
var f model.PortForward
|
||||
if err := json.Unmarshal(mr.Object, &f); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids = append(ids, f.ID)
|
||||
}
|
||||
var mr api.MutationResponse
|
||||
if code := e.call(e.token, "PATCH", "/forwards/"+itoa(ids[2]), map[string]any{"hairpin": false}, &mr); code != 200 {
|
||||
t.Fatalf("update: %d", code)
|
||||
}
|
||||
var out struct{ Diff string }
|
||||
if code := e.call(e.token, "GET", "/history/"+itoa(mr.HistoryID)+"/diff", nil, &out); code != 200 {
|
||||
t.Fatalf("diff: %d", code)
|
||||
}
|
||||
var changed []string
|
||||
for _, l := range strings.Split(out.Diff, "\n") {
|
||||
if (strings.HasPrefix(l, "+") || strings.HasPrefix(l, "-")) && !strings.HasPrefix(l, "+++") && !strings.HasPrefix(l, "---") {
|
||||
changed = append(changed, l)
|
||||
}
|
||||
}
|
||||
if len(changed) != 1 || !strings.HasPrefix(changed[0], "-") || !strings.Contains(changed[0], "-hairpin") ||
|
||||
!strings.Contains(out.Diff, "(before "+itoa(mr.HistoryID)+")") || !strings.Contains(out.Diff, "@@ chain srcnat") {
|
||||
t.Errorf("entry %d's diff, want only the hairpin line gone, in chain srcnat:\n%s", mr.HistoryID, out.Diff)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user