Skip to content

Commit 358b945

Browse files
committed
internal/cuetxtar: ensure ordering of files in txtar is consistent
Currently, if outputs change the ordering of such a file may change within the file, resulting in large diffs. This change ensures that the original ordering, is at least maintained. Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: I3ea91967c39e9656bb958bc23c06f60512276b6d Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199751 Reviewed-by: Matthew Sackman <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent f889101 commit 358b945

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

internal/cuetxtar/txtar.go

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"path"
2525
"path/filepath"
26+
"slices"
2627
"strings"
2728
"testing"
2829

@@ -416,6 +417,14 @@ func (x *TxTarTest) run(t *testing.T, f func(tc *Test)) {
416417
if err != nil {
417418
t.Fatalf("error parsing txtar file: %v", err)
418419
}
420+
421+
// Record ordering of files in the archive to preserve that ordering
422+
// later.
423+
ordering := map[string]int{}
424+
for i, f := range a.Files {
425+
ordering[f.Name] = i
426+
}
427+
419428
tc := &Test{
420429
T: t,
421430
Archive: a,
@@ -472,6 +481,7 @@ func (x *TxTarTest) run(t *testing.T, f func(tc *Test)) {
472481
}
473482
f(tc)
474483

484+
// Track the position of the fallback files.
475485
index := make(map[string]int, len(a.Files))
476486
for i, f := range a.Files {
477487
index[f.Name] = i
@@ -605,6 +615,18 @@ func (x *TxTarTest) run(t *testing.T, f func(tc *Test)) {
605615
a.Files = files
606616

607617
if update {
618+
slices.SortStableFunc(a.Files, func(i, j txtar.File) int {
619+
p, ok := ordering[i.Name]
620+
if !ok {
621+
p = len(a.Files)
622+
}
623+
q, ok := ordering[j.Name]
624+
if !ok {
625+
q = len(a.Files)
626+
}
627+
return q - p
628+
})
629+
608630
err = os.WriteFile(fullpath, txtar.Format(a), 0644)
609631
if err != nil {
610632
t.Fatal(err)

0 commit comments

Comments
 (0)